Skip to content

Added config opts: separateSchemaObject, inputDiscriminator, zodv4 schema, and lazyStrategy #1128

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 82 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ type: `ValidationSchema` default: `'yup'`

Specify generete validation schema you want.

You can specify `yup` or `zod` or `myzod`.
You can specify `yup` or `zod` or `zodv4` or `myzod`.

```yml
generates:
Expand Down Expand Up @@ -87,6 +87,77 @@ import { GeneratedInput } from './graphql'
/* generates validation schema here */
```

### `inputDiscriminator`

type: `string`

When provided, adds a discriminator to the input schemas with a literal value of the input type name.

```yml
generates:
path/to/graphql.ts:
plugins:
- typescript
path/to/validation.ts:
plugins:
- typescript-validation-schema
config:
inputDiscriminator: __kind # discriminator key
```

### `lazyStrategy`

type: `LazyStrategy` default: `'all'`

Specify if lazy() => should be added all references or only circular references.

You can specify `all` or `circular`.

```yml
generates:
path/to/graphql.ts:
plugins:
- typescript
path/to/validation.ts:
plugins:
- typescript-validation-schema
config:
lazyStrategy: circular
```

### `separateSchemaObject`

type: `boolean` default `false`

Will separate the schema object from the object definition when set to true.

```yml
generates:
path/to/graphql.ts:
plugins:
- typescript
path/to/validation.ts:
plugins:
- typescript-validation-schema
config:
schemaObjectSeparate: true
```

Then the generator generates code like below.

```ts
/* When set to false */
// If validationSchemaExportType is 'const' and Zod as an example
export const Schema: z.ZodObject<SchemaType> = z.object({foo: bar})


/* When set to false */
// If validationSchemaExportType is 'const' and Zod as an example
// While these seem the same, Zod for example will add [x: string]: unknown to the first example while this will prevent that.
export const schemaObject: SchemaType = {foo: bar}
export const Schema = z.object(schemaObject)
```

### `schemaNamespacedImportName`

type: `string`
Expand Down Expand Up @@ -215,6 +286,16 @@ config:
Email: z.string().email()
```

#### zodv4 schema

```yml
config:
schema: zodv4
scalarSchemas:
Date: z.date()
Email: z.email()
```

### `defaultScalarTypeSchema`

type: `string`
Expand Down
56 changes: 55 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import type { TypeScriptPluginConfig } from '@graphql-codegen/typescript';
import type { NamingConventionMap } from '@graphql-codegen/visitor-plugin-common';

export type ValidationSchema = 'yup' | 'zod' | 'myzod' | 'valibot';
export type ValidationSchema = 'yup' | 'zod' | 'zodv4' | 'myzod' | 'valibot';
export type ValidationSchemaExportType = 'function' | 'const';
export type LazyStrategy = 'all' | 'circular'

export interface DirectiveConfig {
[directive: string]: {
Expand Down Expand Up @@ -54,6 +55,59 @@ export interface ValidationSchemaPluginConfig extends TypeScriptPluginConfig {
* ```
*/
importFrom?: string
/**
* @description When provided, adds a discriminator to the input schemas with a literal value of the input type name.
* @default ""
*
* @exampleMarkdown
* ```yml
* generates:
* path/to/types.ts:
* plugins:
* - typescript
* path/to/schemas.ts:
* plugins:
* - graphql-codegen-validation-schema
* config:
* schema: yup
* inputDiscriminator: __kind
* ```
*/
inputDiscriminator?: string
/**
* @description Setting to determine when to set a property to lazy. 'Circular' will only use lazy for circular references. 'All' will set lazy for all properties referencing another schema.
* @default all
*
* @exampleMarkdown
* ```yml
* generates:
* path/to/file.ts:
* plugins:
* - typescript
* - graphql-codegen-validation-schema
* config:
* schema: yup
* lazy: circular
* ```
*/
lazyStrategy?: LazyStrategy;
/**
* @description Will separate the schema object from the object definition when set to true.
* @default false
*
* @exampleMarkdown
* ```yml
* generates:
* path/to/file.ts:
* plugins:
* - typescript
* - graphql-codegen-validation-schema
* config:
* schema: yup
* schemaObjectSeparate: true
* ```
*/
separateSchemaObject?: boolean;
/**
* @description If defined, will use named imports from the specified module (defined in `importFrom`)
* rather than individual imports for each type.
Expand Down
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { MyZodSchemaVisitor } from './myzod/index.js';
import { ValibotSchemaVisitor } from './valibot/index.js';
import { YupSchemaVisitor } from './yup/index.js';
import { ZodSchemaVisitor } from './zod/index.js';
import { Zodv4SchemaVisitor } from './zodv4/index.js';

export const plugin: PluginFunction<ValidationSchemaPluginConfig, Types.ComplexPluginOutput> = (
schema: GraphQLSchema,
Expand All @@ -32,6 +33,8 @@ export const plugin: PluginFunction<ValidationSchemaPluginConfig, Types.ComplexP
function schemaVisitor(schema: GraphQLSchema, config: ValidationSchemaPluginConfig): SchemaVisitor {
if (config?.schema === 'zod')
return new ZodSchemaVisitor(schema, config);
else if (config?.schema === 'zodv4')
return new Zodv4SchemaVisitor(schema, config);
else if (config?.schema === 'myzod')
return new MyZodSchemaVisitor(schema, config);
else if (config?.schema === 'valibot')
Expand Down
Loading