Comparing version 3.5.0 to 4.0.0
# Changelog | ||
## 4.0 (2011/08/23) | ||
* Added referencing to JSONSchema, allows for soft-linking to other schemas. | ||
* Environment option "validateReferences" now validates references at validation time for any environment. | ||
* Environment option "enforceReferences" will cause invalid references to throw an error at creation time for any environment. | ||
* Fixed floating point errors with "divisibleBy" attribute. | ||
* Fixed typo in JSV.clone. | ||
* Fixed typo in draft-03 hyper-schema.json. | ||
* Updated uri.js to latest version. | ||
* Fixed bug with Report#addError. (Identified by cellog) | ||
* ValidationError#details is now safe to stringify. | ||
* Removed JSONSchema#createEmptySchema. All empty schemas created by Environment#createEmptySchema now have the same URI (urn:jsv:empty-schema#). | ||
* Added example HTML page. | ||
## 3.5 (2011/03/07) | ||
@@ -4,0 +18,0 @@ |
@@ -6,3 +6,3 @@ /** | ||
* @author <a href="mailto:gary.court@gmail.com">Gary Court</a> | ||
* @version 1.5 | ||
* @version 1.6 | ||
* @see http://github.com/garycourt/JSV | ||
@@ -734,34 +734,27 @@ */ | ||
do { | ||
//if there is a link to the full representation, replace instance | ||
link = instance._schema.getLink("full", instance); | ||
if (link && instance._uri !== link && instance._env._schemas[link]) { | ||
instance = instance._env._schemas[link]; | ||
return instance; //retrieved schemas are guaranteed to be initialized | ||
} | ||
//if there is a link to a different schema, update instance | ||
link = instance._schema.getLink("describedby", instance); | ||
if (link && instance._schema._uri !== link && instance._env._schemas[link]) { | ||
instance._schema = instance._env._schemas[link]; | ||
continue; //start over | ||
} | ||
//extend schema | ||
extension = instance.getAttribute("extends"); | ||
if (JSV.isJSONSchema(extension)) { | ||
extended = JSV.inherits(extension, instance, true); | ||
instance = instance._env.createSchema(extended, instance._schema, instance._uri); | ||
} | ||
break; //get out of the loop | ||
} while (true); | ||
//if there is a link to a different schema, set reference | ||
link = instance._schema.getLink("describedby", instance); | ||
if (link && instance._schema._uri !== link) { | ||
instance.setReference("describedby", link); | ||
} | ||
//if instance has a URI link to itself, update it's own URI | ||
link = instance._schema.getLink("self", instance); | ||
if (link) { | ||
instance._uri = link; | ||
if (JSV.typeOf(link) === "string") { | ||
instance._uri = JSV.formatURI(link); | ||
} | ||
//if there is a link to the full representation, set reference | ||
link = instance._schema.getLink("full", instance); | ||
if (link && instance._uri !== link) { | ||
instance.setReference("full", link); | ||
} | ||
//extend schema | ||
extension = instance.getAttribute("extends"); | ||
if (JSV.isJSONSchema(extension)) { | ||
extended = JSV.inherits(extension, instance, true); | ||
instance = instance._env.createSchema(extended, instance._schema, instance._uri); | ||
} | ||
return instance; | ||
@@ -961,7 +954,2 @@ } | ||
//We need to reinitialize these 3 schemas as they all reference each other | ||
SCHEMA = ENVIRONMENT.createSchema(SCHEMA.getValue(), HYPERSCHEMA, "http://json-schema.org/schema#"); | ||
HYPERSCHEMA = ENVIRONMENT.createSchema(HYPERSCHEMA.getValue(), HYPERSCHEMA, "http://json-schema.org/hyper-schema#"); | ||
LINKS = ENVIRONMENT.createSchema(LINKS.getValue(), HYPERSCHEMA, "http://json-schema.org/links#"); | ||
JSV.registerEnvironment("json-schema-draft-00", ENVIRONMENT); | ||
@@ -968,0 +956,0 @@ JSV.registerEnvironment("json-schema-draft-01", JSV.createEnvironment("json-schema-draft-00")); |
@@ -6,3 +6,3 @@ /** | ||
* @author <a href="mailto:gary.court@gmail.com">Gary Court</a> | ||
* @version 1.5 | ||
* @version 1.6 | ||
* @see http://github.com/garycourt/JSV | ||
@@ -758,34 +758,27 @@ */ | ||
do { | ||
//if there is a link to the full representation, replace instance | ||
link = instance._schema.getLink("full", instance); | ||
if (link && instance._uri !== link && instance._env._schemas[link]) { | ||
instance = instance._env._schemas[link]; | ||
return instance; //retrieved schemas are guaranteed to be initialized | ||
} | ||
//if there is a link to a different schema, update instance | ||
link = instance._schema.getLink("describedby", instance); | ||
if (link && instance._schema._uri !== link && instance._env._schemas[link]) { | ||
instance._schema = instance._env._schemas[link]; | ||
continue; //start over | ||
} | ||
//extend schema | ||
extension = instance.getAttribute("extends"); | ||
if (JSV.isJSONSchema(extension)) { | ||
extended = JSV.inherits(extension, instance, true); | ||
instance = instance._env.createSchema(extended, instance._schema, instance._uri); | ||
} | ||
break; //get out of the loop | ||
} while (true); | ||
//if there is a link to a different schema, set reference | ||
link = instance._schema.getLink("describedby", instance); | ||
if (link && instance._schema._uri !== link) { | ||
instance.setReference("describedby", link); | ||
} | ||
//if instance has a URI link to itself, update it's own URI | ||
link = instance._schema.getLink("self", instance); | ||
if (link) { | ||
instance._uri = link; | ||
if (JSV.typeOf(link) === "string") { | ||
instance._uri = JSV.formatURI(link); | ||
} | ||
//if there is a link to the full representation, set reference | ||
link = instance._schema.getLink("full", instance); | ||
if (link && instance._uri !== link) { | ||
instance.setReference("full", link); | ||
} | ||
//extend schema | ||
extension = instance.getAttribute("extends"); | ||
if (JSV.isJSONSchema(extension)) { | ||
extended = JSV.inherits(extension, instance, true); | ||
instance = instance._env.createSchema(extended, instance._schema, instance._uri); | ||
} | ||
return instance; | ||
@@ -992,7 +985,2 @@ } | ||
//We need to reinitialize these 3 schemas as they all reference each other | ||
SCHEMA = ENVIRONMENT.createSchema(SCHEMA.getValue(), HYPERSCHEMA, "http://json-schema.org/schema#"); | ||
HYPERSCHEMA = ENVIRONMENT.createSchema(HYPERSCHEMA.getValue(), HYPERSCHEMA, "http://json-schema.org/hyper-schema#"); | ||
LINKS = ENVIRONMENT.createSchema(LINKS.getValue(), HYPERSCHEMA, "http://json-schema.org/links#"); | ||
JSV.registerEnvironment("json-schema-draft-02", ENVIRONMENT); | ||
@@ -999,0 +987,0 @@ if (!JSV.getDefaultEnvironmentID() || JSV.getDefaultEnvironmentID() === "json-schema-draft-01") { |
@@ -6,3 +6,3 @@ /** | ||
* @author <a href="mailto:gary.court@gmail.com">Gary Court</a> | ||
* @version 1.3 | ||
* @version 1.4 | ||
* @see http://github.com/garycourt/JSV | ||
@@ -45,3 +45,2 @@ */ | ||
JSV = require('./jsv').JSV, | ||
InitializationError, | ||
TYPE_VALIDATORS, | ||
@@ -74,16 +73,2 @@ ENVIRONMENT, | ||
InitializationError = function InitializationError(instance, schema, attr, message, details) { | ||
Error.call(this, message); | ||
this.uri = instance.getURI(); | ||
this.schemaUri = schema.getURI(); | ||
this.attribute = attr; | ||
this.message = message; | ||
this.description = message; //IE | ||
this.details = details; | ||
} | ||
InitializationError.prototype = new Error(); | ||
InitializationError.prototype.constructor = InitializationError; | ||
InitializationError.prototype.name = "InitializationError"; | ||
TYPE_VALIDATORS = { | ||
@@ -124,4 +109,5 @@ "string" : function (instance, report) { | ||
ENVIRONMENT = new JSV.Environment(); | ||
ENVIRONMENT.setOption("validateReferences", true); | ||
ENVIRONMENT.setOption("enforceReferences", false); | ||
ENVIRONMENT.setOption("strict", false); | ||
ENVIRONMENT.setOption("validateReferences", false); //updated later | ||
@@ -916,27 +902,18 @@ // | ||
//if there is a link to a different schema, update instance | ||
//if there is a link to a different schema, set reference | ||
link = instance._schema.getLink("describedby", instance); | ||
if (link && instance._schema._uri !== link) { | ||
if (instance._env._schemas[link]) { | ||
instance._schema = instance._env._schemas[link]; | ||
initializer = instance._schema.getValueOfProperty("initializer"); | ||
if (typeof initializer === "function") { | ||
return initializer(instance); //this function will finish initialization | ||
} else { | ||
return instance; //no further initialization | ||
} | ||
} else if (instance._env._options["validateReferences"]) { | ||
throw new InitializationError(instance, instance._schema, "{link:describedby}", "Unknown schema reference", link); | ||
} | ||
instance.setReference("describedby", link); | ||
} | ||
//if there is a link to the full representation, replace instance | ||
//if instance has a URI link to itself, update it's own URI | ||
link = instance._schema.getLink("self", instance); | ||
if (JSV.typeOf(link) === "string") { | ||
instance._uri = JSV.formatURI(link); | ||
} | ||
//if there is a link to the full representation, set reference | ||
link = instance._schema.getLink("full", instance); | ||
if (link && instance._uri !== link) { | ||
if (instance._env._schemas[link]) { | ||
instance = instance._env._schemas[link]; | ||
return instance; //retrieved schemas are guaranteed to be initialized | ||
} else if (instance._env._options["validateReferences"]) { | ||
throw new InitializationError(instance, instance._schema, "{link:full}", "Unknown schema reference", link); | ||
} | ||
instance.setReference("full", link); | ||
} | ||
@@ -950,8 +927,2 @@ | ||
} | ||
//if instance has a URI link to itself, update it's own URI | ||
link = instance._schema.getLink("self", instance); | ||
if (JSV.typeOf(link) === "string") { | ||
instance._uri = JSV.formatURI(link); | ||
} | ||
@@ -1040,7 +1011,2 @@ return instance; | ||
//We need to reinitialize these 3 schemas as they all reference each other | ||
SCHEMA_00 = ENVIRONMENT.createSchema(SCHEMA_00.getValue(), HYPERSCHEMA_00, "http://json-schema.org/draft-00/schema#"); | ||
HYPERSCHEMA_00 = ENVIRONMENT.createSchema(HYPERSCHEMA_00.getValue(), HYPERSCHEMA_00, "http://json-schema.org/draft-00/hyper-schema#"); | ||
LINKS_00 = ENVIRONMENT.createSchema(LINKS_00.getValue(), HYPERSCHEMA_00, "http://json-schema.org/draft-00/links#"); | ||
// | ||
@@ -1074,7 +1040,2 @@ // draft-01 | ||
//We need to reinitialize these 3 schemas as they all reference each other | ||
SCHEMA_01 = ENVIRONMENT.createSchema(SCHEMA_01.getValue(), HYPERSCHEMA_01, "http://json-schema.org/draft-01/schema#"); | ||
HYPERSCHEMA_01 = ENVIRONMENT.createSchema(HYPERSCHEMA_01.getValue(), HYPERSCHEMA_01, "http://json-schema.org/draft-01/hyper-schema#"); | ||
LINKS_01 = ENVIRONMENT.createSchema(LINKS_01.getValue(), HYPERSCHEMA_01, "http://json-schema.org/draft-01/links#"); | ||
// | ||
@@ -1130,3 +1091,3 @@ // draft-02 | ||
"validator" : function (instance, schema, self, report, parent, parentSchema, name) { | ||
var divisor; | ||
var divisor, value, digits; | ||
if (instance.getType() === "number") { | ||
@@ -1136,4 +1097,9 @@ divisor = schema.getAttribute("divisibleBy"); | ||
report.addError(instance, schema, "divisibleBy", "Nothing is divisible by 0", divisor); | ||
} else if (divisor !== 1 && ((instance.getValue() / divisor) % 1) !== 0) { | ||
report.addError(instance, schema, "divisibleBy", "Number is not divisible by " + divisor, divisor); | ||
} else if (divisor !== 1) { | ||
value = instance.getValue(); | ||
digits = Math.max((value.toString().split(".")[1] || " ").length, (divisor.toString().split(".")[1] || " ").length); | ||
digits = parseFloat(((value / divisor) % 1).toFixed(digits)); //cut out floating point errors | ||
if (0 < digits && digits < 1) { | ||
report.addError(instance, schema, "divisibleBy", "Number is not divisible by " + divisor, divisor); | ||
} | ||
} | ||
@@ -1182,7 +1148,2 @@ } | ||
//We need to reinitialize these 3 schemas as they all reference each other | ||
SCHEMA_02 = ENVIRONMENT.createSchema(SCHEMA_02.getValue(), HYPERSCHEMA_02, "http://json-schema.org/draft-02/schema#"); | ||
HYPERSCHEMA_02 = ENVIRONMENT.createSchema(HYPERSCHEMA_02.getValue(), HYPERSCHEMA_02, "http://json-schema.org/draft-02/hyper-schema#"); | ||
LINKS_02 = ENVIRONMENT.createSchema(LINKS_02.getValue(), HYPERSCHEMA_02, "http://json-schema.org/draft-02/links#"); | ||
// | ||
@@ -1501,31 +1462,20 @@ // draft-03 | ||
//if there is a link to a different schema, update instance | ||
//if there is a link to a different schema, set reference | ||
if (schemaLink) { | ||
link = instance.resolveURI(schemaLink); | ||
if (link && instance._schema._uri !== link) { | ||
if (instance._env._schemas[link]) { | ||
instance._schema = instance._env._schemas[link]; | ||
initializer = instance._schema.getValueOfProperty("initializer"); | ||
if (typeof initializer === "function") { | ||
return initializer(instance); //this function will finish initialization | ||
} else { | ||
return instance; //no further initialization | ||
} | ||
} else if (instance._env._options["validateReferences"]) { | ||
throw new InitializationError(instance, instance._schema, "$schema", "Unknown schema reference", link); | ||
} | ||
instance.setReference("describedby", link); | ||
} | ||
//if instance has a URI link to itself, update it's own URI | ||
if (idLink) { | ||
link = instance.resolveURI(idLink); | ||
if (JSV.typeOf(link) === "string") { | ||
instance._uri = JSV.formatURI(link); | ||
} | ||
} | ||
//if there is a link to the full representation, replace instance | ||
//if there is a link to the full representation, set reference | ||
if (refLink) { | ||
link = instance.resolveURI(refLink); | ||
if (link && instance._uri !== link) { | ||
if (instance._env._schemas[link]) { | ||
instance = instance._env._schemas[link]; | ||
return instance; //retrieved schemas are guaranteed to be initialized | ||
} else if (instance._env._options["validateReferences"]) { | ||
throw new InitializationError(instance, instance._schema, "$ref", "Unknown schema reference", link); | ||
} | ||
} | ||
instance.setReference("full", link); | ||
} | ||
@@ -1539,10 +1489,2 @@ | ||
} | ||
//if instance has a URI link to itself, update it's own URI | ||
if (idLink) { | ||
link = instance.resolveURI(idLink); | ||
if (JSV.typeOf(link) === "string") { | ||
instance._uri = JSV.formatURI(link); | ||
} | ||
} | ||
@@ -1598,18 +1540,10 @@ return instance; | ||
ENVIRONMENT.setOption("validateReferences", true); | ||
ENVIRONMENT.setOption("defaultSchemaURI", "http://json-schema.org/draft-03/schema#"); //update later | ||
//prevent reference errors | ||
ENVIRONMENT.createSchema({}, true, "http://json-schema.org/draft-03/schema#"); | ||
ENVIRONMENT.createSchema({}, true, "http://json-schema.org/draft-03/hyper-schema#"); | ||
ENVIRONMENT.createSchema({}, true, "http://json-schema.org/draft-03/links#"); | ||
SCHEMA_03 = ENVIRONMENT.createSchema(SCHEMA_03_JSON, true, "http://json-schema.org/draft-03/schema#"); | ||
HYPERSCHEMA_03 = ENVIRONMENT.createSchema(JSV.inherits(SCHEMA_03, ENVIRONMENT.createSchema(HYPERSCHEMA_03_JSON, true, "http://json-schema.org/draft-03/hyper-schema#"), true), true, "http://json-schema.org/draft-03/hyper-schema#"); | ||
LINKS_03 = ENVIRONMENT.createSchema(LINKS_03_JSON, true, "http://json-schema.org/draft-03/links#"); | ||
ENVIRONMENT.setOption("defaultSchemaURI", "http://json-schema.org/draft-03/hyper-schema#"); | ||
//We need to reinitialize these schemas as they reference each other | ||
HYPERSCHEMA_03 = ENVIRONMENT.createSchema(HYPERSCHEMA_03.getValue(), HYPERSCHEMA_03, "http://json-schema.org/draft-03/hyper-schema#"); | ||
LINKS_03 = ENVIRONMENT.createSchema(LINKS_03_JSON, true, "http://json-schema.org/draft-03/links#"); | ||
@@ -1616,0 +1550,0 @@ ENVIRONMENT.setOption("latestJSONSchemaSchemaURI", "http://json-schema.org/draft-03/schema#"); |
275
lib/jsv.js
@@ -6,3 +6,3 @@ /** | ||
* @author <a href="mailto:gary.court@gmail.com">Gary Court</a> | ||
* @version 3.5 | ||
* @version 4.0 | ||
* @see http://github.com/garycourt/JSV | ||
@@ -224,3 +224,54 @@ */ | ||
function stripInstances(o) { | ||
if (o instanceof JSONInstance) { | ||
return o.getURI(); | ||
} | ||
switch (typeOf(o)) { | ||
case "undefined": | ||
case "null": | ||
case "boolean": | ||
case "number": | ||
case "string": | ||
return o; //do nothing | ||
case "object": | ||
return mapObject(o, stripInstances); | ||
case "array": | ||
return mapArray(o, stripInstances); | ||
default: | ||
return o.toString(); | ||
} | ||
} | ||
/** | ||
* The exception that is thrown when a schema fails to be created. | ||
* | ||
* @name InitializationError | ||
* @class | ||
* @param {JSONInstance|String} instance The instance (or instance URI) that is invalid | ||
* @param {JSONSchema|String} schema The schema (or schema URI) that was validating the instance | ||
* @param {String} attr The attribute that failed to validated | ||
* @param {String} message A user-friendly message on why the schema attribute failed to validate the instance | ||
* @param {Any} details The value of the schema attribute | ||
*/ | ||
function InitializationError(instance, schema, attr, message, details) { | ||
Error.call(this, message); | ||
this.uri = instance instanceof JSONInstance ? instance.getURI() : instance; | ||
this.schemaUri = schema instanceof JSONInstance ? schema.getURI() : schema; | ||
this.attribute = attr; | ||
this.message = message; | ||
this.description = message; //IE | ||
this.details = details; | ||
} | ||
InitializationError.prototype = new Error(); | ||
InitializationError.prototype.constructor = InitializationError; | ||
InitializationError.prototype.name = "InitializationError"; | ||
/** | ||
* Defines an error, found by a schema, with an instance. | ||
@@ -342,6 +393,6 @@ * This class can only be instantiated by {@link Report#addError}. | ||
uri : instance instanceof JSONInstance ? instance.getURI() : instance, | ||
schemaUri : instance instanceof JSONInstance ? schema.getURI() : schema, | ||
schemaUri : schema instanceof JSONInstance ? schema.getURI() : schema, | ||
attribute : attr, | ||
message : message, | ||
details : details | ||
details : stripInstances(details) | ||
}); | ||
@@ -590,3 +641,3 @@ }; | ||
} else { | ||
return Array.pototype.slice.call(obj); | ||
return Array.prototype.slice.call(obj); | ||
} | ||
@@ -620,3 +671,3 @@ break; | ||
} else { | ||
this._schema = schema instanceof JSONSchema ? schema : this._env.getDefaultSchema() || JSONSchema.createEmptySchema(this._env); | ||
this._schema = schema instanceof JSONSchema ? schema : this._env.getDefaultSchema() || this._env.createEmptySchema(); | ||
} | ||
@@ -631,2 +682,4 @@ | ||
} | ||
return this.rebuild(); //this works even when called with "new" | ||
} | ||
@@ -637,16 +690,2 @@ | ||
/** | ||
* Creates an empty schema. | ||
* | ||
* @param {Environment} env The environment of the schema | ||
* @returns {JSONSchema} The empty schema, who's schema is itself. | ||
*/ | ||
JSONSchema.createEmptySchema = function (env) { | ||
var schema = createObject(JSONSchema.prototype); | ||
JSONInstance.call(schema, env, {}, undefined, undefined); | ||
schema._schema = schema; | ||
return schema; | ||
}; | ||
/** | ||
* Returns the schema of the schema. | ||
@@ -658,2 +697,18 @@ * | ||
JSONSchema.prototype.getSchema = function () { | ||
var uri = this._refs && this._refs["describedby"], | ||
newSchema; | ||
if (uri) { | ||
newSchema = uri && this._env.findSchema(uri); | ||
if (newSchema) { | ||
if (!newSchema.equals(this._schema)) { | ||
this._schema = newSchema; | ||
this.rebuild(); //if the schema has changed, the context has changed - so everything must be rebuilt | ||
} | ||
} else if (this._env._options["enforceReferences"]) { | ||
throw new InitializationError(this, this._schema, "{describedby}", "Unknown schema reference", uri); | ||
} | ||
} | ||
return this._schema; | ||
@@ -676,3 +731,4 @@ }; | ||
JSONSchema.prototype.getAttribute = function (key, arg) { | ||
var schemaProperty, parser, property, result; | ||
var schemaProperty, parser, property, result, | ||
schema = this.getSchema(); //we do this here to make sure the "describedby" reference has not changed, and that the attribute cache is up-to-date | ||
@@ -683,3 +739,3 @@ if (!arg && this._attributes && this._attributes.hasOwnProperty(key)) { | ||
schemaProperty = this._schema.getProperty("properties").getProperty(key); | ||
schemaProperty = schema.getProperty("properties").getProperty(key); | ||
parser = schemaProperty.getValueOfProperty("parser"); | ||
@@ -705,7 +761,8 @@ property = this.getProperty(key); | ||
JSONSchema.prototype.getAttributes = function () { | ||
var properties, schemaProperties, key, schemaProperty, parser; | ||
var properties, schemaProperties, key, schemaProperty, parser, | ||
schema = this.getSchema(); //we do this here to make sure the "describedby" reference has not changed, and that the attribute cache is up-to-date | ||
if (!this._attributes && this.getType() === "object") { | ||
properties = this.getProperties(); | ||
schemaProperties = this._schema.getProperty("properties"); | ||
schemaProperties = schema.getProperty("properties"); | ||
this._attributes = {}; | ||
@@ -759,3 +816,4 @@ for (key in properties) { | ||
JSONSchema.prototype.validate = function (instance, report, parent, parentSchema, name) { | ||
var validator = this._schema.getValueOfProperty("validator"); | ||
var schemaSchema = this.getSchema(), | ||
validator = schemaSchema.getValueOfProperty("validator"); | ||
@@ -770,5 +828,14 @@ if (!(instance instanceof JSONInstance)) { | ||
if (this._env._options["validateReferences"] && this._refs) { | ||
if (this._refs["describedby"] && !this._env.findSchema(this._refs["describedby"])) { | ||
report.addError(this, this._schema, "{describedby}", "Unknown schema reference", this._refs["describedby"]); | ||
} | ||
if (this._refs["full"] && !this._env.findSchema(this._refs["full"])) { | ||
report.addError(this, this._schema, "{full}", "Unknown schema reference", this._refs["full"]); | ||
} | ||
} | ||
if (typeof validator === "function" && !report.isValidatedBy(instance.getURI(), this.getURI())) { | ||
report.registerValidation(instance.getURI(), this.getURI()); | ||
validator(instance, this, this._schema, report, parent, parentSchema, name); | ||
validator(instance, this, schemaSchema, report, parent, parentSchema, name); | ||
} | ||
@@ -779,3 +846,112 @@ | ||
/** @inner */ | ||
function createFullLookupWrapper(func) { | ||
return /** @inner */ function fullLookupWrapper() { | ||
var scope = this, | ||
stack = [], | ||
uri = scope._refs && scope._refs["full"], | ||
schema; | ||
while (uri) { | ||
schema = scope._env.findSchema(uri); | ||
if (schema) { | ||
if (schema._value === scope._value) { | ||
break; | ||
} | ||
scope = schema; | ||
stack.push(uri); | ||
uri = scope._refs && scope._refs["full"]; | ||
if (stack.indexOf(uri) > -1) { | ||
break; //stop infinite loop | ||
} | ||
} else if (scope._env._options["enforceReferences"]) { | ||
throw new InitializationError(scope, scope._schema, "{full}", "Unknown schema reference", uri); | ||
} else { | ||
uri = null; | ||
} | ||
} | ||
return func.apply(scope, arguments); | ||
}; | ||
} | ||
/** | ||
* Wraps all JSONInstance methods with a function that resolves the "full" reference. | ||
* | ||
* @inner | ||
*/ | ||
(function () { | ||
var key; | ||
for (key in JSONSchema.prototype) { | ||
if (JSONSchema.prototype[key] !== O[key] && typeOf(JSONSchema.prototype[key]) === "function") { | ||
JSONSchema.prototype[key] = createFullLookupWrapper(JSONSchema.prototype[key]); | ||
} | ||
} | ||
}()); | ||
/** | ||
* Reinitializes/re-registers/rebuilds the schema. | ||
* <br/> | ||
* This is used internally, and should only be called when a schema's private variables are modified directly. | ||
* | ||
* @private | ||
* @return {JSONSchema} The newly rebuilt schema | ||
*/ | ||
JSONSchema.prototype.rebuild = function () { | ||
var instance = this, | ||
initializer = instance.getSchema().getValueOfProperty("initializer"); | ||
//clear previous built values | ||
instance._refs = null; | ||
instance._attributes = null; | ||
if (typeof initializer === "function") { | ||
instance = initializer(instance); | ||
} | ||
//register schema | ||
instance._env._schemas[instance._uri] = instance; | ||
//build & cache the rest of the schema | ||
instance.getAttributes(); | ||
return instance; | ||
}; | ||
/** | ||
* Set the provided reference to the given value. | ||
* <br/> | ||
* References are used for establishing soft-links to other {@link JSONSchema}s. | ||
* Currently, the following references are natively supported: | ||
* <dl> | ||
* <dt><code>full</code></dt> | ||
* <dd>The value is the URI to the full instance of this instance.</dd> | ||
* <dt><code>describedby</code></dt> | ||
* <dd>The value is the URI to the schema of this instance.</dd> | ||
* </dl> | ||
* | ||
* @param {String} name The name of the reference | ||
* @param {String} uri The URI of the schema to refer to | ||
*/ | ||
JSONSchema.prototype.setReference = function (name, uri) { | ||
if (!this._refs) { | ||
this._refs = {}; | ||
} | ||
this._refs[name] = this.resolveURI(uri); | ||
}; | ||
/** | ||
* Returns the value of the provided reference name. | ||
* | ||
* @param {String} name The name of the reference | ||
* @return {String} The value of the provided reference name | ||
*/ | ||
JSONSchema.prototype.getReference = function (name) { | ||
return this._refs && this._refs[name]; | ||
}; | ||
/** | ||
* Merges two schemas/instances together. | ||
@@ -826,2 +1002,4 @@ */ | ||
this._options = {}; | ||
this.createSchema({}, true, "urn:jsv:empty-schema#"); | ||
} | ||
@@ -852,3 +1030,2 @@ | ||
Environment.prototype.createInstance = function (data, uri) { | ||
var instance; | ||
uri = formatURI(uri); | ||
@@ -859,6 +1036,4 @@ | ||
} | ||
//else | ||
instance = new JSONInstance(this, data, uri); | ||
return instance; | ||
return new JSONInstance(this, data, uri); | ||
}; | ||
@@ -877,25 +1052,9 @@ | ||
Environment.prototype.createSchema = function (data, schema, uri) { | ||
var instance, | ||
initializer; | ||
uri = formatURI(uri); | ||
if (data instanceof JSONSchema && (!uri || data._uri === uri) && (!schema || data._schema.equals(schema))) { | ||
if (data instanceof JSONSchema && (!uri || data._uri === uri) && (!schema || data.getSchema().equals(schema))) { | ||
return data; | ||
} | ||
instance = new JSONSchema(this, data, uri, schema); | ||
initializer = instance.getSchema().getValueOfProperty("initializer"); | ||
if (typeof initializer === "function") { | ||
instance = initializer(instance); | ||
} | ||
//register schema | ||
this._schemas[instance._uri] = instance; | ||
this._schemas[uri] = instance; | ||
//build & cache the rest of the schema | ||
instance.getAttributes(); | ||
return instance; | ||
return new JSONSchema(this, data, uri, schema); | ||
}; | ||
@@ -906,3 +1065,2 @@ | ||
* | ||
* @param {Environment} env The environment of the schema | ||
* @returns {JSONSchema} The empty schema, who's schema is itself. | ||
@@ -912,5 +1070,3 @@ */ | ||
Environment.prototype.createEmptySchema = function () { | ||
var schema = JSONSchema.createEmptySchema(this); | ||
this._schemas[schema._uri] = schema; | ||
return schema; | ||
return this._schemas["urn:jsv:empty-schema#"]; | ||
}; | ||
@@ -1026,4 +1182,4 @@ | ||
report.schemaSchema = schemaSchema; | ||
} catch (e) { | ||
report.addError(e.uri, e.schemaUri, e.attribute, e.message, e.details); | ||
} catch (f) { | ||
report.addError(f.uri, f.schemaUri, f.attribute, f.message, f.details); | ||
} | ||
@@ -1052,3 +1208,3 @@ | ||
counter = 0, | ||
item, uri, instance, schema, properties, key; | ||
item, uri, instance, properties, key; | ||
@@ -1348,3 +1504,10 @@ while (counter++ < stackSize && stack.length) { | ||
inherits : inherits | ||
inherits : inherits, | ||
/** | ||
* @private | ||
* @event //utility | ||
*/ | ||
InitializationError : InitializationError | ||
}; | ||
@@ -1351,0 +1514,0 @@ |
/** | ||
* URI.js | ||
* | ||
* @fileOverview An RFC 3986 compliant, scheme extendable URI parsing/validating/resolving library for JavaScript. | ||
* @fileoverview An RFC 3986 compliant, scheme extendable URI parsing/validating/resolving library for JavaScript. | ||
* @author <a href="mailto:gary.court@gmail.com">Gary Court</a> | ||
* @version 1.0 | ||
* @version 1.3 | ||
* @see http://github.com/garycourt/uri-js | ||
* @license URI.js v1.3 (c) 2010 Gary Court. License: http://github.com/garycourt/uri-js | ||
*/ | ||
/* | ||
/** | ||
* Copyright 2010 Gary Court. All rights reserved. | ||
@@ -39,12 +40,19 @@ * | ||
/*jslint white: true, sub: true, onevar: true, undef: true, eqeqeq: true, newcap: true, immed: true, indent: 4 */ | ||
/*global exports:true, require:true */ | ||
var exports = exports || this, | ||
require = require || function () { | ||
if (typeof exports === "undefined") { | ||
exports = {}; | ||
} | ||
if (typeof require !== "function") { | ||
require = function (id) { | ||
return exports; | ||
}; | ||
} | ||
(function () { | ||
var Options, URIComponents, SchemeHandler, URI, | ||
mergeSet = function () { | ||
var | ||
/** | ||
* @param {...string} sets | ||
* @return {string} | ||
*/ | ||
mergeSet = function (sets) { | ||
var set = arguments[0], | ||
@@ -62,2 +70,6 @@ x = 1, | ||
/** | ||
* @param {string} str | ||
* @return {string} | ||
*/ | ||
subexp = function (str) { | ||
@@ -81,4 +93,4 @@ return "(?:" + str + ")"; | ||
USERINFO$ = subexp(subexp(PCT_ENCODED$ + "|" + mergeSet(UNRESERVED$$, SUB_DELIMS$$, "[\\:]")) + "*"), | ||
DEC_OCTET$ = subexp(DIGIT$$ + "|" + subexp("[1-9]" + DIGIT$$) + "|" + subexp("1" + DIGIT$$ + DIGIT$$) + "|" + subexp("2[0-4]" + DIGIT$$) + "|" + subexp("25[0-5]")), | ||
IPV4ADDRESS$ = subexp(DEC_OCTET$ + "." + DEC_OCTET$ + "." + DEC_OCTET$ + "." + DEC_OCTET$), | ||
DEC_OCTET$ = subexp(subexp("25[0-5]") + "|" + subexp("2[0-4]" + DIGIT$$) + "|" + subexp("1" + DIGIT$$ + DIGIT$$) + "|" + subexp("[1-9]" + DIGIT$$) + "|" + DIGIT$$), | ||
IPV4ADDRESS$ = subexp(DEC_OCTET$ + "\\." + DEC_OCTET$ + "\\." + DEC_OCTET$ + "\\." + DEC_OCTET$), | ||
H16$ = subexp(HEXDIG$$ + "{1,4}"), | ||
@@ -138,2 +150,6 @@ LS32$ = subexp(subexp(H16$ + "\\:" + H16$) + "|" + IPV4ADDRESS$), | ||
/** | ||
* @param {string} chr | ||
* @return {string} | ||
*/ | ||
pctEncChar = function (chr) { | ||
@@ -153,2 +169,6 @@ var c = chr.charCodeAt(0); | ||
/** | ||
* @param {string} str | ||
* @return {string} | ||
*/ | ||
pctDecUnreserved = function (str) { | ||
@@ -184,2 +204,6 @@ var newStr = "", | ||
/** | ||
* @param {string} str | ||
* @return {string} | ||
*/ | ||
pctDecChars = function (str) { | ||
@@ -213,42 +237,25 @@ var newStr = "", | ||
/** | ||
* @return {string} | ||
*/ | ||
typeOf = function (o) { | ||
return o === undefined ? "undefined" : (o === null ? "null" : Object.prototype.toString.call(o).split(" ").pop().split("]").shift().toLowerCase()); | ||
}; | ||
/** | ||
* @class | ||
*/ | ||
Options = function () {}; | ||
Options.prototype = { | ||
/** | ||
* @type Boolean | ||
*/ | ||
}, | ||
tolerant : undefined, | ||
/** | ||
* @type String | ||
* @constructor | ||
* @implements URIComponents | ||
*/ | ||
Components = function () { | ||
this.errors = []; | ||
}, | ||
scheme : undefined, | ||
/** | ||
* @type String | ||
* @enum "uri", "absolute", "relative", "same-document", "suffix" | ||
*/ | ||
reference : undefined | ||
}; | ||
/** @namespace */ | ||
URI = exports; | ||
/** | ||
* @class | ||
* Components | ||
*/ | ||
URIComponents = function () { | ||
this.errors = []; | ||
}; | ||
URIComponents.prototype = { | ||
Components.prototype = { | ||
/** | ||
@@ -279,3 +286,3 @@ * @type String | ||
/** | ||
* @type Number | ||
* @type number | ||
*/ | ||
@@ -286,3 +293,3 @@ | ||
/** | ||
* @type String | ||
* @type string | ||
*/ | ||
@@ -293,3 +300,3 @@ | ||
/** | ||
* @type String | ||
* @type string | ||
*/ | ||
@@ -300,3 +307,3 @@ | ||
/** | ||
* @type String | ||
* @type string | ||
*/ | ||
@@ -307,4 +314,4 @@ | ||
/** | ||
* @type String | ||
* @enum "uri", "absolute", "relative", "same-document" | ||
* @type string | ||
* @values "uri", "absolute", "relative", "same-document" | ||
*/ | ||
@@ -322,35 +329,13 @@ | ||
/** | ||
* @class | ||
* URI | ||
*/ | ||
SchemeHandler = function () {}; | ||
/** | ||
* @param {URIComponents} components | ||
* @param {Options} options | ||
*/ | ||
SchemeHandler.prototype.parse = function (components, options) {}; | ||
/** | ||
* @param {URIComponents} components | ||
* @param {Options} options | ||
*/ | ||
SchemeHandler.prototype.serialize = function (components, options) {}; | ||
/** | ||
* @namespace | ||
*/ | ||
URI = {}; | ||
/** | ||
* @namespace | ||
*/ | ||
URI.SCHEMES = {}; | ||
/** | ||
* @param {String} uriString | ||
* @param {string} uriString | ||
* @param {Options} [options] | ||
@@ -362,3 +347,3 @@ * @returns {URIComponents} | ||
var matches, | ||
components = new URIComponents(), | ||
components = new Components(), | ||
schemeHandler; | ||
@@ -383,3 +368,5 @@ | ||
} | ||
} else { | ||
} | ||
if (!matches) { | ||
if (!options.tolerant) { | ||
@@ -441,3 +428,3 @@ components.errors.push("URI is not strictly valid."); | ||
//check if a handler for the scheme exists | ||
schemeHandler = URI.SCHEMES[components.scheme || options.scheme]; | ||
schemeHandler = URI.SCHEMES[(components.scheme || options.scheme || "").toLowerCase()]; | ||
if (schemeHandler && schemeHandler.parse) { | ||
@@ -457,3 +444,3 @@ //perform extra parsing | ||
* @param {URIComponents} components | ||
* @returns {String} | ||
* @returns {string|undefined} | ||
*/ | ||
@@ -482,4 +469,4 @@ | ||
/** | ||
* @param {String} input | ||
* @returns {String} | ||
* @param {string} input | ||
* @returns {string} | ||
*/ | ||
@@ -512,4 +499,4 @@ | ||
* @param {URIComponents} components | ||
* @param {Options} options | ||
* @returns {String} | ||
* @param {Options} [options] | ||
* @returns {string} | ||
*/ | ||
@@ -587,3 +574,3 @@ | ||
* @param {Options} [options] | ||
* @param {Boolean} [skipNormalization] | ||
* @param {boolean} [skipNormalization] | ||
* @returns {URIComponents} | ||
@@ -593,3 +580,3 @@ */ | ||
URI.resolveComponents = function (base, relative, options, skipNormalization) { | ||
var target = new URIComponents(); | ||
var target = new Components(); | ||
@@ -655,6 +642,6 @@ if (!skipNormalization) { | ||
/** | ||
* @param {String} baseURI | ||
* @param {String} relativeURI | ||
* @param {string} baseURI | ||
* @param {string} relativeURI | ||
* @param {Options} [options] | ||
* @returns {String} | ||
* @returns {string} | ||
*/ | ||
@@ -667,5 +654,5 @@ | ||
/** | ||
* @param {String|URIComponents} uri | ||
* @param {string|URIComponents} uri | ||
* @param {Options} options | ||
* @returns {String|URIComponents} | ||
* @returns {string|URIComponents} | ||
*/ | ||
@@ -684,4 +671,4 @@ | ||
/** | ||
* @param {String|URIComponents} uriA | ||
* @param {String|URIComponents} uriB | ||
* @param {string|URIComponents} uriA | ||
* @param {string|URIComponents} uriB | ||
* @param {Options} options | ||
@@ -707,4 +694,4 @@ */ | ||
/** | ||
* @param {String} str | ||
* @returns {String} | ||
* @param {string} str | ||
* @returns {string} | ||
*/ | ||
@@ -717,4 +704,4 @@ | ||
/** | ||
* @param {String} str | ||
* @returns {String} | ||
* @param {string} str | ||
* @returns {string} | ||
*/ | ||
@@ -727,7 +714,24 @@ | ||
//export API | ||
exports.Options = Options; | ||
exports.URIComponents = URIComponents; | ||
exports.SchemeHandler = SchemeHandler; | ||
exports.pctEncChar = pctEncChar; | ||
exports.pctDecChars = pctDecChars; | ||
exports.Components = Components; | ||
exports.URI = URI; | ||
//name-safe export API | ||
exports["pctEncChar"] = pctEncChar; | ||
exports["pctDecChars"] = pctDecChars; | ||
exports["Components"] = Components; | ||
exports["URI"] = { | ||
"SCHEMES" : URI.SCHEMES, | ||
"parse" : URI.parse, | ||
"removeDotSegments" : URI.removeDotSegments, | ||
"serialize" : URI.serialize, | ||
"resolveComponents" : URI.resolveComponents, | ||
"resolve" : URI.resolve, | ||
"normalize" : URI.normalize, | ||
"equal" : URI.equal, | ||
"escapeComponent" : URI.escapeComponent, | ||
"unescapeComponent" : URI.unescapeComponent | ||
}; | ||
}()); |
{ | ||
"name" : "JSV", | ||
"version" : "3.5.0", | ||
"version" : "4.0.0", | ||
"description" : "A JavaScript implementation of a extendable, fully compliant JSON Schema validator.", | ||
@@ -5,0 +5,0 @@ "homepage" : "http://github.com/garycourt/JSV", |
@@ -12,3 +12,3 @@ JSV: JSON Schema Validator | ||
* Validates itself, and is bootstrapped from the JSON Schema schemas. | ||
* Includes over 170 unit tests for testing all parts of the specifications. | ||
* Includes over 1100 unit tests for testing all parts of the specifications. | ||
* Works in all ECMAScript 3 environments, including all web browsers and Node.js. | ||
@@ -86,5 +86,2 @@ * Licensed under the FreeBSD License, a very open license. | ||
This environment will also validate links/references to other schemas, and will throw an error if a referenced schema has not already been registered with the environment. | ||
This feature can be disabled with the environment option `validateReferences` is set to `false`. | ||
This is currently the default environment. | ||
@@ -91,0 +88,0 @@ |
{ | ||
"$schema" : "http://json-schema.org/draft-03/hyper-schema#", | ||
"extends" : {"$ref" : "http://json-schema.org/draft-03/schema#"} | ||
"extends" : {"$ref" : "http://json-schema.org/draft-03/schema#"}, | ||
"id" : "http://json-schema.org/draft-03/hyper-schema#", | ||
@@ -5,0 +5,0 @@ |
@@ -381,2 +381,3 @@ var env, | ||
equal(env.validate(7.5, { divisibleBy : 2.5 }).errors.length, 0); | ||
equal(env.validate(9.1, { divisibleBy : 1.3 }).errors.length, 0); | ||
@@ -460,3 +461,3 @@ notEqual(env.validate(0, { divisibleBy : 0 }).errors.length, 0); | ||
//invalid reference | ||
(env.getOption("validateReferences") ? okError : okNoError)(function () { | ||
(env.getOption("enforceReferences") ? okError : okNoError)(function () { | ||
schema = env.createSchema({ "$ref" : "asdf:qwerty" }); //should throw error | ||
@@ -463,0 +464,0 @@ }, "invalid reference"); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
Non-existent author
Supply chain riskThe package was published by an npm account that no longer exists.
Found 1 instance in 1 package
2095980
0
2
15406
171
1