Skip to content

Commit bd07353

Browse files
committed
fixed to generate enum schema
1 parent fa51854 commit bd07353

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

src/yup/index.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ interface InputObjectFieldGeneratorParams {
2323
indentCount?: number;
2424
}
2525

26-
export const ImportYup = (): string => `import * as yup from 'yup'`
26+
export const ImportYup = (): string => `import * as yup from 'yup'`;
2727

2828
export class YupGenerator {
2929
private inputObjects: InputObjectTypeDefinitionNode[];
@@ -37,9 +37,14 @@ export class YupGenerator {
3737
}
3838

3939
public generate(): string {
40-
return this.inputObjects
41-
.map((inputObject) => this.generateInputObjectYupSchema(inputObject))
42-
.join("\n\n");
40+
return [
41+
...Object.values(this.enums).map(
42+
(enumdef) => `export const ${enumdef.name.value}Schema = yup.mixed().oneOf(Object.values(${enumdef.name.value}))`
43+
),
44+
...this.inputObjects.map((inputObject) =>
45+
this.generateInputObjectYupSchema(inputObject)
46+
),
47+
].join("\n\n");
4348
}
4449

4550
protected generateInputObjectYupSchema(
@@ -112,7 +117,7 @@ export class YupGenerator {
112117
}
113118
if (this.enums[node.value]) {
114119
const enumdef = this.enums[node.value];
115-
return `yup.mixed().oneOf(Object.values(${enumdef.name.value}))`;
120+
return `${enumdef.name.value}Schema`;
116121
}
117122
if (this.scalars[node.value]) {
118123
console.warn("unhandled scalar:", this.scalars[node.value]);

0 commit comments

Comments
 (0)