简体   繁体   English

Hapi / Joi验证float()

[英]Hapi/Joi validation of float()

I have the following JavaScript code to test out the Hapi/Joi validation functions: 我有以下JavaScript代码来测试Hapi / Joi验证函数:

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. 我正在使用当前版本的Hapi和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 . 另外,请参阅文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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