【问题标题】:Nested Joi validations not working using when嵌套的 Joi 验证在使用时不起作用
【发布时间】:2021-06-04 16:14:16
【问题描述】:

我一直在尝试嵌套 where 验证,但到目前为止还没有运气。这是我想要实现的目标:

1- 如果 mainType 为 COMMERCIAL,则为 contactMethod 定义所需的模式。 2- 如果 isHousing 为真,则为 contactMethod 定义所需的模式。 3- 如果 mainType 是 REAL_ESTATE 则不允许传递 contactMethod 对象

这是我失败的尝试:

contactMethod: Joi.when('mainType', {
    is: 'COMMERCIAL',
    then: Joi.object()
        .keys({
            create: Joi.object({
                message: Joi.string().allow(''),
            }).required(),
        })
        .required()),
}).when('isHousing', {
    is: true,
    then: Joi.object()
        .keys({
            create: Joi.object({
                message: Joi.string().allow(''),
            }).required(),
        })
        .required(),
    otherwise: Joi.disallow(),
})

我也试过这个:

contactMethod: Joi.when('mainType', {
    switch: [
        {
            is: 'COMMERCIAL',
            then: Joi.object()
                .keys({
                    create: Joi.object({
                        message: Joi.string().allow(''),
                    }).required(),
                })
                .required(),
        },
        {
            is: 'REAL_ESTATE',
            then: Joi.when('isHousing', {
                is: true,
                then: Joi.object()
                    .keys({
                        create: Joi.object({
                            message: Joi.string().allow(''),
                        }).required(),
                    })
                    .required(),
            }),
            otherwise: Joi.disallow(),
        },
    ],    
})   

那么我在这里做错了什么?

【问题讨论】:

    标签: node.js joi hapi


    【解决方案1】:

    好的,这就是我的解决方法:

    contactMethod: Joi.when('mainType', {
            is: 'COMMERCIAL',
            then: Joi.object()
                .keys({
                    create: Joi.object()
                        .keys({
                            message: Joi.string().allow(''),
                        })
                        .required(),
                })
                .required(),
            otherwise: Joi.when('isHousing', {
                is: true,
                then: Joi.object()
                    .keys({
                        create: Joi.object()
                            .keys({
                                message: Joi.string().allow(''),
                            })
                            .required(),
                    })
                    .required(),
                otherwise: Joi.forbidden(),
            }),
        })
    

    我的尝试没有按照上面@hoangdv 的建议包含键({})+ 我还需要将 disallow() 更改为禁止。

    希望这对未来的人有所帮助。

    【讨论】:

      【解决方案2】:

      就像第一级一样 - 使用.keys({}) 定义对象属性。

      contactMethod: Joi.when('mainType', {
        is: 'COMMERCIAL',
        then: Joi.object()
          .keys({
            create: Joi.object() // You missing usage of Joi.object()
              .keys({ // should define object attribute here
                message: Joi.string().allow(''),
              })
              .required(),
          })
          .required(),
      }).when('isHousing', {
        is: true,
        then: Joi.object()
          .keys({
            create: Joi.object() // the same :|
              .keys({
                message: Joi.string().allow(''),
              })
              .required(),
          })
          .required(),
        otherwise: Joi.disallow(),
      });
      

      【讨论】:

      • 不幸的是,这不起作用。我用禁止的()替换了disallow(),它有效,但在所有情况下都不允许contactMethod!
      猜你喜欢
      • 1970-01-01
      • 2018-02-27
      • 2020-06-01
      • 2016-02-19
      • 2019-03-23
      • 1970-01-01
      • 1970-01-01
      • 2021-04-15
      • 2014-01-16
      相关资源
      最近更新 更多