NodeJS 如何为以下数据编写模式验证?

erhoui1w  于 2023-05-17  发布在  Node.js
关注(0)|答案(2)|浏览(116)

我一直在尝试使用Joi数据验证器为以下输入数据编写Joi模式验证。

{
  "submissionReason": "new TPV required",
  "alignerInfo": {
    "name": "UPPER",
    "value": [
      {
        "name": "set",
        "value": "18/20"
      },
      {
        "name": "zenStage",
        "value": "2"
      }
    ]
  },
  "archesToTreat": "BOTH",
  "refinementGoals": [
    {
      "name": "Alignment"
    },
    {
      "name": "Close Residual Space",
      "value": [
        {
          "position": [
            "2.1",
            "2.2"
          ],
          "value": "0.5"
        },
        {
          "position": [
            "2.4",
            "2.5"
          ],
          "value": "0.7"
        }
      ]
    },
    {
      "name": "Resolve posterior Open Bite",
      "value": [
        {
          "name": "Attachments Only",
          "value": {
            "name": "teeth Attachment",
            "value": [
              "2.1",
              "2.2"
            ]
          }
        },
        {
          "name": "Add Button for elastic",
          "value": [
            "2.1",
            "2.2"
          ]
        }
      ]
    }
  ],
  "attachments": "keep all previous attachment and change",
  "iprInfo": {
    "name": "ipr not performed as prescibed",
    "value": [
      {
        "position": [
          "2.1",
          "2.2"
        ],
        "value": "0.5"
      }
    ]
  },
  "specialNotes": "Doctor Notes"
}

这是我目前为止所写的

Joi.object({
  submissionReason: Joi.string().required(),
  alignerInfo: Joi.object({
    name: Joi.string().required(),
    value: Joi.array().items(Joi.object({
      name: Joi.string().required(),
      value: Joi.string().required(),
    })).required()
  }).required(),
  archesToTreat: Joi.string().valid('UPPER', 'LOWER', 'BOTH').required(),
  refinementGoals: Joi.array().items(Joi.object({
    name: Joi.string().required(),
    value: Joi.array().items(Joi.object({
      position: Joi.array().items(Joi.string().required()).required(),
      value: Joi.string().required(),
      name: Joi.string().forbidden()
    })).when('name', {
      is: 'Close Residual Space',
      then: Joi.required(),
      otherwise: Joi.forbidden()
    })
  })).required(),
  attachments: Joi.string().required(),
  iprInfo: Joi.object({
    name: Joi.string().required(),
    value: Joi.array().items(Joi.object({
      position: Joi.array().items(Joi.string().required()).required(),
      value: Joi.string().required()
    })).required()
  }).required(),
  specialNotes: Joi.string().required()
})

但这不起作用,我得到以下错误:
验证错误:不允许“refinementGoals[2].value”
请帮助我正确验证

2nc8po8w

2nc8po8w1#

您可以使用express-validator,它提供适合您的用例的基于模式的验证。
这里是express-validator的schema方法的链接。https://express-validator.github.io/docs/api/check-schema

vwoqyblh

vwoqyblh2#

您的验证允许2个级别的value

1- the first one is an array of objects
2- the second one is a string

但是示例对象的refinementGoals的值有3个级别:

1- first is an array (ok)
2- second is an object (invalid, only string is acceptable)
3- third is an array of strings (invalid, not defined in the schema)

我建议使用joi.alternatives,它允许你设置多个类型为有效类型。就像这样:

value: Joi.alternatives().try(Joi.object(), Joi.array(), Joi.string());

相关问题