Issues (13)

src/Validator.js (5 issues)

1
import {
2
    toArray,
3
    isString,
4
    isObject,
5
    isValue
6
} from 'myrmidon';
7
import BaseError from './errors/format/Base';
8
import ValidationError from './errors/ValidationError';
9
10
export default class Validator {
11
    constructor(cottus, schema, parentContext) {
12
        this._schema = schema;
13
        this._cottus = cottus;
14
        this._hierarchy = [];
15
        if (parentContext) {
16
            this._hierarchy = parentContext.parent._nestedHierarhy(parentContext.key);
17
            if (!parentContext.notLink) {
18
                this.parent = parentContext.parent;
19
            }
20
        }
21
    }
22
23
    _nestedHierarhy(key) {
24
        const nested =  [ ...this._hierarchy ];
25
26
        if (isValue(key)) nested.push(key);
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
27
28
        return nested;
29
    }
30
31
    get isNested() {
32
        return !!this.parent;
33
    }
34
35
    _sendNestedErrors(errors) {
36
        if (this.isNested) {
37
            return this.parent._receiveNestedErrors(errors);
38
        }
39
40
        this.nestedErrors.push(...errors);
0 ignored issues
show
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
41
    }
42
43
    _receiveNestedErrors(errors) {
44
        if (this.isNested) {
45
            return this.parent._sendNestedErrors(errors);
46
        }
47
48
        this.nestedErrors.push(...errors);
0 ignored issues
show
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
49
    }
50
51
    parse() {
52
        this.rules = [];
53
        toArray(this._schema).forEach(schema => {
54
            let rulename = null;
55
56
            let params = null;
57
58
            const isSimple = isString(schema);
59
            const isComplex = isObject(schema) && Object.keys(schema).length === 1;
60
61
            if (isSimple) rulename = schema;
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
62
            if (isComplex) {
63
                rulename = Object.keys(schema)[0];
64
                params = Object.values(schema)[0];
65
            }
66
67
            const Rule = this._cottus.rules[rulename];
68
69
            if (!Rule) throw new Error(`Rule [${rulename}] not found`);
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
70
            this.rules.push(new Rule({
71
                params,
72
                validator : this
73
            }));
74
        });
75
    }
76
77
    validate(input) {
78
        this.nestedErrors = [];
79
        let valid = input;
80
        const errors = [];
81
82
        for (const rule of this.rules) {
83
            try {
84
                valid = rule.run(valid);
85
            } catch (error) {
86
                if (error instanceof BaseError) {
87
                    if (!error.hasContext) {
88
                        error.setContext({
89
                            value : valid,
90
                            path  : this._hierarchy
91
                        });
92
                    }
93
94
                    errors.push(error);
95
                    break;
96
                }
97
98
                throw error;
99
            }
100
        }
101
102
        if (errors.length > 0) {
103
            if (this.isNested) {
104
                return this._sendNestedErrors(errors);
105
            }
106
107
            throw new ValidationError(this._cottus, errors);
108
        }
109
110
        if (this.nestedErrors.length > 0) {
111
            throw new ValidationError(this._cottus, this.nestedErrors);
112
        }
113
114
        return valid;
115
    }
116
}
117