【发布时间】:2019-12-13 14:58:53
【问题描述】:
我尝试使用 joi 验证对象中的未知键和值。
我提到了这个链接([1]:Joi object validation: How to validate values with unknown key names?)。但我试过了,但在我的情况下它不起作用。任何你给出任何解决方案。
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 是对象未知的键和值。在我的情况下,对象值不应以特殊字符开头。但我在产品规范中给它的虚拟产品数据无效,但我运行代码,它成功插入到数据库中。它不会引发验证错误。
【问题讨论】: