Skip to content

Commit

Permalink
Schemas are properly rebuilt when it's schema has changed. More draft…
Browse files Browse the repository at this point in the history
…-03 changes.
  • Loading branch information
garycourt committed Aug 18, 2011
1 parent 809fbb9 commit d57cbca
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 49 deletions.
36 changes: 9 additions & 27 deletions lib/json-schema-draft-03.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
(function () {
var O = {},
JSV = require('./jsv').JSV,
InitializationError = JSV.InitializationError,
TYPE_VALIDATORS,
ENVIRONMENT,
SCHEMA_00_JSON,
Expand Down Expand Up @@ -1444,33 +1443,24 @@
refLink = instance.getValueOfProperty("$ref"),
idLink = instance.getValueOfProperty("id");

//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);
instance.setReference("describedby", link);
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
}
}
}

//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);
instance.setReference("full", link);
if (link && instance._uri !== link) {
if (instance._env._schemas[link]) {
instance = instance._env._schemas[link];
return instance; //retrieved schemas are guaranteed to be initialized
}
}
}

//extend schema
Expand All @@ -1479,14 +1469,6 @@
extended = JSV.inherits(extension, instance, true);
instance = instance._env.createSchema(extended, instance._schema, instance._uri);
}

//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);
}
}

return instance;
}
Expand Down
57 changes: 35 additions & 22 deletions lib/jsv.js
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,8 @@ var exports = exports || this,
} else if (fr === "slash-delimited") {
this._fd = "/";
}

return this.rebuild(); //this works even when called with "new"
}

JSONSchema.prototype = createObject(JSONInstance.prototype);
Expand Down Expand Up @@ -685,10 +687,7 @@ var exports = exports || this,
if (newSchema) {
if (!newSchema.equals(this._schema)) {
this._schema = newSchema;
if (this._attributes) {
this._attributes = null;
this.getAttributes(); //rebuild all attributes
}
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);
Expand Down Expand Up @@ -867,6 +866,35 @@ var exports = exports || this,
}
}());

/**
* 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.
*
* @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/>
Expand Down Expand Up @@ -977,10 +1005,8 @@ var exports = exports || this,
if (data instanceof JSONInstance && (!uri || data.getURI() === uri)) {
return data;
}
//else
instance = new JSONInstance(this, data, uri);

return instance;

return new JSONInstance(this, data, uri);
};

/**
Expand All @@ -1002,20 +1028,7 @@ var exports = exports || this,
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;

//build & cache the rest of the schema
instance.getAttributes();

return instance;
return new JSONSchema(this, data, uri, schema);
};

/**
Expand Down

0 comments on commit d57cbca

Please sign in to comment.