-
Notifications
You must be signed in to change notification settings - Fork 3
/
validation-context.js
50 lines (45 loc) · 1.78 KB
/
validation-context.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
var validationResult = require('./validation-result'),
jsonRefs = require('skeemas-json-refs');
var protoContext = {
addError: function(message, subject, criteria) {
if(!this.silent) this.result.addError(message, subject, criteria, this);
return this;
},
silently: function(fn) {
this.silent = true;
var result = fn();
this.silent = false;
return result;
},
subcontext: function(schema) {
return makeContext(schema, this, this.silent);
},
runValidations: function(validations, subject, schema) {
var breakOnError = this.breakOnError,
args = Array.prototype.slice.call(arguments),
valid = true,
validation;
args[0] = this;
for(var i = 0, len = validations.length; i < len; i++) {
validation = validations[i];
if(!validation[0]) continue;
valid = validation[1].apply(null, args) && valid;
if(breakOnError && !valid) return false;
}
return valid;
}
};
var makeContext = module.exports = function(schema, context, forceNewResult) {
context = context || {};
return Object.create(protoContext, {
id: { enumerable:true, writable:false, value: [] },
schema: { enumerable:true, writable:false, value: schema || context.schema },
path: { enumerable:true, writable:false, value: context.path && context.path.slice() || ['#'] },
result: { enumerable:true, writable:false, value: (!forceNewResult && context.result) || validationResult(context.instance) },
refs: { enumerable:true, writable:false, value: context.refs || jsonRefs() },
silent: { enumerable:true, writable:true, value: false },
breakOnError: { enumerable:true, writable:true, value: context.breakOnError || false },
cleanWithDefaults: { enumerable:true, writable:true, value: context.cleanWithDefaults || false },
cleanSubject: { enumerable:true, writable:true, value: undefined }
});
};