fix: number.multiple() with decimal ref base wrongly rejects valid values - #3128
Open
chatman-media wants to merge 1 commit into
Open
fix: number.multiple() with decimal ref base wrongly rejects valid values#3128chatman-media wants to merge 1 commit into
chatman-media wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When the base of
.multiple()is a ref instead of a literal,baseDecimalPlaceis precomputed asnullat schema-build time (only literal numbers get their decimal places counted upfront). At validate time thatnullgets compared against the value's decimal place count with>, and JS happily coercesnullto0there, so any value with a fractional part gets rejected outright whenever the resolved ref turns out to be a decimal — even for legit multiples like base 0.5 / value 1.5.You can see the asymmetry pretty clearly:
Joi.number().multiple(0.5).validate(1.5)passes, butJoi.number().multiple(Joi.ref('base')).validate({base: 0.5, value: 1.5})(wrapped in an object schema) fails with "must be a multiple of ref:base". Fix just recomputes decimal place/factor from the resolved value when the build-time one is null. Added a test alongside the existing ref-based multiple tests, which previously only covered integer bases.