Skip to content

Commit

Permalink
fix: validation of fractional numbers when using multipleOf schema
Browse files Browse the repository at this point in the history
  • Loading branch information
mikuso committed Jul 13, 2023
1 parent 563552a commit 55a85b9
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions lib/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,24 @@ function createValidator(subprotocol, json) {
const ajv = new Ajv({strictSchema: false});
addFormats(ajv);
ajv.addSchema(json);

ajv.removeKeyword("multipleOf");
ajv.addKeyword({
keyword: "multipleOf",
type: "number",
compile(schema) {
return data => {
const result = data / schema;
const epsilon = 1e-6; // small value to account for floating point precision errors
return Math.abs(Math.round(result) - result) < epsilon;
};
},
errors: false,
metaSchema: {
type: "number",
},
});

return new Validator(subprotocol, ajv);
}

Expand Down

0 comments on commit 55a85b9

Please sign in to comment.