z-schema 6.0.1 → 7.0.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. package/README.md +154 -137
  2. package/bin/z-schema +128 -124
  3. package/dist/Errors.js +50 -0
  4. package/dist/FormatValidators.js +136 -0
  5. package/{src → dist}/JsonValidation.js +186 -212
  6. package/dist/Report.js +220 -0
  7. package/{src → dist}/SchemaCache.js +67 -82
  8. package/{src → dist}/SchemaCompilation.js +89 -129
  9. package/dist/SchemaValidation.js +631 -0
  10. package/{src → dist}/Utils.js +96 -104
  11. package/dist/ZSchema-umd-min.js +1 -0
  12. package/dist/ZSchema-umd.js +13791 -0
  13. package/dist/ZSchema.cjs +13785 -0
  14. package/dist/ZSchema.js +366 -0
  15. package/dist/schemas/hyper-schema.json +156 -0
  16. package/dist/schemas/schema.json +151 -0
  17. package/dist/types/Errors.d.ts +44 -0
  18. package/dist/types/FormatValidators.d.ts +12 -0
  19. package/dist/types/JsonValidation.d.ts +37 -0
  20. package/dist/types/Report.d.ts +87 -0
  21. package/dist/types/SchemaCache.d.ts +26 -0
  22. package/dist/types/SchemaCompilation.d.ts +1 -0
  23. package/dist/types/SchemaValidation.d.ts +6 -0
  24. package/dist/types/Utils.d.ts +64 -0
  25. package/dist/types/ZSchema.d.ts +97 -0
  26. package/package.json +54 -43
  27. package/src/Errors.ts +56 -0
  28. package/src/FormatValidators.ts +136 -0
  29. package/src/JsonValidation.ts +624 -0
  30. package/src/Report.ts +337 -0
  31. package/src/SchemaCache.ts +189 -0
  32. package/src/SchemaCompilation.ts +293 -0
  33. package/src/SchemaValidation.ts +629 -0
  34. package/src/Utils.ts +286 -0
  35. package/src/ZSchema.ts +469 -0
  36. package/src/schemas/_ +0 -0
  37. package/dist/ZSchema-browser-min.js +0 -2
  38. package/dist/ZSchema-browser-min.js.map +0 -1
  39. package/dist/ZSchema-browser-test.js +0 -30633
  40. package/dist/ZSchema-browser.js +0 -13429
  41. package/index.d.ts +0 -175
  42. package/src/Errors.js +0 -60
  43. package/src/FormatValidators.js +0 -129
  44. package/src/Polyfills.js +0 -16
  45. package/src/Report.js +0 -299
  46. package/src/SchemaValidation.js +0 -619
  47. package/src/ZSchema.js +0 -409
