This repository has been archived by the owner on Mar 5, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
jjve.js
355 lines (299 loc) · 9.75 KB
/
jjve.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
(function() {
'use strict';
function make(o) {
var errors = [];
function isArray(obj) {
if (typeof Array.isArray === 'function') {
return Array.isArray(obj);
}
return Object.prototype.toString.call(obj) === '[object Array]';
}
function allowsType(schema, type) {
if (typeof schema.type === 'string') {
return schema.type === type;
}
if (isArray(schema.type)) {
return schema.type.indexOf(type) !== -1;
}
return false;
}
var keys = Object.keys(o.validation);
// when we're on a leaf node we need to handle the validation errors,
// otherwise we continue walking
var leaf = keys.every(function(key) {
return typeof o.validation[key] !== 'object' ||
isArray(o.validation[key]);
});
if (leaf) {
// step through each validation issue
// example: { required: true }
keys.forEach(function(key) {
var error, properties;
try {
switch (key) {
case 'type':
var type = typeof o.data;
// further discover types
if (type === 'number' && ('' + o.data).match(/^\d+$/)) {
type = 'integer';
} else if (type === 'object' && Array.isArray(o.data)) {
type = 'array';
}
// the value of type is the required type (ex: { type: 'string' })
error = {
code: 'INVALID_TYPE',
message: 'Invalid type: ' + type + ' should be ' +
(isArray(o.validation[key]) ? 'one of ' : '') +
o.validation[key]
};
break;
case 'required':
properties = o.ns;
error = {
code: 'OBJECT_REQUIRED',
message: 'Missing required property: ' +
properties[properties.length - 1]
};
break;
case 'minimum':
error = {
code: 'MINIMUM',
message: 'Value ' + o.data + ' is less than minimum ' +
o.schema.minimum
};
break;
case 'maximum':
error = {
code: 'MAXIMUM',
message: 'Value ' + o.data + ' is greater than maximum ' +
o.schema.maximum
};
break;
case 'multipleOf':
error = {
code: 'MULTIPLE_OF',
message: 'Value ' + o.data + ' is not a multiple of ' +
o.schema.multipleOf
};
break;
case 'pattern':
error = {
code: 'PATTERN',
message: 'String does not match pattern: ' + o.schema.pattern
};
break;
case 'minLength':
error = {
code: 'MIN_LENGTH',
message: 'String is too short (' + o.data.length + ' chars), ' +
'minimum ' + o.schema.minLength
};
break;
case 'maxLength':
error = {
code: 'MAX_LENGTH',
message: 'String is too long (' + o.data.length + ' chars), ' +
'maximum ' + o.schema.maxLength
};
break;
case 'minItems':
error = {
code: 'ARRAY_LENGTH_SHORT',
message: 'Array is too short (' + o.data.length + '), ' +
'minimum ' + o.schema.minItems
};
break;
case 'maxItems':
error = {
code: 'ARRAY_LENGTH_LONG',
message: 'Array is too long (' + o.data.length + '), maximum ' +
o.schema.maxItems
};
break;
case 'uniqueItems':
error = {
code: 'ARRAY_UNIQUE',
message: 'Array items are not unique'
};
break;
case 'minProperties':
error = {
code: 'OBJECT_PROPERTIES_MINIMUM',
message: 'Too few properties defined (' +
Object.keys(o.data).length + '), minimum ' +
o.schema.minProperties
};
break;
case 'maxProperties':
error = {
code: 'OBJECT_PROPERTIES_MAXIMUM',
message: 'Too many properties defined (' +
Object.keys(o.data).length + '), maximum ' +
o.schema.maxProperties
};
break;
case 'enum':
error = {
code: 'ENUM_MISMATCH',
message: 'No enum match (' + o.data + '), expects: ' +
o.schema['enum'].join(', ')
};
break;
case 'not':
error = {
code: 'NOT_PASSED',
message: 'Data matches schema from "not"'
};
break;
case 'additional':
properties = o.ns;
error = {
code: 'ADDITIONAL_PROPERTIES',
message: 'Additional properties not allowed: ' +
properties[properties.length - 1]
};
break;
case 'format':
error = {
code: 'FORMAT',
message: 'Value does not satisfy format: ' +
o.schema.format
};
break;
}
} catch (err) {
// ignore errors
}
// unhandled errors
if (!error) {
error = {
code: 'FAILED',
message: 'Validation error: ' + key
};
try {
if (typeof o.validation[key] !== 'boolean') {
error.message = ' (' + o.validation[key] + ')';
}
} catch (err) {
// ignore errors
}
}
error.code = 'VALIDATION_' + error.code;
if (o.data !== undefined) error.data = o.data;
error.path = o.ns;
errors.push(error);
});
} else {
// handle all non-leaf children
keys.forEach(function(key) {
var s;
if (o.schema && o.schema.$ref) {
if (o.schema.$ref.match(/#\/definitions\//)) {
o.schema = o.definitions[o.schema.$ref.slice(14)];
} else {
o.schema = o.schema.$ref;
}
if (typeof o.schema === 'string') {
o.schema = o.env.resolveRef(null, o.schema);
if (o.schema) o.schema = o.schema[0];
}
if (!o.schema.type) o.schema.type = 'object';
}
if (o.schema && o.schema.type) {
if (allowsType(o.schema, 'object')) {
if (o.schema.properties && o.schema.properties[key]) {
s = o.schema.properties[key];
}
if (!s && o.schema.patternProperties) {
Object.keys(o.schema.patternProperties).some(function(pkey) {
if (key.match(new RegExp(pkey))) {
s = o.schema.patternProperties[pkey];
return true;
}
});
}
if (!s && o.schema.hasOwnProperty('additionalProperties')) {
if (typeof o.schema.additionalProperties === 'boolean') {
s = {};
} else {
s = o.schema.additionalProperties;
}
}
}
if (allowsType(o.schema, 'array')) {
s = o.schema.items;
}
}
var opts = {
env: o.env,
schema: s || {},
ns: o.ns.concat(key)
};
try {
opts.data = o.data[key];
} catch (err) {
// ignore errors
}
try {
opts.validation = o.validation[key].schema ?
o.validation[key].schema :
o.validation[key];
} catch (err) {
opts.validation = {};
}
try {
opts.definitions = s.definitions || o.definitions;
} catch (err) {
opts.definitions = o.definitions;
}
errors = errors.concat(make(opts));
});
}
return errors;
}
function formatPath(options) {
var root = options.hasOwnProperty('root') ?
options.root : '$';
var sep = options.hasOwnProperty('sep') ?
options.sep : '.';
return function(error) {
var path = root;
error.path.forEach(function(key) {
path += key.match(/^\d+$/) ?
'[' + key + ']' :
key.match(/^[A-Z_$][0-9A-Z_$]*$/i) ?
(sep + key) :
('[' + JSON.stringify(key) + ']');
});
error.path = path;
return error;
};
}
function jjve(env) {
return function jjve(schema, data, result, options) {
if (!result || !result.validation) return [];
options = options || {};
if (typeof schema === 'string') { schema = env.schema[schema]; }
var errors = make({
env: env,
schema: schema,
data: data,
validation: result.validation,
ns: [],
definitions: schema.definitions || {}
});
if (errors.length && options.formatPath !== false) {
return errors.map(formatPath(options));
}
return errors;
};
}
// Export for use in server and client.
if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
module.exports = jjve;
} else if (typeof define === 'function' && define.amd) {
define(function() { return jjve; });
} else {
this.jjve = jjve;
}
}).call(this);