简体   繁体   中英

Hapi/Joi validation of float()

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM