Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Descriptive errors issue #62 #64

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 23 additions & 23 deletions lib/json-schema-draft-03.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@
}

//if we get to this point, type is invalid
report.addError(instance, schema, "type", "Instance is not a required type", requiredTypes);
report.addError(instance, schema, "type", "Instance `" + name + "` is not a required type", requiredTypes);
return false;
}
//else, anything is allowed if no type is specified
Expand Down Expand Up @@ -256,7 +256,7 @@
if (itemSchema !== false) {
itemSchema.validate(properties[x], report, instance, schema, x);
} else {
report.addError(instance, schema, "additionalProperties", "Additional items are not allowed", itemSchema);
report.addError(instance, schema, "additionalProperties", "Additional item `" + name + "` are not allowed", itemSchema);
}
}
} else {
Expand All @@ -280,7 +280,7 @@

"validator" : function (instance, schema, self, report, parent, parentSchema, name) {
if (instance.getType() === "undefined" && !schema.getAttribute("optional")) {
report.addError(instance, schema, "optional", "Property is required", false);
report.addError(instance, schema, "optional", "Property `" + name + "` is required", false);
}
},

Expand Down Expand Up @@ -314,7 +314,7 @@
if (JSV.isJSONSchema(additionalProperties)) {
additionalProperties.validate(properties[key], report, instance, schema, key);
} else if (additionalProperties === false) {
report.addError(instance, schema, "additionalProperties", "Additional properties are not allowed", additionalProperties);
report.addError(instance, schema, "additionalProperties", "Additional property `" + key + "` are not allowed", additionalProperties);
}
}
}
Expand All @@ -340,7 +340,7 @@
requires = schema.getAttribute("requires");
if (typeof requires === "string") {
if (parent.getProperty(requires).getType() === "undefined") {
report.addError(instance, schema, "requires", 'Property requires sibling property "' + requires + '"', requires);
report.addError(instance, schema, "requires", 'Property `' + name + '`requires sibling property "' + requires + '"', requires);
}
} else if (JSV.isJSONSchema(requires)) {
requires.validate(parent, report); //WATCH: A "requires" schema does not support the "requires" attribute
Expand All @@ -365,7 +365,7 @@
minimum = schema.getAttribute("minimum");
minimumCanEqual = schema.getAttribute("minimumCanEqual");
if (typeof minimum === "number" && (instance.getValue() < minimum || (minimumCanEqual === false && instance.getValue() === minimum))) {
report.addError(instance, schema, "minimum", "Number is less than the required minimum value", minimum);
report.addError(instance, schema, "minimum", "Property `" + name + "` of type `Number` is less than the required minimum value", minimum);
}
}
}
Expand All @@ -387,7 +387,7 @@
maximum = schema.getAttribute("maximum");
maximumCanEqual = schema.getAttribute("maximumCanEqual");
if (typeof maximum === "number" && (instance.getValue() > maximum || (maximumCanEqual === false && instance.getValue() === maximum))) {
report.addError(instance, schema, "maximum", "Number is greater than the required maximum value", maximum);
report.addError(instance, schema, "maximum", "Property `" + name + "` of type `Number` is greater than the required maximum value", maximum);
}
}
}
Expand Down Expand Up @@ -442,7 +442,7 @@
if (instance.getType() === "array") {
minItems = schema.getAttribute("minItems");
if (typeof minItems === "number" && instance.getProperties().length < minItems) {
report.addError(instance, schema, "minItems", "The number of items is less than the required minimum", minItems);
report.addError(instance, schema, "minItems", "The number of items in `" + name + "` is less than the required minimum", minItems);
}
}
}
Expand All @@ -464,7 +464,7 @@
if (instance.getType() === "array") {
maxItems = schema.getAttribute("maxItems");
if (typeof maxItems === "number" && instance.getProperties().length > maxItems) {
report.addError(instance, schema, "maxItems", "The number of items is greater than the required maximum", maxItems);
report.addError(instance, schema, "maxItems", "The number of items in `" + name + "` is greater than the required maximum", maxItems);
}
}
}
Expand Down Expand Up @@ -513,7 +513,7 @@
if (instance.getType() === "string") {
minLength = schema.getAttribute("minLength");
if (typeof minLength === "number" && instance.getValue().length < minLength) {
report.addError(instance, schema, "minLength", "String is less than the required minimum length", minLength);
report.addError(instance, schema, "minLength", "Property `" + name + "` of type `String` is less than the required minimum length", minLength);
}
}
}
Expand All @@ -534,7 +534,7 @@
if (instance.getType() === "string") {
maxLength = schema.getAttribute("maxLength");
if (typeof maxLength === "number" && instance.getValue().length > maxLength) {
report.addError(instance, schema, "maxLength", "String is greater than the required maximum length", maxLength);
report.addError(instance, schema, "maxLength", "Property `" + name + "` of type `String` is greater than the required maximum length", maxLength);
}
}
}
Expand Down Expand Up @@ -594,7 +594,7 @@
format = schema.getAttribute("format");
formatValidators = self.getValueOfProperty("formatValidators");
if (typeof format === "string" && formatValidators[format] !== O[format] && typeof formatValidators[format] === "function" && !formatValidators[format].call(this, instance, report)) {
report.addError(instance, schema, "format", "String is not in the required format", format);
report.addError(instance, schema, "format", "Property `" + name + "` of type `String` is not in the required format", format);
}
}
},
Expand Down Expand Up @@ -630,7 +630,7 @@
if (typeof maxDecimal === "number") {
decimals = instance.getValue().toString(10).split('.')[1];
if (decimals && decimals.length > maxDecimal) {
report.addError(instance, schema, "maxDecimal", "The number of decimal places is greater than the allowed maximum", maxDecimal);
report.addError(instance, schema, "maxDecimal", "The number of decimal places in `" + name + "` is greater than the allowed maximum", maxDecimal);
}
}
}
Expand Down Expand Up @@ -666,12 +666,12 @@
subreport.validated = JSV.clone(report.validated);
if (key.validate(instance, subreport, parent, parentSchema, name).errors.length === 0) {
//instance matches this schema
report.addError(instance, schema, "disallow", "Instance is a disallowed type", disallowedTypes);
report.addError(instance, schema, "disallow", "Instance `" + name + "` is a disallowed type", disallowedTypes);
return false;
}
} else if (typeValidators[key] !== O[key] && typeof typeValidators[key] === "function") {
if (typeValidators[key](instance, report)) {
report.addError(instance, schema, "disallow", "Instance is a disallowed type", disallowedTypes);
report.addError(instance, schema, "disallow", "Instance `" + name + "` is a disallowed type", disallowedTypes);
return false;
}
}
Expand Down Expand Up @@ -849,7 +849,7 @@
if (typeof pathStart === "string") {
//TODO: Find out what pathStart is relative to
if (instance.getURI().indexOf(pathStart) !== 0) {
report.addError(instance, schema, "pathStart", "Instance's URI does not start with " + pathStart, pathStart);
report.addError(instance, schema, "pathStart", "URI in Instance `" + name + "` does not start with " + pathStart, pathStart);
}
}
}
Expand Down Expand Up @@ -1205,7 +1205,7 @@
if (JSV.isJSONSchema(additionalProperties)) {
additionalProperties.validate(properties[key], report, instance, schema, key);
} else if (additionalProperties === false) {
report.addError(instance, schema, "additionalProperties", "Additional properties are not allowed", additionalProperties);
report.addError(instance, schema, "additionalProperties", "Additional property `" + key + "` are not allowed", additionalProperties);
}
}
}
Expand All @@ -1228,7 +1228,7 @@
if (itemSchema !== false) {
itemSchema.validate(properties[x], report, instance, schema, x);
} else {
report.addError(instance, schema, "additionalItems", "Additional items are not allowed", itemSchema);
report.addError(instance, schema, "additionalItems", "Additional items in `" + name + "` are not allowed", itemSchema);
}
}
} else {
Expand Down Expand Up @@ -1259,7 +1259,7 @@
additionalItems.validate(properties[x], report, instance, schema, x);
}
} else if (properties.length) {
report.addError(instance, schema, "additionalItems", "Additional items are not allowed", additionalItems);
report.addError(instance, schema, "additionalItems", "Additional items in `" + name + "` are not allowed", additionalItems);
}
}
}
Expand All @@ -1280,7 +1280,7 @@

