Skip to content

Remove Number.float() #112

@fluidsonic

Description

@fluidsonic

I suggest to remove Number.float() because it's not just useless (all numbers are doubles in JavaScript and Joi filters NaN in any case) and is also bugged.

Currently .float() does not allow values which have no fractional part (e.g. 3.0, 6.0), i.e. integers. But these value are valid doubles.
Fixing that bug essentially makes float() redundant.

Also .float() uses .integer() internally which also breaks it for floats.

Test 1 (Integer)

var Joi = require('./index');
var T = Joi.types;

var input = { child: { amount: 2 } };
var schema = { child: T.Object({ amount: T.Number().float() }) };

console.log(Joi.validate(input, schema));

Output

{ _errors: 
       [ { message: 'the value of amount must be a float or double',
           path: 'child.amount' } ],
      _object: { child: { amount: 2 } },
      message: 'the value of amount must be a float or double' }

Test 2 (Double)

var Joi = require('./index');
var T = Joi.types;

var input = { child: { amount: 2.5 } };
var schema = { child: T.Object({ amount: T.Number().float() }) };

console.log(Joi.validate(input, schema));

Output

{ _errors: 
       [ { message: 'the value of amount must be an integer',
           path: 'child.amount' } ],
      _object: { child: { amount: 2.5 } },
      message: 'the value of amount must be an integer' }

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