Skip to content

Commit

Permalink
Fixed floating point errors with "divisibleBy" attribute.
Browse files Browse the repository at this point in the history
  • Loading branch information
garycourt committed Aug 23, 2011
1 parent 0859632 commit bcdacab
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
## 4.0 (2011/08/22)

* 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.
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ JSV is a JavaScript implementation of a extendable, fully compliant JSON Schema
* Provides an intuitive API for creating new validating schema attributes, or whole new custom schema schemas.
* Supports `self`, `full` and `describedby` hyper links.
* 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.
* Licensed under the FreeBSD License, a very open license.

Expand Down
11 changes: 8 additions & 3 deletions lib/json-schema-draft-03.js
Original file line number Diff line number Diff line change
Expand Up @@ -1080,13 +1080,18 @@
},

"validator" : function (instance, schema, self, report, parent, parentSchema, name) {
var divisor;
var divisor, value, digits;
if (instance.getType() === "number") {
divisor = schema.getAttribute("divisibleBy");
if (divisor === 0) {
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);
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions tests/tests3.js
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@ test("DivisibleBy Validation", function () {
equal(env.validate(0, { divisibleBy : 2.5 }).errors.length, 0);
equal(env.validate(5, { divisibleBy : 2.5 }).errors.length, 0);
equal(env.validate(7.5, { divisibleBy : 2.5 }).errors.length, 0);
equal(env.validate(9.1, { divisibleBy : 1.3 }).errors.length, 0);

notEqual(env.validate(0, { divisibleBy : 0 }).errors.length, 0);
notEqual(env.validate(7, { divisibleBy : 5 }).errors.length, 0);
Expand Down

0 comments on commit bcdacab

Please sign in to comment.