试图验证一个数组在一个案例中有零个或多个字符串,并且它在另一个案例中有零个或多个对象,与Joi文档挣扎:(
validate: {
headers: Joi.object({
'content-type': "application/vnd.api+json",
accept: "application/vnd.api+json"
}).options({ allowUnknown: true }),
payload : Joi.object().keys({
data : Joi.object().keys({
type: Joi.any().allow('BY_TEMPLATE').required(),
attributes: Joi.object({
to : Joi.string().email().required(),
templateId : Joi.string().required(),
categories : Joi.array().items( //trying to validate here that each element is a string),
variables : Joi.array({
//also trying to validate here that each element is an Object with one key and value
})
})
}).required()
})
}
Run Code Online (Sandbox Code Playgroud) 我得到一个后端数组,每个对象包含一个服务名称.结构如下所示
[{"serviceName":"service1"},
{"serviceName":"service2"},..]
Run Code Online (Sandbox Code Playgroud)
当我在后端获取数组时,我想验证数组中的每个对象都有serviceName属性.
我编写了以下代码,但即使我传递有效数组,我也会收到验证错误.
var Joi = require('joi');
var service = Joi.object().keys({
serviceName: Joi.string().required()
});
var services = Joi.array().ordered(service);
var test = Joi.validate([{serviceName:'service1'},{serviceName:'service2'}],services)
Run Code Online (Sandbox Code Playgroud)
对于上面的代码,我总是收到带有消息的验证错误
"value" at position 1 fails because array must contain at most 1 items
Run Code Online (Sandbox Code Playgroud) 有没有其他方法可以在Joi验证模式中为关键的期望正则表达式模式设置特定值?
我的示例架构:
const schema = joi.object().keys({
query: joi.object().keys({
// allow only apple and banana
id: joi.string().regex(/^(apple|banana)$/).required(),
}).required(),
})
Run Code Online (Sandbox Code Playgroud) 在Node.js RESTapi中验证用户的输入似乎非常简单Joi
.
但问题是我的应用程序不是用英文写的.这意味着我需要向前端用户发送自定义书面消息.
我搜索了这个,只发现了问题.
也许有人可以为此提供解决方案?
这是我用来验证Joi
系统的代码:
var schema = Joi.object().keys({
firstName: Joi.string().min(5).max(10).required(),
lastName: Joi.string().min(5).max(10).required()
..
});
Joi.validate(req.body, schema, function(err, value) {
if (err) {
return catched(err.details);
}
});
function catched(reject) {
res.json({
validData: false,
errors: reject
});
}
Run Code Online (Sandbox Code Playgroud)
另外,有没有办法Joi
在客户端使用?
谢谢!
我有这个 Joi 架构:
let schema = {};
let stations = {
contact: {
first_name: Joi.string().min(2).max(10).regex(Regex.alphabeta, 'alphabeta').allow("").error(JoiCustomErrors),
last_name: Joi.string().min(2).max(10).regex(Regex.alphabeta, 'alphabeta').allow("").error(JoiCustomErrors),
phone: Joi.string().min(10).max(10).regex(Regex.num, 'num').allow("").error(JoiCustomErrors),
},
address: {
place: Joi.string().min(2).max(10).regex(Regex.alphanum, 'alphanum').required().error(JoiCustomErrors),
city: Joi.string().min(2).max(30).required().error(JoiCustomErrors),
street: Joi.string().min(2).max(30).regex(Regex.alphabeta, 'alphabeta').required().error(JoiCustomErrors),
house_number: Joi.string().min(1).max(6).regex(Regex.alphanum, 'alphanum').allow("").error(JoiCustomErrors)
},
passengers_amount: Joi.number().min(0).max(4).required().error(JoiCustomErrors),
notes: Joi.string().min(2).max(100).regex(Regex.alphanum, 'alphanum').allow("").error(JoiCustomErrors)
};
schema.stations = Joi.array().items(stations).min(1).max(5).required().error(JoiCustomErrors);
Run Code Online (Sandbox Code Playgroud)
如您所见,schema.stations
是一个包含最少 1 个和最多 5 个元素的数组。我希望每个元素只有在"contact.first_name" AND "contact.last_name" AND "contact.phone"
存在(或根据架构正确填充)时才具有“address.place”字段。
我怎样才能做到这一点?
我已经使用 Joi 创建了以下用于验证的架构:
const createProfileSchema = Joi.object().keys({
username: Joi.string()
.required()
.message("username is required")
.empty()
.message("username is not allowed to be empty")
.min(5)
.message("username must be greater than 5 characters")
.max(20)
.message("username must be less than 5 characters")
});
Run Code Online (Sandbox Code Playgroud)
但它引发了流动错误:
Cannot apply rules to empty ruleset or the last rule added does not support rule properties
4 | username: Joi.string()
5 | .required()
> 6 | .message("username is required")
| ^
7 | .empty()
8 | .message("username is not allowed to …
Run Code Online (Sandbox Code Playgroud) 我有一个简单的要求.我试图在互联网上搜索以及文档,但失败了.
所以这就是我想要实现的目标:
我有一个架构:
const schema = Joi.object().keys({
a: Joi.string().required(),
b: Joi.string().required()
});
Run Code Online (Sandbox Code Playgroud)
现在,我如何配置它以允许对象中的任何其他键?
使用此架构,它只允许两个键a
和b
.如果我传递任何其他密钥,比如说,c
它会抛出一个错误,说c
不允许这样做.
根据 Joi 文档,您可以Joi.object()
像这样使用:
const object = Joi.object({
a: Joi.number().min(1).max(10).integer(),
b: Joi.any()
});
Run Code Online (Sandbox Code Playgroud)
但是你也可以Joi.object().keys()
像这样写一个等效的代码:
const object = Joi.object().keys({
a: Joi.number().min(1).max(10).integer(),
b: Joi.any()
});
Run Code Online (Sandbox Code Playgroud)
两者有什么区别?
我有以下架构:
var testSchema = Joi.object().keys({
a: Joi.string(),
b: Joi.string(),
c: Joi.string().when('a', {'is': 'avalue', then: Joi.string().required()})
});
Run Code Online (Sandbox Code Playgroud)
但是我想在c
字段定义中添加一个条件,以便在以下情况下使用它:
a == 'avalue' AND b=='bvalue'
我怎样才能做到这一点?
我试图在joi中创建嵌套模式,它抛出错误
[错误:对象架构不能是一个joi架构]
var nestedSchema = joi.object({
b: joi.number()
});
var base = joi.object({
a: joi.string(),
nestedData:joi.object(nestedSchema)
});
Run Code Online (Sandbox Code Playgroud)
我应该如何在joi中定义嵌套模式?