Gathering detailed insights and metrics for @acaldas/graphql-codegen-typescript-validation-schema
Gathering detailed insights and metrics for @acaldas/graphql-codegen-typescript-validation-schema
Gathering detailed insights and metrics for @acaldas/graphql-codegen-typescript-validation-schema
Gathering detailed insights and metrics for @acaldas/graphql-codegen-typescript-validation-schema
npm install @acaldas/graphql-codegen-typescript-validation-schema
Typescript
Module System
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
5
1
22
GraphQL code generator plugin to generate form validation schema from your GraphQL schema.
Start by installing this plugin and write simple plugin config;
1$ npm i graphql-codegen-typescript-validation-schema
1generates: 2 path/to/graphql.ts: 3 plugins: 4 - typescript 5 - typescript-validation-schema # specify to use this plugin 6 config: 7 # You can put the config for typescript plugin here 8 # see: https://www.graphql-code-generator.com/plugins/typescript 9 strictScalars: true 10 # Overrides built-in ID scalar to both input and output types as string. 11 # see: https://the-guild.dev/graphql/codegen/plugins/typescript/typescript#scalars 12 scalars: 13 ID: string 14 # You can also write the config for this plugin together 15 schema: yup # or zod
It is recommended to write scalars
config for built-in type ID
, as in the yaml example shown above. For more information: #375
You can check example directory if you want to see more complex config example or how is generated some files.
The Q&A for each schema is written in the README in the respective example directory.
schema
type: ValidationSchema
default: 'yup'
Specify generete validation schema you want.
You can specify yup
or zod
or myzod
.
1generates: 2 path/to/graphql.ts: 3 plugins: 4 - typescript 5 - typescript-validation-schema 6 config: 7 schema: yup
importFrom
type: string
When provided, import types from the generated typescript types file path. if not given, omit import statement.
1generates: 2 path/to/graphql.ts: 3 plugins: 4 - typescript 5 path/to/validation.ts: 6 plugins: 7 - typescript-validation-schema 8 config: 9 importFrom: ./graphql # path for generated ts code
Then the generator generates code with import statement like below.
1import { GeneratedInput } from './graphql' 2 3/* generates validation schema here */
useTypeImports
type: boolean
default: false
Will use import type {}
rather than import {}
when importing generated TypeScript types.
This gives compatibility with TypeScript's "importsNotUsedAsValues": "error" option.
Should used in conjunction with importFrom
option.
typesPrefix
type: string
default: (empty)
Prefixes all import types from generated typescript type.
1generates: 2 path/to/graphql.ts: 3 plugins: 4 - typescript 5 path/to/validation.ts: 6 plugins: 7 - typescript-validation-schema 8 config: 9 typesPrefix: I 10 importFrom: ./graphql # path for generated ts code
Then the generator generates code with import statement like below.
1import { IGeneratedInput } from './graphql' 2 3/* generates validation schema here */
typesSuffix
type: string
default: (empty)
Suffixes all import types from generated typescript type.
1generates: 2 path/to/graphql.ts: 3 plugins: 4 - typescript 5 path/to/validation.ts: 6 plugins: 7 - typescript-validation-schema 8 config: 9 typesSuffix: I 10 importFrom: ./graphql # path for generated ts code
Then the generator generates code with import statement like below.
1import { GeneratedInputI } from './graphql' 2 3/* generates validation schema here */
enumsAsTypes
type: boolean
default: false
Generates enum as TypeScript type
instead of enum
.
notAllowEmptyString
type: boolean
default: false
Generates validation string schema as do not allow empty characters by default.
scalarSchemas
type: ScalarSchemas
Extends or overrides validation schema for the built-in scalars and custom GraphQL scalars.
1config: 2 schema: yup 3 scalarSchemas: 4 Date: yup.date() 5 Email: yup.string().email()
1config: 2 schema: zod 3 scalarSchemas: 4 Date: z.date() 5 Email: z.string().email()
withObjectType
type: boolean
default: false
Generates validation schema with GraphQL type objects. But excludes Query
, Mutation
, Subscription
objects.
It is currently added for the purpose of using simple objects. See also #20, #107.
This option currently does not support fragment generation. If you are interested, send me PR would be greatly appreciated!
validationSchemaExportType
type: ValidationSchemaExportType
default: 'function'
Specify validation schema export type.
directives
type: DirectiveConfig
Generates validation schema with more API based on directive schema. For example, yaml config and GraphQL schema is here.
1input ExampleInput { 2 email: String! @required(msg: "Hello, World!") @constraint(minLength: 50, format: "email") 3 message: String! @constraint(startsWith: "Hello") 4}
1generates: 2 path/to/graphql.ts: 3 plugins: 4 - typescript 5 - typescript-validation-schema 6 config: 7 schema: yup 8 directives: 9 # Write directives like 10 # 11 # directive: 12 # arg1: schemaApi 13 # arg2: ["schemaApi2", "Hello $1"] 14 # 15 # See more examples in `./tests/directive.spec.ts` 16 # https://github.com/Code-Hex/graphql-codegen-typescript-validation-schema/blob/main/tests/directive.spec.ts 17 required: 18 msg: required 19 constraint: 20 minLength: min 21 # Replace $1 with specified `startsWith` argument value of the constraint directive 22 startsWith: ["matches", "/^$1/"] 23 format: 24 email: email
Then generates yup validation schema like below.
1export function ExampleInputSchema(): yup.SchemaOf<ExampleInput> { 2 return yup.object({ 3 email: yup.string().defined().required("Hello, World!").min(50).email(), 4 message: yup.string().defined().matches(/^Hello/) 5 }) 6}
1generates: 2 path/to/graphql.ts: 3 plugins: 4 - typescript 5 - typescript-validation-schema 6 config: 7 schema: zod 8 directives: 9 # Write directives like 10 # 11 # directive: 12 # arg1: schemaApi 13 # arg2: ["schemaApi2", "Hello $1"] 14 # 15 # See more examples in `./tests/directive.spec.ts` 16 # https://github.com/Code-Hex/graphql-codegen-typescript-validation-schema/blob/main/tests/directive.spec.ts 17 constraint: 18 minLength: min 19 # Replace $1 with specified `startsWith` argument value of the constraint directive 20 startsWith: ["regex", "/^$1/", "message"] 21 format: 22 email: email
Then generates zod validation schema like below.
1export function ExampleInputSchema(): z.ZodSchema<ExampleInput> { 2 return z.object({ 3 email: z.string().min(50).email(), 4 message: z.string().regex(/^Hello/, "message") 5 }) 6}
Please see example directory.
Their is currently a compatibility issue with the client-preset. A workaround for this is to split the generation into two (one for client-preset and one for typescript-validation-schema).
1generates: 2 path/to/graphql.ts: 3 plugins: 4 - typescript-validation-schema 5 /path/to/graphql/: 6 preset: 'client', 7 plugins: 8 ...
No vulnerabilities found.
No security vulnerabilities found.