z-schema 3.19.0 → 3.22.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.19.0",
3
+ "version": "3.22.0",
4
4
  "description": "JSON schema validator",
5
5
  "homepage": "https://github.com/zaggino/z-schema",
6
6
  "authors": [
@@ -31,8 +31,8 @@
31
31
  "README.md"
32
32
  ],
33
33
  "scripts": {
34
- "prepublish": "grunt",
35
- "test": "grunt test && grunt lint"
34
+ "prepublish": "npm test && grunt",
35
+ "test": "jasmine-node test/ && grunt lint"
36
36
  },
37
37
  "testling": {
38
38
  "scripts": [
@@ -60,7 +60,7 @@
60
60
  "dependencies": {
61
61
  "lodash.get": "^4.0.0",
62
62
  "lodash.isequal": "^4.0.0",
63
- "validator": "^9.0.0"
63
+ "validator": "^10.0.0"
64
64
  },
65
65
  "optionalDependencies": {
66
66
  "commander": "^2.7.1"
@@ -74,8 +74,6 @@
74
74
  "grunt-contrib-jasmine": "^1.1.0",
75
75
  "grunt-contrib-jshint": "^1.1.0",
76
76
  "grunt-contrib-uglify": "^3.1.0",
77
- "grunt-jasmine-node": "^0.3.1",
78
- "grunt-jasmine-node-coverage": "^2.0.1",
79
77
  "grunt-jscs": "^3.0.1",
80
78
  "grunt-lineending": "^1.0.0",
81
79
  "jasmine-node": "^1.14.5",
package/src/Errors.js CHANGED
@@ -5,6 +5,7 @@ module.exports = {
5
5
  INVALID_TYPE: "Expected type {0} but found type {1}",
6
6
  INVALID_FORMAT: "Object didn't pass validation for format {0}: {1}",
7
7
  ENUM_MISMATCH: "No enum match for: {0}",
8
+ ENUM_CASE_MISMATCH: "Enum does not match case for: {0}",
8
9
  ANY_OF_MISSING: "Data does not match any schemas from 'anyOf'",
9
10
  ONE_OF_MISSING: "Data does not match any schemas from 'oneOf'",
10
11
  ONE_OF_MULTIPLE: "Data is valid against more than one schema from 'oneOf'",
@@ -246,23 +246,35 @@ var JsonValidators = {
246
246
  enum: function (report, schema, json) {
247
247
  // http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.5.1.2
248
248
  var match = false,
249
+ caseInsensitiveMatch = false,
249
250
  idx = schema.enum.length;
250
251
  while (idx--) {
251
252
  if (Utils.areEqual(json, schema.enum[idx])) {
252
253
  match = true;
253
254
  break;
255
+ } else if (Utils.areEqual(json, schema.enum[idx]), { caseInsensitiveComparison: true }) {
256
+ caseInsensitiveMatch = true;
254
257
  }
255
258
  }
259
+
256
260
  if (match === false) {
257
- report.addError("ENUM_MISMATCH", [json], null, schema.description);
261
+ var error = caseInsensitiveMatch && this.options.enumCaseInsensitiveComparison ? "ENUM_CASE_MISMATCH" : "ENUM_MISMATCH";
262
+ report.addError(error, [json], null, schema.description);
258
263
  }
259
264
  },
260
- /*
261
265
  type: function (report, schema, json) {
262
266
  // http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.5.2.2
263
- // type is handled before this is called so ignore
267
+ var jsonType = Utils.whatIs(json);
268
+ if (typeof schema.type === "string") {
269
+ if (jsonType !== schema.type && (jsonType !== "integer" || schema.type !== "number")) {
270
+ report.addError("INVALID_TYPE", [schema.type, jsonType], null, schema.description);
271
+ }
272
+ } else {
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);
275
+ }
276
+ }
264
277
  },
265
- */
266
278
  allOf: function (report, schema, json) {
267
279
  // http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.5.3.2
268
280
  var idx = schema.allOf.length;
@@ -489,23 +501,12 @@ exports.validate = function (report, schema, json) {
489
501
  }
490
502
 
491
503
  // type checking first
492
- // http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.5.2.2
493
504
  var jsonType = Utils.whatIs(json);
494
505
  if (schema.type) {
495
- if (typeof schema.type === "string") {
496
- if (jsonType !== schema.type && (jsonType !== "integer" || schema.type !== "number")) {
497
- report.addError("INVALID_TYPE", [schema.type, jsonType], null, schema.description);
498
- if (this.options.breakOnFirstError) {
499
- return false;
500
- }
501
- }
502
- } else {
503
- if (schema.type.indexOf(jsonType) === -1 && (jsonType !== "integer" || schema.type.indexOf("number") === -1)) {
504
- report.addError("INVALID_TYPE", [schema.type, jsonType], null, schema.description);
505
- if (this.options.breakOnFirstError) {
506
- return false;
507
- }
508
- }
506
+ keys.splice(keys.indexOf("type"), 1);
507
+ JsonValidators.type.call(this, report, schema, json);
508
+ if (report.errors.length && this.options.breakOnFirstError) {
509
+ return false;
509
510
  }
510
511
  }
511
512
 
package/src/Report.js CHANGED
@@ -57,7 +57,8 @@ Report.prototype.processAsyncTasks = function (timeout, callback) {
57
57
  };
58
58
  }
59
59
 
60
- if (tasksCount === 0 || this.errors.length > 0) {
60
+ // finish if tasks are completed or there are any errors and breaking on first error was requested
61
+ if (tasksCount === 0 || (this.errors.length > 0 && this.options.breakOnFirstError)) {
61
62
  finish();
62
63
  return;
63
64
  }
package/src/Utils.js CHANGED
@@ -41,7 +41,11 @@ exports.whatIs = function (what) {
41
41
 
42
42
  };
43
43
 
44
- exports.areEqual = function areEqual(json1, json2) {
44
+ exports.areEqual = function areEqual(json1, json2, options) {
45
+
46
+ options = options || {};
47
+ var caseInsensitiveComparison = options.caseInsensitiveComparison || false;
48
+
45
49
  // http://json-schema.org/latest/json-schema-core.html#rfc.section.3.6
46
50
 
47
51
  // Two JSON values are said to be equal if and only if:
@@ -52,6 +56,12 @@ exports.areEqual = function areEqual(json1, json2) {
52
56
  if (json1 === json2) {
53
57
  return true;
54
58
  }
59
+ if (
60
+ caseInsensitiveComparison === true &&
61
+ typeof json1 === "string" && typeof json2 === "string" &&
62
+ json1.toUpperCase() === json2.toUpperCase()) {
63
+ return true;
64
+ }
55
65
 
56
66
  var i, len;
57
67
 
@@ -64,7 +74,7 @@ exports.areEqual = function areEqual(json1, json2) {
64
74
  // items at the same index are equal according to this definition; or
65
75
  len = json1.length;
66
76
  for (i = 0; i < len; i++) {
67
- if (!areEqual(json1[i], json2[i])) {
77
+ if (!areEqual(json1[i], json2[i], { caseInsensitiveComparison: caseInsensitiveComparison })) {
68
78
  return false;
69
79
  }
70
80
  }
@@ -76,13 +86,13 @@ exports.areEqual = function areEqual(json1, json2) {
76
86
  // have the same set of property names; and
77
87
  var keys1 = Object.keys(json1);
78
88
  var keys2 = Object.keys(json2);
79
- if (!areEqual(keys1, keys2)) {
89
+ if (!areEqual(keys1, keys2, { caseInsensitiveComparison: caseInsensitiveComparison })) {
80
90
  return false;
81
91
  }
82
92
  // values for a same property name are equal according to this definition.
83
93
  len = keys1.length;
84
94
  for (i = 0; i < len; i++) {
85
- if (!areEqual(json1[keys1[i]], json2[keys1[i]])) {
95
+ if (!areEqual(json1[keys1[i]], json2[keys1[i]], { caseInsensitiveComparison: caseInsensitiveComparison })) {
86
96
  return false;
87
97
  }
88
98
  }
package/src/ZSchema.js CHANGED
@@ -22,6 +22,8 @@ var defaultOptions = {
22
22
  forceAdditional: false,
23
23
  // assume additionalProperties and additionalItems are defined as "false" where appropriate
24
24
  assumeAdditional: false,
25
+ // do case insensitive comparison for enums
26
+ enumCaseInsensitiveComparison: false,
25
27
  // force items to be defined on "array" types
26
28
  forceItems: false,
27
29
  // force minItems to be defined on "array" types
@@ -112,6 +114,7 @@ function normalizeOptions(options) {
112
114
  function ZSchema(options) {
113
115
  this.cache = {};
114
116
  this.referenceCache = [];
117
+ this.validateOptions = {};
115
118
 
116
119
  this.options = normalizeOptions(options);
117
120
 
@@ -158,6 +161,8 @@ ZSchema.prototype.validate = function (json, schema, options, callback) {
158
161
  }
159
162
  if (!options) { options = {}; }
160
163
 
164
+ this.validateOptions = options;
165
+
161
166
  var whatIs = Utils.whatIs(schema);
162
167
  if (whatIs !== "string" && whatIs !== "object") {
163
168
  var e = new Error("Invalid .validate call - schema must be an string or object but " + whatIs + " was passed!");