Formik Yup事物数组验证

时间:2019-11-23 19:51:17

标签: reactjs formik yup

这是我的数据的架构。它不仅包含一些顶级属性,还包含一些嵌套输入,它们既是集合things的一部分,也是对象thing2的一部分。 things数组中第二项的第一项输入的输入名称示例为:things[0].inputs[1].value。 Formik似乎通过名称正确附加了值,但验证似乎不起作用。知道我哪里出错了吗?

const schema = {
    name: "", // string - required
    feed_id: "", //string - required
    things: [
      {
        inputs: [
          // zero, one, or more of the following
          // They all require a value, but it can really be any type, 
          {
            name: "InputName",
            value: "StringInput" // String - required
          },
          {
            name: "InputName",
            value: true // boolean - required
          },
          {
            name: "InputName",
            value: 5 // number - required
          }
        ]
      }
    ],
    thingB: {
      inputs: [
        // zero, one, or more of the following
          {
            name: "InputName",
            value: "StringInput" // String - required
          },
          {
            name: "InputName",
            value: true // boolean - required
          },
          {
            name: "InputName",
            value: 5 // number - required
          }
      ]
    }
  };

这是我尝试对其进行YUP验证

  const inputValidationSchema = yup.array().of(
    yup.object().shape({
      value: yup
        .mixed()
        .required()
    })
  );

  const validationSchema = yup.object().shape({
    name: yup.string().required(),
    feed_id: yup.string().required(),
    config: yup.object().shape({
      things: yup
        .array()
        .of(yup.object().shape({ inputs: inputValidationSchema })),
      thingB: yup.mixed().oneOf([
        yup.object().shape({
          inputs: inputValidationSchema
        }),
        null
      ])
    })
  });

它似乎无法正常工作,并且将错误正确地附加到了表单上。

0 个答案:

没有答案