Skip to content

Commit 58658c2

Browse files
committed
Merge branch 'main' into jcisneros/default-scalarTypeSchema
2 parents 8fc3c7d + d2a4240 commit 58658c2

File tree

16 files changed

+1520
-982
lines changed

16 files changed

+1520
-982
lines changed

README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ type: `ValidationSchemaExportType` default: `'function'`
203203

204204
Specify validation schema export type.
205205

206-
### `useEnumTypeAsDefault`
206+
### `useEnumTypeAsDefaultValue`
207207

208208
type: `boolean` default: `false`
209209

@@ -215,8 +215,6 @@ type: `NamingConventionMap` default: `{ enumValues: "change-case-all#pascalCase"
215215

216216
Uses the full path of the enum type as the default value instead of the stringified value.
217217

218-
Note: This option has not been tested with `namingConvention.transformUnderscore` and `namingConvention.typeNames` options and may not work as expected.
219-
220218
Related: https://the-guild.dev/graphql/codegen/docs/config-reference/naming-convention#namingconvention
221219

222220
### `directives`

package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"name": "graphql-codegen-typescript-validation-schema",
33
"type": "module",
4-
"version": "0.16.0",
5-
"packageManager": "[email protected].1",
4+
"version": "0.16.1",
5+
"packageManager": "[email protected].3",
66
"description": "GraphQL Code Generator plugin to generate form validation schema from your GraphQL schema",
77
"respository": {
88
"type": "git",
@@ -88,13 +88,13 @@
8888
"@antfu/eslint-config": "^3.0.0",
8989
"@graphql-codegen/cli": "5.0.3",
9090
"@graphql-codegen/typescript": "^4.0.0",
91-
"@tsconfig/recommended": "1.0.7",
91+
"@tsconfig/recommended": "1.0.8",
9292
"@types/graphlib": "^2.1.8",
93-
"@types/node": "^20.0.0",
94-
"eslint": "9.12.0",
93+
"@types/node": "^22.0.0",
94+
"eslint": "9.14.0",
9595
"jest": "29.7.0",
9696
"myzod": "1.12.0",
97-
"npm-run-all2": "6.2.3",
97+
"npm-run-all2": "7.0.1",
9898
"ts-dedent": "^2.2.0",
9999
"ts-jest": "29.2.5",
100100
"typescript": "5.6.3",

pnpm-lock.yaml

Lines changed: 1102 additions & 951 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ export interface ValidationSchemaPluginConfig extends TypeScriptPluginConfig {
241241
* - typescript
242242
* - graphql-codegen-validation-schema
243243
* config:
244-
* useEnumTypeAsDefault: true
244+
* useEnumTypeAsDefaultValue: true
245245
* ```
246246
*/
247247
useEnumTypeAsDefaultValue?: boolean

src/directive.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { ConstArgumentNode, ConstDirectiveNode, ConstValueNode } from 'graphql';
22
import type { DirectiveConfig, DirectiveObjectArguments } from './config.js';
3-
43
import { Kind, valueFromASTUntyped } from 'graphql';
4+
55
import { isConvertableRegexp } from './regexp.js';
66

