Skip to content

Commit

Permalink
Fixed bug with "enum" and undefined instances.
Browse files Browse the repository at this point in the history
  • Loading branch information
garycourt committed Jan 10, 2011
1 parent 4eae4c6 commit 0867977
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 21 deletions.
17 changes: 10 additions & 7 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.4
* @version 1.5
* @see http://github.com/garycourt/JSV
*/

Expand Down Expand Up @@ -533,14 +533,17 @@
},

"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) {
if (instance.equals(enums[x])) {
return true;
var enums, x, xl;
if (instance.getType() !== "undefined") {
enums = schema.getAttribute("enum")
if (enums) {
for (x = 0, xl = enums.length; x < xl; ++x) {
if (instance.equals(enums[x])) {
return true;
}
}
report.addError(instance, schema, "enum", "Instance is not one of the possible values", enums);
}
report.addError(instance, schema, "enum", "Instance is not one of the possible values", enums);
}
}
},
Expand Down
17 changes: 10 additions & 7 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.4
* @version 1.5
* @see http://github.com/garycourt/JSV
*/

Expand Down Expand Up @@ -557,14 +557,17 @@
},

"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) {
if (instance.equals(enums[x])) {
return true;
var enums, x, xl;
if (instance.getType() !== "undefined") {
enums = schema.getAttribute("enum")
if (enums) {
for (x = 0, xl = enums.length; x < xl; ++x) {
if (instance.equals(enums[x])) {
return true;
}
}
report.addError(instance, schema, "enum", "Instance is not one of the possible values", enums);
}
report.addError(instance, schema, "enum", "Instance is not one of the possible values", enums);
}
}
},
Expand Down
17 changes: 10 additions & 7 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.0
* @version 1.1
* @see http://github.com/garycourt/JSV
*/

Expand Down Expand Up @@ -552,14 +552,17 @@
},

"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) {
if (instance.equals(enums[x])) {
return true;
var enums, x, xl;
if (instance.getType() !== "undefined") {
enums = schema.getAttribute("enum")
if (enums) {
for (x = 0, xl = enums.length; x < xl; ++x) {
if (instance.equals(enums[x])) {
return true;
}
}
report.addError(instance, schema, "enum", "Instance is not one of the possible values", enums);
}
report.addError(instance, schema, "enum", "Instance is not one of the possible values", enums);
}
}
},
Expand Down
2 changes: 2 additions & 0 deletions tests/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,10 +243,12 @@ test("Enum Validation", function () {
equal(env.validate(true, { 'enum' : [false, true] }).errors.length, 0);
equal(env.validate(2, { 'enum' : [1, 2, 3] }).errors.length, 0);
equal(env.validate('a', { 'enum' : ['a'] }).errors.length, 0);
equal(env.validate({}, { 'properties' : { 'a' : { 'enum' : ['a'], 'optional' : true } } }).errors.length, 0);

notEqual(env.validate(true, { 'enum' : ['false', 'true'] }).errors.length, 0);
notEqual(env.validate(4, { 'enum' : [1, 2, 3, '4'] }).errors.length, 0);
notEqual(env.validate('', { 'enum' : [] }).errors.length, 0);
notEqual(env.validate({}, { 'properties' : { 'a' : { 'enum' : ['a'] } } }).errors.length, 0);
});

test("Format Validation", function () {
Expand Down
2 changes: 2 additions & 0 deletions tests/tests3.js
Original file line number Diff line number Diff line change
Expand Up @@ -346,10 +346,12 @@ test("Enum Validation", function () {
equal(env.validate(true, { 'enum' : [false, true] }).errors.length, 0);
equal(env.validate(2, { 'enum' : [1, 2, 3] }).errors.length, 0);
equal(env.validate('a', { 'enum' : ['a'] }).errors.length, 0);
equal(env.validate({}, { 'properties' : { 'a' : { 'enum' : ['a'], 'optional' : true, 'required' : false } } }).errors.length, 0);

notEqual(env.validate(true, { 'enum' : ['false', 'true'] }).errors.length, 0);
notEqual(env.validate(4, { 'enum' : [1, 2, 3, '4'] }).errors.length, 0);
notEqual(env.validate('', { 'enum' : [] }).errors.length, 0);
notEqual(env.validate({}, { 'properties' : { 'a' : { 'enum' : ['a'], 'optional' : false, 'required' : true } } }).errors.length, 0);
});

test("Format Validation", function () {
Expand Down

0 comments on commit 0867977

Please sign in to comment.