"validator" : function (instance, schema, self, report, parent, parentSchema, name) {
if (instance.getType() === "undefined" && schema.getAttribute("required")) {
report.addError(instance, schema, "required", "Property is required", true);
report.addError(instance, schema, "required", "Property `" + name + "` is required", true);
}
}
},
Expand Down Expand Up @@ -1380,7 +1380,7 @@
minimum = schema.getAttribute("minimum");
exclusiveMinimum = schema.getAttribute("exclusiveMinimum") || (!instance.getEnvironment().getOption("strict") && !schema.getAttribute("minimumCanEqual"));
if (typeof minimum === "number" && (instance.getValue() < minimum || (exclusiveMinimum === true && instance.getValue() === minimum))) {
report.addError(instance, schema, "minimum", "Number is less than the required minimum value", minimum);
report.addError(instance, schema, "minimum", "Property `" + name + "` of type `Number` is less than the required minimum value", minimum);
}
}
}
Expand All @@ -1393,7 +1393,7 @@
maximum = schema.getAttribute("maximum");
exclusiveMaximum = schema.getAttribute("exclusiveMaximum") || (!instance.getEnvironment().getOption("strict") && !schema.getAttribute("maximumCanEqual"));
if (typeof maximum === "number" && (instance.getValue() > maximum || (exclusiveMaximum === true && instance.getValue() === maximum))) {
report.addError(instance, schema, "maximum", "Number is greater than the required maximum value", maximum);
report.addError(instance, schema, "maximum", "Property `" + name + "` of type `Number` is greater than the required maximum value", maximum);
}
}
}
Expand Down Expand Up @@ -1549,4 +1549,4 @@
JSV.setDefaultEnvironmentID("json-schema-draft-03");
}

}());
}());