diff --git a/src/directive.ts b/src/directive.ts index 59caa56e..f6e13cc1 100644 --- a/src/directive.ts +++ b/src/directive.ts @@ -162,7 +162,7 @@ const applyArgToApiSchemaTemplate = (template: string, apiArgs: any[]): string = const placeholder = match[0]; // `$1` const idx = parseInt(match[1], 10) - 1; // start with `1 - 1` const apiArg = apiArgs[idx]; - if (!apiArg) { + if (apiArg === undefined) { template = template.replace(placeholder, ''); continue; } diff --git a/tests/myzod.spec.ts b/tests/myzod.spec.ts index 4ec29034..b4e3d827 100644 --- a/tests/myzod.spec.ts +++ b/tests/myzod.spec.ts @@ -539,4 +539,38 @@ describe('myzod', () => { } }); }); + + it('properly generates custom directive values', async () => { + const schema = buildSchema(/* GraphQL */ ` + input UserCreateInput { + name: String! @constraint(startsWith: "Sir") + age: Int! @constraint(min: 0, max: 100) + } + directive @constraint(startsWith: String, min: Int, max: Int) on INPUT_FIELD_DEFINITION + `); + const result = await plugin( + schema, + [], + { + schema: 'myzod', + directives: { + constraint: { + min: 'min', + max: 'max', + startsWith: ['pattern', '/^$1/'], + }, + }, + }, + {} + ); + const wantContains = [ + // User Create Input + 'export function UserCreateInputSchema(): myzod.Type {', + 'name: myzod.string().pattern(/^Sir/),', + 'age: myzod.number().min(0).max(100)', + ]; + for (const wantContain of wantContains) { + expect(result.content).toContain(wantContain); + } + }); }); diff --git a/tests/yup.spec.ts b/tests/yup.spec.ts index 975d40e8..6a8d5462 100644 --- a/tests/yup.spec.ts +++ b/tests/yup.spec.ts @@ -452,4 +452,38 @@ describe('yup', () => { } }); }); + + it('properly generates custom directive values', async () => { + const schema = buildSchema(/* GraphQL */ ` + input UserCreateInput { + name: String! @constraint(startsWith: "Sir") + age: Int! @constraint(min: 0, max: 100) + } + directive @constraint(startsWith: String, min: Int, max: Int) on INPUT_FIELD_DEFINITION + `); + const result = await plugin( + schema, + [], + { + schema: 'yup', + directives: { + constraint: { + min: 'min', + max: 'max', + startsWith: ['matches', '/^$1/'], + }, + }, + }, + {} + ); + const wantContains = [ + // User Create Input + 'export function UserCreateInputSchema(): yup.SchemaOf {', + 'name: yup.string().defined().matches(/^Sir/),', + 'age: yup.number().defined().min(0).max(100)', + ]; + for (const wantContain of wantContains) { + expect(result.content).toContain(wantContain); + } + }); }); diff --git a/tests/zod.spec.ts b/tests/zod.spec.ts index f9f3fe41..c48008aa 100644 --- a/tests/zod.spec.ts +++ b/tests/zod.spec.ts @@ -637,4 +637,38 @@ describe('zod', () => { } }); }); + + it('properly generates custom directive values', async () => { + const schema = buildSchema(/* GraphQL */ ` + input UserCreateInput { + name: String! @constraint(startsWith: "Sir") + age: Int! @constraint(min: 0, max: 100) + } + directive @constraint(startsWith: String, min: Int, max: Int) on INPUT_FIELD_DEFINITION + `); + const result = await plugin( + schema, + [], + { + schema: 'zod', + directives: { + constraint: { + min: 'min', + max: 'max', + startsWith: ['regex', '/^$1/'], + }, + }, + }, + {} + ); + const wantContains = [ + // User Create Input + 'export function UserCreateInputSchema(): z.ZodObject> {', + 'name: z.string().regex(/^Sir/),', + 'age: z.number().min(0).max(100)', + ]; + for (const wantContain of wantContains) { + expect(result.content).toContain(wantContain); + } + }); });