Reputation: 746
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.
Upvotes: 0
Views: 2893
Reputation: 3482
This part here:
product_specification: Joi.object().pattern(
regexConstants.description.pattern,
Joi.string()
.min(productSchema.product_specification.min)
.max(productSchema.product_specification.max)
.required()
)
says that keys should match the regexConstants.description.pattern
pattern, and the values for those keys should be Joi.string()
with the given minimum and maximum length constraints. In other words, you only limit the values to be strings of certain length, not saying anything about what characters are valid.
My guess is that actually wanted to use regexConstants.description.pattern
as a pattern for the values and don't care about validating the keys? In that case, you should do something like this instead:
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()
)
UPDATE:
To validate keys using same pattern as well:
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()
)
Upvotes: 2