-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjassi.js
314 lines (254 loc) · 9.57 KB
/
jassi.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
/*
* Jassi v0.1.2
* https://github.com/iclanzan/jassi
*
* Copyright (c) 2014 Sorin Iclanzan <[email protected]>
* License: https://github.com/iclanzan/jassi
*/
(function(root, undefined) {
'use strict';
var isArray = Array.isArray;
var keys = Object.keys;
/**
* Check if a given value is an instance of a JSON object.
* This means that arrays and the null object are not considered objects.
*
* @param {any} value Any value to be checked
* @return {Boolean} Returns true if the value is an instance of a JSON object, false otherwise.
*/
function isObject(value) {
return null !== value && typeof value == 'object' && !isArray(value);
}
/**
* Get the type of a value.
*
* JSON primitive types:
* Array, Boolean, Number, null, Object, String
*
* @param {any} value Any value
* @return {String} One of the JSON primitive types.
*/
function getType(value) {
return isObject(value) && 'object' ||
isArray(value) && 'array' ||
null === value && 'null' ||
typeof value;
}
/**
* Check if two items are equal as per the JSON Schema spec.
*
* @param {any} item1 The first item
* @param {any} item2 The second item
* @return {Boolean} Returns true if the items are equal.
*/
function areEqual(item1, item2) {
var type1 = getType(item1);
var type2 = getType(item2);
var i, l, keys1, keys2, key;
if (type1 != type2) return false;
if ('array' == type1) {
if (item1.length !== item2.length) return false;
for (i = 0, l = item1.length; i < l; i ++)
if (!areEqual(item1[i], item2[i])) return false;
return true;
}
if ('object' == type1) {
keys1 = keys(item1);
keys2 = keys(item2);
if (keys1.length !== keys2.length) return false;
for (i = 0, l = keys1.length; i < l; i ++) {
key = keys1[i];
if (!item2.hasOwnProperty(key) || !areEqual(item1[key], item2[key])) return false;
}
return true;
}
return item1 === item2;
}
function or(item1, item2) {
return undefined !== item1 ? item1 : item2;
}
/**
* Validate a JSON instance against a schema.
*
* The function returns an empty array if validation is successful.
*
* @param {any} instance An instance of a JSON data that needs to be validated
* @param {Object} schema The schema to validate the instance against
* @param {String} path Optional. The path to the property that is being validated.
* @return {Array} An array of objects describing validation errors.
*/
var validate = function(instance, schema, path) {
var errors = [], type, l, i, j, items, itemsIsArray, additional, additionalIsObject, found, properties, pattern, pp;
function addError(message) {
errors.push({property:path, message: message});
return errors;
}
if (undefined === path) path = '';
if (!isObject(schema)) return addError('Invalid schema.');
type = getType(instance);
if (schema.type) {
items = isArray(schema.type) ? schema.type : [schema.type];
if (!~items.indexOf(type) && (type != 'number' || !~items.indexOf('integer') || instance % 1 != 0)) {
addError('Invalid type. Was expecting ' + schema.type + ' but found ' + type + '.');
}
}
if ('array' == type) {
l = instance.length;
if (schema.items || schema.additionalItems) {
items = schema.items || {};
itemsIsArray = isArray(schema.items);
additional = schema.additionalItems;
additionalIsObject = isObject(schema.additionalItems);
if (itemsIsArray && false === additional && l > (j = items.length))
addError('The instance can only have up to ' + j + ' items.');
else for (i = 0; i < l; i ++)
errors = errors.concat(validate(
instance[i],
itemsIsArray ? items[i] || additionalIsObject && additional || {} : items,
path + '[' + i + ']'
));
}
if (schema.maxItems && l > schema.maxItems)
addError('There must be a maximum of ' + schema.maxItems + ' item(s) in the array.');
if (schema.minItems && l < schema.minItems)
addError('There must be a minimum of ' + schema.minItems + ' item(s) in the array.');
if (schema.uniqueItems) {
dance: for (i = 0; i < l; i ++) {
for (j = i + 1; j < l; j ++) {
if (areEqual(instance[i], instance[j])) {
addError("The items in the array must be unique.");
break dance;
}
}
}
}
}
if ('object' == type) {
if (schema.maxProperties && keys(instance).length > schema.maxProperties)
addError('The instance must have at most ' + schema.maxProperties + ' members.');
if (schema.minProperties && keys(instance).length < schema.minProperties)
addError('The instance must have at least ' + schema.minProperties + ' members.');
if (schema.required)
schema.required.forEach(function(requiredProperty) {
if (!instance.hasOwnProperty(requiredProperty))
addError('Required property "' + requiredProperty + '" is missing.');
});
if (schema.properties || schema.additionalProperties || schema.patternProperties) {
properties = or(schema.properties, {});
pattern = or(schema.patternProperties, {});
additional = or(schema.additionalProperties, {});
pp = keys(pattern);
}
keys(instance).forEach(function(key) {
var schemas, dependency;
if (schema.dependencies && (dependency = schema.dependencies[key])) {
if (isArray(dependency)) {
dependency.forEach(function (prop) {
if (!instance.hasOwnProperty(prop)) {
addError('Property "' + key + '" requires "' + prop + '" to also be present.');
}
});
}
else {
errors = errors.concat(validate(instance, dependency, path));
}
}
if (
properties &&
false === additional &&
!properties.hasOwnProperty(key) &&
!(pp && pp.some(function(regex) { return key.match(regex); }))
)
addError('The key "' + key + '" is not allowed to be set.');
else {
schemas = [];
if (properties && properties.hasOwnProperty(key))
schemas.push(properties[key]);
pp && pp.forEach(function(regex) {
if (key.match(regex) && pattern[regex]) {
schemas.push(pattern[regex]);
}
});
if (!schemas.length && additional)
schemas.push(additional);
schemas.forEach(function(schema) {
errors = errors.concat(validate(instance[key], schema, path ? path + '.' + key : key));
});
}
});
}
if ('string' == type) {
if (schema.maxLength && instance.length > schema.maxLength)
addError('The instance must not be more than ' + schema.maxLength + ' character(s) long.');
if (schema.minLength && instance.length < schema.minLength)
addError('The instance must be at least ' + schema.minLength + ' character(s) long.');
if (schema.pattern && !instance.match(schema.pattern))
addError('Regex pattern /' + schema.pattern + '/ is a mismatch.');
}
if ('number' == type) {
if (schema.multipleOf !== undefined && instance / schema.multipleOf % 1 != 0)
addError('The instance is required to be a multiple of ' + schema.multipleOf + '.');
if (schema.maximum !== undefined) {
if (!schema.exclusiveMaximum && schema.maximum < instance)
addError('The instance must have a maximum value of ' + schema.maximum + '.');
if (schema.exclusiveMaximum && schema.maximum <= instance)
addError('The instance must be lower than ' + schema.maximum + '.');
}
if (schema.minimum !== undefined) {
if (!schema.exclusiveMinimum && schema.minimum > instance)
addError('The instance must have a minimum value of ' + schema.minimum + '.');
if (schema.exclusiveMinimum && schema.minimum >= instance)
addError('The instance must be greater than ' + schema.minimum + '.');
}
}
if (schema['enum']) {
items = schema['enum'];
l = items.length;
for (i = 0, found = 0; i < l && !found; i++)
if (areEqual(items[i], instance))
found = 1;
if (!found) addError('The instance must have one of the following values: ' + items.join(', ') + '.');
}
if (schema.allOf) {
schema.allOf.forEach(function(schema) {
errors = errors.concat(validate(instance, schema, path));
});
}
if (schema.anyOf) {
items = schema.anyOf;
l = items.length;
for(i = 0, found = 0; i < l && !found; i++)
if (!validate(instance, items[i], path).length)
found = 1;
if (!found) addError('The instance must validate against at least one schema defined by the "anyOf" keyword.');
}
if (schema.oneOf) {
items = schema.oneOf;
l = items.length;
for (i = 0, found = 0; i < l; i++)
if (!validate(instance, items[i], path).length) {
if (found) {
addError('The instance must validate against exactly one schema defined by the "oneOf" keyword.');
break;
}
found = 1;
}
if (!found) {
addError('The instance must validate against one schema defined by the "oneOf" keyword.');
}
}
if (schema.not && !validate(instance, schema.not, path).length)
addError('The instance must not validate against the schema defined by the "not" keyword.');
return errors;
};
// Expose Jassi as a CommonJS module for Node
if (typeof module !== 'undefined' && module.exports) {
module.exports = validate;
}
// Expose Jassi as an AMD module
else if (typeof define === 'function' && define.amd) {
define(function () { return validate; });
}
// Attach to the root object, usually window.
else root.jassi = validate;
})(this);