首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Yup验证yup字符串架构不能指定键入“string”

Yup验证yup字符串架构不能指定键入“string”
EN

Stack Overflow用户
提问于 2021-03-29 18:57:02
回答 1查看 1.8K关注 0票数 1

对于电话号码验证中的项或选项值,我在tsx中有一个类型错误。Yup正在抛出类型错误,表示类型的参数。

'(_item: any,options:{ path: string;}) => yup.StringSchema‘不能分配给类型为’(值:字符串) =>架构‘的参数。

这是我的代码:

代码语言:javascript
复制
export const schema = yup.object().shape({
  contactID: yup.string().required('Please select an option').nullable(),
  firstName: yup.string().when('contactID', {
    is: 'other',
    then: yup.string().required('First Name is required').nullable(),
    otherwise: yup.string().nullable(),
  }),
  lastName: yup.string().when('contactID', {
    is: 'other',
    then: yup.string().required('Last Name is required').nullable(),
    otherwise: yup.string().nullable(),
  }),
  phoneNumber: yup.array().of(
    yup.object().shape({
      phoneNumber: yup.lazy((item: any, options: { path: string }) => {
        if (options.path === 'insuredContactInformation.phoneNumber[0].phoneNumber') {
          return phoneValidation(true).required('Phone Number is required');
        } else return phoneValidation(true);
      }),
      phoneType: yup.string().nullable()
    })
  ),
});

这是错误

EN

回答 1

Stack Overflow用户

发布于 2021-06-30 09:49:54

我通过在传递给组件时更改PropTypes类型来解决这个问题。我传递的不是对象,而是func

代码语言:javascript
复制
// Proptypes
BrandForm.propTypes = {
  t: PropTypes.func.isRequired,
  onFormSubmit: PropTypes.func.isRequired,
  validationSchema: PropTypes.func.isRequired,
};

当以这种方式传递到组件时:

代码语言:javascript
复制
  <BrandForm
      validationSchema={() => addNewBrandSchema(language)}
  />
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66860005

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档