This repository has been archived by the owner on Oct 11, 2018. It is now read-only.
/
validate-strict.js
86 lines (61 loc) · 1.55 KB
/
validate-strict.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
module.exports = {
friendlyName: 'Validate (strict)',
description: 'Validate a value against a type schema.',
cacheable: true,
sync: true,
inputs: {
value: {
friendlyName: 'Value',
description: 'The actual value to validate.',
example: '===',
required: true
},
typeSchema: {
friendlyName: 'Type schema',
description: 'The expected type schema.',
example: '*',
required: true
}
},
exits: {
invalid: {
description: 'The value is invalid against the provided schema.',
example: [{
code: 'E_INVALID_TYPE',
keypath: 'foo.bar.0.baz',
expected: '*',
actual: '==='
}]
},
success: {
friendlyName: 'valid',
description: 'Valid.'
}
},
fn: function (inputs,exits) {
var _ = require('lodash');
var rttc = require('rttc');
try {
rttc.validateStrict(inputs.typeSchema, inputs.value);
}
catch (e) {
// Generally, the is E_INVALID_TYPE.
if (e.code === 'E_INVALID_TYPE') {
var errors = _.reduce(e.errors, function (memo,subErr){
// TODO: do this in rttc itself instead of here.
memo.push({
code: subErr.code,
expected: subErr.expected,
actual: subErr.actual,
keypath: subErr.inputKey
});
return memo;
}, []);
return exits.invalid(errors);
}
// Otherwise this is some unknown error.
else return exits.error(e);
}
return exits.success();
},
};