Skip to content

Commit 618956b

Browse files
committed
fix(enum): propagate namingConvention.transformUnderscore to convertNameParts
1 parent 5772bb8 commit 618956b

File tree

6 files changed

+231
-6
lines changed

6 files changed

+231
-6
lines changed

src/myzod/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,10 +290,10 @@ function generateFieldTypeMyZodSchema(config: ValidationSchemaPluginConfig, visi
290290

291291
if (defaultValue?.kind === Kind.STRING || defaultValue?.kind === Kind.ENUM) {
292292
if (config.useEnumTypeAsDefaultValue && defaultValue?.kind !== Kind.STRING) {
293-
let value = convertNameParts(defaultValue.value, resolveExternalModuleAndFn('change-case-all#pascalCase'));
293+
let value = convertNameParts(defaultValue.value, resolveExternalModuleAndFn('change-case-all#pascalCase'), config?.namingConvention?.transformUnderscore);
294294

295295
if (config.namingConvention?.enumValues)
296-
value = convertNameParts(defaultValue.value, resolveExternalModuleAndFn(config.namingConvention?.enumValues));
296+
value = convertNameParts(defaultValue.value, resolveExternalModuleAndFn(config.namingConvention?.enumValues), config?.namingConvention?.transformUnderscore);
297297

298298
appliedDirectivesGen = `${appliedDirectivesGen}.default(${visitor.convertName(type.name.value)}.${value})`;
299299
}

src/yup/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,10 +292,10 @@ function shapeFields(fields: readonly (FieldDefinitionNode | InputValueDefinitio
292292

293293
if (defaultValue?.kind === Kind.STRING || defaultValue?.kind === Kind.ENUM) {
294294
if (config.useEnumTypeAsDefaultValue && defaultValue?.kind !== Kind.STRING) {
295-
let value = convertNameParts(defaultValue.value, resolveExternalModuleAndFn('change-case-all#pascalCase'));
295+
let value = convertNameParts(defaultValue.value, resolveExternalModuleAndFn('change-case-all#pascalCase'), config?.namingConvention?.transformUnderscore);
296296

297297
if (config.namingConvention?.enumValues)
298-
value = convertNameParts(defaultValue.value, resolveExternalModuleAndFn(config.namingConvention?.enumValues));
298+
value = convertNameParts(defaultValue.value, resolveExternalModuleAndFn(config.namingConvention?.enumValues), config?.namingConvention?.transformUnderscore);
299299

300300
fieldSchema = `${fieldSchema}.default(${visitor.convertName(field.name.value)}.${value})`;
301301
}

src/zod/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,10 +306,10 @@ function generateFieldTypeZodSchema(config: ValidationSchemaPluginConfig, visito
306306

307307
if (defaultValue?.kind === Kind.STRING || defaultValue?.kind === Kind.ENUM) {
308308
if (config.useEnumTypeAsDefaultValue && defaultValue?.kind !== Kind.STRING) {
309-
let value = convertNameParts(defaultValue.value, resolveExternalModuleAndFn('change-case-all#pascalCase'));
309+
let value = convertNameParts(defaultValue.value, resolveExternalModuleAndFn('change-case-all#pascalCase'), config.namingConvention?.transformUnderscore);
310310

311311
if (config.namingConvention?.enumValues)
312-
value = convertNameParts(defaultValue.value, resolveExternalModuleAndFn(config.namingConvention?.enumValues));
312+
value = convertNameParts(defaultValue.value, resolveExternalModuleAndFn(config.namingConvention?.enumValues), config.namingConvention?.transformUnderscore);
313313

314314
appliedDirectivesGen = `${appliedDirectivesGen}.default(${type.name.value}.${value})`;
315315
}

tests/myzod.spec.ts

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1444,4 +1444,77 @@ describe('myzod', () => {
14441444
expect(result.content).toContain('ratio: myzod.number().default(0.5).optional().nullable()');
14451445
expect(result.content).toContain('isMember: myzod.boolean().default(true).optional().nullable()');
14461446
});
1447+
1448+
it('with default input values as enum types with underscores', async () => {
1449+
const schema = buildSchema(/* GraphQL */ `
1450+
enum PageType {
1451+
PUBLIC
1452+
BASIC_AUTH
1453+
}
1454+
input PageInput {
1455+
pageType: PageType! = BASIC_AUTH
1456+
greeting: String = "Hello"
1457+
score: Int = 100
1458+
ratio: Float = 0.5
1459+
isMember: Boolean = true
1460+
}
1461+
`);
1462+
const result = await plugin(
1463+
schema,
1464+
[],
1465+
{
1466+
schema: 'myzod',
1467+
importFrom: './types',
1468+
useEnumTypeAsDefaultValue: true,
1469+
},
1470+
{},
1471+
);
1472+
1473+
expect(result.content).toContain('export const PageTypeSchema = myzod.enum(PageType)');
1474+
expect(result.content).toContain('export function PageInputSchema(): myzod.Type<PageInput>');
1475+
1476+
expect(result.content).toContain('pageType: PageTypeSchema.default(PageType.Basic_Auth)');
1477+
expect(result.content).toContain('greeting: myzod.string().default("Hello").optional().nullable()');
1478+
expect(result.content).toContain('score: myzod.number().default(100).optional().nullable()');
1479+
expect(result.content).toContain('ratio: myzod.number().default(0.5).optional().nullable()');
1480+
expect(result.content).toContain('isMember: myzod.boolean().default(true).optional().nullable()');
1481+
});
1482+
1483+
it('with default input values as enum types with no underscores', async () => {
1484+
const schema = buildSchema(/* GraphQL */ `
1485+
enum PageType {
1486+
PUBLIC
1487+
BASIC_AUTH
1488+
}
1489+
input PageInput {
1490+
pageType: PageType! = BASIC_AUTH
1491+
greeting: String = "Hello"
1492+
score: Int = 100
1493+
ratio: Float = 0.5
1494+
isMember: Boolean = true
1495+
}
1496+
`);
1497+
const result = await plugin(
1498+
schema,
1499+
[],
1500+
{
1501+
schema: 'myzod',
1502+
importFrom: './types',
1503+
useEnumTypeAsDefaultValue: true,
1504+
namingConvention: {
1505+
transformUnderscore: true
1506+
}
1507+
},
1508+
{},
1509+
);
1510+
1511+
expect(result.content).toContain('export const PageTypeSchema = myzod.enum(PageType)');
1512+
expect(result.content).toContain('export function PageInputSchema(): myzod.Type<PageInput>');
1513+
1514+
expect(result.content).toContain('pageType: PageTypeSchema.default(PageType.BasicAuth)');
1515+
expect(result.content).toContain('greeting: myzod.string().default("Hello").optional().nullable()');
1516+
expect(result.content).toContain('score: myzod.number().default(100).optional().nullable()');
1517+
expect(result.content).toContain('ratio: myzod.number().default(0.5).optional().nullable()');
1518+
expect(result.content).toContain('isMember: myzod.boolean().default(true).optional().nullable()');
1519+
});
14471520
});

tests/yup.spec.ts

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1470,4 +1470,81 @@ describe('yup', () => {
14701470
expect(result.content).toContain('ratio: yup.number().defined().nullable().default(0.5).optional()');
14711471
expect(result.content).toContain('isMember: yup.boolean().defined().nullable().default(true).optional()');
14721472
});
1473+
1474+
it('with default input values as enum types with underscores', async () => {
1475+
const schema = buildSchema(/* GraphQL */ `
1476+
enum PageType {
1477+
PUBLIC
1478+
BASIC_AUTH
1479+
}
1480+
input PageInput {
1481+
pageType: PageType! = BASIC_AUTH
1482+
greeting: String = "Hello"
1483+
score: Int = 100
1484+
ratio: Float = 0.5
1485+
isMember: Boolean = true
1486+
}
1487+
`);
1488+
const result = await plugin(
1489+
schema,
1490+
[],
1491+
{
1492+
schema: 'yup',
1493+
importFrom: './types',
1494+
useEnumTypeAsDefaultValue: true,
1495+
},
1496+
{},
1497+
);
1498+
1499+
expect(result.content).toContain(
1500+
'export const PageTypeSchema = yup.string<PageType>().oneOf(Object.values(PageType)).defined()',
1501+
);
1502+
expect(result.content).toContain('export function PageInputSchema(): yup.ObjectSchema<PageInput>');
1503+
1504+
expect(result.content).toContain('pageType: PageTypeSchema.nonNullable().default(PageType.Basic_Auth)');
1505+
expect(result.content).toContain('greeting: yup.string().defined().nullable().default("Hello").optional()');
1506+
expect(result.content).toContain('score: yup.number().defined().nullable().default(100).optional()');
1507+
expect(result.content).toContain('ratio: yup.number().defined().nullable().default(0.5).optional()');
1508+
expect(result.content).toContain('isMember: yup.boolean().defined().nullable().default(true).optional()');
1509+
});
1510+
1511+
it('with default input values as enum types with no underscores', async () => {
1512+
const schema = buildSchema(/* GraphQL */ `
1513+
enum PageType {
1514+
PUBLIC
1515+
BASIC_AUTH
1516+
}
1517+
input PageInput {
1518+
pageType: PageType! = BASIC_AUTH
1519+
greeting: String = "Hello"
1520+
score: Int = 100
1521+
ratio: Float = 0.5
1522+
isMember: Boolean = true
1523+
}
1524+
`);
1525+
const result = await plugin(
1526+
schema,
1527+
[],
1528+
{
1529+
schema: 'yup',
1530+
importFrom: './types',
1531+
useEnumTypeAsDefaultValue: true,
1532+
namingConvention: {
1533+
transformUnderscore: true,
1534+
}
1535+
},
1536+
{},
1537+
);
1538+
1539+
expect(result.content).toContain(
1540+
'export const PageTypeSchema = yup.string<PageType>().oneOf(Object.values(PageType)).defined()',
1541+
);
1542+
expect(result.content).toContain('export function PageInputSchema(): yup.ObjectSchema<PageInput>');
1543+
1544+
expect(result.content).toContain('pageType: PageTypeSchema.nonNullable().default(PageType.BasicAuth)');
1545+
expect(result.content).toContain('greeting: yup.string().defined().nullable().default("Hello").optional()');
1546+
expect(result.content).toContain('score: yup.number().defined().nullable().default(100).optional()');
1547+
expect(result.content).toContain('ratio: yup.number().defined().nullable().default(0.5).optional()');
1548+
expect(result.content).toContain('isMember: yup.boolean().defined().nullable().default(true).optional()');
1549+
});
14731550
});

tests/zod.spec.ts

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,81 @@ describe('zod', () => {
566566
expect(result.content).toContain('isMember: z.boolean().default(true).nullish()');
567567
});
568568

569+
it('with default input values as enum types with underscores', async () => {
570+
const schema = buildSchema(/* GraphQL */ `
571+
enum PageType {
572+
BASIC_AUTH
573+
PUBLIC
574+
}
575+
input PageInput {
576+
pageType: PageType! = BASIC_AUTH
577+
greeting: String = "Hello"
578+
score: Int = 100
579+
ratio: Float = 0.5
580+
isMember: Boolean = true
581+
}
582+
`);
583+
const result = await plugin(
584+
schema,
585+
[],
586+
{
587+
schema: 'zod',
588+
importFrom: './types',
589+
useEnumTypeAsDefaultValue: true,
590+
},
591+
{
592+
},
593+
);
594+
595+
expect(result.content).toContain('export const PageTypeSchema = z.nativeEnum(PageType)');
596+
expect(result.content).toContain('export function PageInputSchema(): z.ZodObject<Properties<PageInput>>');
597+
598+
expect(result.content).toContain('pageType: PageTypeSchema.default(PageType.Basic_Auth)');
599+
expect(result.content).toContain('greeting: z.string().default("Hello").nullish()');
600+
expect(result.content).toContain('score: z.number().default(100).nullish()');
601+
expect(result.content).toContain('ratio: z.number().default(0.5).nullish()');
602+
expect(result.content).toContain('isMember: z.boolean().default(true).nullish()');
603+
});
604+
605+
it('with default input values as enum types with no underscores', async () => {
606+
const schema = buildSchema(/* GraphQL */ `
607+
enum PageType {
608+
BASIC_AUTH
609+
PUBLIC
610+
}
611+
input PageInput {
612+
pageType: PageType! = BASIC_AUTH
613+
greeting: String = "Hello"
614+
score: Int = 100
615+
ratio: Float = 0.5
616+
isMember: Boolean = true
617+
}
618+
`);
619+
const result = await plugin(
620+
schema,
621+
[],
622+
{
623+
schema: 'zod',
624+
importFrom: './types',
625+
useEnumTypeAsDefaultValue: true,
626+
namingConvention: {
627+
transformUnderscore: true,
628+
}
629+
},
630+
{
631+
},
632+
);
633+
634+
expect(result.content).toContain('export const PageTypeSchema = z.nativeEnum(PageType)');
635+
expect(result.content).toContain('export function PageInputSchema(): z.ZodObject<Properties<PageInput>>');
636+
637+
expect(result.content).toContain('pageType: PageTypeSchema.default(PageType.BasicAuth)');
638+
expect(result.content).toContain('greeting: z.string().default("Hello").nullish()');
639+
expect(result.content).toContain('score: z.number().default(100).nullish()');
640+
expect(result.content).toContain('ratio: z.number().default(0.5).nullish()');
641+
expect(result.content).toContain('isMember: z.boolean().default(true).nullish()');
642+
});
643+
569644
it('with default input values', async () => {
570645
const schema = buildSchema(/* GraphQL */ `
571646
enum PageType {

0 commit comments

Comments
 (0)