{"id":13767526,"url":"https://github.com/Code-Hex/graphql-codegen-typescript-validation-schema","last_synced_at":"2025-05-10T23:30:47.097Z","repository":{"id":37021528,"uuid":"449375273","full_name":"Code-Hex/graphql-codegen-typescript-validation-schema","owner":"Code-Hex","description":"GraphQL Code Generator plugin to generate form validation schema from your GraphQL schema","archived":false,"fork":false,"pushed_at":"2025-05-05T15:10:24.000Z","size":4009,"stargazers_count":338,"open_issues_count":19,"forks_count":51,"subscribers_count":5,"default_branch":"main","last_synced_at":"2025-05-05T16:28:28.629Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Code-Hex.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null},"funding":{"github":"Code-Hex"}},"created_at":"2022-01-18T17:08:27.000Z","updated_at":"2025-05-05T15:10:28.000Z","dependencies_parsed_at":"2023-12-18T20:17:55.026Z","dependency_job_id":"f7fcdad9-5167-4d11-a62b-24adb6470ed6","html_url":"https://github.com/Code-Hex/graphql-codegen-typescript-validation-schema","commit_stats":{"total_commits":1001,"total_committers":22,"mean_commits":45.5,"dds":"0.35764235764235763","last_synced_commit":"d05453f32ef6558a518de0b89762a410325ac631"},"previous_names":[],"tags_count":28,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Code-Hex%2Fgraphql-codegen-typescript-validation-schema","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Code-Hex%2Fgraphql-codegen-typescript-validation-schema/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Code-Hex%2Fgraphql-codegen-typescript-validation-schema/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Code-Hex%2Fgraphql-codegen-typescript-validation-schema/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Code-Hex","download_url":"https://codeload.github.com/Code-Hex/graphql-codegen-typescript-validation-schema/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253497296,"owners_count":21917683,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-03T16:01:09.437Z","updated_at":"2025-05-10T23:30:46.776Z","avatar_url":"https://github.com/Code-Hex.png","language":"TypeScript","readme":"# graphql-codegen-typescript-validation-schema\n\n[![Test](https://github.com/Code-Hex/graphql-codegen-typescript-validation-schema/actions/workflows/ci.yml/badge.svg)](https://github.com/Code-Hex/graphql-codegen-typescript-validation-schema/actions/workflows/ci.yml) [![npm version](https://badge.fury.io/js/graphql-codegen-typescript-validation-schema.svg)](https://badge.fury.io/js/graphql-codegen-typescript-validation-schema)\n\n[GraphQL code generator](https://github.com/dotansimha/graphql-code-generator) plugin to generate form validation schema from your GraphQL schema.\n\n- [x] support [yup](https://github.com/jquense/yup)\n- [x] support [zod](https://github.com/colinhacks/zod)\n- [x] support [myzod](https://github.com/davidmdm/myzod)\n- [x] support [valibot](https://valibot.dev/)\n\n## Quick Start\n\nStart by installing this plugin and write simple plugin config;\n\n```sh\n$ npm i graphql-codegen-typescript-validation-schema\n```\n\n```yml\ngenerates:\n  path/to/graphql.ts:\n    plugins:\n      - typescript\n      - typescript-validation-schema # specify to use this plugin\n    config:\n      # You can put the config for typescript plugin here\n      # see: https://www.graphql-code-generator.com/plugins/typescript\n      strictScalars: true\n      # Overrides built-in ID scalar to both input and output types as string.\n      # see: https://the-guild.dev/graphql/codegen/plugins/typescript/typescript#scalars\n      scalars:\n        ID: string\n      # You can also write the config for this plugin together\n      schema: yup # or zod\n```\n\nIt is recommended to write `scalars` config for built-in type `ID`, as in the yaml example shown above. For more information: [#375](https://github.com/Code-Hex/graphql-codegen-typescript-validation-schema/pull/375)\n\nYou can check [example](https://github.com/Code-Hex/graphql-codegen-typescript-validation-schema/tree/main/example) directory if you want to see more complex config example or how is generated some files.\n\nThe Q\u0026A for each schema is written in the README in the respective example directory.\n\n## Config API Reference\n\n### `schema`\n\ntype: `ValidationSchema` default: `'yup'`\n\nSpecify generete validation schema you want.\n\nYou can specify `yup` or `zod` or `myzod`.\n\n```yml\ngenerates:\n  path/to/graphql.ts:\n    plugins:\n      - typescript\n      - typescript-validation-schema\n    config:\n      schema: yup\n```\n\n### `importFrom`\n\ntype: `string`\n\nWhen provided, import types from the generated typescript types file path. if not given, omit import statement.\n\n```yml\ngenerates:\n  path/to/graphql.ts:\n    plugins:\n      - typescript\n  path/to/validation.ts:\n    plugins:\n      - typescript-validation-schema\n    config:\n      importFrom: ./graphql # path for generated ts code\n```\n\nThen the generator generates code with import statement like below.\n\n```ts\nimport { GeneratedInput } from './graphql'\n\n/* generates validation schema here */\n```\n\n### `schemaNamespacedImportName`\n\ntype: `string`\n\nIf defined, will use named imports from the specified module (defined in `importFrom`) rather than individual imports for each type.\n\n```yml\ngenerates:\n  path/to/types.ts:\n    plugins:\n      - typescript\n  path/to/schemas.ts:\n    plugins:\n      - graphql-codegen-validation-schema\n    config:\n      schema: yup\n      importFrom: ./path/to/types\n      schemaNamespacedImportName: types\n```\n\nThen the generator generates code with import statement like below.\n\n```ts\nimport * as types from './graphql'\n\n/* generates validation schema here */\n```\n\n### `useTypeImports`\n\ntype: `boolean` default: `false`\n\nWill use `import type {}` rather than `import {}` when importing generated TypeScript types.\nThis gives compatibility with TypeScript's \"importsNotUsedAsValues\": \"error\" option.\nShould used in conjunction with `importFrom` option.\n\n### `typesPrefix`\n\ntype: `string` default: (empty)\n\nPrefixes all import types from generated typescript type.\n\n```yml\ngenerates:\n  path/to/graphql.ts:\n    plugins:\n      - typescript\n  path/to/validation.ts:\n    plugins:\n      - typescript-validation-schema\n    config:\n      typesPrefix: I\n      importFrom: ./graphql # path for generated ts code\n```\n\nThen the generator generates code with import statement like below.\n\n```ts\nimport { IGeneratedInput } from './graphql'\n\n/* generates validation schema here */\n```\n\n### `typesSuffix`\n\ntype: `string` default: (empty)\n\nSuffixes all import types from generated typescript type.\n\n```yml\ngenerates:\n  path/to/graphql.ts:\n    plugins:\n      - typescript\n  path/to/validation.ts:\n    plugins:\n      - typescript-validation-schema\n    config:\n      typesSuffix: I\n      importFrom: ./graphql # path for generated ts code\n```\n\nThen the generator generates code with import statement like below.\n\n```ts\nimport { GeneratedInputI } from './graphql'\n\n/* generates validation schema here */\n```\n\n### `enumsAsTypes`\n\ntype: `boolean` default: `false`\n\nGenerates enum as TypeScript `type` instead of `enum`.\n\n### `notAllowEmptyString`\n\ntype: `boolean` default: `false`\n\nGenerates validation string schema as do not allow empty characters by default.\n\n### `scalarSchemas`\n\ntype: `ScalarSchemas`\n\nExtends or overrides validation schema for the built-in scalars and custom GraphQL scalars.\n\n#### yup schema\n\n```yml\nconfig:\n  schema: yup\n  scalarSchemas:\n    Date: yup.date()\n    Email: yup.string().email()\n```\n\n#### zod schema\n\n```yml\nconfig:\n  schema: zod\n  scalarSchemas:\n    Date: z.date()\n    Email: z.string().email()\n```\n\n### `defaultScalarTypeSchema`\n\ntype: `string`\n\nFallback scalar type for undefined scalar types in the schema not found in `scalarSchemas`.\n\n#### yup schema\n```yml\nconfig:\n  schema: yup\n  defaultScalarSchema: yup.unknown()\n```\n\n#### zod schema\n```yml\nconfig:\n  schema: zod\n  defaultScalarSchema: z.unknown()\n```\n\n### `withObjectType`\n\ntype: `boolean` default: `false`\n\nGenerates validation schema with GraphQL type objects. But excludes `Query`, `Mutation`, `Subscription` objects.\n\nIt is currently added for the purpose of using simple objects. See also [#20](https://github.com/Code-Hex/graphql-codegen-typescript-validation-schema/issues/20#issuecomment-1058969191), [#107](https://github.com/Code-Hex/graphql-codegen-typescript-validation-schema/issues/107).\n\nThis option currently **does not support fragment** generation. If you are interested, send me PR would be greatly appreciated!\n\n### `validationSchemaExportType`\n\ntype: `ValidationSchemaExportType` default: `'function'`\n\nSpecify validation schema export type.\n\n### `useEnumTypeAsDefaultValue`\n\ntype: `boolean` default: `false`\n\nUses the full path of the enum type as the default value instead of the stringified value.\n\n### `namingConvention`\n\ntype: `NamingConventionMap` default: `{ enumValues: \"change-case-all#pascalCase\" }`\n\nUses the full path of the enum type as the default value instead of the stringified value.\n\nRelated: https://the-guild.dev/graphql/codegen/docs/config-reference/naming-convention#namingconvention\n\n### `directives`\n\ntype: `DirectiveConfig`\n\nGenerates validation schema with more API based on directive schema. For example, yaml config and GraphQL schema is here.\n\n```graphql\ninput ExampleInput {\n  email: String! @required(msg: \"Hello, World!\") @constraint(minLength: 50, format: \"email\")\n  message: String! @constraint(startsWith: \"Hello\")\n}\n```\n\n#### yup schema\n\n```yml\ngenerates:\n  path/to/graphql.ts:\n    plugins:\n      - typescript\n      - typescript-validation-schema\n    config:\n      schema: yup\n      directives:\n        # Write directives like\n        #\n        # directive:\n        #   arg1: schemaApi\n        #   arg2: [\"schemaApi2\", \"Hello $1\"]\n        #\n        # See more examples in `./tests/directive.spec.ts`\n        # https://github.com/Code-Hex/graphql-codegen-typescript-validation-schema/blob/main/tests/directive.spec.ts\n        required:\n          msg: required\n        constraint:\n          minLength: min\n          # Replace $1 with specified `startsWith` argument value of the constraint directive\n          startsWith: [matches, /^$1/]\n          format:\n            # This example means `validation-schema: directive-arg`\n            # directive-arg is supported String and Enum.\n            email: email\n```\n\nThen generates yup validation schema like below.\n\n```ts\nexport function ExampleInputSchema(): yup.SchemaOf\u003cExampleInput\u003e {\n  return yup.object({\n    email: yup.string().defined().required('Hello, World!').min(50).email(),\n    message: yup.string().defined().matches(/^Hello/)\n  })\n}\n```\n\n#### zod schema\n\n```yml\ngenerates:\n  path/to/graphql.ts:\n    plugins:\n      - typescript\n      - typescript-validation-schema\n    config:\n      schema: zod\n      directives:\n        # Write directives like\n        #\n        # directive:\n        #   arg1: schemaApi\n        #   arg2: [\"schemaApi2\", \"Hello $1\"]\n        #\n        # See more examples in `./tests/directive.spec.ts`\n        # https://github.com/Code-Hex/graphql-codegen-typescript-validation-schema/blob/main/tests/directive.spec.ts\n        constraint:\n          minLength: min\n          # Replace $1 with specified `startsWith` argument value of the constraint directive\n          startsWith: [regex, /^$1/, message]\n          format:\n            # This example means `validation-schema: directive-arg`\n            # directive-arg is supported String and Enum.\n            email: email\n```\n\nThen generates zod validation schema like below.\n\n```ts\nexport function ExampleInputSchema(): z.ZodSchema\u003cExampleInput\u003e {\n  return z.object({\n    email: z.string().min(50).email(),\n    message: z.string().regex(/^Hello/, 'message')\n  })\n}\n```\n\n#### other schema\n\nPlease see [example](https://github.com/Code-Hex/graphql-codegen-typescript-validation-schema/tree/main/example) directory.\n\n## Notes\n\nTheir 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).\n\n```yml\ngenerates:\n  path/to/graphql.ts:\n    plugins:\n      - typescript-validation-schema\n  /path/to/graphql/:\n    preset: 'client',\n      plugins:\n      ...\n```\n","funding_links":["https://github.com/sponsors/Code-Hex"],"categories":["TypeScript","Convertors and Generators"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FCode-Hex%2Fgraphql-codegen-typescript-validation-schema","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FCode-Hex%2Fgraphql-codegen-typescript-validation-schema","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FCode-Hex%2Fgraphql-codegen-typescript-validation-schema/lists"}