Yup: How to define a schema for a Typescript Enum

Created on 21 Aug 2020  路  2Comments  路  Source: jquense/yup

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();

Most helpful comment

Hello,
i am using:
Yup.mixed<MyEnum>().oneOf(Object.values(MyEnum))

All 2 comments

Hello,
i am using:
Yup.mixed<MyEnum>().oneOf(Object.values(MyEnum))

@jannsk's solution worked for me. Thanks!

Was this page helpful?
0 / 5 - 0 ratings