forked from garycourt/JSV
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Validators are now passed the name of the instance being validated.
Replaced merge algorithm to one that is JSON Schema compliant. Added JSONSchema.prototype.getAttributes().
- Loading branch information
Showing
5 changed files
with
187 additions
and
156 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
*/ | ||
|
||
|
@@ -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 = {}, | ||
|
@@ -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; | ||
|
||
|
@@ -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 { | ||
|
@@ -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") { | ||
|
@@ -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); | ||
} | ||
} | ||
} | ||
|
@@ -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") { | ||
|
@@ -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); | ||
} | ||
} | ||
} | ||
|
@@ -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); | ||
} | ||
|
@@ -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") { | ||
|
@@ -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); | ||
} | ||
|
@@ -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"); | ||
|
@@ -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"); | ||
|
@@ -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"); | ||
|
@@ -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"); | ||
|
@@ -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"); | ||
|
@@ -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"); | ||
|
@@ -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"); | ||
|
@@ -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"); | ||
|
@@ -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) { | ||
|
@@ -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"); | ||
|
@@ -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"); | ||
|
@@ -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; | ||
|
||
|
@@ -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); | ||
} | ||
} | ||
} | ||
|
@@ -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"), | ||
|
@@ -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); | ||
} | ||
} | ||
} | ||
|
@@ -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); | ||
} | ||
|
@@ -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#", | ||
|
||
|
@@ -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"); | ||
|
@@ -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#", | ||
|
Oops, something went wrong.