javascript - 从 JSON 创建动态 Yup 验证模式

标签 javascript reactjs redux formik yup

我正在尝试在我的 React 表单中使用 YupFormik。表单字段将是动态的,它们的验证也是如此。

export const formData = [
  {
    id: "name",
    label: "Full name",
    placeholder: "Enter full name",
    type: "text",
    required: true,
    value: "User name",
    values: [],
    validations: [
      {
        type: "minLength",
        value: "5",
        error_message: "name should be atleast 5 char long"
      },
      {
        type: "maxLength",
        value: "10",
        error_message: "name should be atleast 5 char long"
      }
    ]
  },
  {
    id: "email",
    label: "Email",
    placeholder: "Email",
    type: "text",
    required: true,
    value: "email",
    values: [],
    validations: [
      {
        type: "minLength",
        value: "5",
        error_message: "name should be atleast 5 char long"
      },
      {
        type: "maxLength",
        value: "10",
        error_message: "name should be atleast 5 char long"
      },
      {
        type: "email",
        error_message: "Valid email"
      }
    ]
  },
  {
    id: "phoneNumber",
    label: "phone number",
    type: "text",
    required: true,
    value: "7878787878",
    values: [],
    validations: [
      {
        type: "minLength",
        value: "5",
        error_message: "name should be atleast 5 char long"
      },
      {
        type: "maxLength",
        value: "10",
        error_message: "name should be atleast 5 char long"
      },
      {
        type: "required",
        error_message: "phone number is required"
      }
    ]
  },
  {
    id: "total",
    label: "Total People in Family",
    placeholder: "family members count",
    type: "text",
    required: false,
    value: "1",
    values: [],
    validations: [
      {
        type: "minLength",
        value: "1",
        error_message: "there should be atleast 1 family member"
      },
      {
        type: "maxLength",
        value: "5",
        error_message: "max family members can be 5"
      }
    ]
  }
]

 let validateSchema = yup.object().shape({
     name: yup.string().required("name is required"),
     email: yup.string().email(),
     phoneNumber: yup.number().min(10, "minium 10 numbers"),
     total: yup
       .number()
       .min(1, "minium 1 member")
       .max(5, "max 5 member")
       .required("member is required")    });
  • 我目前正在做的是遍历上述数组并调用相应的 React 表单组件。
  • 验证目前由 Yup 处理。我知道您可以像上面的“validateSchema”变量一样创建静态 Yup 验证模式。
  • 现在我想根据这些值创建这个验证模式 在 formData.validation 数组中。我尝试了一些方法 codesandbox但仍然无法弄清楚。另外,我看了 进入 Yup.lazy 但它似乎让我非常困惑。

任何帮助将不胜感激:)

Codesandbox

最佳答案

以防有人试图即时创建 yupschema。在一些帮助下,我能够做到。

import * as yup from "yup";

export function createYupSchema(schema, config) {
  const { id, validationType, validations = [] } = config;
  if (!yup[validationType]) {
    return schema;
  }
  let validator = yup[validationType]();
  validations.forEach(validation => {
    const { params, type } = validation;
    if (!validator[type]) {
      return;
    }
    console.log(type, params);
    validator = validator[type](...params);
  });
  schema[id] = validator;
  return schema;
}

Codesandbox

关于javascript - 从 JSON 创建动态 Yup 验证模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56725383/

相关文章:

javascript - 从 iPad 中 html5 <video> 元素上的点击/触摸事件重定向到另一个页面?

javascript - 如何使用 redux saga 从 redux store 获取商品

reactjs - 如何在 redux-persist 中同时配置 localStorage 和 sessionStorage?

reactjs - 用 enzyme 和 Jasmine 测试高阶成分

reactjs - 使用 redux 表单时控制台中出现未知的 props 消息

javascript - 如何让 ExtJS 网格响应式

javascript - Safari 12.1 中的变换原点动画问题

javascript - 可以对文件 ://protocol? 使用react

javascript - 如何在 Javascript 中检测数字键盘输入

json - [错误: Can't find npm module 'react/package.json' .您是否忘记在 'Npm.depends'包内的package.js中调用 'modules-runtime'?]