Total Complexity | 8 |
Complexity/F | 8 |
Lines of Code | 29 |
Function Count | 1 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
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 |