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

src/validate.js   A

Complexity

Total Complexity 8
Complexity/F 8

Size

Lines of Code 29
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 0
c 1
b 0
f 0
nc 1
dl 0
loc 29
rs 10
noi 0
wmc 8
mnd 3
bc 5
fnc 1
bpm 5
cpm 8

1 Function

Rating   Name   Duplication   Size   Complexity  
B validate.js ➔ ??? 0 19 8
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