-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathdataValidation.js
366 lines (282 loc) · 11.8 KB
/
dataValidation.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
356
357
358
359
360
361
362
363
364
365
366
import {normalizeKeyword, getKeyword, getKey, joinCoords, getSchemaType, valueInChoices} from './util';
import {JOIN_SYMBOL} from './constants';
import EditorState from './editorState';
export default function DataValidator(schema) {
this.schema = schema;
this.errorMap = {};
this.validate = function(data) {
// reset errorMap so that this validator object
// can be reused for same schema
this.errorMap = {};
let validator = this.getValidator(getSchemaType(schema));
if (validator)
validator(this.schema, data, '');
else
this.addError('', 'Invalid schema type: "' + schema.type + '"');
let validation = {isValid: true, errorMap: this.errorMap};
if (Object.keys(this.errorMap).length)
validation['isValid'] = false;
return validation;
};
this.getValidator = function (schema_type) {
schema_type = normalizeKeyword(schema_type);
let func;
switch (schema_type) {
case 'array':
func = this.validateArray;
break;
case 'object':
func = this.validateObject;
break;
case 'allOf':
func = this.validateAllOf;
break;
case 'oneOf':
func = this.validateOneOf;
break;
case 'anyOf':
func = this.validateAnyOf;
break;
case 'string':
func = this.validateString;
break;
case 'boolean':
func = this.validateBoolean;
break;
case 'integer':
func = this.validateInteger;
break;
case 'number':
func = this.validateNumber;
break;
}
if (func)
return func.bind(this);
return func;
};
this.getRef = function(ref) {
return EditorState.getRef(ref, this.schema);
};
this.addError = function(coords, msg) {
if (!this.errorMap.hasOwnProperty(coords))
this.errorMap[coords] = [];
this.errorMap[coords].push(msg);
};
this.joinCoords = function(coords) {
let c = joinCoords.apply(null, coords);
if (c.startsWith(JOIN_SYMBOL))
c = c.slice(1);
return c;
}
this.validateArray = function(schema, data, coords) {
if (!Array.isArray(data)) {
this.addError(coords, "Invalid data type. Expected array.");
return;
}
let next_schema = schema.items;
if (next_schema.hasOwnProperty('$ref'))
next_schema = this.getRef(next_schema.$ref);
let next_type = getSchemaType(next_schema);
let minItems = getKeyword(schema, 'minItems', 'min_items');
let maxItems = getKeyword(schema, 'maxItems', 'max_items');
if (minItems && data.length < parseInt(minItems))
this.addError(coords, 'Minimum ' + minItems + ' items required.');
if (maxItems && data.length > parseInt(maxItems))
this.addError(coords, 'Maximum ' + maxItems + ' items allowed.');
if (getKey(schema, 'uniqueItems')) {
let items_type = next_type;
if (items_type === 'array' || items_type === 'object') {
if (data.length !== new Set(data.map((i) => JSON.stringify(i))).size)
this.addError(coords, 'All items in this list must be unique.');
} else {
if (data.length !== new Set(data).size)
this.addError(coords, 'All items in this list must be unique.');
}
}
let next_validator = this.getValidator(next_type);
// currently allOf is not supported in array items
if (next_type === 'allOf')
next_validator = null;
if (next_validator) {
for (let i = 0; i < data.length; i++)
next_validator(next_schema, data[i], this.joinCoords([coords, i]));
} else
this.addError(coords, 'Unsupported type "' + next_type + '" for array items.');
};
this.validateObject = function(schema, data, coords) {
if (typeof data !== 'object' || Array.isArray(data)) {
this.addError(coords, "Invalid data type. Expected object.");
return;
}
let fields = getKeyword(schema, 'properties', 'keys', {});
let data_keys = Object.keys(data);
let missing_keys = Object.keys(fields).filter((i) => data_keys.indexOf(i) === -1);
if (missing_keys.length) {
this.addError(coords, 'These fields are missing from the data: ' + missing_keys.join(', '));
return;
}
for (let key in data) {
if (!data.hasOwnProperty(key))
continue;
let next_schema;
if (fields.hasOwnProperty(key))
next_schema = fields[key];
else {
if (!schema.hasOwnProperty('additionalProperties'))
continue;
next_schema = schema.additionalProperties;
if (next_schema === true)
next_schema = {type: 'string'};
}
if (next_schema.hasOwnProperty('$ref'))
next_schema = this.getRef(next_schema.$ref);
if (schema.hasOwnProperty('required') && Array.isArray(schema.required)) {
if (schema.required.indexOf(key) > -1 && !next_schema.hasOwnProperty('required'))
next_schema['required'] = true;
}
let next_type = getSchemaType(next_schema);
let next_validator = this.getValidator(next_type);
if (next_validator)
next_validator(next_schema, data[key], this.joinCoords([coords, key]));
else {
this.addError(coords, 'Unsupported type "' + next_type + '" for object properties (keys).');
return;
}
}
if (schema.hasOwnProperty('allOf'))
this.validateAllOf(schema, data, coords);
};
this.validateAllOf = function(schema, data, coords) {
/* Currently, we only support allOf inside object
so we assume the given type to be an object.
*/
let newSchema = {type: 'object', properties: {}};
// combine subschemas
for (let i = 0; i < schema.allOf.length; i++) {
let subschema = schema.allOf[i];
if (subschema.hasOwnProperty('$ref'))
subschema = this.getRef(subschema.$ref);
let fields = getKeyword(subschema, 'properties', 'keys', {});
for (let field in fields)
newSchema.properties[field] = fields[field];
}
this.validateObject(newSchema, data, coords);
};
this.validateOneOf = function(schema, data, coords) {
// :TODO:
};
this.validateAnyOf = function(schema, data, coords) {
// :TODO:
};
this.validateString = function(schema, data, coords) {
if (schema.required && !data) {
this.addError(coords, 'This field is required.');
return;
}
if (typeof data !== 'string') {
this.addError(coords, 'This value is invalid. Must be a valid string.');
return;
}
if (!data) // not required, can be empty
return;
if (schema.minLength && data.length < parseInt(schema.minLength))
this.addError(coords, 'This value must be at least ' + schema.minLength + ' characters long.');
if ((schema.maxLength || schema.maxLength == 0) && data.length > parseInt(schema.maxLength))
this.addError(coords, 'This value may not be longer than ' + schema.maxLength + ' characters.');
if (!valueInChoices(schema, data)) {
this.addError(coords, 'Invalid choice "' + data + '"');
return;
}
let format = normalizeKeyword(schema.format);
let format_invalid = false;
let format_validator;
switch (format) {
case 'email':
format_validator = this.validateEmail;
break;
case 'date':
format_validator = this.validateDate;
break;
case 'time':
format_validator = this.validateTime;
break;
case 'date-time':
format_validator = this.validateDateTime;
break;
}
if (format_validator)
format_validator.call(this, schema, data, coords);
};
this.validateBoolean = function(schema, data, coords) {
if (schema.required && (data === null || data === undefined)) {
this.addError(coords, 'This field is required.');
return;
}
if (typeof data !== 'boolean' && data !== null && data !== undefined)
this.addError(coords, 'Invalid value.');
};
this.validateInteger = function(schema, data, coords) {
if (schema.required && (data === null || data === undefined)) {
this.addError(coords, 'This field is required.');
return;
}
if (data === null) // not required, integer can be null
return;
if (typeof data !== 'number') {
this.addError(coords, 'Invalid value. Only integers allowed.');
return;
}
// 1.0 and 1 must be treated equal
if (data !== parseInt(data)) {
this.addError(coords, 'Invalid value. Only integers allowed.');
return;
}
this.validateNumber(schema, data, coords);
};
this.validateNumber = function(schema, data, coords) {
if (schema.required && (data === null || data === undefined)) {
this.addError(coords, 'This field is required.');
return;
}
if (data === null) // not required, number can be null
return;
if (typeof data !== 'number') {
this.addError(coords, 'Invalid value. Only numbers allowed.');
return;
}
if ((schema.minimum || schema.minimum === 0) && data < schema.minimum)
this.addError(coords, 'This value must not be less than ' + schema.minimum);
if ((schema.maximum || schema.maximum === 0) && data > schema.maximum)
this.addError(coords, 'This value must not be greater than ' + schema.maximum);
if ((schema.exclusiveMinimum || schema.exclusiveMinimum === 0) && data <= schema.exclusiveMinimum)
this.addError(coords, 'This value must be greater than ' + schema.exclusiveMinimum);
if ((schema.exclusiveMaximum || schema.exclusiveMaximum === 0) && data >= schema.exclusiveMaximum)
this.addError(coords, 'This value must be less than ' + schema.exclusiveMaximum);
if ((schema.multipleOf || schema.multipleOf === 0) && ((data * 100) % (schema.multipleOf * 100)) / 100)
this.addError(coords, 'This value must be a multiple of ' + schema.multipleOf);
if (!valueInChoices(schema, data)) {
this.addError(coords, 'Invalid choice "' + data + '"');
return;
}
};
this.validateEmail = function(schema, data, coords) {
// half-arsed validation but will do for the time being
if (data.indexOf(' ') > -1 ) {
this.addError(coords, 'Enter a valid email address.');
return;
}
if (data.length > 320) {
this.addError(coords, 'Email may not be longer than 320 characters');
return;
}
};
this.validateDate = function(schema, data, coords) {
// :TODO:
};
this.validateTime = function(schema, data, coords) {
// :TODO:
};
this.validateDateTime = function(schema, data, coords) {
// :TODO:
};
}