Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some exponential notation strings are validated as unsafe numbers #2672

Closed
vvandens opened this issue Sep 9, 2021 · 2 comments · Fixed by #2867
Closed

Some exponential notation strings are validated as unsafe numbers #2672

vvandens opened this issue Sep 9, 2021 · 2 comments · Fixed by #2867
Assignees
Labels
bug Bug or defect
Milestone

Comments

@vvandens
Copy link

vvandens commented Sep 9, 2021

Support plan

  • is this issue currently blocking your project? (yes/no): no
  • is this issue affecting a production system? (yes/no): yes

Context

  • node version: v12.22.6
  • module version with issue: 17.4.2
  • last module version without issue: N/A
  • environment (e.g. node, browser, native): node
  • used with (e.g. hapi application, another framework, standalone, ...): standalone
  • any other relevant information:

What are you trying to achieve or the steps to reproduce?

const schema = Joi.number();
const validation = schema.validate('9.4e-1');
console.log(`value: ${validation.value}, message: ${validation.error}`);

What was the result you got?

value: 0.94, message: ValidationError: "value" must be a safe number

What result did you expect?

value: 0.94, message: undefined

I beleive that the problem lies in number.js#42 when the division hits a double precision value problem, i.e. :
0.94 / Math.pow(10, -1) results in 9.399999999999999

@martin-petersen
Copy link
Contributor

I did a PR which once is approved gives the possibility to specify a precision number which will resolve the double precision number, but there is another way to resolve your problem and is by using:
const schema = Joi.number().unsafe();

@Marsup
Copy link
Collaborator

Marsup commented Sep 29, 2022

For reference, this is likely the same issue as in #2542.

Marsup added a commit that referenced this issue Oct 22, 2022
@Marsup Marsup self-assigned this Oct 22, 2022
@Marsup Marsup added the bug Bug or defect label Oct 22, 2022
@Marsup Marsup added this to the 17.7.0 milestone Oct 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Bug or defect
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants