-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
280 lines (217 loc) · 7.08 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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
The MIT License (MIT)
Copyright (c) 2014 The Australian National University
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
'use strict';
var checkNumber = function(schema, instance) {
var errors = [];
if (schema.maximum !== null) {
if (instance > schema.maximum)
errors.push('may be at most ' + schema.maximum);
else if (schema.exclusiveMaximum && instance >= schema.maximum)
errors.push('must be less than ' + schema.maximum);
}
if (schema.minimum !== null) {
if (instance < schema.minimum)
errors.push('must be at least ' + schema.minimum);
else if (schema.exclusiveMinimum && instance <= schema.minimum)
errors.push('must be more than ' + schema.minimum);
}
if (schema.multipleOf != null) {
if ((instance / schema.multipleOf) % 1 != 0)
errors.push('must be a multiple of ' + schema.multipleOf);
}
return errors;
};
var fieldErrors = function(errors) {
if (errors.length > 0)
return [ { path: [], errors: errors } ];
else
return [];
};
var validator = {};
validator.boolean = function(schema, instance) {
var errors = [];
if (typeof instance != 'boolean')
errors.push('must be boolean');
return fieldErrors(errors);
};
validator.enum = function(schema, instance) {
var errors = [];
if (schema.enum.indexOf(instance) < 0)
errors.push('value not in list');
return fieldErrors(errors);
};
validator.number = function(schema, instance) {
var errors = [];
if (typeof instance != 'number')
errors.push('must be a number');
else
errors = checkNumber(schema, instance);
return fieldErrors(errors);
};
validator.integer = function(schema, instance) {
var errors = [];
if (typeof instance != 'number')
errors.push('must be a number');
else {
errors = checkNumber(schema, instance);
if (instance % 1 > 0)
errors.unshift('must be an integer');
}
return fieldErrors(errors);
};
validator.string = function(schema, instance) {
var errors = [];
if (typeof instance != 'string')
errors.push('must be a string');
else {
if (schema.maxLength != null && instance.length > schema.maxLength)
errors.push('may have at most ' + schema.maxLength + ' characters');
if (schema.minLength != null && instance.length < schema.minLength)
errors.push('must have at least ' + schema.minLength + ' characters');
if (schema.pattern != null && !(RegExp(schema.pattern).test(instance)))
errors.push('must match ' + schema.pattern);
}
return fieldErrors(errors);
};
validator.array = function(schema, instance, context) {
var errors = [];
var result, i, j;
if (!Array.isArray(instance))
return fieldErrors(['must be an array']);
else {
if (schema.maxItems != null && instance.length > schema.maxItems)
errors.push('may have at most ' + schema.maxItems + ' items');
if (schema.minItems != null && instance.length < schema.minItems)
errors.push('must have at least ' + schema.minItems + ' items');
result = fieldErrors(errors);
if (schema.items != null) {
for (i in instance) {
errors = validate(schema.items, instance[i], context);
for (j in errors) {
result.push({
path : [i].concat(errors[j].path),
errors: errors[j].errors
});
}
}
}
}
return result;
};
var requires = function(schema, key) {
var subschema;
if (schema.required != null && schema.required.indexOf(key) >= 0)
return 'must be present';
else {
subschema = schema.properties[key];
if (subschema.type == 'array' && subschema.minItems > 0)
return 'must have at least ' + subschema.minItems + ' items';
else
return null;
}
};
validator.object = function(schema, instance, context) {
var result = [];
var key, errors, i;
if (instance == null)
instance = {};
if (instance.constructor !== Object)
result.push({ path: [], errors: ['must be a plain object'] });
else {
for (key in schema.properties) {
if (instance.hasOwnProperty(key)) {
errors = validate(schema.properties[key], instance[key], context);
for (i = 0; i < errors.length; ++i)
result.push({
path : [key].concat(errors[i].path),
errors: errors[i].errors
});
}
else if (requires(schema, key)) {
result.push({
path : [key],
errors: [requires(schema, key)]
});
}
}
}
return result;
};
var merge = function() {
var args = [].slice.call(arguments);
var result = args.every(Array.isArray) ? [] : {};
var i, obj, key;
for (i in args) {
obj = args[i];
for (key in obj)
result[key] = obj[key];
}
return result;
};
var without = function(obj) {
var args = [].slice.call(arguments);
var result = Array.isArray(obj) ? [] : {};
for (var key in obj)
if (args.indexOf(key) < 0)
result[key] = obj[key];
return result;
};
var getIn = function(root, path) {
if (path.length == 0 || root == undefined)
return root;
else
return getIn(root[path[0]], path.slice(1))
};
var cat = function(arrayOfArrays) {
return [].concat.apply([], arrayOfArrays);
};
var resolve = function(schema, context) {
var reference = schema['$ref'];
if (reference) {
if (!reference.match(/^#(\/([a-zA-Z_][a-zA-Z_0-9]*|[0-9]+))*$/))
throw new Error('reference '+reference+' has unsupported format');
return {
allOf: [
without(schema, '$ref'),
getIn(context, reference.split('/').slice(1))
]
};
} else
return schema;
};
var validate = function(schema, instance, context) {
var effectiveContext = context || schema;
var effectiveSchema = resolve(schema, effectiveContext);
if (effectiveSchema.allOf) {
var results = [without(effectiveSchema, 'allOf')]
.concat(effectiveSchema.allOf)
.map(function(schema) {
return validate(schema, instance, effectiveContext);
});
return cat(results);
} else {
var type = effectiveSchema.enum ? 'enum' : effectiveSchema.type;
if (type)
return validator[type](effectiveSchema, instance, effectiveContext);
else
return [];
}
};
module.exports = validate;