-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
71 lines (62 loc) · 2.3 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
const typeOf = require('typeof.pddivine');
module.exports = _validate;
/**
* Test the validity of a value given a schema.
*
* @param {any} value
* @param {object} schema
* @returns
*/
function _validate (value, schema, options) {
const verbose = typeOf(options) === Object && !!options.verbose;
if (!schema) { return false; }
const valueType = typeOf(value);
// Handle null allowed
if ( valueType === null ) {
if ( schema.options.allowNull ) { return handleValidation(value, schema, verbose); }
}
// Handle undefined allowed
if ( valueType === undefined ) {
if ( !schema.options.required ) { return handleValidation(value, schema, verbose); }
}
// Handle value match
if ( valueType !== schema.type ) { return _response(false, value, schema, verbose); }
// Handle object
if ( schema.schema !== undefined && ( valueType === Object || valueType === Array ) ) {
const subSchema = schema.schema;
const isPrototypal = subSchema.type !== undefined && subSchema.options !== undefined;
// Validate each element
if (isPrototypal) {
for ( let elem in value ) {
const nested = _validate(value[elem], subSchema, {verbose: true});
if ( !nested.success ) { return _response(false, value[elem], subSchema, verbose); }
}
} else {
// Ensure all schema keys are on value
for ( let elem in subSchema ) {
if (value[elem] === undefined) {
value[elem] = undefined;
}
}
for ( let elem in value ) {
const nested = _validate(value[elem], subSchema[elem], {verbose: true});
if ( !nested.success ) { return _response(false, value[elem], subSchema[elem], verbose); }
}
}
}
return handleValidation(value, schema, verbose);
}
/**
* Test the validity of a value against a custom function, otherwise is valid.
*
* @param {any} value - Input to be validated.
* @param {any} schema - Schema which constains the validation function
* @returns {boolean}
*/
function handleValidation (value, schema, verbose) {
if ( typeOf(schema.options.validation) !== Function ) { return _response(true, null, null, verbose) };
return _response(schema.options.validation(value), value, schema, verbose);
}
function _response (success, value, schema, verbose) {
return verbose ? { success, value, schema } : success;
}