当字段匹配条件时,对一系列对象验证的YUP验证

发布于 2025-02-03 19:03:13 字数 1690 浏览 3 评论 0原文

我想对此数据形状进行YUP验证:

{
   "values": [
      {
         "id": "Card ID",
         "checkBox": true,
         "csvField": ""
      },
      {
         "id": "title",
         "checkBox": false,
         "csvField": ""
      },
      {
         "id": "subtitle",
         "checkBox": false,
         "csvField": ""
      },
      {
         "id": "phase",
         "checkBox": false,
         "csvField": "",
         "fields": [
            {
               "id": "61d5bc37b995d200116bc73c",
               "phaseInCsv": ""
            },
            {
               "id": "61d5c43759bd07001134076a",
               "phaseInCsv": ""
            },
            {
               "id": "6203f08dc0eb7000120052a1",
               "phaseInCsv": ""
            }
         ]
      }
   ]
}

因此,在ID = apeas时,该字段数组的验证应发生 我这样做了,但没有起作用!

任何建议!


这是我的验证,但行不通:

  const validationSchema = Yup.object().shape({
    values: Yup.array().of(
      Yup.object().shape({
        id: Yup.string(),
        checkBox: Yup.boolean(),
        csvField: Yup.string().when('checkBox', {
          is: true,
          then: Yup.string().required(
            t('IMPORT.VALIDATION_ERROR.CSV_FIELD_MISSING')
          ),
        }),
        fields: Yup.array().when('id', {
          is: FormFieldType.PHASE,
          then: Yup.array()
            .of(
              Yup.object().shape({
                id: Yup.string(),
                phaseInCsv: Yup.string().required(
                  t('IMPORT.VALIDATION_ERROR.CSV_FIELD_MISSING')
                ),
              })
            )
            .required(),
        }),
      })
    ),
  })

i want to have yup validation for this shape of data :

{
   "values": [
      {
         "id": "Card ID",
         "checkBox": true,
         "csvField": ""
      },
      {
         "id": "title",
         "checkBox": false,
         "csvField": ""
      },
      {
         "id": "subtitle",
         "checkBox": false,
         "csvField": ""
      },
      {
         "id": "phase",
         "checkBox": false,
         "csvField": "",
         "fields": [
            {
               "id": "61d5bc37b995d200116bc73c",
               "phaseInCsv": ""
            },
            {
               "id": "61d5c43759bd07001134076a",
               "phaseInCsv": ""
            },
            {
               "id": "6203f08dc0eb7000120052a1",
               "phaseInCsv": ""
            }
         ]
      }
   ]
}

so the validation of the fields array should happen only once the id = phase
i did it this way but didn't work !

any suggestions guys !


this is my validation but it doesn't work :

  const validationSchema = Yup.object().shape({
    values: Yup.array().of(
      Yup.object().shape({
        id: Yup.string(),
        checkBox: Yup.boolean(),
        csvField: Yup.string().when('checkBox', {
          is: true,
          then: Yup.string().required(
            t('IMPORT.VALIDATION_ERROR.CSV_FIELD_MISSING')
          ),
        }),
        fields: Yup.array().when('id', {
          is: FormFieldType.PHASE,
          then: Yup.array()
            .of(
              Yup.object().shape({
                id: Yup.string(),
                phaseInCsv: Yup.string().required(
                  t('IMPORT.VALIDATION_ERROR.CSV_FIELD_MISSING')
                ),
              })
            )
            .required(),
        }),
      })
    ),
  })

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文