带有空值、未定义值、空值和默认值的 npm joi [英] npm joi with null, undefined, empty values and a default value
问题描述
我正在使用旧版本的 npm 模块 joi => 10.2.2,我想弄清楚如何构建架构,以便使用默认值允许空、空、未定义的值.
I am using older version of npm module joi => 10.2.2 and i am trying to figure out how can i build the schema so empty, null, undefined values are allowed with a default value.
这有效https://github.com/hapijs/joi/issues/516一个>
var schema = joi.object().keys({a:[joi.string().optional(), joi.allow(null)]})
但我不知道如何指定默认值.
but i don't know how to specify a default value with this.
我正在寻找的是这个 https://github.com/hapijs/joi/问题/1066(版本 v10.5.2)但使用旧版本 joi 的语法.
What i am looking for is this https://github.com/hapijs/joi/issues/1066 (version v10.5.2) but with the syntax of older version of joi.
推荐答案
empty() 在 joi 10.2.2 中可用.您引用的问题只是文档更改.
empty() is available in joi 10.2.2. The issue you referenced is just a documentation change.
const joi = require('joi');
const schema = joi.object().keys({
a: joi.string().optional().allow(null).allow('').empty('').default('default value')
});
let t = {
a: ''
};
let result = joi.validate(t, schema);
console.log(result);
// { error: null, value: { a: 'default value' } }
这篇关于带有空值、未定义值、空值和默认值的 npm joi的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!