Skip to content

Commit 2820f7b

Browse files
authored
Merge pull request #185 from elijaholmos/patch-falsy-directive-values
fix(directive): dropping falsy directive args
2 parents da8a426 + 15fb857 commit 2820f7b

File tree

4 files changed

+103
-1
lines changed

4 files changed

+103
-1
lines changed

src/directive.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ const applyArgToApiSchemaTemplate = (template: string, apiArgs: any[]): string =
162162
const placeholder = match[0]; // `$1`
163163
const idx = parseInt(match[1], 10) - 1; // start with `1 - 1`
164164
const apiArg = apiArgs[idx];
165-
if (!apiArg) {
165+
if (apiArg === undefined) {
166166
template = template.replace(placeholder, '');
167167
continue;
168168
}

tests/myzod.spec.ts

+34
Original file line numberDiff line numberDiff line change
@@ -539,4 +539,38 @@ describe('myzod', () => {
539539
}
540540
});
541541
});
542+
543+
it('properly generates custom directive values', async () => {
544+
const schema = buildSchema(/* GraphQL */ `
545+
input UserCreateInput {
546+
name: String! @constraint(startsWith: "Sir")
547+
age: Int! @constraint(min: 0, max: 100)
548+
}
549+
directive @constraint(startsWith: String, min: Int, max: Int) on INPUT_FIELD_DEFINITION
550+
`);
551+
const result = await plugin(
552+
schema,
553+
[],
554+
{
555+
schema: 'myzod',
556+
directives: {
557+
constraint: {
558+
min: 'min',
559+
max: 'max',
560+
startsWith: ['pattern', '/^$1/'],
561+
},
562+
},
563+
},
564+
{}
565+
);
566+
const wantContains = [
567+
// User Create Input
568+
'export function UserCreateInputSchema(): myzod.Type<UserCreateInput> {',
569+
'name: myzod.string().pattern(/^Sir/),',
570+
'age: myzod.number().min(0).max(100)',
571+
];
572+
for (const wantContain of wantContains) {
573+
expect(result.content).toContain(wantContain);
574+
}
575+
});
542576
});

tests/yup.spec.ts

+34
Original file line numberDiff line numberDiff line change
@@ -452,4 +452,38 @@ describe('yup', () => {
452452
}
453453
});
454454
});
455+
456+
it('properly generates custom directive values', async () => {
457+
const schema = buildSchema(/* GraphQL */ `
458+
input UserCreateInput {
459+
name: String! @constraint(startsWith: "Sir")
460+
age: Int! @constraint(min: 0, max: 100)
461+
}
462+
directive @constraint(startsWith: String, min: Int, max: Int) on INPUT_FIELD_DEFINITION
463+
`);
464+
const result = await plugin(
465+
schema,
466+
[],
467+
{
468+
schema: 'yup',
469+
directives: {
470+
constraint: {
471+
min: 'min',
472+
max: 'max',
473+
startsWith: ['matches', '/^$1/'],
474+
},
475+
},
476+
},
477+
{}
478+
);
479+
const wantContains = [
480+
// User Create Input
481+
'export function UserCreateInputSchema(): yup.SchemaOf<UserCreateInput> {',
482+
'name: yup.string().defined().matches(/^Sir/),',
483+
'age: yup.number().defined().min(0).max(100)',
484+
];
485+
for (const wantContain of wantContains) {
486+
expect(result.content).toContain(wantContain);
487+
}
488+
});
455489
});

tests/zod.spec.ts

+34
Original file line numberDiff line numberDiff line change
@@ -637,4 +637,38 @@ describe('zod', () => {
637637
}
638638
});
639639
});
640+
641+
it('properly generates custom directive values', async () => {
642+
const schema = buildSchema(/* GraphQL */ `
643+
input UserCreateInput {
644+
name: String! @constraint(startsWith: "Sir")
645+
age: Int! @constraint(min: 0, max: 100)
646+
}
647+
directive @constraint(startsWith: String, min: Int, max: Int) on INPUT_FIELD_DEFINITION
648+
`);
649+
const result = await plugin(
650+
schema,
651+
[],
652+
{
653+
schema: 'zod',
654+
directives: {
655+
constraint: {
656+
min: 'min',
657+
max: 'max',
658+
startsWith: ['regex', '/^$1/'],
659+
},
660+
},
661+
},
662+
{}
663+
);
664+
const wantContains = [
665+
// User Create Input
666+
'export function UserCreateInputSchema(): z.ZodObject<Properties<UserCreateInput>> {',
667+
'name: z.string().regex(/^Sir/),',
668+
'age: z.number().min(0).max(100)',
669+
];
670+
for (const wantContain of wantContains) {
671+
expect(result.content).toContain(wantContain);
672+
}
673+
});
640674
});

0 commit comments

Comments
 (0)