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?
This might be helpful:
export const sampleDtoSchema: yup.SchemaOf<SampleDTO> = yup.object({
testEnum: yup
.array(
yup
.mixed()
.oneOf<TestEnum>(Object.values(TestEnum) as TestEnum[])
.required()
)
.min(1)
.ensure()
});