-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidate.js
184 lines (151 loc) · 4.21 KB
/
validate.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
'use strict';
var isBrowser = require('is-browser')
, indexOf = isBrowser ? require('indexof') : require('indexof-component')
, type = isBrowser ? require('type') : require('component-type')
module.exports = validate;
function validate(fn){
validateType.call(this);
validateTypes.call(this);
validateFormat.call(this);
validateCombinations.call(this, fn);
return (this.valid());
}
validate._types = {};
validate._formats = {};
validate.addType = function(key,fn){
validate._types[key] = fn;
return this;
}
validate.addFormat = function(key,fn){
if (type(fn)=="regexp"){
var r = fn;
fn = function(){
var val = this.instance();
if (val == undefined) return false;
return !!r.test(val.toString());
};
}
validate._formats[key] = fn;
return this;
}
validate.getTypes = function(){ return this._types; }
validate.getFormat = function(key){ return this._formats[key]; }
function validateType(){
var types = this.property('type')
, instance = this.instance()
, actual = type(instance)
, isinteger = actual == 'number' && (instance==(instance|0))
if (!types) return;
if (instance === undefined) return;
types = ('array' == type(types) ? types : [types])
var valid = (indexOf(types,actual)>=0) ||
(isinteger && indexOf(types,'integer')>=0)
, isnull = (actual == 'null')
this.assert( valid,
isnull ? "is missing" : "does not match type",
"type",
actual
);
}
// Note: assertions done in concrete validator functions
function validateTypes(){
var types = validate.getTypes()
for (var k in types){
var validator = types[k]
validator.call(this);
}
}
function validateFormat(){
var format = this.property('format')
, instance = this.instance()
if (!format) return;
if (instance === undefined) return;
var formatter = validate.getFormat(format)
if (!formatter) return;
this.assert( formatter.call(this),
'does not match format',
'format'
);
}
function validateCombinations(fn){
var allOf = this.get('allOf')
, anyOf = this.get('anyOf')
, oneOf = this.get('oneOf')
, not = this.get('not')
var valids = [this.schema()]
var collect = function(schemas){
valids.push.apply(valids,schemas);
}
if (allOf){
this.assert( validateAllOf.call(this,collect),
"not all of conditions valid",
"allOf"
);
}
if (anyOf){
this.assert( validateAnyOf.call(this,collect),
"not any of conditions valid",
"anyOf"
);
}
if (oneOf){
this.assert( validateOneOf.call(this,collect),
"not any of conditions valid, or more than one of conditions valid",
"oneOf"
);
}
if (not){
this.assert( validateNot.call(this),
"not condition invalid",
"not"
);
}
if (this.valid() && fn) fn(valids);
}
/*************
* Combinations
*
*/
function validateAllOf(fn){
var validfn = function(ctx,valid,collect){
validate.call(ctx,collect);
return (ctx.valid() && valid);
}
return validateCombination.call(this,'allOf',true,validfn,fn);
}
function validateAnyOf(fn){
var validfn = function(ctx,valid,collect){
validate.call(ctx,collect);
return (ctx.valid() || valid);
}
return validateCombination.call(this,'anyOf',false,validfn,fn);
}
function validateOneOf(fn){
var validfn = function(ctx,valid,collect){
validate.call(ctx,collect);
return (ctx.valid() ? !valid : valid);
}
return validateCombination.call(this,'oneOf',false,validfn,fn);
}
function validateNot(){
var ctx = this.subcontext('not')
, not = ctx.schema
if (!not) return;
validate.call(ctx)
return (!ctx.valid());
}
function validateCombination(key,valid,validfn,fn){
var cond = this.get(key);
if (!cond) return;
var valids = []
var collect = function(schemas){
valids.push.apply(valids,schemas);
}
var self = this
cond.each( function(i,schema){
var ctx = self.subcontext([key,i].join('/'))
valid = validfn(ctx,valid,collect);
});
if (valid && fn && valids.length > 0) fn(valids);
return (valid);
}