简体   繁体   English

是的,基于非字段值的条件验证

[英]Yup conditional validation based on a a non field value

I am trying to create a yup schema where based on a variables value the schema changes slightly.我正在尝试创建一个基于变量值的 yup 架构,架构略有变化。 In my case depending on the value of prop myCondition I need to make a field as required.在我的情况下,根据 prop myCondition的值,我需要根据需要创建一个字段。 I would like to know if there is any better way I can achieve the same using Yup.我想知道是否有更好的方法可以使用 Yup 实现相同的目标。 Here's my current code structure that works:这是我当前有效的代码结构:

// config.js
const COMMON_SCHEMA = {
  str1: yup
    .string()
    .nullable()
    .required('Please input str1'),
  str3: yup
    .string()
    .nullable()
};
const VALIDATION_SCHEMA_1 = yup.object().shape({
  ...COMMON_SCHEMA,
  str2: yup.string().nullable(),
});

const VALIDATION_SCHEMA_2 = yup.object().shape({
  ...COMMON_SCHEMA,
  str2: yup
    .string()
    .nullable()
    .required('Please input str2'),
});

const SCHEMAS = {
  VALIDATION_SCHEMA_1,
  VALIDATION_SCHEMA_2
}
export default SCHEMAS;

the following is how I conditionally choose different schemas:以下是我有条件地选择不同模式的方式:

// app.js
import SCHEMAS from './config';
...

<Formik
  validationSchema={
    this.props.myCondition === true
      ? SCHEMAS.VALIDATION_SCHEMA_1
      : SCHEMAS.VALIDATION_SCHEMA_2
  }
>
...
</Formik>

I feel like I can achieve whatever I am doing above in an easier way with yup .我觉得我可以用yup以更简单的方式实现上面所做的任何事情。 One of the approaches I tried was to pass in the prop myCondition into config file and work with it's value by using yup.when() but when only works if the value you want to use is also a part of the form so I couldn't achieve the same.我尝试的一种方法是将道具myCondition到配置文件中,并通过使用yup.when()来处理它的值,但只有when您要使用的值也是表单的一部分时才有效,所以我不能t 达到同样的效果。

You can conditionally add validation directly in the validationSchema, like this: https://stackoverflow.com/a/56216753/8826314您可以直接在validationSchema中有条件地添加验证,如下所示: https://stackoverflow.com/a/56216753/8826314

Edited to add, you can include your value in the initial form, so that you can do the when check against that field.编辑添加,您可以在初始表单中包含您的值,以便您可以在该字段上when检查。 For example:例如:

<Formik 
  initialValues={
    ...,
    str2
  }
  validationSchema={
    ...,
    str2: yup.object().when('str2', {
      is: str2 => condition check that returns boolean,
      then: Yup.string().nullable(),
      otherwise: Yup.string().nullable().required('Please input str2')
    })
}
>
...
</Formik>

This can be achieved by the following way:这可以通过以下方式实现:

You can wrap your schema in a function and pass your conditonal prop:您可以将架构包装在 function 中并传递您的条件属性:

const wrapperSchema = (condition) => 
  Yup.object().shape({
    ...COMMON_SCHEMA,
    str2: yup.string()
     .when([], {
       is: () => condition === true,
       then: yup.string().nullable(),
       otherwise: yup.string().nullable().required('Please input str2'),
     }),
  });

Please note that first param of when is a required param, you can also pass eg ["title", "name"] where title and name can be your field values, and you can retrieve and use them in is callback, in the same order, if you have to.请注意,when 的第一个参数是必需参数,您也可以传递 eg ["title", "name"] ,其中 title 和 name 可以是您的字段值,您可以在is回调中检索和使用它们,同样订购,如果必须的话。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM