对Date对象进行验证



我们当前的项目结构包括一个使用Yup和TypeScript的表单验证。我们开始这样使用它:

// Used as a type in `useForm` from `react-hook-form`
export type MySlideSchemaType = {
questionField: string
}
// Used with `useForm` as a `resolver`
export const mySlideSchema: yup.SchemaOf<MySlideSchemaType> =
yup.object({
questionField: yup.string().required()
})

上面例子中的TypeScript验证是正确的——它没有导致任何错误,甚至建议在mySlideSchema中使用字段来匹配MySlideSchemaType


当我们使用Date对象时,问题发生了。是的,有一个自定义方法称为date,我认为将匹配Date对象类型从javaScript。但是我错了,我们得到了一个错误。

// Used as a type in `useForm` from `react-hook-form`
export type MySlideSchemaType = {
questionField: Date
}
// Used with `useForm` as a `resolver`
export const mySlideSchema: yup.SchemaOf<MySlideSchemaType> =
yup.object({
questionField: yup.date().required()
})

Intellij错误:

The types of 'fields.questionField' are incompatible between these types.
Type 'RequiredDateSchema<Date, Record<string, any>>' is not assignable to type 'ObjectSchemaOf<Date> | ObjectSchemaOf<Lazy<Date, never>>'.
Type 'RequiredDateSchema<Date, Record<string, any>>' is not assignable to type 'ObjectSchemaOf<Lazy<Date, never>>'.

如何在yup模式中实现日期对象类型?

修复了yup 0.32.11版本的验证问题

相关内容

  • 没有找到相关文章

最新更新