如何使用 joi 验证验证对象中的未知键和值
How to validate unknown keys and values in object using joi validation
我尝试使用 joi 验证对象中的未知键和值。
我提到了这个 link([1]: ) 。但我试过了,但它对我的情况不起作用。任何你给任何解决方案。
Product_collection
{ "product_name": "electricals",
"product_category": "5d452fb9cc012a8e7c368e30",
"product_images": [ "http://www.Vr.com", "http://www.qU.com" ],
"product_overview": "i29bbA55a1",
"product_description": "oXm8uFbIc3",
"product_specification": { "brand": "#$%EYE" } }
JOI 验证
static validateNewProduct(Product) {
const joiNewProductSchema = {
product_name: Joi.string()
.min(productSchema.product_name.min)
.max(productSchema.product_name.max)
.regex(regexConstants.name.pattern)
.required(),
product_category: Joi.objectId().required(),
product_images: Joi.array()
.items(Joi.string().uri())
.required(),
product_description: Joi.string()
.min(productSchema.product_description.min)
.max(productSchema.product_description.max)
.regex(regexConstants.description.pattern)
.required(),
product_overview: Joi.string()
.min(productSchema.product_overview.min)
.max(productSchema.product_overview.max)
.regex(regexConstants.overview.pattern)
.required(),
product_specification: Joi.object().pattern(
regexConstants.description.pattern,
Joi.string()
.min(productSchema.product_specification.min)
.max(productSchema.product_specification.max)
.required()
)
.required(),
product_warranty_text: Joi.string()
.min(productSchema.product_warranty_text.min)
.max(productSchema.product_warranty_text.max)
.regex(regexConstants.description.pattern),
product_promotion: Joi.array().items(Joi.objectId())
};
return Joi.validate(Product, joiNewProductSchema);
}
Product_Specification 是对象未知键,values.in 我的案例对象值不应以特殊字符开头。但我在产品规范中给它的虚拟产品数据无效,但我 运行 成功插入数据库的代码。它没有抛出验证错误。
这部分在这里:
product_specification: Joi.object().pattern(
regexConstants.description.pattern,
Joi.string()
.min(productSchema.product_specification.min)
.max(productSchema.product_specification.max)
.required()
)
表示键应该匹配 regexConstants.description.pattern
模式,并且这些键的值应该是 Joi.string()
并具有给定的最小和最大长度限制。换句话说,您只将值限制为特定长度的字符串,而不是说明哪些字符有效。
我的猜测是实际上想使用 regexConstants.description.pattern
作为值的模式而不关心验证密钥?在这种情况下,您应该改为执行以下操作:
product_specification: Joi.object().pattern(
/^/, // any key is ok
Joi.string()
.min(productSchema.product_specification.min)
.max(productSchema.product_specification.max)
.regex(regexConstants.description.pattern)
.required()
)
更新:
也使用相同的模式验证密钥:
product_specification: Joi.object().pattern(
regexConstants.description.pattern,
Joi.string()
.min(productSchema.product_specification.min)
.max(productSchema.product_specification.max)
.regex(regexConstants.description.pattern)
.required()
)
我尝试使用 joi 验证对象中的未知键和值。
我提到了这个 link([1]:
Product_collection
{ "product_name": "electricals",
"product_category": "5d452fb9cc012a8e7c368e30",
"product_images": [ "http://www.Vr.com", "http://www.qU.com" ],
"product_overview": "i29bbA55a1",
"product_description": "oXm8uFbIc3",
"product_specification": { "brand": "#$%EYE" } }
JOI 验证
static validateNewProduct(Product) {
const joiNewProductSchema = {
product_name: Joi.string()
.min(productSchema.product_name.min)
.max(productSchema.product_name.max)
.regex(regexConstants.name.pattern)
.required(),
product_category: Joi.objectId().required(),
product_images: Joi.array()
.items(Joi.string().uri())
.required(),
product_description: Joi.string()
.min(productSchema.product_description.min)
.max(productSchema.product_description.max)
.regex(regexConstants.description.pattern)
.required(),
product_overview: Joi.string()
.min(productSchema.product_overview.min)
.max(productSchema.product_overview.max)
.regex(regexConstants.overview.pattern)
.required(),
product_specification: Joi.object().pattern(
regexConstants.description.pattern,
Joi.string()
.min(productSchema.product_specification.min)
.max(productSchema.product_specification.max)
.required()
)
.required(),
product_warranty_text: Joi.string()
.min(productSchema.product_warranty_text.min)
.max(productSchema.product_warranty_text.max)
.regex(regexConstants.description.pattern),
product_promotion: Joi.array().items(Joi.objectId())
};
return Joi.validate(Product, joiNewProductSchema);
}
Product_Specification 是对象未知键,values.in 我的案例对象值不应以特殊字符开头。但我在产品规范中给它的虚拟产品数据无效,但我 运行 成功插入数据库的代码。它没有抛出验证错误。
这部分在这里:
product_specification: Joi.object().pattern(
regexConstants.description.pattern,
Joi.string()
.min(productSchema.product_specification.min)
.max(productSchema.product_specification.max)
.required()
)
表示键应该匹配 regexConstants.description.pattern
模式,并且这些键的值应该是 Joi.string()
并具有给定的最小和最大长度限制。换句话说,您只将值限制为特定长度的字符串,而不是说明哪些字符有效。
我的猜测是实际上想使用 regexConstants.description.pattern
作为值的模式而不关心验证密钥?在这种情况下,您应该改为执行以下操作:
product_specification: Joi.object().pattern(
/^/, // any key is ok
Joi.string()
.min(productSchema.product_specification.min)
.max(productSchema.product_specification.max)
.regex(regexConstants.description.pattern)
.required()
)
更新:
也使用相同的模式验证密钥:
product_specification: Joi.object().pattern(
regexConstants.description.pattern,
Joi.string()
.min(productSchema.product_specification.min)
.max(productSchema.product_specification.max)
.regex(regexConstants.description.pattern)
.required()
)