Hapi/Joi validation of float()

user2870918 picture user2870918 · Oct 11, 2013 · Viewed 11.6k times · Source

I have the following JavaScript code to test out the Hapi/Joi validation functions:

var Joi = require('joi');
var schema = { free: Joi.Types.Number().float() };
var value = { free: 3.3333 };
var err = Joi.validate(value, schema); 

//err is set if value fails to validate against the schema
if (err) throw err;

The validation throws the error:

Error: the value of free must be an integer

I would like to know what I am doing wrong. I am using the current versions of Hapi and Joi.

Answer

mikl picture mikl · May 2, 2014

This is rather out of date now, but in case others happen on this, the syntax now is (accepting all numbers, including floats):

Joi.number();

or, if you want it to be required:

Joi.number().required();

Also, see the docs.