I tried to valid unknown keys and values in object using joi.
I referred this link([1]: Joi object validation: How to validate values with unknown key names?) .but I tried it but its not working in my case. any you give any solution.
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 Validation
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 is object unknown keys and values.in my case object value should not start with special characters .but I give it dummy product data in product specification is not valid but I run code its sucessfully inserted in db. Its not throwing validation error.