z-schema 3.22.0 → 3.23.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,6 @@
1
1
  {
2
2
  "name": "z-schema",
3
- "version": "3.22.0",
3
+ "version": "3.23.0",
4
4
  "description": "JSON schema validator",
5
5
  "homepage": "https://github.com/zaggino/z-schema",
6
6
  "authors": [
@@ -1,8 +1,8 @@
1
1
  "use strict";
2
2
 
3
- var FormatValidators = require("./FormatValidators"),
4
- Report = require("./Report"),
5
- Utils = require("./Utils");
3
+ var FormatValidators = require("./FormatValidators"),
4
+ Report = require("./Report"),
5
+ Utils = require("./Utils");
6
6
 
7
7
  var JsonValidators = {
8
8
  multipleOf: function (report, schema, json) {
@@ -11,7 +11,7 @@ var JsonValidators = {
11
11
  return;
12
12
  }
13
13
  if (Utils.whatIs(json / schema.multipleOf) !== "integer") {
14
- report.addError("MULTIPLE_OF", [json, schema.multipleOf], null, schema.description);
14
+ report.addError("MULTIPLE_OF", [json, schema.multipleOf], null, schema);
15
15
  }
16
16
  },
17
17
  maximum: function (report, schema, json) {
@@ -21,11 +21,11 @@ var JsonValidators = {
21
21
  }
22
22
  if (schema.exclusiveMaximum !== true) {
23
23
  if (json > schema.maximum) {
24
- report.addError("MAXIMUM", [json, schema.maximum], null, schema.description);
24
+ report.addError("MAXIMUM", [json, schema.maximum], null, schema);
25
25
  }
26
26
  } else {
27
27
  if (json >= schema.maximum) {
28
- report.addError("MAXIMUM_EXCLUSIVE", [json, schema.maximum], null, schema.description);
28
+ report.addError("MAXIMUM_EXCLUSIVE", [json, schema.maximum], null, schema);
29
29
  }
30
30
  }
31
31
  },
@@ -39,11 +39,11 @@ var JsonValidators = {
39
39
  }
40
40
  if (schema.exclusiveMinimum !== true) {
41
41
  if (json < schema.minimum) {
42
- report.addError("MINIMUM", [json, schema.minimum], null, schema.description);
42
+ report.addError("MINIMUM", [json, schema.minimum], null, schema);
43
43
  }
44
44
  } else {
45
45
  if (json <= schema.minimum) {
46
- report.addError("MINIMUM_EXCLUSIVE", [json, schema.minimum], null, schema.description);
46
+ report.addError("MINIMUM_EXCLUSIVE", [json, schema.minimum], null, schema);
47
47
  }
48
48
  }
49
49
  },
@@ -56,7 +56,7 @@ var JsonValidators = {
56
56
  return;
57
57
  }
58
58
  if (Utils.ucs2decode(json).length > schema.maxLength) {
59
- report.addError("MAX_LENGTH", [json.length, schema.maxLength], null, schema.description);
59
+ report.addError("MAX_LENGTH", [json.length, schema.maxLength], null, schema);
60
60
  }
61
61
  },
62
62
  minLength: function (report, schema, json) {
@@ -65,7 +65,7 @@ var JsonValidators = {
65
65
  return;
66
66
  }
67
67
  if (Utils.ucs2decode(json).length < schema.minLength) {
68
- report.addError("MIN_LENGTH", [json.length, schema.minLength], null, schema.description);
68
+ report.addError("MIN_LENGTH", [json.length, schema.minLength], null, schema);
69
69
  }
70
70
  },
71
71
  pattern: function (report, schema, json) {
@@ -74,7 +74,7 @@ var JsonValidators = {
74
74
  return;
75
75
  }
76
76
  if (RegExp(schema.pattern).test(json) === false) {
77
- report.addError("PATTERN", [schema.pattern, json], null, schema.description);
77
+ report.addError("PATTERN", [schema.pattern, json], null, schema);
78
78
  }
79
79
  },
80
80
  additionalItems: function (report, schema, json) {
@@ -86,7 +86,7 @@ var JsonValidators = {
86
86
  // the json is valid if its size is less than, or equal to, the size of "items".
87
87
  if (schema.additionalItems === false && Array.isArray(schema.items)) {
88
88
  if (json.length > schema.items.length) {
89
- report.addError("ARRAY_ADDITIONAL_ITEMS", null, null, schema.description);
89
+ report.addError("ARRAY_ADDITIONAL_ITEMS", null, null, schema);
90
90
  }
91
91
  }
92
92
  },
@@ -99,7 +99,7 @@ var JsonValidators = {
99
99
  return;
100
100
  }
101
101
  if (json.length > schema.maxItems) {
102
- report.addError("ARRAY_LENGTH_LONG", [json.length, schema.maxItems], null, schema.description);
102
+ report.addError("ARRAY_LENGTH_LONG", [json.length, schema.maxItems], null, schema);
103
103
  }
104
104
  },
105
105
  minItems: function (report, schema, json) {
@@ -108,7 +108,7 @@ var JsonValidators = {
108
108
  return;
109
109
  }
110
110
  if (json.length < schema.minItems) {
111
- report.addError("ARRAY_LENGTH_SHORT", [json.length, schema.minItems], null, schema.description);
111
+ report.addError("ARRAY_LENGTH_SHORT", [json.length, schema.minItems], null, schema);
112
112
  }
113
113
  },
114
114
  uniqueItems: function (report, schema, json) {
@@ -119,7 +119,7 @@ var JsonValidators = {
119
119
  if (schema.uniqueItems === true) {
120
120
  var matches = [];
121
121
  if (Utils.isUniqueArray(json, matches) === false) {
122
- report.addError("ARRAY_UNIQUE", matches, null, schema.description);
122
+ report.addError("ARRAY_UNIQUE", matches, null, schema);
123
123
  }
124
124
  }
125
125
  },
@@ -130,7 +130,7 @@ var JsonValidators = {
130
130
  }
131
131
  var keysCount = Object.keys(json).length;
132
132
  if (keysCount > schema.maxProperties) {
133
- report.addError("OBJECT_PROPERTIES_MAXIMUM", [keysCount, schema.maxProperties], null, schema.description);
133
+ report.addError("OBJECT_PROPERTIES_MAXIMUM", [keysCount, schema.maxProperties], null, schema);
134
134
  }
135
135
  },
136
136
  minProperties: function (report, schema, json) {
@@ -140,7 +140,7 @@ var JsonValidators = {
140
140
  }
141
141
  var keysCount = Object.keys(json).length;
142
142
  if (keysCount < schema.minProperties) {
143
- report.addError("OBJECT_PROPERTIES_MINIMUM", [keysCount, schema.minProperties], null, schema.description);
143
+ report.addError("OBJECT_PROPERTIES_MINIMUM", [keysCount, schema.minProperties], null, schema);
144
144
  }
145
145
  },
146
146
  required: function (report, schema, json) {
@@ -152,7 +152,7 @@ var JsonValidators = {
152
152
  while (idx--) {
153
153
  var requiredPropertyName = schema.required[idx];
154
154
  if (json[requiredPropertyName] === undefined) {
155
- report.addError("OBJECT_MISSING_REQUIRED_PROPERTY", [requiredPropertyName], null, schema.description);
155
+ report.addError("OBJECT_MISSING_REQUIRED_PROPERTY", [requiredPropertyName], null, schema);
156
156
  }
157
157
  }
158
158
  },
@@ -208,7 +208,7 @@ var JsonValidators = {
208
208
  }
209
209
  }
210
210
  if (s.length > 0) {
211
- report.addError("OBJECT_ADDITIONAL_PROPERTIES", [s], null, schema.description);
211
+ report.addError("OBJECT_ADDITIONAL_PROPERTIES", [s], null, schema);
212
212
  }
213
213
  }
