[英]Is there a way to validate strings that represent nested object keys in Joi?
I'm using joi@^17.4.0 and have a schema that looks like this:我正在使用 joi@^17.4.0 并有一个如下所示的架构:
settings: Joi.object({
isHidden: Joi.boolean(),
payoutData: Joi.object({
email: Joi.string().email(),
}),
}),
I would like to input the string 'settings.isHidden'
or 'settings.payoutData.email'
without hard coding each of these strings in the schema.我想输入字符串
'settings.isHidden'
或'settings.payoutData.email'
而不在架构中对这些字符串中的每一个进行硬编码。 Is there a way that I can make joi know that these strings are valid based on the object schema I already have?有没有一种方法可以让我知道这些字符串基于我已经拥有的 object 模式是有效的? Currently, when I try to validate these strings I get a validation error that says those keys are not allowed.
目前,当我尝试验证这些字符串时,我收到一个验证错误,指出这些键是不允许的。
Example object to validate:示例 object 验证:
{ 'settings.isHidden': false }
Your Joi schema:您的 Joi 架构:
settings: Joi.object({
isHidden: Joi.boolean(),
payoutData: Joi.object({
email: Joi.string().email(),
}),
}),
says that settings
(presumably a property of an object) is an object that has two properties:说
settings
(可能是一个对象的属性)是一个 object 有两个属性:
isHidden
, a boolean, and isHidden
,一个 boolean 和payoutData
, an object with the property email
, a string payoutData
,一个具有 email 属性的email
,一个字符串Your example object你的例子 object
{ 'settings.isHidden': false }
is an object with a single property settings.isHidden
and it so fails validation.是具有单个属性
settings.isHidden
的 object ,因此验证失败。
Ergo, your object fails validation against the schema.因此,您的 object 未能针对架构进行验证。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.