Skip to content

Joi.number() throws error for "0.00000095" string. #1876

@alekbarszczewski

Description

@alekbarszczewski

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).

Metadata

Metadata

Assignees

Labels

bugBug or defect

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions