Skip to content

Commit 078c82d

Browse files
committed
added config for yup strict option
1 parent a552eb9 commit 078c82d

File tree

3 files changed

+76
-26
lines changed

3 files changed

+76
-26
lines changed

src/config.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export type ValidationSchema = "yup";
44

55
export interface ValidationSchemaPluginConfig extends TypeScriptPluginConfig {
66
/**
7-
* @description specify generate schema.
7+
* @description specify generate schema
88
* @default yup
99
*
1010
* @exampleMarkdown
@@ -63,4 +63,28 @@ export interface ValidationSchemaPluginConfig extends TypeScriptPluginConfig {
6363
* ```
6464
*/
6565
enumsAsTypes?: boolean;
66+
/**
67+
* @description this is for yup schema. use this when you specified `schema: yup`
68+
*/
69+
yup?: YupSchemaPluginConfig
70+
}
71+
72+
interface YupSchemaPluginConfig {
73+
/**
74+
* @description Generates yup schema as strict.
75+
* @default false
76+
* @see https://github.com/jquense/yup#schemastrictenabled-boolean--false-schema
77+
*
78+
* @exampleMarkdown
79+
* ```yml
80+
* generates:
81+
* path/to/file.ts:
82+
* plugins:
83+
* - graphql-codegen-validation-schema
84+
* config:
85+
* yup:
86+
* strict: true
87+
* ```
88+
*/
89+
strict?: boolean
6690
}

src/yup/index.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export const YupSchemaVisitor = (
4040

4141
const shape = node.fields
4242
?.map((field) =>
43-
generateInputObjectFieldYupSchema(tsVisitor, schema, field, 2)
43+
generateInputObjectFieldYupSchema(config, tsVisitor, schema, field, 2)
4444
)
4545
.join(",\n");
4646

@@ -63,9 +63,7 @@ export const YupSchemaVisitor = (
6363
.withName(`${enumname}Schema`)
6464
.withContent(
6565
`yup.mixed().oneOf([${node.values
66-
?.map(
67-
(enumOption) => `'${enumOption.name.value}'`
68-
)
66+
?.map((enumOption) => `'${enumOption.name.value}'`)
6967
.join(", ")}])`
7068
).string;
7169
}
@@ -108,13 +106,15 @@ export const YupSchemaVisitor = (
108106
};
109107

110108
const generateInputObjectFieldYupSchema = (
109+
config: ValidationSchemaPluginConfig,
111110
tsVisitor: TsVisitor,
112111
schema: GraphQLSchema,
113112
field: InputValueDefinitionNode,
114113
indentCount: number
115114
): string => {
116115
// TOOD(codehex): handle directive
117116
const gen = generateInputObjectFieldTypeYupSchema(
117+
config,
118118
tsVisitor,
119119
schema,
120120
field.type
@@ -126,17 +126,19 @@ const generateInputObjectFieldYupSchema = (
126126
};
127127

128128
const generateInputObjectFieldTypeYupSchema = (
129+
config: ValidationSchemaPluginConfig,
129130
tsVisitor: TsVisitor,
130131
schema: GraphQLSchema,
131132
type: TypeNode,
132133
parentType?: TypeNode
133134
): string => {
134135
if (isListType(type)) {
135136
const gen = generateInputObjectFieldTypeYupSchema(
137+
config,
136138
tsVisitor,
137139
schema,
138140
type.type,
139-
type,
141+
type
140142
);
141143
if (!isNonNullType(parentType)) {
142144
return `yup.array().of(${maybeLazy(type.type, gen)}).optional()`;
@@ -145,21 +147,23 @@ const generateInputObjectFieldTypeYupSchema = (
145147
}
146148
if (isNonNullType(type)) {
147149
const gen = generateInputObjectFieldTypeYupSchema(
150+
config,
148151
tsVisitor,
149152
schema,
150153
type.type,
151-
type,
154+
type
152155
);
153156
return maybeLazy(type.type, `${gen}.defined()`);
154157
}
155158
if (isNamedType(type)) {
156-
return generateNameNodeYupSchema(tsVisitor, schema, type.name);
159+
return generateNameNodeYupSchema(config, tsVisitor, schema, type.name);
157160
}
158161
console.warn("unhandled type:", type);
159162
return "";
160163
};
161164

162165
const generateNameNodeYupSchema = (
166+
config: ValidationSchemaPluginConfig,
163167
tsVisitor: TsVisitor,
164168
schema: GraphQLSchema,
165169
node: NameNode
@@ -176,7 +180,8 @@ const generateNameNodeYupSchema = (
176180
return `${enumName}Schema`;
177181
}
178182

179-
return yup4Scalar(tsVisitor, node.value);
183+
const primitive = yup4Scalar(tsVisitor, node.value);
184+
return config.yup?.strict ? `${primitive}.strict(true)` : primitive;
180185
};
181186

182187
const maybeLazy = (type: TypeNode, schema: string): string => {

tests/yup.spec.ts

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -216,22 +216,43 @@ describe("yup", () => {
216216
"export const PageTypeSchema = yup.mixed().oneOf(['PUBLIC', 'BASIC_AUTH'])"
217217
);
218218
});
219-
// describe("with importFrom", () => {
220-
// it("Should work", async () => {
221-
// const schema = buildSchema(/* GraphQL */ `
222-
// "custom enum"
223-
// enum MyEnum {
224-
// "this is a"
225-
// A
226-
// "this is b"
227-
// B
228-
// }
229-
// `);
230-
// const result = (await plugin(schema, [], {
231-
// importFrom: "./generated-types",
232-
// })) as Types.ComplexPluginOutput;
219+
it("with yup.strict", async () => {
220+
const schema = buildSchema(/* GraphQL */ `
221+
input PrimitiveInput {
222+
a: ID
223+
b: String
224+
c: Boolean
225+
d: Int
226+
e: Float
227+
f: F!
228+
}
233229
234-
// expect(1).toBe(1)
235-
// });
236-
// });
230+
input F {
231+
a: String!
232+
}
233+
`);
234+
const result = await plugin(
235+
schema,
236+
[],
237+
{
238+
yup: {
239+
strict: true,
240+
},
241+
},
242+
{}
243+
);
244+
const wantContains = [
245+
"export function PrimitiveInputSchema(): yup.SchemaOf<PrimitiveInput>",
246+
"a: yup.string().strict(true),",
247+
"b: yup.string().strict(true),",
248+
"c: yup.boolean().strict(true),",
249+
"d: yup.number().strict(true),",
250+
"e: yup.number().strict(true),",
251+
"f: FSchema().defined()",
252+
"a: yup.string().strict(true).defined()", // for FSchema
253+
];
254+
for (const wantContain of wantContains) {
255+
expect(result.content).toContain(wantContain);
256+
}
257+
});
237258
});

0 commit comments

Comments
 (0)