yup验证了打字稿中的枚举数组
我正在尝试使用YUP来验证DTO的验证,但无法通过检查枚举解决一个问题。我有一个与下面类似的代码:
export enum TestEnum {
TEST = 0,
NOT_TEST = 1,
}
export interface SampleDTO {
testEnum: TestEnum[];
}
export const sampleDtoSchema: SchemaOf<SampleDTO> = yup.object({
testEnum: yup
.array(
yup
.mixed<TestEnum>()
.oneOf(Object.values(TestEnum) as TestEnum[])
.required(),
)
.ensure(),
});
我会收到此错误:
Type 'MixedSchema<TestEnum | undefined, AnyObject, TestEnum>' is not assignable to type 'BaseSchema<Maybe<TestEnum.NOT_TEST>, AnyObject, TestEnum.NOT_TEST>'.
Types of property '__inputType' are incompatible.
Type 'TestEnum | undefined' is not assignable to type 'Maybe<TestEnum.NOT_TEST>'.
Type 'TestEnum.TEST' is not assignable to type 'Maybe<TestEnum.NOT_TEST>'
应该如何正确完成它?
I am trying to validate DTO's using Yup and i can't resolve an issue with checking enums. I have a code similar to the one below:
export enum TestEnum {
TEST = 0,
NOT_TEST = 1,
}
export interface SampleDTO {
testEnum: TestEnum[];
}
export const sampleDtoSchema: SchemaOf<SampleDTO> = yup.object({
testEnum: yup
.array(
yup
.mixed<TestEnum>()
.oneOf(Object.values(TestEnum) as TestEnum[])
.required(),
)
.ensure(),
});
And i get this error:
Type 'MixedSchema<TestEnum | undefined, AnyObject, TestEnum>' is not assignable to type 'BaseSchema<Maybe<TestEnum.NOT_TEST>, AnyObject, TestEnum.NOT_TEST>'.
Types of property '__inputType' are incompatible.
Type 'TestEnum | undefined' is not assignable to type 'Maybe<TestEnum.NOT_TEST>'.
Type 'TestEnum.TEST' is not assignable to type 'Maybe<TestEnum.NOT_TEST>'
How it should be done correctly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这可能会有所帮助:
This might be helpful: