Passed
Push — master ( b81e0e...d03e63 )
by Olof
02:06
created

validate.js ➔ ???   B

Complexity

Conditions 8
Paths 10

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
c 1
b 0
f 0
nc 10
nop 3
dl 0
loc 19
rs 7.7777
1
"use strict";
2
/**
3
 * Validate the data against the schema.
4
 *
5
 * @param {Object} data The data to validate.
6
 * @param {Object} schema The schema built of jov classes.
7
 * @param {String} format The format to return error as, "obj" or "json".
8
 */
9
const validate = (data, schema, format = "obj") => {
10
    let returnError = null;
11
12
    for (let key in schema) {
13
        if (!schema[key].isRequired && !data.hasOwnProperty(key)) {
14
            continue;
15
        } else if (schema[key].isRequired && !data.hasOwnProperty(key)) {
16
            returnError = schema[key].error("Any:required", key, schema[key]);
17
            break;
18
        }
19
20
        returnError = schema[key].validate(data, key);
21
        if (returnError) {
22
            break;
23
        }
24
    }
25
26
    return format === "json" ? JSON.stringify(returnError, null, 4) : returnError;
27
};
28
29
module.exports = validate;
30