Skip to content

Commit 43033d9

Browse files
committed
Merge branch 'main' into jcisneros/ns-imports
2 parents dbc0ef8 + d2a4240 commit 43033d9

File tree

15 files changed

+1519
-981
lines changed

15 files changed

+1519
-981
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
@@ -243,7 +243,7 @@ export interface ValidationSchemaPluginConfig extends TypeScriptPluginConfig {
243243
* - typescript
244244
* - graphql-codegen-validation-schema
245245
* config:
246-
* useEnumTypeAsDefault: true
246+
* useEnumTypeAsDefaultValue: true
247247
* ```
248248
*/
249249
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 {
@@ -294,10 +294,10 @@ function generateFieldTypeMyZodSchema(config: ValidationSchemaPluginConfig, visi
294294

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

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

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

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 {
@@ -228,8 +228,13 @@ function generateFieldTypeValibotSchema(config: ValidationSchemaPluginConfig, vi
228228

229229
const actions = actionsFromDirectives(config, field);
230230

231-
if (isNonNullType(parentType))
232-
return pipeSchemaAndActions(gen, actions); ;
231+
if (isNonNullType(parentType)) {
232+
if (visitor.shouldEmitAsNotAllowEmptyString(type.name.value)) {
233+
actions.push('v.minLength(1)');
234+
}
235+
236+
return pipeSchemaAndActions(gen, actions);
237+
}
233238

234239
return `v.nullish(${pipeSchemaAndActions(gen, actions)})`;
235240
}

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 {
@@ -297,10 +297,10 @@ function shapeFields(fields: readonly (FieldDefinitionNode | InputValueDefinitio
297297

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

302302
if (config.namingConvention?.enumValues)
303-
value = convertNameParts(defaultValue.value, resolveExternalModuleAndFn(config.namingConvention?.enumValues));
303+
value = convertNameParts(defaultValue.value, resolveExternalModuleAndFn(config.namingConvention?.enumValues), config?.namingConvention?.transformUnderscore);
304304

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

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 {
@@ -310,10 +310,10 @@ function generateFieldTypeZodSchema(config: ValidationSchemaPluginConfig, visito
310310

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

315315
if (config.namingConvention?.enumValues)
316-
value = convertNameParts(defaultValue.value, resolveExternalModuleAndFn(config.namingConvention?.enumValues));
316+
value = convertNameParts(defaultValue.value, resolveExternalModuleAndFn(config.namingConvention?.enumValues), config.namingConvention?.transformUnderscore);
317317

318318
appliedDirectivesGen = `${appliedDirectivesGen}.default(${type.name.value}.${value})`;
319319
}

0 commit comments

Comments
 (0)