Skip to content

Commit

Permalink
Draft-03 schema no longer depends on hyper-schema.
Browse files Browse the repository at this point in the history
Added latest JSON Schema links.
  • Loading branch information
garycourt committed Nov 26, 2010
1 parent e65ba86 commit 9be261a
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 19 deletions.
21 changes: 18 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,30 @@ There is no one way to validate JSON, just like there is no one way to validate

When creating an environment, you can optionally specify how you want that environment to behave. For example, this allows you to specify which version of the JSON Schema you would like the environment to behave like. JSV already provides the following environments:

* `json-schema-draft-03`

A complete implementation of the [third revision](http://tools.ietf.org/html/draft-zyp-json-schema-03) of the JSON Schema specification. This is the same as the second revision, except:

* "optional" has been replaced by "required"
* "requires" has been replaced by "dependencies"
* "minimumCanEqual"/"maximumCanEqual" has been replaced by "exclusiveMinimum"/"exclusiveMaximum"
* "self"/"full"/"describedby" links have been moved to the core schema, and are provided by "id"/"$ref"/"$schema"
* Adds the attributes "patternSchema" and "additionalItems"
* Deprecates the attributes "root" and "alternate"
* Schemas are now versioned under the URIs "http://json-schema.org/draft-XX/", where XX is the draft number

In addition to this, all schemas from the previous versions of the JSON Schema draft are included in this environment, and are backwards compatible (where possible) with it's previous version.
This backwards compatibility can be disabled with the new environment option "strict" is set to `true`.

This is currently the default environment.

* `json-schema-draft-02`

A complete implementation of the [second revision](http://tools.ietf.org/html/draft-zyp-json-schema-02) of the JSON Schema specification. This is the same as the first revision, except adds:

* "targetSchema" attribute
* slash-delimited fragment identifiers, which is now the default behavior

This is currently the default environment.

* `json-schema-draft-01`

A complete implementation of the [first revision](http://tools.ietf.org/html/draft-zyp-json-schema-01) of the JSON Schema specification, which is exactly the same as the [original draft](http://tools.ietf.org/html/draft-zyp-json-schema-00).
Expand Down Expand Up @@ -149,7 +164,7 @@ Details and instruction on this feature will be provided at a later date.

## Unit Tests

Open `tests/index.html` in your web browser to run the unit tests.
Open `tests/index.html` and `tests/index3.html` in your web browser to run the unit tests.

## License

Expand Down
3 changes: 2 additions & 1 deletion lib/environments.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
require("./json-schema-draft-01");
require("./json-schema-draft-02");
require("./json-schema-draft-02");
require("./json-schema-draft-03");
49 changes: 36 additions & 13 deletions lib/json-schema-draft-03.js
Original file line number Diff line number Diff line change
Expand Up @@ -789,7 +789,7 @@

//if instance has a URI link to itself, update it's own URI
link = instance._schema.getLink("self", instance);
if (link) {
if (JSV.typeOf(link) === "string") {
instance._uri = link;
}

Expand Down Expand Up @@ -1019,9 +1019,6 @@
// draft-02
//

ENVIRONMENT.setOption("defaultFragmentDelimiter", "/");
ENVIRONMENT.setOption("defaultSchemaURI", "http://json-schema.org/draft-02/schema#"); //update later

SCHEMA_02_JSON = {
"$schema" : "http://json-schema.org/draft-02/hyper-schema#",
"id" : "http://json-schema.org/draft-02/schema#",
Expand Down Expand Up @@ -1094,6 +1091,9 @@
}
});

ENVIRONMENT.setOption("defaultFragmentDelimiter", "/");
ENVIRONMENT.setOption("defaultSchemaURI", "http://json-schema.org/draft-02/schema#"); //update later

SCHEMA_02 = ENVIRONMENT.createSchema(JSV.inherits(SCHEMA_01, ENVIRONMENT.createSchema(SCHEMA_02_JSON)), true, "http://json-schema.org/draft-02/schema#");
HYPERSCHEMA_02 = ENVIRONMENT.createSchema(JSV.inherits(HYPERSCHEMA_01, ENVIRONMENT.createSchema(HYPERSCHEMA_02_JSON)), true, "http://json-schema.org/draft-02/hyper-schema#");

Expand Down Expand Up @@ -1157,10 +1157,8 @@
return matchedProperties;
}

ENVIRONMENT.setOption("defaultSchemaURI", "http://json-schema.org/draft-03/schema#"); //update later

SCHEMA_03_JSON = {
"$schema" : "http://json-schema.org/draft-03/hyper-schema#", //TODO: Change to /schema
"$schema" : "http://json-schema.org/draft-03/schema#",
"id" : "http://json-schema.org/draft-03/schema#",

"properties" : {
Expand Down Expand Up @@ -1427,10 +1425,29 @@
"dependencies" : {
"exclusiveMinimum" : "minimum",
"exclusiveMaximum" : "maximum"
}
},

//non-standard, but required to get links working
"links" : [
{
"href" : "{$ref}",
"rel" : "full"
},

{
"href" : "{$schema}",
"rel" : "describedby"
},

{
"href" : "{id}",
"rel" : "self"
}
]
};

HYPERSCHEMA_03_JSON = JSV.inherits(SCHEMA_03_JSON, {
"$schema" : "http://json-schema.org/draft-03/hyper-schema#",
"id" : "http://json-schema.org/draft-03/hyper-schema#",

"properties" : {
Expand All @@ -1448,6 +1465,8 @@
}
});

ENVIRONMENT.setOption("defaultSchemaURI", "http://json-schema.org/draft-03/schema#"); //update later

SCHEMA_03 = ENVIRONMENT.createSchema(JSV.inherits(SCHEMA_02, ENVIRONMENT.createSchema(SCHEMA_03_JSON)), true, "http://json-schema.org/draft-03/schema#");
HYPERSCHEMA_03 = ENVIRONMENT.createSchema(JSV.inherits(HYPERSCHEMA_02, ENVIRONMENT.createSchema(HYPERSCHEMA_03_JSON)), true, "http://json-schema.org/draft-03/hyper-schema#");

Expand Down Expand Up @@ -1475,15 +1494,19 @@
}
})), HYPERSCHEMA_03, "http://json-schema.org/draft-03/links#");

