是的,带有打字稿的嵌套对象中的条件验证

时间:2021-02-23 15:14:31

标签: typescript yup

我有以下用打字稿编写的 yup 验证架构:

export const billingAddressStepSchema = yup.object().shape<Partial<BillingAddressStepData>>({
  sameAsContractHolder: yup.bool(),
  address: yup.
    string()
    .when("sameAsContractHolder",
      {
        is: false,
        then: yup.string().required("Campo obbligatorio"),
      }),
  addressNumber: yup.
    string()
    .when("sameAsContractHolder",
      {
        is: false,
        then: yup.string().required("Campo obbligatorio"),
      }),
  zipCode: yup.
    string()
    .when("sameAsContractHolder",
      {
        is: false,
        then: yup.string().required("Campo obbligatorio"),
      }),
  city: yup.object().when("sameAsContractHolder", {
    is: true,
    then: yup.object().shape<LookupItem>({
      id: yup.string(),
      description: yup.string(),
      //@ts-ignore
      type: yup.string(),
    }),
    otherwise: yup.object().shape<LookupItem>({
      id: yup.string().required("campo obbligatorio"),
      description: yup.string().required("campo obbligatorio"),
      //@ts-ignore
      type: yup.string().required("campo obbligatorio"),
    })

  })
});

在表单中我必须使用此模式验证,有一个复选框“与法律地址相同”,当检查时,字段地址 addressnumber zipCodecity 不能是必需的。

上述验证模式完美运行,事实上,我的问题与打字稿更相关:对于城市字段,它给出以下错误

enter image description here

如何正确编写此规则以免出现打字稿错误?

非常感谢

0 个答案:

没有答案