Skip to content

Commit 55a85b9

Browse files
committed
fix: validation of fractional numbers when using multipleOf schema
1 parent 563552a commit 55a85b9

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

lib/validator.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,24 @@ function createValidator(subprotocol, json) {
5858
const ajv = new Ajv({strictSchema: false});
5959
addFormats(ajv);
6060
ajv.addSchema(json);
61+
62+
ajv.removeKeyword("multipleOf");
63+
ajv.addKeyword({
64+
keyword: "multipleOf",
65+
type: "number",
66+
compile(schema) {
67+
return data => {
68+
const result = data / schema;
69+
const epsilon = 1e-6; // small value to account for floating point precision errors
70+
return Math.abs(Math.round(result) - result) < epsilon;
71+
};
72+
},
73+
errors: false,
74+
metaSchema: {
75+
type: "number",
76+
},
77+
});
78+
6179
return new Validator(subprotocol, ajv);
6280
}
6381

0 commit comments

Comments
 (0)