Joi: Joi.number() throws error for "0.00000095" string.

Created on 10 Jun 2019  路  3Comments  路  Source: sideway/joi

Context

  • node version: v10.15.0
  • joi version: 14.3.1
  • environment (node, browser): node
  • used with (hapi, standalone, ...): standalone
  • any other relevant information:

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

const schema = Joi.number();
const result = schema.validate('0.00000095');
console.log(result);

Which result you had ?

{ error:
   { ValidationError: "value" must be a safe number
       at Object.exports.process (/Users/aleksanderbarszczewski/Desktop/projects/sm3/node_modules/joi/lib/errors.js:203:19)
       at internals.Number._validateWithOptions (/Users/aleksanderbarszczewski/Desktop/projects/sm3/node_modules/joi/lib/types/any/index.js:764:31)
       at internals.Number.validate (/Users/aleksanderbarszczewski/Desktop/projects/sm3/node_modules/joi/lib/types/any/index.js:798:21)
       at Object.<anonymous> (/Users/aleksanderbarszczewski/Desktop/projects/sm3/src/test.ts:40:23)
       at Module._compile (internal/modules/cjs/loader.js:689:30)
       at Module.m._compile (/Users/aleksanderbarszczewski/Desktop/projects/sm3/node_modules/ts-node/src/index.ts:439:23)
       at Module._extensions..js (internal/modules/cjs/loader.js:700:10)
       at Object.require.extensions.(anonymous function) [as .ts] (/Users/aleksanderbarszczewski/Desktop/projects/sm3/node_modules/ts-node/src/index.ts:442:12)
       at Module.load (internal/modules/cjs/loader.js:599:32)
       at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
       at Function.Module._load (internal/modules/cjs/loader.js:530:3)
       at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
       at startup (internal/bootstrap/node.js:283:19)
       at bootstrapNodeJSCore (internal/bootstrap/node.js:743:3)
     isJoi: true,
     name: 'ValidationError',
     details: [ [Object] ],
     _object: '0.00000095',
     annotate: [Function] },
  value: 9.5e-7,
  then: [Function: then],
  catch: [Function: catch] }

What did you expect ?

It should not throw error. I believe this is because of this line https://github.com/hapijs/joi/blob/master/lib/types/number.js#L48 and value stringified to '9.5e-7' which does not match internals.normalizeDecimal(value).

bug

Most helpful comment

Thanks!

All 3 comments

Hi, I would like to fix this issue, Can I?

@raghavmac sure, but you only have a day or two before I'll close it myself.

Thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

neroaugustus1 picture neroaugustus1  路  4Comments

mohamadresaaa picture mohamadresaaa  路  3Comments

normancarcamo picture normancarcamo  路  3Comments

Taxi4you picture Taxi4you  路  3Comments

sergibondarenko picture sergibondarenko  路  3Comments