Hello,
How can I define a schema to validate a Typescript Enum?
For example, consider the following type script code
export enum MyEnum {
FOO = "Foo",
BAR = "Bar"
}
interface MyInterface {
name: string;
type: MyEnum;
}
// I want Typescript to show compiler errors if my schema does not match my interface
const mySchema = yup.object().shape
name: yup.string().required(),
type: ??? How to describe the schema that would validate MyEnum
}).defined();
Hello,
i am using:
Yup.mixed<MyEnum>().oneOf(Object.values(MyEnum))
@jannsk's solution worked for me. Thanks!
Most helpful comment
Hello,
i am using:
Yup.mixed<MyEnum>().oneOf(Object.values(MyEnum))