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": "pnpm@9.12.1",
4+
"version": "0.16.1",
5+
"packageManager": "pnpm@9.12.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
}

0 commit comments

Comments
 (0)