214
214
  }
@@ -236,7 +236,7 @@ var JsonValidators = {
236
236
  while (idx2--) {
237
237
  var requiredPropertyName = dependencyDefinition[idx2];
238
238
  if (json[requiredPropertyName] === undefined) {
239
- report.addError("OBJECT_DEPENDENCY_KEY", [requiredPropertyName, dependencyName], null, schema.description);
239
+ report.addError("OBJECT_DEPENDENCY_KEY", [requiredPropertyName, dependencyName], null, schema);
240
240
  }
241
241
  }
242
242
  }
@@ -259,7 +259,7 @@ var JsonValidators = {
259
259
 
260
260
  if (match === false) {
261
261
  var error = caseInsensitiveMatch && this.options.enumCaseInsensitiveComparison ? "ENUM_CASE_MISMATCH" : "ENUM_MISMATCH";
262
- report.addError(error, [json], null, schema.description);
262
+ report.addError(error, [json], null, schema);
263
263
  }
264
264
  },
265
265
  type: function (report, schema, json) {
@@ -267,11 +267,11 @@ var JsonValidators = {
267
267
  var jsonType = Utils.whatIs(json);
268
268
  if (typeof schema.type === "string") {
269
269
  if (jsonType !== schema.type && (jsonType !== "integer" || schema.type !== "number")) {
270
- report.addError("INVALID_TYPE", [schema.type, jsonType], null, schema.description);
270
+ report.addError("INVALID_TYPE", [schema.type, jsonType], null, schema);
271
271
  }
272
272
  } else {
273
273
  if (schema.type.indexOf(jsonType) === -1 && (jsonType !== "integer" || schema.type.indexOf("number") === -1)) {
274
- report.addError("INVALID_TYPE", [schema.type, jsonType], null, schema.description);
274
+ report.addError("INVALID_TYPE", [schema.type, jsonType], null, schema);
275
275
  }
276
276
  }
277
277
  },
@@ -298,7 +298,7 @@ var JsonValidators = {
298
298
  }
299
299
 
300
300
  if (passed === false) {
301
- report.addError("ANY_OF_MISSING", undefined, subReports, schema.description);
301
+ report.addError("ANY_OF_MISSING", undefined, subReports, schema);
302
302
  }
303
303
  },
