Skip to content

Commit

Permalink
Merge pull request #4 from rotu/substantial-jellyfish
Browse files Browse the repository at this point in the history
Use numeric test for multipleOf
  • Loading branch information
nuxodin authored Apr 6, 2024
2 parents 00052d1 + 940ecbf commit 8b22755
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
5 changes: 2 additions & 3 deletions schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -461,9 +461,8 @@ const vocabulary = {
},
multipleOf: {
valid(mOf, value) {
if ( Number.isInteger(value) && Number.isInteger(1 / mOf) ) return true;
if ( Number.isInteger( +(value / mOf).toPrecision(15) ) ) return true;
//if (Number.isInteger(value / mOf)) return true; // old zzz
const q = value / mOf
return Number.isFinite(q) && q * mOf === Math.round(q) * mOf
},
affects:'number'
},
Expand Down
5 changes: 5 additions & 0 deletions tests/tests.html
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@
chai.expect( schema.validate(-8599.3) ).to.be.equal(true);
});

it('1+EPSILON not a multiple of 0.5', () => {
const schema = new Schema({ 'multipleOf': 0.5 });
chai.expect( schema.validate(1 + Number.EPSILON) ).to.be.equal(false);
});

it('is not integer', () => {
const schema = new Schema({ type: 'integer' });
chai.expect( schema.validate(3.1) ).to.be.equal(false);
Expand Down

0 comments on commit 8b22755

Please sign in to comment.