【问题标题】:How to validate unknown keys and values in object using joi validation如何使用 joi 验证来验证对象中的未知键和值
【发布时间】: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 是对象未知的键和值。在我的情况下,对象值不应以特殊字符开头。但我在产品规范中给它的虚拟产品数据无效,但我运行代码,它成功插入到数据库中。它不会引发验证错误。

【问题讨论】:

    标签: node.js joi


    【解决方案1】:

    这部分在这里:

                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()
                )
    

    【讨论】:

    • 我想要使用 key 如何实现它的相同验证。
    • 更新答案,现在可以了吗?
    猜你喜欢
    • 2021-10-10
    • 2017-06-04
    • 2017-08-05
    • 2015-10-19
    • 2019-04-30
    • 2016-10-11
    • 2021-06-26
    • 2019-01-20
    • 2019-04-14
    相关资源
    最近更新 更多