From 376f76a513ffb720dcaafe57c570a5322c8595de Mon Sep 17 00:00:00 2001 From: Alfonso Date: Thu, 29 Nov 2012 19:44:51 +0100 Subject: [PATCH 1/2] Adding property name to the error message in draft 3 --- lib/json-schema-draft-03.js | 42 ++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/lib/json-schema-draft-03.js b/lib/json-schema-draft-03.js index e0e4843..87bb00f 100644 --- a/lib/json-schema-draft-03.js +++ b/lib/json-schema-draft-03.js @@ -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 @@ -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 { @@ -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); } }, @@ -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); } } } @@ -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 @@ -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); } } } @@ -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); } } } @@ -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); } } } @@ -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); } } } @@ -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); } } } @@ -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); } } } @@ -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); } } }, @@ -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); } } } @@ -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; } } @@ -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); } } } @@ -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); } } } @@ -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 { @@ -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); } } } @@ -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); } } } @@ -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); } } } From 0710b9456b71ab1ed4482a16315f5e938ae4fbc5 Mon Sep 17 00:00:00 2001 From: Alfonso Abad Date: Wed, 23 Jan 2013 10:03:32 +0100 Subject: [PATCH 2/2] Update lib/json-schema-draft-03.js --- lib/json-schema-draft-03.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/json-schema-draft-03.js b/lib/json-schema-draft-03.js index 87bb00f..ef41490 100644 --- a/lib/json-schema-draft-03.js +++ b/lib/json-schema-draft-03.js @@ -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); } } }, @@ -1549,4 +1549,4 @@ JSV.setDefaultEnvironmentID("json-schema-draft-03"); } -}()); \ No newline at end of file +}());