77
export interface FormattedDirectiveConfig {

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import type { PluginFunction, Types } from '@graphql-codegen/plugin-helpers';
22
import type { GraphQLSchema } from 'graphql';
33
import type { ValidationSchemaPluginConfig } from './config.js';
44
import type { SchemaVisitor } from './types.js';
5-
65
import { transformSchemaAST } from '@graphql-codegen/schema-ast';
76
import { buildSchema, printSchema, visit } from 'graphql';
7+
88
import { isGeneratedByIntrospection, topologicalSortAST } from './graphql.js';
99
import { MyZodSchemaVisitor } from './myzod/index.js';
1010
import { ValibotSchemaVisitor } from './valibot/index.js';

src/myzod/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import type {
1010
TypeNode,
1111
UnionTypeDefinitionNode,
1212
} from 'graphql';
13+
1314
import type { ValidationSchemaPluginConfig } from '../config.js';
1415
import type { Visitor } from '../visitor.js';
15-
1616
import { resolveExternalModuleAndFn } from '@graphql-codegen/plugin-helpers';
1717
import { convertNameParts, DeclarationBlock, indent } from '@graphql-codegen/visitor-plugin-common';
1818
import {
@@ -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/valibot/index.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import type {
1111
UnionTypeDefinitionNode,
1212
} from 'graphql';
1313
import type { ValidationSchemaPluginConfig } from '../config.js';
14-
1514
import type { Visitor } from '../visitor.js';
15+
1616
import { DeclarationBlock, indent } from '@graphql-codegen/visitor-plugin-common';
1717
import { buildApiForValibot, formatDirectiveConfig } from '../directive.js';
1818
import {
@@ -224,8 +224,13 @@ function generateFieldTypeValibotSchema(config: ValidationSchemaPluginConfig, vi
224224

225225
const actions = actionsFromDirectives(config, field);
226226

227-
if (isNonNullType(parentType))
228-
return pipeSchemaAndActions(gen, actions); ;
227+
if (isNonNullType(parentType)) {
228+
if (visitor.shouldEmitAsNotAllowEmptyString(type.name.value)) {
229+
actions.push('v.minLength(1)');
230+
}
231+
232+
return pipeSchemaAndActions(gen, actions);
233+
}
229234

230235
return `v.nullish(${pipeSchemaAndActions(gen, actions)})`;
231236
}

src/visitor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import type {
66
ObjectTypeDefinitionNode,
77
} from 'graphql';
88
import type { ValidationSchemaPluginConfig } from './config.js';
9-
import { TsVisitor } from '@graphql-codegen/typescript';
109

10+
import { TsVisitor } from '@graphql-codegen/typescript';
1111
import {
1212
specifiedScalarTypes,
1313
} from 'graphql';

src/yup/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import type {
1010
TypeNode,
1111
UnionTypeDefinitionNode,
1212
} from 'graphql';
13+
1314
import type { ValidationSchemaPluginConfig } from '../config.js';
1415
import type { Visitor } from '../visitor.js';
15-
1616
import { resolveExternalModuleAndFn } from '@graphql-codegen/plugin-helpers';
1717
import { convertNameParts, DeclarationBlock, indent } from '@graphql-codegen/visitor-plugin-common';
1818
import {
@@ -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: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import type {
1010
TypeNode,
1111
UnionTypeDefinitionNode,
1212
} from 'graphql';
13+
1314
import type { ValidationSchemaPluginConfig } from '../config.js';
1415
import type { Visitor } from '../visitor.js';
15-
1616
import { resolveExternalModuleAndFn } from '@graphql-codegen/plugin-helpers';
1717
import { convertNameParts, DeclarationBlock, indent } from '@graphql-codegen/visitor-plugin-common';
1818
import {
@@ -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/directive.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import type { ConstArgumentNode, ConstDirectiveNode, ConstValueNode, NameNode } from 'graphql';
22
import type { DirectiveConfig, DirectiveObjectArguments } from '../src/config';
3-
43
import type {
54
FormattedDirectiveArguments,
65
FormattedDirectiveConfig,
76
FormattedDirectiveObjectArguments,
87
} from '../src/directive';
8+
99
import { Kind, parseConstValue } from 'graphql';
1010
import {
1111
buildApi,

tests/myzod.spec.ts

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1479,4 +1479,77 @@ describe('myzod', () => {
14791479
expect(result.content).toContain('ratio: myzod.number().default(0.5).optional().nullable()');
14801480
expect(result.content).toContain('isMember: myzod.boolean().default(true).optional().nullable()');
14811481
});
1482+
1483+
it('with default input values as enum types with 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+
},
1505+
{},
1506+
);
1507+
1508+
expect(result.content).toContain('export const PageTypeSchema = myzod.enum(PageType)');
1509+
expect(result.content).toContain('export function PageInputSchema(): myzod.Type<PageInput>');
1510+
1511+
expect(result.content).toContain('pageType: PageTypeSchema.default(PageType.Basic_Auth)');
1512+
expect(result.content).toContain('greeting: myzod.string().default("Hello").optional().nullable()');
1513+
expect(result.content).toContain('score: myzod.number().default(100).optional().nullable()');
1514+
expect(result.content).toContain('ratio: myzod.number().default(0.5).optional().nullable()');
1515+
expect(result.content).toContain('isMember: myzod.boolean().default(true).optional().nullable()');
1516+
});
1517+
1518+
it('with default input values as enum types with no underscores', async () => {
1519+
const schema = buildSchema(/* GraphQL */ `
1520+
enum PageType {
1521+
PUBLIC
1522+
BASIC_AUTH
1523+
}
1524+
input PageInput {
1525+
pageType: PageType! = BASIC_AUTH
1526+
greeting: String = "Hello"
1527+
score: Int = 100
1528+
ratio: Float = 0.5
1529+
isMember: Boolean = true
1530+
}
1531+
`);
1532+
const result = await plugin(
1533+
schema,
1534+
[],
1535+
{
1536+
schema: 'myzod',
1537+
importFrom: './types',
1538+
useEnumTypeAsDefaultValue: true,
1539+
namingConvention: {
1540+
transformUnderscore: true,
1541+
},
1542+
},
1543+
{},
1544+
);
1545+
1546+
expect(result.content).toContain('export const PageTypeSchema = myzod.enum(PageType)');
1547+
expect(result.content).toContain('export function PageInputSchema(): myzod.Type<PageInput>');
1548+
1549+
expect(result.content).toContain('pageType: PageTypeSchema.default(PageType.BasicAuth)');
1550+
expect(result.content).toContain('greeting: myzod.string().default("Hello").optional().nullable()');
1551+
expect(result.content).toContain('score: myzod.number().default(100).optional().nullable()');
1552+
expect(result.content).toContain('ratio: myzod.number().default(0.5).optional().nullable()');
1553+
expect(result.content).toContain('isMember: myzod.boolean().default(true).optional().nullable()');
1554+
});
14821555
});

0 commit comments

Comments
 (0)