Skip to content

Commit eee96cf

Browse files
committed
Refactor empty string handling across schemas
- Replace direct checks for `notAllowEmptyString` configuration with a new method `shouldEmitAsNotAllowEmptyString` in the `Visitor` class. - The new method `shouldEmitAsNotAllowEmptyString` encapsulates the check for the `notAllowEmptyString` configuration and the type of the variable, making the code cleaner and more readable. - The changes have been made across `myzod`, `yup`, and `zod` schemas.
1 parent 6940b13 commit eee96cf

File tree

4 files changed

+25
-12
lines changed

4 files changed

+25
-12
lines changed

src/myzod/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -212,9 +212,8 @@ const generateFieldTypeMyZodSchema = (
212212
}
213213
const appliedDirectivesGen = applyDirectives(config, field, gen);
214214
if (isNonNullType(parentType)) {
215-
if (config.notAllowEmptyString === true) {
216-
const tsType = visitor.getScalarType(type.name.value);
217-
if (tsType === 'string') return `${gen}.min(1)`;
215+
if (visitor.shouldEmitAsNotAllowEmptyString(type.name.value)) {
216+
return `${gen}.min(1)`;
218217
}
219218
return appliedDirectivesGen;
220219
}

src/visitor.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
import { TsVisitor } from '@graphql-codegen/typescript';
2-
import { GraphQLSchema, NameNode } from 'graphql';
2+
import { GraphQLSchema, NameNode, specifiedScalarTypes } from 'graphql';
33

44
import { ValidationSchemaPluginConfig } from './config';
55

66
export class Visitor extends TsVisitor {
77
constructor(
88
private scalarDirection: 'input' | 'output' | 'both',
99
private schema: GraphQLSchema,
10-
config: ValidationSchemaPluginConfig
10+
private pluginConfig: ValidationSchemaPluginConfig
1111
) {
12-
super(schema, config);
12+
super(schema, pluginConfig);
13+
}
14+
15+
private isSpecifiedScalarName(scalarName: string) {
16+
return specifiedScalarTypes.some(({ name }) => name === scalarName);
1317
}
1418

1519
public getType(name: string) {
@@ -34,4 +38,16 @@ export class Visitor extends TsVisitor {
3438
}
3539
return this.scalars[scalarName][this.scalarDirection];
3640
}
41+
42+
public shouldEmitAsNotAllowEmptyString(name: string): boolean {
43+
if (this.pluginConfig.notAllowEmptyString !== true) {
44+
return false;
45+
}
46+
const typ = this.getType(name);
47+
if (typ?.astNode?.kind !== 'ScalarTypeDefinition' && !this.isSpecifiedScalarName(name)) {
48+
return false;
49+
}
50+
const tsType = this.getScalarType(name);
51+
return tsType === 'string';
52+
}
3753
}

src/yup/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -262,9 +262,8 @@ const generateFieldTypeYupSchema = (
262262
if (isNamedType(type)) {
263263
const gen = generateNameNodeYupSchema(config, visitor, type.name);
264264
if (isNonNullType(parentType)) {
265-
if (config.notAllowEmptyString === true) {
266-
const tsType = visitor.getScalarType(type.name.value);
267-
if (tsType === 'string') return `${gen}.required()`;
265+
if (visitor.shouldEmitAsNotAllowEmptyString(type.name.value)) {
266+
return `${gen}.required()`;
268267
}
269268
return `${gen}.nonNullable()`;
270269
}

src/zod/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -222,9 +222,8 @@ const generateFieldTypeZodSchema = (
222222
}
223223
const appliedDirectivesGen = applyDirectives(config, field, gen);
224224
if (isNonNullType(parentType)) {
225-
if (config.notAllowEmptyString === true) {
226-
const tsType = visitor.getScalarType(type.name.value);
227-
if (tsType === 'string') return `${appliedDirectivesGen}.min(1)`;
225+
if (visitor.shouldEmitAsNotAllowEmptyString(type.name.value)) {
226+
return `${appliedDirectivesGen}.min(1)`;
228227
}
229228
return appliedDirectivesGen;
230229
}

0 commit comments

Comments
 (0)