Skip to content

Commit

Permalink
Fixed bug with extended schemas that contain a "pattern" attribute. (…
Browse files Browse the repository at this point in the history
…Identified by DrDyne)
  • Loading branch information
garycourt committed May 9, 2012
1 parent 793c171 commit 7054240
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 37 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 4.0.1 (2012/05/09)

* Fixed bug with extended schemas that contain a "pattern" attribute. (Identified by DrDyne)

## 4.0 (2011/08/23)

* Added referencing to JSONSchema, allows for soft-linking to other schemas.
Expand Down
42 changes: 41 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,50 @@ JSV provides an API for extending available schemas, adding new attributes and v
In fact, in the [eat-your-own-dog-food](http://en.wikipedia.org/wiki/Eating_your_own_dog_food) approach, all the default JSON Schema environments available are implemented using this API.
Details and instruction on this feature will be provided at a later date.

## Installation

The preferred method of installation is to download the latest copy from GitHub:

git clone git://github.com/garycourt/JSV.git

If you are using JSV within Node.js, you can quickly install it using:

npm install JSV

Then you can reference it within your application using:

var JSV = require("JSV").JSV;

## Unit Tests

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

Currently, the unit tests can not be run in Node.js.

## License

JSV is licensed under the FreeBSD License.
Copyright 2010 Gary Court. All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are
permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of
conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, this list
of conditions and the following disclaimer in the documentation and/or other materials
provided with the distribution.

THIS SOFTWARE IS PROVIDED BY GARY COURT ``AS IS'' AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARY COURT OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

The views and conclusions contained in the software and documentation are those of the
authors and should not be interpreted as representing official policies, either expressed
or implied, of Gary Court or the JSON Schema specification.
14 changes: 4 additions & 10 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:gary.court@gmail.com">Gary Court</a>
* @version 1.6
* @version 1.7
* @see http://github.com/garycourt/JSV
*/

Expand Down Expand Up @@ -451,21 +451,15 @@

"parser" : function (instance, self) {
if (instance.getType() === "string") {
try {
return new RegExp(instance.getValue());
} catch (e) {
return e;
}
return instance.getValue();
}
},

"validator" : function (instance, schema, self, report, parent, parentSchema, name) {
var pattern;
try {
pattern = schema.getAttribute("pattern");
if (pattern instanceof Error) {
report.addError(instance, schema, "pattern", "Invalid pattern", pattern);
} else if (instance.getType() === "string" && pattern && !pattern.test(instance.getValue())) {
pattern = new RegExp(schema.getAttribute("pattern"));
if (instance.getType() === "string" && pattern && !pattern.test(instance.getValue())) {
report.addError(instance, schema, "pattern", "String does not match pattern", pattern.toString());
}
} catch (e) {
Expand Down
14 changes: 4 additions & 10 deletions lib/json-schema-draft-02.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*
* @fileOverview Implementation of the second revision of the JSON Schema specification draft.
* @author <a href="mailto:gary.court@gmail.com">Gary Court</a>
* @version 1.6
* @version 1.7
* @see http://github.com/garycourt/JSV
*/

Expand Down Expand Up @@ -475,21 +475,15 @@

"parser" : function (instance, self) {
if (instance.getType() === "string") {
try {
return new RegExp(instance.getValue());
} catch (e) {
return e;
}
return instance.getValue();
}
},

"validator" : function (instance, schema, self, report, parent, parentSchema, name) {
var pattern;
try {
pattern = schema.getAttribute("pattern");
if (pattern instanceof Error) {
report.addError(instance, schema, "pattern", "Invalid pattern", pattern);
} else if (instance.getType() === "string" && pattern && !pattern.test(instance.getValue())) {
pattern = new RegExp(schema.getAttribute("pattern"));
if (instance.getType() === "string" && pattern && !pattern.test(instance.getValue())) {
report.addError(instance, schema, "pattern", "String does not match pattern", pattern.toString());
}
} catch (e) {
Expand Down
14 changes: 4 additions & 10 deletions lib/json-schema-draft-03.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*
* @fileOverview Implementation of the third revision of the JSON Schema specification draft.
* @author <a href="mailto:gary.court@gmail.com">Gary Court</a>
* @version 1.4
* @version 1.5
* @see http://github.com/garycourt/JSV
*/

Expand Down Expand Up @@ -477,21 +477,15 @@

"parser" : function (instance, self) {
if (instance.getType() === "string") {
try {
return new RegExp(instance.getValue());
} catch (e) {
return e;
}
return instance.getValue();
}
},

"validator" : function (instance, schema, self, report, parent, parentSchema, name) {
var pattern;
try {
pattern = schema.getAttribute("pattern");
if (pattern instanceof Error) {
report.addError(schema, self, "pattern", "Invalid pattern", schema.getValueOfProperty("pattern"));
} else if (instance.getType() === "string" && pattern && !pattern.test(instance.getValue())) {
pattern = new RegExp(schema.getAttribute("pattern"));
if (instance.getType() === "string" && pattern && !pattern.test(instance.getValue())) {
report.addError(instance, schema, "pattern", "String does not match pattern", pattern.toString());
}
} catch (e) {
Expand Down
2 changes: 1 addition & 1 deletion lib/jsv.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*
* @fileOverview A JavaScript implementation of a extendable, fully compliant JSON Schema validator.
* @author <a href="mailto:gary.court@gmail.com">Gary Court</a>
* @version 4.0
* @version 4.0.1
* @see http://github.com/garycourt/JSV
*/

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" : "4.0.0",
"version" : "4.0.1",
"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
33 changes: 31 additions & 2 deletions tests/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,8 @@ test("Register Schemas", function () {
});

test("Complex Examples", function () {
env.createSchema({
//example 1
var schema = env.createSchema({
"id":"Common#",
"type":"object",
"properties":{
Expand Down Expand Up @@ -417,7 +418,35 @@ test("Complex Examples", function () {
}
);

notEqual(report.errors.length, 0);
notEqual(report.errors.length, 0, "example 1");

//example 2
schema = env.createSchema({
"extends": {
"type": "object",
"properties": {
"id": {
"type": "string",
"minLength": 1,
"pattern": "^\\S.+\\S$"
}
}
},
"properties": {
"role": {
"extends": {
"type": "string",
"minLength": 1,
"pattern": "^\\S.+\\S$"
},
"description": "some description"
}
}
});

report = env.validate({ "id" : "some id", "role" : "yunowork?"}, schema);

equal(report.errors.length, 0, "example 2");
});

}
32 changes: 30 additions & 2 deletions tests/tests3.js
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ test("Register Schemas", function () {

test("Complex Examples", function () {
//example 1
env.createSchema({
var schema = env.createSchema({
"id":"Common#",
"type":"object",
"properties":{
Expand Down Expand Up @@ -533,7 +533,35 @@ test("Complex Examples", function () {
}
);

notEqual(report.errors.length, 0);
notEqual(report.errors.length, 0, "example 1");

//example 2
schema = env.createSchema({
"extends": {
"type": "object",
"properties": {
"id": {
"type": "string",
"minLength": 1,
"pattern": "^\\S.+\\S$"
}
}
},
"properties": {
"role": {
"extends": {
"type": "string",
"minLength": 1,
"pattern": "^\\S.+\\S$"
},
"description": "some description"
}
}
});

report = env.validate({ "id" : "some id", "role" : "yunowork?"}, schema);

equal(report.errors.length, 0, "example 2");
});

}

0 comments on commit 7054240

Please sign in to comment.