//We need to reinitialize these 3 schemas as they all reference each other
SCHEMA_03 = ENVIRONMENT.createSchema(SCHEMA_03.getValue(), HYPERSCHEMA_03, "http://json-schema.org/draft-03/schema#");
HYPERSCHEMA_03 = ENVIRONMENT.createSchema(HYPERSCHEMA_03.getValue(), HYPERSCHEMA_03, "http://json-schema.org/draft-03/hyper-schema#");
LINKS_03 = ENVIRONMENT.createSchema(LINKS_03.getValue(), HYPERSCHEMA_03, "http://json-schema.org/draft-03/links#");

ENVIRONMENT.setOption("latestJSONSchemaSchemaURI", "http://json-schema.org/draft-03/schema#");
ENVIRONMENT.setOption("latestJSONSchemaHyperSchemaURI", "http://json-schema.org/draft-03/hyper-schema#");
ENVIRONMENT.setOption("latestJSONSchemaLinksURI", "http://json-schema.org/draft-03/links#");

//
//Latest JSON Schema
//

//Hack, but WAY faster then instantiating a new schema
ENVIRONMENT._schemas["http://json-schema.org/schema#"] = SCHEMA_03;
ENVIRONMENT._schemas["http://json-schema.org/hyper-schema#"] = HYPERSCHEMA_03;
ENVIRONMENT._schemas["http://json-schema.org/links#"] = LINKS_03;

//
//register environment
//
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name" : "JSV",
"version" : "3.1",
"version" : "3.3",
"description" : "A JavaScript implementation of a extendable, fully compliant JSON Schema validator.",
"homepage" : "http://github.com/garycourt/JSV",
"author" : "Gary Court <gary.court@gmail.com>",
Expand Down
2 changes: 1 addition & 1 deletion tests/tests3.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ test("MinimumCanEqual/MaximumCanEqual Validation", function () {
});

if (curDraftId > 2) {
test("ExclusiveMinimum/exclusiveMaximum Validation", function () {
test("ExclusiveMinimum/ExclusiveMaximum Validation", function () {
//true
notEqual(env.validate(0, { exclusiveMinimum : false, exclusiveMaximum : false }).errors.length, 0); //illegal
equal(env.validate(1, { minimum : 1, maximum : 10, exclusiveMinimum : false, exclusiveMaximum : false }).errors.length, 0);
Expand Down

0 comments on commit 9be261a

Please sign in to comment.