z-schema 3.25.1 → 4.1.0

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.
package/package.json CHANGED
@@ -1,6 +1,9 @@
1
1
  {
2
2
  "name": "z-schema",
3
- "version": "3.25.1",
3
+ "version": "4.1.0",
4
+ "engines": {
5
+ "node": ">=6.0.0"
6
+ },
4
7
  "description": "JSON schema validator",
5
8
  "homepage": "https://github.com/zaggino/z-schema",
6
9
  "authors": [
@@ -64,9 +67,9 @@
64
67
  },
65
68
  "dependencies": {
66
69
  "core-js": "^2.5.7",
67
- "lodash.get": "^4.0.0",
68
- "lodash.isequal": "^4.0.0",
69
- "validator": "^10.0.0"
70
+ "lodash.get": "^4.4.2",
71
+ "lodash.isequal": "^4.5.0",
72
+ "validator": "^10.11.0"
70
73
  },
71
74
  "optionalDependencies": {
72
75
  "commander": "^2.7.1"
@@ -4,18 +4,32 @@ var FormatValidators = require("./FormatValidators"),
4
4
  Report = require("./Report"),
5
5
  Utils = require("./Utils");
6
6
 
7
+ var shouldSkipValidate = function (options, errors) {
8
+ return options &&
9
+ Array.isArray(options.includeErrors) &&
10
+ options.includeErrors.length > 0 &&
11
+ !errors.some(function (err) { return options.includeErrors.includes(err);});
12
+ };
13
+
7
14
  var JsonValidators = {
8
15
  multipleOf: function (report, schema, json) {
9
16
  // http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.1.1.2
17
+ if (shouldSkipValidate(this.validateOptions, ["MULTIPLE_OF"])) {
18
+ return;
19
+ }
10
20
  if (typeof json !== "number") {
11
21
  return;
12
22
  }
23
+
13
24
  if (Utils.whatIs(json / schema.multipleOf) !== "integer") {
14
25
  report.addError("MULTIPLE_OF", [json, schema.multipleOf], null, schema);
15
26
  }
16
27
  },
17
28
  maximum: function (report, schema, json) {
18
29
  // http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.1.2.2
30
+ if (shouldSkipValidate(this.validateOptions, ["MAXIMUM", "MAXIMUM_EXCLUSIVE"])) {
31
+ return;
32
+ }
19
33
  if (typeof json !== "number") {
20
34
  return;
21
35
  }
@@ -34,6 +48,9 @@ var JsonValidators = {
34
48
  },
35
49
  minimum: function (report, schema, json) {
36
50
  // http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.1.3.2
51
+ if (shouldSkipValidate(this.validateOptions, ["MINIMUM", "MINIMUM_EXCLUSIVE"])) {
52
+ return;
53
+ }
37
54
  if (typeof json !== "number") {
38
55
  return;
39
56
  }
@@ -52,6 +69,9 @@ var JsonValidators = {
52
69
  },
53
70
  maxLength: function (report, schema, json) {
54
71
  // http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.2.1.2
72
+ if (shouldSkipValidate(this.validateOptions, ["MAX_LENGTH"])) {
73
+ return;
74
+ }
55
75
  if (typeof json !== "string") {
56
76
  return;
57
77
  }
@@ -61,6 +81,9 @@ var JsonValidators = {
61
81
  },
62
82
  minLength: function (report, schema, json) {
63
83
  // http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.2.2.2
84
+ if (shouldSkipValidate(this.validateOptions, ["MIN_LENGTH"])) {
85
+ return;
86
+ }
64
87
  if (typeof json !== "string") {
65
88
  return;
66
89
  }
@@ -70,6 +93,9 @@ var JsonValidators = {
70
93
  },
71
94
  pattern: function (report, schema, json) {
72
95
  // http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.2.3.2
96
+ if (shouldSkipValidate(this.validateOptions, ["PATTERN"])) {
97
+ return;
98
+ }
73
99
  if (typeof json !== "string") {
74
100
  return;
75
101
  }
@@ -79,6 +105,9 @@ var JsonValidators = {
79
105
  },
80
106
  additionalItems: function (report, schema, json) {
81
107
  // http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.3.1.2
108
+ if (shouldSkipValidate(this.validateOptions, ["ARRAY_ADDITIONAL_ITEMS"])) {
109
+ return;
110
+ }
82
111
  if (!Array.isArray(json)) {
83
112
  return;
84
113
  }
@@ -95,6 +124,9 @@ var JsonValidators = {
95
124
  },
96
125
  maxItems: function (report, schema, json) {
97
126
  // http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.3.2.2
127
+ if (shouldSkipValidate(this.validateOptions, ["ARRAY_LENGTH_LONG"])) {
128
+ return;
129
+ }
98
130
  if (!Array.isArray(json)) {
99
131
  return;
100
132
  }
@@ -104,6 +136,9 @@ var JsonValidators = {
104
136
  },
105
137
  minItems: function (report, schema, json) {
106
138
  // http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.3.3.2
139
+ if (shouldSkipValidate(this.validateOptions, ["ARRAY_LENGTH_SHORT"])) {
140
+ return;
141
+ }
107
142
  if (!Array.isArray(json)) {
108
143
  return;
109
144
  }
@@ -113,6 +148,9 @@ var JsonValidators = {
113
148
  },
114
149
  uniqueItems: function (report, schema, json) {
115
150
  // http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.3.4.2
151
+ if (shouldSkipValidate(this.validateOptions, ["ARRAY_UNIQUE"])) {
152
+ return;
153
+ }
116
154
  if (!Array.isArray(json)) {
117
155
  return;
118
156
  }
@@ -125,6 +163,9 @@ var JsonValidators = {
125
163
  },
126
164
  maxProperties: function (report, schema, json) {
127
165
  // http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.4.1.2
166
+ if (shouldSkipValidate(this.validateOptions, ["OBJECT_PROPERTIES_MAXIMUM"])) {
167
+ return;
168
+ }
128
169
  if (Utils.whatIs(json) !== "object") {
129
170
  return;
130
171
  }
@@ -135,6 +176,9 @@ var JsonValidators = {
135
176
  },
136
177
  minProperties: function (report, schema, json) {
137
178
  // http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.4.2.2
179
+ if (shouldSkipValidate(this.validateOptions, ["OBJECT_PROPERTIES_MINIMUM"])) {
180
+ return;
181
+ }
138
182
  if (Utils.whatIs(json) !== "object") {
139
183
  return;
140
184
  }
@@ -145,6 +189,9 @@ var JsonValidators = {
145
189
  },
146
190
  required: function (report, schema, json) {
147
191
  // http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.4.3.2
192
+ if (shouldSkipValidate(this.validateOptions, ["OBJECT_MISSING_REQUIRED_PROPERTY"])) {
193
+ return;
194
+ }
148
195
  if (Utils.whatIs(json) !== "object") {
149
196
  return;
150
197
  }
@@ -170,6 +217,9 @@ var JsonValidators = {
170
217
  },
171
218
  properties: function (report, schema, json) {
172
219
  // http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.4.4.2
220
+ if (shouldSkipValidate(this.validateOptions, ["OBJECT_ADDITIONAL_PROPERTIES"])) {
221
+ return;
222
+ }
173
223
  if (Utils.whatIs(json) !== "object") {
174
224
  return;
175
225
  }
@@ -218,6 +268,9 @@ var JsonValidators = {
218
268
  },
219
269
  dependencies: function (report, schema, json) {
220
270
  // http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.4.5.2
271
+ if (shouldSkipValidate(this.validateOptions, ["OBJECT_DEPENDENCY_KEY"])) {
272
+ return;
273
+ }
221
274
  if (Utils.whatIs(json) !== "object") {
222
275
  return;
223
276
  }
@@ -248,6 +301,9 @@ var JsonValidators = {
248
301
  },
249
302
  enum: function (report, schema, json) {
250
303
  // http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.5.1.2
304
+ if (shouldSkipValidate(this.validateOptions, ["ENUM_CASE_MISMATCH", "ENUM_MISMATCH"])) {
305
+ return;
306
+ }
251
307
  var match = false,
252
308
  caseInsensitiveMatch = false,
253
309
  idx = schema.enum.length;
@@ -267,6 +323,9 @@ var JsonValidators = {
267
323
  },
268
324
  type: function (report, schema, json) {
269
325
  // http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.5.2.2
326
+ if (shouldSkipValidate(this.validateOptions, ["INVALID_TYPE"])) {
327
+ return;
328
+ }
270
329
  var jsonType = Utils.whatIs(json);
271
330
  if (typeof schema.type === "string") {
272
331
  if (jsonType !== schema.type && (jsonType !== "integer" || schema.type !== "number")) {
@@ -339,6 +398,9 @@ var JsonValidators = {
339
398
  // http://json-schema.org/latest/json-schema-validation.html#rfc.section.7.2
340
399
  var formatValidatorFn = FormatValidators[schema.format];
341
400
  if (typeof formatValidatorFn === "function") {
401
+ if (shouldSkipValidate(this.validateOptions, ["INVALID_FORMAT"])) {
402
+ return;
403
+ }
342
404
  if (formatValidatorFn.length === 2) {
343
405
  // async - need to clone the path here, because it will change by the time async function reports back
344
406
  var pathBeforeAsync = Utils.clone(report.path);
@@ -376,13 +438,13 @@ var recurseArray = function (report, schema, json) {
376
438
  while (idx--) {
377
439
  // equal to doesn't make sense here
378
440
  if (idx < schema.items.length) {
379
- report.path.push(idx.toString());
441
+ report.path.push(idx);
380
442
  exports.validate.call(this, report, schema.items[idx], json[idx]);
381
443
  report.path.pop();
382
444
  } else {
383
445
  // might be boolean, so check that it's an object
384
446
  if (typeof schema.additionalItems === "object") {
385
- report.path.push(idx.toString());
447
+ report.path.push(idx);
386
448
  exports.validate.call(this, report, schema.additionalItems, json[idx]);
387
449
  report.path.pop();
388
450
  }
@@ -394,7 +456,7 @@ var recurseArray = function (report, schema, json) {
394
456
  // If items is a schema, then the child instance must be valid against this schema,
395
457
  // regardless of its index, and regardless of the value of "additionalItems".
396
458
  while (idx--) {
397
- report.path.push(idx.toString());
459
+ report.path.push(idx);
398
460
  exports.validate.call(this, report, schema.items, json[idx]);
399
461
  report.path.pop();
400
462
  }
package/src/Report.js CHANGED
@@ -53,6 +53,16 @@ Report.prototype.addAsyncTask = function (fn, args, asyncTaskResultProcessFn) {
53
53
  this.asyncTasks.push([fn, args, asyncTaskResultProcessFn]);
54
54
  };
55
55
 
56
+ Report.prototype.getAncestor = function (id) {
57
+ if (!this.parentReport) {
58
+ return undefined;
59
+ }
60
+ if (this.parentReport.getSchemaId() === id) {
61
+ return this.parentReport;
62
+ }
63
+ return this.parentReport.getAncestor(id);
64
+ };
65
+
56
66
  /**
57
67
  *
58
68
  * @param {*} timeout
@@ -126,6 +136,7 @@ Report.prototype.getPath = function (returnPathAsString) {
126
136
  if (returnPathAsString !== true) {
127
137
  // Sanitize the path segments (http://tools.ietf.org/html/rfc6901#section-4)
128
138
  path = "#/" + path.map(function (segment) {
139
+ segment = segment.toString();
129
140
 
130
141
  if (Utils.isAbsoluteUri(segment)) {
131
142
  return "uri(" + segment + ")";
@@ -138,16 +138,23 @@ exports.getSchemaByUri = function (report, uri, root) {
138
138
 
139
139
  report.path.push(remotePath);
140
140
 
141
- var remoteReport = new Report(report);
142
- if (SchemaCompilation.compileSchema.call(this, remoteReport, result)) {
143
- var savedOptions = this.options;
144
- try {
145
- // If custom validationOptions were provided to setRemoteReference(),
146
- // use them instead of the default options
147
- this.options = result.__$validationOptions || this.options;
148
- SchemaValidation.validateSchema.call(this, remoteReport, result);
149
- } finally {
150
- this.options = savedOptions;
141
+ var remoteReport;
142
+
143
+ var anscestorReport = report.getAncestor(result.id);
144
+ if (anscestorReport) {
145
+ remoteReport = anscestorReport;
146
+ } else {
147
+ remoteReport = new Report(report);
148
+ if (SchemaCompilation.compileSchema.call(this, remoteReport, result)) {
149
+ var savedOptions = this.options;
150
+ try {
151
+ // If custom validationOptions were provided to setRemoteReference(),
152
+ // use them instead of the default options
153
+ this.options = result.__$validationOptions || this.options;
154
+ SchemaValidation.validateSchema.call(this, remoteReport, result);
155
+ } finally {
156
+ this.options = savedOptions;
157
+ }
151
158
  }
152
159
  }
153
160
  var remoteReportIsValid = remoteReport.isValid();