Skip to content

Commit 2e29ddb

Browse files
authored
Merge pull request #561 from milan-wisdom/fix
Support yup enums with any naming conventions (EnumName.ENUM_VALUE)
2 parents 107e98a + f973f7f commit 2e29ddb

File tree

3 files changed

+8
-17
lines changed

3 files changed

+8
-17
lines changed

example/yup/schemas.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import * as yup from 'yup'
22
import { Admin, AttributeInput, ButtonComponentType, ComponentInput, DropDownComponentInput, EventArgumentInput, EventInput, EventOptionType, Guest, HttpInput, HttpMethod, LayoutInput, MyType, MyTypeFooArgs, PageInput, PageType, User, UserKind } from '../types'
33

4-
export const ButtonComponentTypeSchema = yup.string<ButtonComponentType>().oneOf([ButtonComponentType.Button, ButtonComponentType.Submit]).defined();
4+
export const ButtonComponentTypeSchema = yup.string<ButtonComponentType>().oneOf(Object.values(ButtonComponentType)).defined();
55

6-
export const EventOptionTypeSchema = yup.string<EventOptionType>().oneOf([EventOptionType.Reload, EventOptionType.Retry]).defined();
6+
export const EventOptionTypeSchema = yup.string<EventOptionType>().oneOf(Object.values(EventOptionType)).defined();
77

8-
export const HttpMethodSchema = yup.string<HttpMethod>().oneOf([HttpMethod.Get, HttpMethod.Post]).defined();
8+
export const HttpMethodSchema = yup.string<HttpMethod>().oneOf(Object.values(HttpMethod)).defined();
99

10-
export const PageTypeSchema = yup.string<PageType>().oneOf([PageType.BasicAuth, PageType.Lp, PageType.Restricted, PageType.Service]).defined();
10+
export const PageTypeSchema = yup.string<PageType>().oneOf(Object.values(PageType)).defined();
1111

1212
function union<T extends {}>(...schemas: ReadonlyArray<yup.Schema<T>>): yup.MixedSchema<T> {
1313
return yup.mixed<T>().test({

src/yup/index.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -128,21 +128,12 @@ export class YupSchemaVisitor extends BaseSchemaVisitor {
128128
.withContent(`yup.string().oneOf([${enums?.join(', ')}]).defined()`).string
129129
);
130130
} else {
131-
const values = node.values
132-
?.map(
133-
enumOption =>
134-
`${enumname}.${visitor.convertName(enumOption.name, {
135-
useTypesPrefix: false,
136-
transformUnderscore: true,
137-
})}`
138-
)
139-
.join(', ');
140131
this.enumDeclarations.push(
141132
new DeclarationBlock({})
142133
.export()
143134
.asKind('const')
144135
.withName(`${enumname}Schema`)
145-
.withContent(`yup.string<${enumname}>().oneOf([${values}]).defined()`).string
136+
.withContent(`yup.string<${enumname}>().oneOf(Object.values(${enumname})).defined()`).string
146137
);
147138
}
148139
},

tests/yup.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ describe('yup', () => {
137137
}
138138
`,
139139
wantContains: [
140-
'export const PageTypeSchema = yup.string<PageType>().oneOf([PageType.Public, PageType.BasicAuth]).defined();',
140+
'export const PageTypeSchema = yup.string<PageType>().oneOf(Object.values(PageType)).defined();',
141141
'export function PageInputSchema(): yup.ObjectSchema<PageInput>',
142142
'pageType: PageTypeSchema.nonNullable()',
143143
],
@@ -162,7 +162,7 @@ describe('yup', () => {
162162
`,
163163
wantContains: [
164164
'export function HttpInputSchema(): yup.ObjectSchema<HttpInput>',
165-
'export const HttpMethodSchema = yup.string<HttpMethod>().oneOf([HttpMethod.Get, HttpMethod.Post]).defined();',
165+
'export const HttpMethodSchema = yup.string<HttpMethod>().oneOf(Object.values(HttpMethod)).defined();',
166166
'method: HttpMethodSchema.nullable().optional(),',
167167
'url: yup.mixed().nonNullable()',
168168
],
@@ -931,7 +931,7 @@ describe('yup', () => {
931931
);
932932

933933
expect(result.content).toContain(
934-
'export const PageTypeSchema = yup.string<PageType>().oneOf([PageType.Public, PageType.BasicAuth]).defined()'
934+
'export const PageTypeSchema = yup.string<PageType>().oneOf(Object.values(PageType)).defined()'
935935
);
936936
expect(result.content).toContain('export function PageInputSchema(): yup.ObjectSchema<PageInput>');
937937

0 commit comments

Comments
 (0)