@@ -1,619 +0,0 @@
1
- "use strict";
2
-
3
- var FormatValidators = require("./FormatValidators"),
4
- JsonValidation = require("./JsonValidation"),
5
- Report = require("./Report"),
6
- Utils = require("./Utils");
7
-
8
- var SchemaValidators = {
9
- $ref: function (report, schema) {
10
- // http://tools.ietf.org/html/draft-ietf-appsawg-json-pointer-07
11
- // http://tools.ietf.org/html/draft-pbryan-zyp-json-ref-03
12
- if (typeof schema.$ref !== "string") {
13
- report.addError("KEYWORD_TYPE_EXPECTED", ["$ref", "string"]);
14
- }
15
- },
16
- $schema: function (report, schema) {
17
- // http://json-schema.org/latest/json-schema-core.html#rfc.section.6
18
- if (typeof schema.$schema !== "string") {
19
- report.addError("KEYWORD_TYPE_EXPECTED", ["$schema", "string"]);
20
- }
21
- },
22
- multipleOf: function (report, schema) {
23
- // http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.1.1.1
24
- if (typeof schema.multipleOf !== "number") {
25
- report.addError("KEYWORD_TYPE_EXPECTED", ["multipleOf", "number"]);
26
- } else if (schema.multipleOf <= 0) {
27
- report.addError("KEYWORD_MUST_BE", ["multipleOf", "strictly greater than 0"]);
28
- }
29
- },
30
- maximum: function (report, schema) {
31
- // http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.1.2.1
32
- if (typeof schema.maximum !== "number") {
33
- report.addError("KEYWORD_TYPE_EXPECTED", ["maximum", "number"]);
34
- }
35
- },
36
- exclusiveMaximum: function (report, schema) {
37
- // http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.1.2.1
38
- if (typeof schema.exclusiveMaximum !== "boolean") {
39
- report.addError("KEYWORD_TYPE_EXPECTED", ["exclusiveMaximum", "boolean"]);
40
- } else if (schema.maximum === undefined) {
41
- report.addError("KEYWORD_DEPENDENCY", ["exclusiveMaximum", "maximum"]);
42
- }
43
- },
44
- minimum: function (report, schema) {
45
- // http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.1.3.1
46
- if (typeof schema.minimum !== "number") {
47
- report.addError("KEYWORD_TYPE_EXPECTED", ["minimum", "number"]);
48
- }
49
- },
50
- exclusiveMinimum: function (report, schema) {
51
- // http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.1.3.1
52
- if (typeof schema.exclusiveMinimum !== "boolean") {
53
- report.addError("KEYWORD_TYPE_EXPECTED", ["exclusiveMinimum", "boolean"]);
54
- } else if (schema.minimum === undefined) {
55
- report.addError("KEYWORD_DEPENDENCY", ["exclusiveMinimum", "minimum"]);
56
- }
57
- },
58
- maxLength: function (report, schema) {
59
- // http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.2.1.1
60
- if (Utils.whatIs(schema.maxLength) !== "integer") {
61
- report.addError("KEYWORD_TYPE_EXPECTED", ["maxLength", "integer"]);
62
- } else if (schema.maxLength < 0) {
63
- report.addError("KEYWORD_MUST_BE", ["maxLength", "greater than, or equal to 0"]);
64
- }
65
- },
66
- minLength: function (report, schema) {
67
- // http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.2.2.1
68
- if (Utils.whatIs(schema.minLength) !== "integer") {
69
- report.addError("KEYWORD_TYPE_EXPECTED", ["minLength", "integer"]);
70
- } else if (schema.minLength < 0) {
71
- report.addError("KEYWORD_MUST_BE", ["minLength", "greater than, or equal to 0"]);
72
- }
73
- },
74
- pattern: function (report, schema) {
75
- // http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.2.3.1
76
- if (typeof schema.pattern !== "string") {
77
- report.addError("KEYWORD_TYPE_EXPECTED", ["pattern", "string"]);
78
- } else {
79
- try {
80
- RegExp(schema.pattern);
81
- } catch (e) {
82
- report.addError("KEYWORD_PATTERN", ["pattern", schema.pattern]);
83
- }
84
- }
85
- },
86
- additionalItems: function (report, schema) {
87
- // http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.3.1.1
88
- var type = Utils.whatIs(schema.additionalItems);
89
- if (type !== "boolean" && type !== "object") {
90
- report.addError("KEYWORD_TYPE_EXPECTED", ["additionalItems", ["boolean", "object"]]);
91
- } else if (type === "object") {
92
- report.path.push("additionalItems");
93
- exports.validateSchema.call(this, report, schema.additionalItems);
94
- report.path.pop();
95
- }
96
- },
97
- items: function (report, schema) {
98
- // http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.3.1.1
99
- var type = Utils.whatIs(schema.items);
100
-
101
- if (type === "object") {
102
- report.path.push("items");
103
- exports.validateSchema.call(this, report, schema.items);
104
- report.path.pop();
105
- } else if (type === "array") {
106
- var idx = schema.items.length;
107
- while (idx--) {
108
- report.path.push("items");
109
- report.path.push(idx.toString());
110
- exports.validateSchema.call(this, report, schema.items[idx]);
111
- report.path.pop();
112
- report.path.pop();
113
- }
114
- } else {
115
- report.addError("KEYWORD_TYPE_EXPECTED", ["items", ["array", "object"]]);
116
- }
117
-
118
- // custom - strict mode
119
- if (this.options.forceAdditional === true && schema.additionalItems === undefined && Array.isArray(schema.items)) {
120
- report.addError("KEYWORD_UNDEFINED_STRICT", ["additionalItems"]);
121
- }
122
- // custome - assume defined false mode
123
- if (this.options.assumeAdditional && schema.additionalItems === undefined && Array.isArray(schema.items)) {
124
- schema.additionalItems = false;
125
- }
126
- },
127
- maxItems: function (report, schema) {
128
- // http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.3.2.1
129
- if (typeof schema.maxItems !== "number") {
130
- report.addError("KEYWORD_TYPE_EXPECTED", ["maxItems", "integer"]);
131
- } else if (schema.maxItems < 0) {
132
- report.addError("KEYWORD_MUST_BE", ["maxItems", "greater than, or equal to 0"]);
133
- }
134
- },
135
- minItems: function (report, schema) {
136
- // http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.3.3.1
137
- if (Utils.whatIs(schema.minItems) !== "integer") {
138
- report.addError("KEYWORD_TYPE_EXPECTED", ["minItems", "integer"]);
139
- } else if (schema.minItems < 0) {
140
- report.addError("KEYWORD_MUST_BE", ["minItems", "greater than, or equal to 0"]);
141
- }
142
- },
143
- uniqueItems: function (report, schema) {
144
- // http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.3.4.1
145
- if (typeof schema.uniqueItems !== "boolean") {
146
- report.addError("KEYWORD_TYPE_EXPECTED", ["uniqueItems", "boolean"]);
147
- }
148
- },
149
- maxProperties: function (report, schema) {
150
- // http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.4.1.1
151
- if (Utils.whatIs(schema.maxProperties) !== "integer") {
152
- report.addError("KEYWORD_TYPE_EXPECTED", ["maxProperties", "integer"]);
153
- } else if (schema.maxProperties < 0) {
154
- report.addError("KEYWORD_MUST_BE", ["maxProperties", "greater than, or equal to 0"]);
155
- }
156
- },
157
- minProperties: function (report, schema) {
158
- // http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.4.2.1
159
- if (Utils.whatIs(schema.minProperties) !== "integer") {
160
- report.addError("KEYWORD_TYPE_EXPECTED", ["minProperties", "integer"]);
161
- } else if (schema.minProperties < 0) {
162
- report.addError("KEYWORD_MUST_BE", ["minProperties", "greater than, or equal to 0"]);
163
- }
164
- },
165
- required: function (report, schema) {
166
- // http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.4.3.1
167
- if (Utils.whatIs(schema.required) !== "array") {
168
- report.addError("KEYWORD_TYPE_EXPECTED", ["required", "array"]);
169
- } else if (schema.required.length === 0) {
170
- report.addError("KEYWORD_MUST_BE", ["required", "an array with at least one element"]);
171
- } else {
172
- var idx = schema.required.length;
173
- while (idx--) {
174
- if (typeof schema.required[idx] !== "string") {
175
- report.addError("KEYWORD_VALUE_TYPE", ["required", "string"]);
176
- }
177
- }
178
- if (Utils.isUniqueArray(schema.required) === false) {
179
- report.addError("KEYWORD_MUST_BE", ["required", "an array with unique items"]);
180
- }
181
- }
182
- },
183
- additionalProperties: function (report, schema) {
184
- // http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.4.4.1
185
- var type = Utils.whatIs(schema.additionalProperties);
186
- if (type !== "boolean" && type !== "object") {
187
- report.addError("KEYWORD_TYPE_EXPECTED", ["additionalProperties", ["boolean", "object"]]);
188
- } else if (type === "object") {
189
- report.path.push("additionalProperties");
190
- exports.validateSchema.call(this, report, schema.additionalProperties);
191
- report.path.pop();
192
- }
193
- },
194
- properties: function (report, schema) {
195
- // http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.4.4.1
196
- if (Utils.whatIs(schema.properties) !== "object") {
197
- report.addError("KEYWORD_TYPE_EXPECTED", ["properties", "object"]);
198
- return;
199
- }
200
-
201
- var keys = Object.keys(schema.properties),
202
- idx = keys.length;
203
- while (idx--) {
204
- var key = keys[idx],
205
- val = schema.properties[key];
206
- report.path.push("properties");
207
- report.path.push(key);
208
- exports.validateSchema.call(this, report, val);
209
- report.path.pop();
210
- report.path.pop();
211
- }
212
-
213
- // custom - strict mode
214
- if (this.options.forceAdditional === true && schema.additionalProperties === undefined) {
215
- report.addError("KEYWORD_UNDEFINED_STRICT", ["additionalProperties"]);
216
- }
217
- // custome - assume defined false mode
218
- if (this.options.assumeAdditional && schema.additionalProperties === undefined) {
219
- schema.additionalProperties = false;
220
- }
221
- // custom - forceProperties
222
- if (this.options.forceProperties === true && keys.length === 0) {
223
- report.addError("CUSTOM_MODE_FORCE_PROPERTIES", ["properties"]);
224
- }
225
- },
226
- patternProperties: function (report, schema) {
227
- // http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.4.4.1
228
- if (Utils.whatIs(schema.patternProperties) !== "object") {
229
- report.addError("KEYWORD_TYPE_EXPECTED", ["patternProperties", "object"]);
230
- return;
231
- }
232
-
233
- var keys = Object.keys(schema.patternProperties),
234
- idx = keys.length;
235
- while (idx--) {
236
- var key = keys[idx],
237
- val = schema.patternProperties[key];
238
- try {
239
- RegExp(key);
240
- } catch (e) {
241
- report.addError("KEYWORD_PATTERN", ["patternProperties", key]);
242
- }
243
- report.path.push("patternProperties");
244
- report.path.push(key.toString());
245
- exports.validateSchema.call(this, report, val);
246
- report.path.pop();
247
- report.path.pop();
248
- }
249
-
250
- // custom - forceProperties
251
- if (this.options.forceProperties === true && keys.length === 0) {
252
- report.addError("CUSTOM_MODE_FORCE_PROPERTIES", ["patternProperties"]);
253
- }
254
- },
255
- dependencies: function (report, schema) {
256
- // http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.4.5.1
257
- if (Utils.whatIs(schema.dependencies) !== "object") {
258
- report.addError("KEYWORD_TYPE_EXPECTED", ["dependencies", "object"]);
259
- } else {
260
- var keys = Object.keys(schema.dependencies),
261
- idx = keys.length;
262
- while (idx--) {
263
- var schemaKey = keys[idx],
264
- schemaDependency = schema.dependencies[schemaKey],
265
- type = Utils.whatIs(schemaDependency);
266
-
267
- if (type === "object") {
268
- report.path.push("dependencies");
269
- report.path.push(schemaKey);
270
- exports.validateSchema.call(this, report, schemaDependency);
271
- report.path.pop();
272
- report.path.pop();
273
- } else if (type === "array") {
274
- var idx2 = schemaDependency.length;
275
- if (idx2 === 0) {
276
- report.addError("KEYWORD_MUST_BE", ["dependencies", "not empty array"]);
277
- }
278
- while (idx2--) {
279
- if (typeof schemaDependency[idx2] !== "string") {
280
- report.addError("KEYWORD_VALUE_TYPE", ["dependensices", "string"]);
281
- }
282
- }
283
- if (Utils.isUniqueArray(schemaDependency) === false) {
284
- report.addError("KEYWORD_MUST_BE", ["dependencies", "an array with unique items"]);
285
- }
286
- } else {
287
- report.addError("KEYWORD_VALUE_TYPE", ["dependencies", "object or array"]);
288
- }
289
- }
290
- }
291
- },
292
- enum: function (report, schema) {
293
- // http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.5.1.1
294
- if (Array.isArray(schema.enum) === false) {
295
- report.addError("KEYWORD_TYPE_EXPECTED", ["enum", "array"]);
296
- } else if (schema.enum.length === 0) {
297
- report.addError("KEYWORD_MUST_BE", ["enum", "an array with at least one element"]);
298
- } else if (Utils.isUniqueArray(schema.enum) === false) {
299
- report.addError("KEYWORD_MUST_BE", ["enum", "an array with unique elements"]);
300
- }
301
- },
302
- type: function (report, schema) {
303
- // http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.5.2.1
304
- var primitiveTypes = ["array", "boolean", "integer", "number", "null", "object", "string"],
305
- primitiveTypeStr = primitiveTypes.join(","),
306
- isArray = Array.isArray(schema.type);
307
-
308
- if (isArray) {
309
- var idx = schema.type.length;
310
- while (idx--) {
311
- if (primitiveTypes.indexOf(schema.type[idx]) === -1) {
312
- report.addError("KEYWORD_TYPE_EXPECTED", ["type", primitiveTypeStr]);
313
- }
314
- }
315
- if (Utils.isUniqueArray(schema.type) === false) {
316
- report.addError("KEYWORD_MUST_BE", ["type", "an object with unique properties"]);
317
- }
318
- } else if (typeof schema.type === "string") {
319
- if (primitiveTypes.indexOf(schema.type) === -1) {
320
- report.addError("KEYWORD_TYPE_EXPECTED", ["type", primitiveTypeStr]);
321
- }
322
- } else {
323
- report.addError("KEYWORD_TYPE_EXPECTED", ["type", ["string", "array"]]);
324
- }
325
-
326
- if (this.options.noEmptyStrings === true) {
327
- if (schema.type === "string" || isArray && schema.type.indexOf("string") !== -1) {
328
- if (schema.minLength === undefined &&
329
- schema.enum === undefined &&
330
- schema.format === undefined) {
331
-
332
- schema.minLength = 1;
333
- }
334
- }
335
- }
336
- if (this.options.noEmptyArrays === true) {
337
- if (schema.type === "array" || isArray && schema.type.indexOf("array") !== -1) {
338
- if (schema.minItems === undefined) {
339
- schema.minItems = 1;
340
- }
341
- }
342
- }
343
- if (this.options.forceProperties === true) {
344
- if (schema.type === "object" || isArray && schema.type.indexOf("object") !== -1) {
345
- if (schema.properties === undefined && schema.patternProperties === undefined) {
346
- report.addError("KEYWORD_UNDEFINED_STRICT", ["properties"]);
347
- }
348
- }
349
- }
350
- if (this.options.forceItems === true) {
351
- if (schema.type === "array" || isArray && schema.type.indexOf("array") !== -1) {
352
- if (schema.items === undefined) {
353
- report.addError("KEYWORD_UNDEFINED_STRICT", ["items"]);
354
- }
355
- }
356
- }
357
- if (this.options.forceMinItems === true) {
358
- if (schema.type === "array" || isArray && schema.type.indexOf("array") !== -1) {
359
- if (schema.minItems === undefined) {
360
- report.addError("KEYWORD_UNDEFINED_STRICT", ["minItems"]);
361
- }
362
- }
363
- }
364
- if (this.options.forceMaxItems === true) {
365
- if (schema.type === "array" || isArray && schema.type.indexOf("array") !== -1) {
366
- if (schema.maxItems === undefined) {
367
- report.addError("KEYWORD_UNDEFINED_STRICT", ["maxItems"]);
368
- }
369
- }
370
- }
371
- if (this.options.forceMinLength === true) {
372
- if (schema.type === "string" || isArray && schema.type.indexOf("string") !== -1) {
373
- if (schema.minLength === undefined &&
374
- schema.format === undefined &&
375
- schema.enum === undefined &&
376
- schema.pattern === undefined) {
377
- report.addError("KEYWORD_UNDEFINED_STRICT", ["minLength"]);
378
- }
379
- }
380
- }
381
- if (this.options.forceMaxLength === true) {
382
- if (schema.type === "string" || isArray && schema.type.indexOf("string") !== -1) {
383
- if (schema.maxLength === undefined &&
384
- schema.format === undefined &&
385
- schema.enum === undefined &&
386
- schema.pattern === undefined) {
387
- report.addError("KEYWORD_UNDEFINED_STRICT", ["maxLength"]);
388
- }
389
- }
390
- }
391
- },
392
- allOf: function (report, schema) {
393
- // http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.5.3.1
394
- if (Array.isArray(schema.allOf) === false) {
395
- report.addError("KEYWORD_TYPE_EXPECTED", ["allOf", "array"]);
396
- } else if (schema.allOf.length === 0) {
397
- report.addError("KEYWORD_MUST_BE", ["allOf", "an array with at least one element"]);
398
- } else {
399
- var idx = schema.allOf.length;
400
- while (idx--) {
401
- report.path.push("allOf");
402
- report.path.push(idx.toString());
403
- exports.validateSchema.call(this, report, schema.allOf[idx]);
404
- report.path.pop();
405
- report.path.pop();
406
- }
407
- }
408
- },
409
- anyOf: function (report, schema) {
410
- // http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.5.4.1
411
- if (Array.isArray(schema.anyOf) === false) {
412
- report.addError("KEYWORD_TYPE_EXPECTED", ["anyOf", "array"]);
413
- } else if (schema.anyOf.length === 0) {
414
- report.addError("KEYWORD_MUST_BE", ["anyOf", "an array with at least one element"]);
415
- } else {
416
- var idx = schema.anyOf.length;
417
- while (idx--) {
418
- report.path.push("anyOf");
419
- report.path.push(idx.toString());
420
- exports.validateSchema.call(this, report, schema.anyOf[idx]);
421
- report.path.pop();
422
- report.path.pop();
423
- }
424
- }
425
- },
426
- oneOf: function (report, schema) {
427
- // http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.5.5.1
428
- if (Array.isArray(schema.oneOf) === false) {
429
- report.addError("KEYWORD_TYPE_EXPECTED", ["oneOf", "array"]);
430
- } else if (schema.oneOf.length === 0) {
431
- report.addError("KEYWORD_MUST_BE", ["oneOf", "an array with at least one element"]);
432
- } else {
433
- var idx = schema.oneOf.length;
434
- while (idx--) {
435
- report.path.push("oneOf");
436
- report.path.push(idx.toString());
437
- exports.validateSchema.call(this, report, schema.oneOf[idx]);
438
- report.path.pop();
439
- report.path.pop();
440
- }
441
- }
442
- },
443
- not: function (report, schema) {
444
- // http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.5.6.1
445
- if (Utils.whatIs(schema.not) !== "object") {
446
- report.addError("KEYWORD_TYPE_EXPECTED", ["not", "object"]);
447
- } else {
448
- report.path.push("not");
449
- exports.validateSchema.call(this, report, schema.not);
450
- report.path.pop();
451
- }
452
- },
453
- definitions: function (report, schema) {
454
- // http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.5.7.1
455
- if (Utils.whatIs(schema.definitions) !== "object") {
456
- report.addError("KEYWORD_TYPE_EXPECTED", ["definitions", "object"]);
457
- } else {
458
- var keys = Object.keys(schema.definitions),
459
- idx = keys.length;
460
- while (idx--) {
461
- var key = keys[idx],
462
- val = schema.definitions[key];
463
- report.path.push("definitions");
464
- report.path.push(key);
465
- exports.validateSchema.call(this, report, val);
466
- report.path.pop();
467
- report.path.pop();
468
- }
469
- }
470
- },
471
- format: function (report, schema) {
472
- if (typeof schema.format !== "string") {
473
- report.addError("KEYWORD_TYPE_EXPECTED", ["format", "string"]);
474
- } else {
475
- if (FormatValidators[schema.format] === undefined && this.options.ignoreUnknownFormats !== true) {
476
- report.addError("UNKNOWN_FORMAT", [schema.format]);
477
- }
478
- }
479
- },
480
- id: function (report, schema) {
481
- // http://json-schema.org/latest/json-schema-core.html#rfc.section.7.2
482
- if (typeof schema.id !== "string") {
483
- report.addError("KEYWORD_TYPE_EXPECTED", ["id", "string"]);
484
- }
485
- },
486
- title: function (report, schema) {
487
- // http://json-schema.org/latest/json-schema-validation.html#rfc.section.6.1
488
- if (typeof schema.title !== "string") {
489
- report.addError("KEYWORD_TYPE_EXPECTED", ["title", "string"]);
490
- }
491
- },
492
- description: function (report, schema) {
493
- // http://json-schema.org/latest/json-schema-validation.html#rfc.section.6.1
494
- if (typeof schema.description !== "string") {
495
- report.addError("KEYWORD_TYPE_EXPECTED", ["description", "string"]);
496
- }
497
- },
498
- "default": function (/* report, schema */) {
499
- // http://json-schema.org/latest/json-schema-validation.html#rfc.section.6.2
500
- // There are no restrictions placed on the value of this keyword.
501
- }
502
- };
503
-
504
- /**
505
- *
506
- * @param {Report} report
507
- * @param {*[]} arr
508
- *
509
- * @returns {boolean}
510
- */
511
- var validateArrayOfSchemas = function (report, arr) {
512
- var idx = arr.length;
513
- while (idx--) {
514
- exports.validateSchema.call(this, report, arr[idx]);
515
- }
516
- return report.isValid();
517
- };
518
-
519
- /**
520
- *
521
- * @param {Report} report
522
- * @param {*} schema
523
- */
524
- exports.validateSchema = function (report, schema) {
525
-
526
- report.commonErrorMessage = "SCHEMA_VALIDATION_FAILED";
527
-
528
- // if schema is an array, assume it's an array of schemas
529
- if (Array.isArray(schema)) {
530
- return validateArrayOfSchemas.call(this, report, schema);
531
- }
532
-
533
- // do not revalidate schema that has already been validated once
534
- if (schema.__$validated) {
535
- return true;
536
- }
537
-
538
- // if $schema is present, this schema should validate against that $schema
539
- var hasParentSchema = schema.$schema && schema.id !== schema.$schema;
540
- if (hasParentSchema) {
541
- if (schema.__$schemaResolved && schema.__$schemaResolved !== schema) {
542
- var subReport = new Report(report);
543
- var valid = JsonValidation.validate.call(this, subReport, schema.__$schemaResolved, schema);
544
- if (valid === false) {
545
- report.addError("PARENT_SCHEMA_VALIDATION_FAILED", null, subReport);
546
- }
547
- } else {
548
- if (this.options.ignoreUnresolvableReferences !== true) {
549
- report.addError("REF_UNRESOLVED", [schema.$schema]);
550
- }
551
- }
552
- }
553
-
554
- if (this.options.noTypeless === true) {
555
- // issue #36 - inherit type to anyOf, oneOf, allOf if noTypeless is defined
556
- if (schema.type !== undefined) {
557
- var schemas = [];
558
- if (Array.isArray(schema.anyOf)) { schemas = schemas.concat(schema.anyOf); }
559
- if (Array.isArray(schema.oneOf)) { schemas = schemas.concat(schema.oneOf); }
560
- if (Array.isArray(schema.allOf)) { schemas = schemas.concat(schema.allOf); }
561
- schemas.forEach(function (sch) {
562
- if (!sch.type) { sch.type = schema.type; }
563
- });
564
- }
565
- // end issue #36
566
- if (schema.enum === undefined &&
567
- schema.type === undefined &&
568
- schema.anyOf === undefined &&
569
- schema.oneOf === undefined &&
570
- schema.not === undefined &&
571
- schema.$ref === undefined) {
572
- report.addError("KEYWORD_UNDEFINED_STRICT", ["type"]);
573
- }
574
- }
575
-
576
- var keys = Object.keys(schema),
577
- idx = keys.length;
578
- while (idx--) {
579
- var key = keys[idx];
580
- if (key.indexOf("__") === 0) { continue; }
581
- if (SchemaValidators[key] !== undefined) {
582
- SchemaValidators[key].call(this, report, schema);
583
- } else if (!hasParentSchema) {
584
- if (this.options.noExtraKeywords === true) {
585
- report.addError("KEYWORD_UNEXPECTED", [key]);
586
- }
587
- }
588
- }
589
-
590
- if (this.options.pedanticCheck === true) {
591
- if (schema.enum) {
592
- // break recursion
593
- var tmpSchema = Utils.clone(schema);
594
- delete tmpSchema.enum;
595
- delete tmpSchema.default;
596
-
597
- report.path.push("enum");
598
- idx = schema.enum.length;
599
- while (idx--) {
600
- report.path.push(idx.toString());
601
- JsonValidation.validate.call(this, report, tmpSchema, schema.enum[idx]);
602
- report.path.pop();
603
- }
604
- report.path.pop();
605
- }
606
-
607
- if (schema.default) {
608
- report.path.push("default");
609
- JsonValidation.validate.call(this, report, schema, schema.default);
610
- report.path.pop();
611
- }
612
- }
613
-
614
- var isValid = report.isValid();
615
- if (isValid) {
616
- schema.__$validated = true;
617
- }
618
- return isValid;
619
- };