Skip to content

Commit

Permalink
Validators are now passed the name of the instance being validated.
Browse files Browse the repository at this point in the history
Replaced merge algorithm to one that is JSON Schema compliant.
Added JSONSchema.prototype.getAttributes().
  • Loading branch information
garycourt committed Nov 13, 2010
1 parent 4e6eb93 commit c1b7299
Show file tree
Hide file tree
Showing 5 changed files with 187 additions and 156 deletions.
68 changes: 34 additions & 34 deletions lib/json-schema-draft-01.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*
* @fileOverview Implementation of the first revision of the JSON Schema specification draft.
* @author <a href="mailto:[email protected]">Gary Court</a>
* @version 1.0
* @version 1.1
* @see http://github.com/garycourt/JSV
*/

Expand Down Expand Up @@ -36,6 +36,7 @@
*/

/*jslint white: true, sub: true, onevar: true, undef: true, eqeqeq: true, newcap: true, immed: true, indent: 4 */
/*global require */

(function () {
var O = {},
Expand Down Expand Up @@ -119,7 +120,7 @@
return "any";
},

"validator" : function (instance, schema, self, report, parent, parentSchema) {
"validator" : function (instance, schema, self, report, parent, parentSchema, name) {
var requiredTypes = JSV.toArray(schema.getAttribute("type")),
x, xl, type, subreport, typeValidators;

Expand All @@ -134,7 +135,7 @@
subreport = JSV.createObject(report);
subreport.errors = [];
subreport.validated = JSV.clone(report.validated);
if (type.validate(instance, subreport, parent, parentSchema).errors.length === 0) {
if (type.validate(instance, subreport, parent, parentSchema, name).errors.length === 0) {
return true; //instance matches this schema
}
} else {
Expand Down Expand Up @@ -181,7 +182,7 @@
return {};
},

"validator" : function (instance, schema, self, report, parent, parentSchema) {
"validator" : function (instance, schema, self, report, parent, parentSchema, name) {
var propertySchemas, key;
//this attribute is for object type instances only
if (instance.getType() === "object") {
Expand All @@ -190,7 +191,7 @@
for (key in propertySchemas) {
if (propertySchemas[key] !== O[key] && propertySchemas[key]) {
//ensure that instance property is valid
propertySchemas[key].validate(instance.getProperty(key), report, instance, schema);
propertySchemas[key].validate(instance.getProperty(key), report, instance, schema, key);
}
}
}
Expand All @@ -215,7 +216,7 @@
return instance.getEnvironment().createEmptySchema();
},

"validator" : function (instance, schema, self, report, parent, parentSchema) {
"validator" : function (instance, schema, self, report, parent, parentSchema, name) {
var properties, items, x, xl, itemSchema, additionalProperties;

if (instance.getType() === "array") {
Expand All @@ -227,15 +228,15 @@
for (x = 0, xl = properties.length; x < xl; ++x) {
itemSchema = items[x] || additionalProperties;
if (itemSchema !== false) {
itemSchema.validate(properties[x], report, instance, schema);
itemSchema.validate(properties[x], report, instance, schema, x);
} else {
report.addError(instance, schema, "additionalProperties", "Additional items are not allowed", itemSchema);
}
}
} else {
itemSchema = items || additionalProperties;
for (x = 0, xl = properties.length; x < xl; ++x) {
itemSchema.validate(properties[x], report, instance, schema);
itemSchema.validate(properties[x], report, instance, schema, x);
}
}
}
Expand All @@ -251,7 +252,7 @@
return !!instance.getValue();
},

"validator" : function (instance, schema, self, report, parent, parentSchema) {
"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);
}
Expand All @@ -275,7 +276,7 @@
return instance.getEnvironment().createEmptySchema();
},

"validator" : function (instance, schema, self, report, parent, parentSchema) {
"validator" : function (instance, schema, self, report, parent, parentSchema, name) {
var additionalProperties, propertySchemas, properties, key;
//we only need to check against object types as arrays do their own checking on this property
if (instance.getType() === "object") {
Expand All @@ -285,7 +286,7 @@
for (key in properties) {
if (properties[key] !== O[key] && properties[key] && !propertySchemas[key]) {
if (JSV.isJSONSchema(additionalProperties)) {
additionalProperties.validate(properties[key], report, instance, schema);
additionalProperties.validate(properties[key], report, instance, schema, key);
} else if (additionalProperties === false) {
report.addError(instance, schema, "additionalProperties", "Additional properties are not allowed", additionalProperties);
}
Expand All @@ -307,7 +308,7 @@
}
},

"validator" : function (instance, schema, self, report, parent, parentSchema) {
"validator" : function (instance, schema, self, report, parent, parentSchema, name) {
var requires;
if (instance.getType() !== "undefined" && parent && parent.getType() !== "undefined") {
requires = schema.getAttribute("requires");
Expand All @@ -332,7 +333,7 @@
}
},

"validator" : function (instance, schema, self, report, parent, parentSchema) {
"validator" : function (instance, schema, self, report, parent, parentSchema, name) {
var minimum, minimumCanEqual;
if (instance.getType() === "number") {
minimum = schema.getAttribute("minimum");
Expand All @@ -354,7 +355,7 @@
}
},

"validator" : function (instance, schema, self, report, parent, parentSchema) {
"validator" : function (instance, schema, self, report, parent, parentSchema, name) {
var maximum, maximumCanEqual;
if (instance.getType() === "number") {
maximum = schema.getAttribute("maximum");
Expand Down Expand Up @@ -410,7 +411,7 @@
return 0;
},

"validator" : function (instance, schema, self, report, parent, parentSchema) {
"validator" : function (instance, schema, self, report, parent, parentSchema, name) {
var minItems;
if (instance.getType() === "array") {
minItems = schema.getAttribute("minItems");
Expand All @@ -432,7 +433,7 @@
}
},

"validator" : function (instance, schema, self, report, parent, parentSchema) {
"validator" : function (instance, schema, self, report, parent, parentSchema, name) {
var maxItems;
if (instance.getType() === "array") {
maxItems = schema.getAttribute("maxItems");
Expand All @@ -458,7 +459,7 @@
}
},

"validator" : function (instance, schema, self, report, parent, parentSchema) {
"validator" : function (instance, schema, self, report, parent, parentSchema, name) {
var pattern;
try {
pattern = schema.getAttribute("pattern");
Expand Down Expand Up @@ -487,7 +488,7 @@
return 0;
},

"validator" : function (instance, schema, self, report, parent, parentSchema) {
"validator" : function (instance, schema, self, report, parent, parentSchema, name) {
var minLength;
if (instance.getType() === "string") {
minLength = schema.getAttribute("minLength");
Expand All @@ -508,7 +509,7 @@
}
},

"validator" : function (instance, schema, self, report, parent, parentSchema) {
"validator" : function (instance, schema, self, report, parent, parentSchema, name) {
var maxLength;
if (instance.getType() === "string") {
maxLength = schema.getAttribute("maxLength");
Expand All @@ -531,7 +532,7 @@
}
},

"validator" : function (instance, schema, self, report, parent, parentSchema) {
"validator" : function (instance, schema, self, report, parent, parentSchema, name) {
var enums = schema.getAttribute("enum"), x, xl;
if (enums) {
for (x = 0, xl = enums.length; x < xl; ++x) {
Expand Down Expand Up @@ -564,7 +565,7 @@
}
},

"validator" : function (instance, schema, self, report, parent, parentSchema) {
"validator" : function (instance, schema, self, report, parent, parentSchema, name) {
var format, formatValidators;
if (instance.getType() === "string") {
format = schema.getAttribute("format");
Expand Down Expand Up @@ -599,7 +600,7 @@
}
},

"validator" : function (instance, schema, self, report, parent, parentSchema) {
"validator" : function (instance, schema, self, report, parent, parentSchema, name) {
var maxDecimal, decimals;
if (instance.getType() === "number") {
maxDecimal = schema.getAttribute("maxDecimal");
Expand All @@ -625,7 +626,7 @@
}
},

"validator" : function (instance, schema, self, report, parent, parentSchema) {
"validator" : function (instance, schema, self, report, parent, parentSchema, name) {
var disallowedTypes = JSV.toArray(schema.getAttribute("disallow")),
x, xl, key, typeValidators;

Expand Down Expand Up @@ -676,14 +677,14 @@
}
},

"validator" : function (instance, schema, self, report, parent, parentSchema) {
"validator" : function (instance, schema, self, report, parent, parentSchema, name) {
var extensions = schema.getAttribute("extends"), x, xl;
if (extensions) {
if (JSV.isJSONSchema(extensions)) {
extensions.validate(instance, report, parent, parentSchema);
extensions.validate(instance, report, parent, parentSchema, name);
} else if (JSV.typeOf(extensions) === "array") {
for (x = 0, xl = extensions.length; x < xl; ++x) {
extensions[x].validate(instance, report, parent, parentSchema);
extensions[x].validate(instance, report, parent, parentSchema, name);
}
}
}
Expand All @@ -701,7 +702,7 @@
}
},

"validator" : function (instance, schema, self, report, parent, parentSchema) {
"validator" : function (instance, schema, self, report, parent, parentSchema, name) {
var propNames = schema.getPropertyNames(),
x, xl,
attributeSchemas = self.getAttribute("properties"),
Expand All @@ -717,7 +718,7 @@
if (attributeSchemas[propNames[x]] !== O[propNames[x]]) {
validator = attributeSchemas[propNames[x]].getValueOfProperty("validator");
if (typeof validator === "function") {
validator(instance, schema, attributeSchemas[propNames[x]], report, parent, parentSchema);
validator(instance, schema, attributeSchemas[propNames[x]], report, parent, parentSchema, name);
}
}
}
Expand All @@ -744,8 +745,7 @@
//extend schema
extension = instance.getAttribute("extends");
if (JSV.isJSONSchema(extension)) {
extended = JSV.clone(extension._value, true);
JSV.mergeSchemas(extended, instance._value, true);
extended = JSV.inherits(extension, instance, true);

instance = instance._env.createSchema(extended, instance._schema, instance._uri);
}
Expand All @@ -763,7 +763,7 @@
}
}, true, "http://json-schema.org/schema#");

HYPERSCHEMA = ENVIRONMENT.createSchema(JSV.mergeSchemas(JSV.clone(SCHEMA.getValue(), true), {
HYPERSCHEMA = ENVIRONMENT.createSchema(JSV.inherits(SCHEMA, ENVIRONMENT.createSchema({
"$schema" : "http://json-schema.org/hyper-schema#",
"id" : "http://json-schema.org/hyper-schema#",

Expand Down Expand Up @@ -832,7 +832,7 @@
"optional" : true,
"format" : "uri",

"validator" : function (instance, schema, self, report, parent, parentSchema) {
"validator" : function (instance, schema, self, report, parent, parentSchema, name) {
var pathStart;
if (instance.getType() !== "undefined") {
pathStart = schema.getAttribute("pathStart");
Expand Down Expand Up @@ -876,8 +876,8 @@
}
],

"extends" : {"$ref" : "http://json-schema.org/schema#"}
}, true), true, "http://json-schema.org/hyper-schema#");
"extends" : [{"$ref" : "http://json-schema.org/schema#"}]
}, true, "http://json-schema.org/hyper-schema.temp#"), true), true, "http://json-schema.org/hyper-schema#");

LINKS = ENVIRONMENT.createSchema({
"$schema" : "http://json-schema.org/hyper-schema#",
Expand Down
Loading

0 comments on commit c1b7299

Please sign in to comment.