Joi: rounded numbers

Created on 12 Jul 2016  路  2Comments  路  Source: sideway/joi

Context

  • _node version_: 4.4.3
  • _joi version_: 9.0.0
  • _environment_ (node, browser): node
  • _used with_ (hapi, standalone, ...): hapi

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

const schema = Joi.number().precision(4).min(0).max(1)
schema.validate(0.9999)
// { error: null, value: 0.9999 }
schema.validate(0.9999999)
// { error: null, value: 1 }
schema.validate(3)
// { error:
//    { [ValidationError: "value" must be less than or equal to 1]
//      isJoi: true,
//      name: 'ValidationError',
//      details: [ [Object] ],
//      _object: 3,
//      annotate: [Function] },
//   value: 3 }

What did you expect ?

I expected schema.validate(0.9999999) to return an error, not to change my value to 1

non issue

Most helpful comment

Joi.number().precision(4).strict().min(0).max(1) does the job. thank you

All 2 comments

Indeed that's a lack in documentation, you need strict mode (or convert: false) to disable that behavior.

Joi.number().precision(4).strict().min(0).max(1) does the job. thank you

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ashrafkm picture ashrafkm  路  3Comments

kailashyogeshwar85 picture kailashyogeshwar85  路  4Comments

leore picture leore  路  4Comments

normancarcamo picture normancarcamo  路  3Comments

longweiquan picture longweiquan  路  3Comments