Skip to content

Commit

Permalink
option multipleOfPrecision, closes #84
Browse files Browse the repository at this point in the history
  • Loading branch information
epoberezkin committed Jan 2, 2016
1 parent bd35ea9 commit 5f2cc30
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 3 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,7 @@ Defaults:
validateSchema: true,
inlineRefs: true,
loopRequired: Infinity,
multipleOfPrecision: false,
missingRefs: true,
loadSchema: function(uri, cb) { /* ... */ cb(err, schema); },
uniqueItems: true,
Expand All @@ -599,6 +600,7 @@ Defaults:
- _validateSchema_: validate added/compiled schemas against meta-schema (true by default). `$schema` property in the schema can either be http://json-schema.org/schema or http://json-schema.org/draft-04/schema or absent (draft-4 meta-schema will be used) or can be a reference to the schema previously added with `addMetaSchema` method. If the validation fails, the exception is thrown. Pass "log" in this option to log error instead of throwing exception. Pass `false` to skip schema validation.
- _inlineRefs_: by default the referenced schemas that don't have refs in them are inlined, regardless of their size - that substantially improves performance at the cost of the bigger size of compiled schema functions. Pass `false` to not inline referenced schemas (they will be compiled as separate functions). Pass integer number to limit the maximum number of keywords of the schema that will be inlined.
- _loopRequired_: by default `required` keyword is compiled into a single expression (or a sequence of statements in `allErrors` mode). In case of a very large number of properties in this keyword it may result in a very big validation function. Pass integer to set the number of properties above which `required` keyword will be validated in a loop - smaller validation function size but also worse performance.
- _multipleOfPrecision_: by default `multipleOf` keyword is validated by comparing the result of division with parseInt() of that result. It works for dividers that are bigger than 1. For small dividers such as 0.01 the result of the division is usually not integer (even when it should be integer, see issue #84). If you need to use fractional dividers set this option to some positive integer N to have `multipleOf` validated using this formula: `Math.abs(Math.round(division) - division) < 1e-N` (it is slower but allows for float arithmetics deviations).
- _missingRefs_: by default if the reference cannot be resolved during compilation the exception is thrown. The thrown error has properties `missingRef` (with hash fragment) and `missingSchema` (without it). Both properties are resolved relative to the current base id (usually schema id, unless it was substituted). Pass 'ignore' to log error during compilation and pass validation. Pass 'fail' to log error and successfully compile schema but fail validation if this rule is checked.
- _loadSchema_: asynchronous function that will be used to load remote schemas when the method `compileAsync` is used and some reference is missing (option `missingRefs` should not be 'fail' or 'ignore'). This function should accept 2 parameters: remote schema uri and node-style callback. See example in Asynchronous compilation.
- _uniqueItems_: validate `uniqueItems` keyword (true by default).
Expand Down
8 changes: 7 additions & 1 deletion lib/dot/multipleOf.jst
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@ if ({{?$isData}}
{{=$schemaValue}} !== undefined && (
typeof {{=$schemaValue}} != 'number' ||
{{?}}
(division{{=$lvl}} = {{=$data}} / {{=$schemaValue}}) !== parseInt(division{{=$lvl}})
(division{{=$lvl}} = {{=$data}} / {{=$schemaValue}},
{{? it.opts.multipleOfPrecision }}
Math.abs(Math.round(division{{=$lvl}}) - division{{=$lvl}}) > 1e-{{=it.opts.multipleOfPrecision}}
{{??}}
division{{=$lvl}} !== parseInt(division{{=$lvl}})
{{?}}
)
{{?$isData}} ) {{?}} ) {
{{# def.error:'multipleOf' }}
} {{? $breakOnError }} else { {{?}}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ajv",
"version": "3.0.4",
"version": "3.1.0",
"description": "Another JSON Schema Validator",
"main": "lib/ajv.js",
"files": [
Expand Down
23 changes: 23 additions & 0 deletions spec/options.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,4 +308,27 @@ describe('Ajv Options', function () {
}
});
});


describe('multipleOfPrecision', function() {
it('should allow for some deviation from 0 when validating multipleOf with value < 1', function() {
test(Ajv({ multipleOfPrecision: 7 }));
test(Ajv({ multipleOfPrecision: 7, allErrors: true }));

function test(ajv) {
var schema = { multipleOf: 0.01 };
var validate = ajv.compile(schema);

validate(4.18) .should.equal(true);
validate(4.181) .should.equal(false);

var schema = { multipleOf: 0.0000001 };
var validate = ajv.compile(schema);

validate(53.198098) .should.equal(true);
validate(53.1980981) .should.equal(true);
validate(53.19809811) .should.equal(false);
}
});
});
});
2 changes: 1 addition & 1 deletion spec/tests/issues/94_dependencies_fail.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,4 @@
}
]
}
]
]

0 comments on commit 5f2cc30

Please sign in to comment.