gpt4 book ai didi

javascript - 乔伊 :allow null values in array

转载 作者:数据小太阳 更新时间:2023-10-29 06:00:21 25 4
gpt4 key购买 nike

我正在尝试在 POST 请求中添加对数组的验证

Joi.array().items(Joi.string()).single().optional()

我需要在负载中允许空值。你能告诉我如何做到这一点吗?

最佳答案

如果你想让数组为空,使用:

Joi.array().items(Joi.string()).allow(null);

如果你想在数组中允许空字符串或空白字符串使用:

Joi.array().items(Joi.string().allow(null).allow(''));

示例:

const Joi = require('joi');

var schema = Joi.array().items(Joi.string()).allow(null);

var arr = null;

var result = Joi.validate(arr, schema);

console.log(result); // {error: null}

arr = ['1', '2'];

result = Joi.validate(arr, schema);

console.log(result); // {error: null}


var insideSchema = Joi.array().items(Joi.string().allow(null).allow(''));

var insideResult = Joi.validate(['1', null, '2'], insideSchema);

console.log(insideResult);

关于javascript - 乔伊 :allow null values in array,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41470251/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com