Skip to content

Commit

Permalink
Merge pull request #208 from vladbarosan/exposeValidators
Browse files Browse the repository at this point in the history
move type check together with the other validators
  • Loading branch information
zaggino authored May 17, 2018
2 parents 4abf55d + 65f6ae3 commit ae67181
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 38 deletions.
32 changes: 14 additions & 18 deletions dist/ZSchema-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -6384,12 +6384,19 @@ var JsonValidators = {
report.addError(error, [json], null, schema.description);
}
},
/*
type: function (report, schema, json) {
// http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.5.2.2
// type is handled before this is called so ignore
var jsonType = Utils.whatIs(json);
if (typeof schema.type === "string") {
if (jsonType !== schema.type && (jsonType !== "integer" || schema.type !== "number")) {
report.addError("INVALID_TYPE", [schema.type, jsonType], null, schema.description);
}
} else {
if (schema.type.indexOf(jsonType) === -1 && (jsonType !== "integer" || schema.type.indexOf("number") === -1)) {
report.addError("INVALID_TYPE", [schema.type, jsonType], null, schema.description);
}
}
},
*/
allOf: function (report, schema, json) {
// http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.5.3.2
var idx = schema.allOf.length;
Expand Down Expand Up @@ -6616,23 +6623,12 @@ exports.validate = function (report, schema, json) {
}

// type checking first
// http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.5.2.2
var jsonType = Utils.whatIs(json);
if (schema.type) {
if (typeof schema.type === "string") {
if (jsonType !== schema.type && (jsonType !== "integer" || schema.type !== "number")) {
report.addError("INVALID_TYPE", [schema.type, jsonType], null, schema.description);
if (this.options.breakOnFirstError) {
return false;
}
}
} else {
if (schema.type.indexOf(jsonType) === -1 && (jsonType !== "integer" || schema.type.indexOf("number") === -1)) {
report.addError("INVALID_TYPE", [schema.type, jsonType], null, schema.description);
if (this.options.breakOnFirstError) {
return false;
}
}
keys.splice(keys.indexOf("type"), 1);
JsonValidators.type.call(this, report, schema, json);
if (report.errors.length && this.options.breakOnFirstError) {
return false;
}
}

Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "z-schema",
"version": "3.21.0",
"version": "3.22.0",
"description": "JSON schema validator",
"homepage": "https://github.com/zaggino/z-schema",
"authors": [
Expand Down
32 changes: 14 additions & 18 deletions src/JsonValidation.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,12 +262,19 @@ var JsonValidators = {
report.addError(error, [json], null, schema.description);
}
},
/*
type: function (report, schema, json) {
// http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.5.2.2
// type is handled before this is called so ignore
var jsonType = Utils.whatIs(json);
if (typeof schema.type === "string") {
if (jsonType !== schema.type && (jsonType !== "integer" || schema.type !== "number")) {
report.addError("INVALID_TYPE", [schema.type, jsonType], null, schema.description);
}
} else {
if (schema.type.indexOf(jsonType) === -1 && (jsonType !== "integer" || schema.type.indexOf("number") === -1)) {
report.addError("INVALID_TYPE", [schema.type, jsonType], null, schema.description);
}
}
},
*/
allOf: function (report, schema, json) {
// http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.5.3.2
var idx = schema.allOf.length;
Expand Down Expand Up @@ -494,23 +501,12 @@ exports.validate = function (report, schema, json) {
}

// type checking first
// http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.5.2.2
var jsonType = Utils.whatIs(json);
if (schema.type) {
if (typeof schema.type === "string") {
if (jsonType !== schema.type && (jsonType !== "integer" || schema.type !== "number")) {
report.addError("INVALID_TYPE", [schema.type, jsonType], null, schema.description);
if (this.options.breakOnFirstError) {
return false;
}
}
} else {
if (schema.type.indexOf(jsonType) === -1 && (jsonType !== "integer" || schema.type.indexOf("number") === -1)) {
report.addError("INVALID_TYPE", [schema.type, jsonType], null, schema.description);
if (this.options.breakOnFirstError) {
return false;
}
}
keys.splice(keys.indexOf("type"), 1);
JsonValidators.type.call(this, report, schema, json);
if (report.errors.length && this.options.breakOnFirstError) {
return false;
}
}

Expand Down

0 comments on commit ae67181

Please sign in to comment.