【问题标题】:Validate object with condition in array with Joi使用 Joi 在数组中验证具有条件的对象
【发布时间】:2021-09-06 12:04:55
【问题描述】:

我正在编写以下代码,我必须在其中使用 Joi 库执行一些验证。

验证解释如下:当type 等于ABC 并且在数组arrayOfObjects 中有一个objectInArray.id 等于允许值范围内的特定值(在这种情况下,UUID @ 987654325@) 并且当对象anotherObjectInArray 不为空时,则objectToValidate 应该存在,并具有必需的属性field1field2,否则objectToValidate 是可选的。

const data = {
    type: "ABC",
    arrayOfObjects: [
        {
            objectInArray: {
                id: '012345ab-c678-910d-e111-f21g3h14i15j',
                value: "test"
            },
            anotherObjectInArray: {
                some_field: "some_value"
            },
            otherData: "...",
        },
        {
            objectInArray: {
                id: '161718kl-m192-021n-o222-p32q4r25s26t',
                value: "test2"
            },
            otherData: "..."
        }
    ],
    objectToValidate: {
        field1: "value",
        field2: true
    }
}

我尝试进行以下验证,但失败了。

objectToValidate: Joi.when('type', {
    is: 'ABC',
    then: Joi.when('arrayOfObjects', {
        is: Joi.array().items(Joi.object({
            objectInArray: Joi.object({
                id: Joi.string().guid().required().valid('012345ab-c678-910d-e111-f21g3h14i15j', 'other_uuid')
            }).required()
        }).required()),
        then: Joi.when('arrayOfObjects', {
            is: Joi.array().items(Joi.object({
                anotherObjectInArray: Joi.object().not(null)
            })),
            then: Joi.object({
                field1: Joi.string().required(),
                field2: Joi.boolean().strict().required()
            }).required(),
            otherwise: Joi.object().optional()
        }),
        otherwise: Joi.object().optional()
    }),
    otherwise: Joi.object().optional()
})

谁能指出我的代码有什么问题? :)

【问题讨论】:

    标签: node.js validation hapijs joi hapi


    【解决方案1】:

    您需要玩弄.when.has

    此架构只是一个示例,您可能需要进行一些调整:

    Joi.object({
      type: Joi.string(),
      arrayOfObjects:Joi.array(),
      objectToValidate: Joi.object()
    }).when(Joi.object({ 
      type: Joi.valid('ABC'),
      arrayOfObjects: Joi.array().items().has(Joi.object({ 
          objectInArray: Joi.object({ id: Joi.valid('012345ab-c678-910d-e111-f21g3h14i15j'), value: Joi.valid() }),
          anotherObjectInArray: Joi.not(null)
        }).unknown()),
    }).unknown(), {
      then: Joi.object({
        objectToValidate: Joi.required()
      }),
    })
    

    对于.has,我们说必须至少存在一个符合这些条件的对象,这意味着此示例将失败,因为至少有一个对象具有objectInArray.id = 012345ab-c678-910d-e111-f21g3h14i15janotherObjectInArray 不为空,因此objectToValidate 将是必填:

    {
      type: "ABC",
      arrayOfObjects: [
        {
          objectInArray: {
            id: 'abcd',
            value: "test"
          },
          anotherObjectInArray: null
        },
          {
          objectInArray: {
            id: '012345ab-c678-910d-e111-f21g3h14i15j',
            value: "test"
          },
          anotherObjectInArray: { }
        }
      ]
    }
    

    这是错误:

    验证错误:需要“objectToValidate”

    并且这个对象会通过:

    {
      type: "ABC",
      arrayOfObjects: [
        {
          objectInArray: {
            id: 'abcd',
            value: "test"
          },
          anotherObjectInArray: null
        },
          {
          objectInArray: {
            id: '12313',
            value: "test"
          },
          anotherObjectInArray: null
        }
      ]
    }
    

    因为我们没有必填字段,这意味着不需要objectToValidate

    而且,您不需要多次使用 Joi.optional(),这是默认设置。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-11
      • 1970-01-01
      • 2016-08-05
      • 2020-03-02
      • 2018-12-30
      • 2018-02-27
      • 1970-01-01
      • 2017-06-04
      相关资源
      最近更新 更多