【问题标题】:Getting type error 'RequiredStringSchema<string | undefined, AnyObject>' on yup validation获取类型错误 \'RequiredStringSchema<string |未定义,AnyObject>\' 是的验证
【发布时间】:2023-01-12 23:03:32
【问题描述】:

我在 Next TS 项目中使用 yup 验证库时遇到问题。我在验证架构中的 PostWithSig 对象下的类型属性上收到类型错误。我不确定此错误的原因,一种可能性是名称 type 本身,但我不确定。

这是我收到的错误消息:

(property) BaseSchema<any, any, any>.type: string 类型 'RequiredStringSchema<字符串 |未定义,AnyObject>' 不是 可分配给类型 'string'.ts(2322) schema.d.ts(53, 14):预期的 类型来自在此处声明的属性“类型” 'AnySchema<任何,任何,任何>'

这是我的验证模式:

data: object().shape({
    createPostTypedData: object().shape({
      id: string().required(),
      expiresAt: date().required(),
      typedData: object().shape({
        types: object().shape({
          PostWithSig: array().of({
            name: string().required(),
            type: string().required(),
          }),
        }),
        domain: object().shape({
          name: string().required(),
          chainId: number().required(),
          version: string().required(),
          verifyingContract: string().required(),
        }),
        value: object().shape({
          nonce: number().required(),
          deadline: number().required(),
          profileId: string().required(),
          contentURI: string().required(),
          collectModule: string().required(),
          collectModuleInitData: string().required(),
          referenceModule: string().required(),
          referenceModuleInitData: string().required(),
        }),
      }),
    }),
  }),

谁能解释为什么我会遇到此错误并提供解决方案来解决它?

【问题讨论】:

    标签: typescript yup


    【解决方案1】:

    我花了一段时间才意识到您忘记使用对象模式:

    array().of(object().shape({
        name: string().required(),
        type: string().required(),
    })),
    

    【讨论】:

      猜你喜欢
      • 2020-08-29
      • 2019-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-16
      • 2014-09-19
      相关资源
      最近更新 更多