Skip to content

Commit 1ca4334

Browse files
committed
fully implemented myzod with test closes #25
1 parent 97da322 commit 1ca4334

File tree

4 files changed

+389
-12
lines changed

4 files changed

+389
-12
lines changed

src/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { TypeScriptPluginConfig } from '@graphql-codegen/typescript';
22

3-
export type ValidationSchema = 'yup' | 'zod';
3+
export type ValidationSchema = 'yup' | 'zod' | 'myzod';
44

55
export interface DirectiveConfig {
66
[directive: string]: {

src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { ZodSchemaVisitor } from './zod/index';
2+
import { MyZodSchemaVisitor } from './myzod/index';
23
import { transformSchemaAST } from '@graphql-codegen/schema-ast';
34
import { YupSchemaVisitor } from './yup/index';
45
import { ValidationSchemaPluginConfig } from './config';
@@ -30,6 +31,8 @@ export const plugin: PluginFunction<ValidationSchemaPluginConfig, Types.ComplexP
3031
const schemaVisitor = (schema: GraphQLSchema, config: ValidationSchemaPluginConfig) => {
3132
if (config?.schema === 'zod') {
3233
return ZodSchemaVisitor(schema, config);
34+
} else if (config?.schema === 'myzod') {
35+
return MyZodSchemaVisitor(schema, config);
3336
}
3437
return YupSchemaVisitor(schema, config);
3538
};

src/myzod/index.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { buildApi, formatDirectiveConfig } from '../directive';
1515
const importZod = `import myzod from 'myzod'`;
1616
const anySchema = `definedNonNullAnySchema`;
1717

18-
export const ZodSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchemaPluginConfig) => {
18+
export const MyZodSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchemaPluginConfig) => {
1919
const tsVisitor = new TsVisitor(schema, config);
2020

2121
const importTypes: string[] = [];
@@ -27,7 +27,6 @@ export const ZodSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchema
2727
}
2828
return [importZod];
2929
},
30-
3130
initialEmit: (): string =>
3231
'\n' +
3332
[
@@ -75,15 +74,16 @@ export const ZodSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchema
7574
EnumTypeDefinition: (node: EnumTypeDefinitionNode) => {
7675
const enumname = tsVisitor.convertName(node.name.value);
7776
importTypes.push(enumname);
78-
/*
77+
// z.enum are basically myzod.literals
7978
if (config.enumsAsTypes) {
8079
return new DeclarationBlock({})
8180
.export()
82-
.asKind('const')
81+
.asKind('type')
8382
.withName(`${enumname}Schema`)
84-
.withContent(`z.enum([${node.values?.map(enumOption => `'${enumOption.name.value}'`).join(', ')}])`).string;
83+
.withContent(`myzod.literals(${node.values?.map(enumOption => `'${enumOption.name.value}'`).join(', ')})`)
84+
.string;
8585
}
86-
*/
86+
8787
return new DeclarationBlock({})
8888
.export()
8989
.asKind('const')
@@ -117,7 +117,7 @@ const generateInputObjectFieldTypeMyZodSchema = (
117117
if (!isNonNullType(parentType)) {
118118
const arrayGen = `myzod.array(${maybeLazy(type.type, gen)})`;
119119
const maybeLazyGen = applyDirectives(config, field, arrayGen);
120-
return `${maybeLazyGen}.optional()`; // to make it equivalent to nullish: `${maybeLazyGen}.optional().optional().or(nullable())`;
120+
return `${maybeLazyGen}.optional().nullable()`;
121121
}
122122
return `myzod.array(${maybeLazy(type.type, gen)})`;
123123
}
@@ -128,7 +128,7 @@ const generateInputObjectFieldTypeMyZodSchema = (
128128
if (isNamedType(type)) {
129129
const gen = generateNameNodeMyZodSchema(config, tsVisitor, schema, type.name);
130130
if (isListType(parentType)) {
131-
return `${gen}.nullable()`; //TODO: Test this later
131+
return `${gen}.nullable()`;
132132
}
133133
const appliedDirectivesGen = applyDirectives(config, field, gen);
134134
if (isNonNullType(parentType)) {
@@ -139,15 +139,14 @@ const generateInputObjectFieldTypeMyZodSchema = (
139139
return appliedDirectivesGen;
140140
}
141141
if (isListType(parentType)) {
142-
return `${appliedDirectivesGen}.nullable()`; //TODO: Test this later
142+
return `${appliedDirectivesGen}.nullable()`;
143143
}
144-
return `${appliedDirectivesGen}.optional()`; // to make it equivalent to nullish: `${appliedDirectivesGen}.optional().or(nullable())`;
144+
return `${appliedDirectivesGen}.optional().nullable()`;
145145
}
146146
console.warn('unhandled type:', type);
147147
return '';
148148
};
149149

150-
// TODO: Find out how it works and implement it
151150
const applyDirectives = (
152151
config: ValidationSchemaPluginConfig,
153152
field: InputValueDefinitionNode,

0 commit comments

Comments
 (0)