-
Notifications
You must be signed in to change notification settings - Fork 0
/
ModelService.js
64 lines (63 loc) · 1.69 KB
/
ModelService.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
/**
* ModelService.js
*/
module.exports = {
/**
* return true if the structures don't match || values don't match types
*
* * Schema boilerplate:
* - Array Schema
* ```
* schema = {
* type: 'array',
* item: {
* type: 'object',
* struct: {
* field: 'fieldType',
* ...
* },
* },
* };
* ```
*
* - Object Schema
* ```
* schema = {
* type: 'object',
* struct: {
* field: 'fieldType',
* ...
* },
* };
* ```
*
* @param {Object} schema schema definition
* @param {Object} input input json object
*/
invalidStructure(schema, input) {
try {
if (schema.type === 'array') {
// Invalid: if input is not array
// OR some of input[items] doesn't match schema.item
return !_.isArray(input) ||
_.some(input, item => this.invalidStructure(schema.item, item));
}
else if (schema.type === 'object') {
// Invalid if input is not an object
// OR if input.keys doesn't match schema.struct.keys
// OR if typeof input[key] doesn't match schema.struct[key]
return !_.isObjectLike(input) ||
!_.isEqual(_.keys(schema.struct), _.keys(input)) ||
_.some(_.keys(input), key => this.invalidStructure(schema.struct[key], input[key]));
}
else { // verifying field value vs schema.type
// TODO: Add other field validations here (i.e. isEmail, required,...etc.)
return typeof input !== schema.type;
}
}
catch (err) {
sails.log.error('Exception in [invalidStructure] : ', err);
return true;
}
},
};