304
304
  oneOf: function (report, schema, json) {
@@ -316,16 +316,16 @@ var JsonValidators = {
316
316
  }
317
317
 
318
318
  if (passes === 0) {
319
- report.addError("ONE_OF_MISSING", undefined, subReports, schema.description);
319
+ report.addError("ONE_OF_MISSING", undefined, subReports, schema);
320
320
  } else if (passes > 1) {
321
- report.addError("ONE_OF_MULTIPLE", null, null, schema.description);
321
+ report.addError("ONE_OF_MULTIPLE", null, null, schema);
322
322
  }
323
323
  },
324
324
  not: function (report, schema, json) {
325
325
  // http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.5.6.2
326
326
  var subReport = new Report(report);
327
327
  if (exports.validate.call(this, subReport, schema.not, json) === true) {
328
- report.addError("NOT_PASSED", null, null, schema.description);
328
+ report.addError("NOT_PASSED", null, null, schema);
329
329
  }
330
330
  },
331
331
  definitions: function () { /*report, schema, json*/
@@ -340,17 +340,17 @@ var JsonValidators = {
340
340
  // async
341
341
  report.addAsyncTask(formatValidatorFn, [json], function (result) {
342
342
  if (result !== true) {
343
- report.addError("INVALID_FORMAT", [schema.format, json], null, schema.description);
343
+ report.addError("INVALID_FORMAT", [schema.format, json], null, schema);
344
344
  }
345
345
  });
346
346
  } else {
347
347
  // sync
348
348
  if (formatValidatorFn.call(this, json) !== true) {
349
- report.addError("INVALID_FORMAT", [schema.format, json], null, schema.description);
349
+ report.addError("INVALID_FORMAT", [schema.format, json], null, schema);
350
350
  }
351
351
  }
352
352
  } else if (this.options.ignoreUnknownFormats !== true) {
353
- report.addError("UNKNOWN_FORMAT", [schema.format], null, schema.description);
353
+ report.addError("UNKNOWN_FORMAT", [schema.format], null, schema);
354
354
  }
355
355
  }
356
356
  };
@@ -462,7 +462,7 @@ exports.validate = function (report, schema, json) {
462
462
  // check if schema is an object
463
463
  var to = Utils.whatIs(schema);
464
464
  if (to !== "object") {
465
- report.addError("SCHEMA_NOT_AN_OBJECT", [to], null, schema.description);
465
+ report.addError("SCHEMA_NOT_AN_OBJECT", [to], null, schema);
466
466
  return false;
467
467
  }
468
468
 
@@ -485,7 +485,7 @@ exports.validate = function (report, schema, json) {
485
485
  var maxRefs = 99;
486
486
  while (schema.$ref && maxRefs > 0) {
487
487
  if (!schema.__$refResolved) {
488
- report.addError("REF_UNRESOLVED", [schema.$ref], null, schema.description);
488
+ report.addError("REF_UNRESOLVED", [schema.$ref], null, schema);
489
489
  break;
490
490
  } else if (schema.__$refResolved === schema) {
491
491
  break;
package/src/Report.js CHANGED
@@ -42,7 +42,7 @@ Report.prototype.processAsyncTasks = function (timeout, callback) {
42
42
  function finish() {
43
43
  process.nextTick(function () {
44
44
  var valid = self.errors.length === 0,
45
- err = valid ? undefined : self.errors;
45
+ err = valid ? undefined : self.errors;
46
46
  callback(err, valid);
47
47
  });
48
48
  }
@@ -145,13 +145,13 @@ Report.prototype.hasError = function (errorCode, params) {
145
145
  return false;
146
146
  };
147
147
 
148
- Report.prototype.addError = function (errorCode, params, subReports, schemaDescription) {
148
+ Report.prototype.addError = function (errorCode, params, subReports, schema) {
149
149
  if (!errorCode) { throw new Error("No errorCode passed into addError()"); }
150
150
 
151
- this.addCustomError(errorCode, Errors[errorCode], params, subReports, schemaDescription);
151
+ this.addCustomError(errorCode, Errors[errorCode], params, subReports, schema);
152
152
  };
153
153
 
154
- Report.prototype.addCustomError = function (errorCode, errorMessage, params, subReports, schemaDescription) {
154
+ Report.prototype.addCustomError = function (errorCode, errorMessage, params, subReports, schema) {
155
155
  if (this.errors.length >= this.reportOptions.maxErrors) {
156
156
  return;
157
157
  }
@@ -175,8 +175,15 @@ Report.prototype.addCustomError = function (errorCode, errorMessage, params, sub
175
175
  schemaId: this.getSchemaId()
176
176
  };
177
177
 
178
- if (schemaDescription) {
179
- err.description = schemaDescription;
178
+ if (schema && typeof schema === "string") {
179
+ err.description = schema;
180
+ } else if (schema && typeof schema === "object") {
181
+ if (schema.title) {
182
+ err.title = schema.title;
183
+ }
184
+ if (schema.description) {
185
+ err.description = schema.description;
186
+ }
180
187
  }
181
188
 
182
189
  if (subReports != null) {