|
| 1 | +import { TypeScriptPluginConfig } from '@graphql-codegen/typescript'; |
| 2 | +export type ValidationSchema = 'yup' | 'zod' | 'myzod'; |
| 3 | +export type ValidationSchemaExportType = 'function' | 'const'; |
| 4 | +export interface DirectiveConfig { |
| 5 | + [directive: string]: { |
| 6 | + [argument: string]: string | string[] | DirectiveObjectArguments; |
| 7 | + }; |
| 8 | +} |
| 9 | +export interface DirectiveObjectArguments { |
| 10 | + [matched: string]: string | string[]; |
| 11 | +} |
| 12 | +interface ScalarSchemas { |
| 13 | + [name: string]: string; |
| 14 | +} |
| 15 | +export interface ValidationSchemaPluginConfig extends TypeScriptPluginConfig { |
| 16 | + /** |
| 17 | + * @description specify generate schema |
| 18 | + * @default yup |
| 19 | + * |
| 20 | + * @exampleMarkdown |
| 21 | + * ```yml |
| 22 | + * generates: |
| 23 | + * path/to/file.ts: |
| 24 | + * plugins: |
| 25 | + * - typescript |
| 26 | + * - graphql-codegen-validation-schema |
| 27 | + * config: |
| 28 | + * schema: yup |
| 29 | + * ``` |
| 30 | + */ |
| 31 | + schema?: ValidationSchema; |
| 32 | + /** |
| 33 | + * @description import types from generated typescript type path |
| 34 | + * if not given, omit import statement. |
| 35 | + * |
| 36 | + * @exampleMarkdown |
| 37 | + * ```yml |
| 38 | + * generates: |
| 39 | + * path/to/types.ts: |
| 40 | + * plugins: |
| 41 | + * - typescript |
| 42 | + * path/to/schemas.ts: |
| 43 | + * plugins: |
| 44 | + * - graphql-codegen-validation-schema |
| 45 | + * config: |
| 46 | + * schema: yup |
| 47 | + * importFrom: ./path/to/types |
| 48 | + * ``` |
| 49 | + */ |
| 50 | + importFrom?: string; |
| 51 | + /** |
| 52 | + * @description Will use `import type {}` rather than `import {}` when importing generated typescript types. |
| 53 | + * This gives compatibility with TypeScript's "importsNotUsedAsValues": "error" option |
| 54 | + * Should used in conjunction with `importFrom` option. |
| 55 | + * @default false |
| 56 | + * |
| 57 | + * @exampleMarkdown |
| 58 | + * ```yml |
| 59 | + * generates: |
| 60 | + * path/to/types.ts: |
| 61 | + * plugins: |
| 62 | + * - typescript |
| 63 | + * path/to/schemas.ts: |
| 64 | + * plugins: |
| 65 | + * - graphql-codegen-validation-schema |
| 66 | + * config: |
| 67 | + * schema: yup |
| 68 | + * importFrom: ./path/to/types |
| 69 | + * useTypeImports: true |
| 70 | + * ``` |
| 71 | + */ |
| 72 | + useTypeImports?: boolean; |
| 73 | + /** |
| 74 | + * @description Prefixes all import types from generated typescript type. |
| 75 | + * @default "" |
| 76 | + * |
| 77 | + * @exampleMarkdown |
| 78 | + * ```yml |
| 79 | + * generates: |
| 80 | + * path/to/types.ts: |
| 81 | + * plugins: |
| 82 | + * - typescript |
| 83 | + * path/to/schemas.ts: |
| 84 | + * plugins: |
| 85 | + * - graphql-codegen-validation-schema |
| 86 | + * config: |
| 87 | + * typesPrefix: I |
| 88 | + * importFrom: ./path/to/types |
| 89 | + * ``` |
| 90 | + */ |
| 91 | + typesPrefix?: string; |
| 92 | + /** |
| 93 | + * @description Suffixes all import types from generated typescript type. |
| 94 | + * @default "" |
| 95 | + * |
| 96 | + * @exampleMarkdown |
| 97 | + * ```yml |
| 98 | + * generates: |
| 99 | + * path/to/types.ts: |
| 100 | + * plugins: |
| 101 | + * - typescript |
| 102 | + * path/to/schemas.ts: |
| 103 | + * plugins: |
| 104 | + * - graphql-codegen-validation-schema |
| 105 | + * config: |
| 106 | + * typesSuffix: I |
| 107 | + * importFrom: ./path/to/types |
| 108 | + * ``` |
| 109 | + */ |
| 110 | + typesSuffix?: string; |
| 111 | + /** |
| 112 | + * @description Generates validation schema for enum as TypeScript `type` |
| 113 | + * @default false |
| 114 | + * |
| 115 | + * @exampleMarkdown |
| 116 | + * ```yml |
| 117 | + * generates: |
| 118 | + * path/to/file.ts: |
| 119 | + * plugins: |
| 120 | + * - graphql-codegen-validation-schema |
| 121 | + * config: |
| 122 | + * enumsAsTypes: true |
| 123 | + * ``` |
| 124 | + * |
| 125 | + * ```yml |
| 126 | + * generates: |
| 127 | + * path/to/file.ts: |
| 128 | + * plugins: |
| 129 | + * - typescript |
| 130 | + * - graphql-codegen-validation-schema |
| 131 | + * config: |
| 132 | + * enumsAsTypes: true |
| 133 | + * ``` |
| 134 | + */ |
| 135 | + enumsAsTypes?: boolean; |
| 136 | + /** |
| 137 | + * @description Generates validation string schema as do not allow empty characters by default. |
| 138 | + * @default false |
| 139 | + * |
| 140 | + * @exampleMarkdown |
| 141 | + * ```yml |
| 142 | + * generates: |
| 143 | + * path/to/file.ts: |
| 144 | + * plugins: |
| 145 | + * - graphql-codegen-validation-schema |
| 146 | + * config: |
| 147 | + * notAllowEmptyString: true |
| 148 | + * ``` |
| 149 | + */ |
| 150 | + notAllowEmptyString?: boolean; |
| 151 | + /** |
| 152 | + * @description Extends or overrides validation schema for the built-in scalars and custom GraphQL scalars. |
| 153 | + * |
| 154 | + * @exampleMarkdown |
| 155 | + * ```yml |
| 156 | + * config: |
| 157 | + * schema: yup |
| 158 | + * scalarSchemas: |
| 159 | + * Date: yup.date() |
| 160 | + * Email: yup.string().email() |
| 161 | + * ``` |
| 162 | + * |
| 163 | + * @exampleMarkdown |
| 164 | + * ```yml |
| 165 | + * config: |
| 166 | + * schema: zod |
| 167 | + * scalarSchemas: |
| 168 | + * Date: z.date() |
| 169 | + * Email: z.string().email() |
| 170 | + * ``` |
| 171 | + */ |
| 172 | + scalarSchemas?: ScalarSchemas; |
| 173 | + /** |
| 174 | + * @description Generates validation schema with GraphQL type objects. |
| 175 | + * but excludes "Query", "Mutation", "Subscription" objects. |
| 176 | + * |
| 177 | + * @exampleMarkdown |
| 178 | + * ```yml |
| 179 | + * generates: |
| 180 | + * path/to/types.ts: |
| 181 | + * plugins: |
| 182 | + * - typescript |
| 183 | + * path/to/schemas.ts: |
| 184 | + * plugins: |
| 185 | + * - graphql-codegen-validation-schema |
| 186 | + * config: |
| 187 | + * schema: yup |
| 188 | + * withObjectType: true |
| 189 | + * ``` |
| 190 | + */ |
| 191 | + withObjectType?: boolean; |
| 192 | + /** |
| 193 | + * @description Specify validation schema export type. |
| 194 | + * @default function |
| 195 | + * |
| 196 | + * @exampleMarkdown |
| 197 | + * ```yml |
| 198 | + * generates: |
| 199 | + * path/to/file.ts: |
| 200 | + * plugins: |
| 201 | + * - typescript |
| 202 | + * - graphql-codegen-validation-schema |
| 203 | + * config: |
| 204 | + * validationSchemaExportType: const |
| 205 | + * ``` |
| 206 | + */ |
| 207 | + validationSchemaExportType?: ValidationSchemaExportType; |
| 208 | + /** |
| 209 | + * @description Generates validation schema with more API based on directive schema. |
| 210 | + * @exampleMarkdown |
| 211 | + * ```yml |
| 212 | + * generates: |
| 213 | + * path/to/file.ts: |
| 214 | + * plugins: |
| 215 | + * - graphql-codegen-validation-schema |
| 216 | + * config: |
| 217 | + * schema: yup |
| 218 | + * directives: |
| 219 | + * required: |
| 220 | + * msg: required |
| 221 | + * # This is example using constraint directive. |
| 222 | + * # see: https://github.com/confuser/graphql-constraint-directive |
| 223 | + * constraint: |
| 224 | + * minLength: min # same as ['min', '$1'] |
| 225 | + * maxLength: max |
| 226 | + * startsWith: ["matches", "/^$1/"] |
| 227 | + * endsWith: ["matches", "/$1$/"] |
| 228 | + * contains: ["matches", "/$1/"] |
| 229 | + * notContains: ["matches", "/^((?!$1).)*$/"] |
| 230 | + * pattern: ["matches", "/$1/"] |
| 231 | + * format: |
| 232 | + * # For example, `@constraint(format: "uri")`. this case $1 will be "uri". |
| 233 | + * # Therefore the generator generates yup schema `.url()` followed by `uri: 'url'` |
| 234 | + * # If $1 does not match anywhere, the generator will ignore. |
| 235 | + * uri: url |
| 236 | + * email: email |
| 237 | + * uuid: uuid |
| 238 | + * # yup does not have `ipv4` API. If you want to add this, |
| 239 | + * # you need to add the logic using `yup.addMethod`. |
| 240 | + * # see: https://github.com/jquense/yup#addmethodschematype-schema-name-string-method--schema-void |
| 241 | + * ipv4: ipv4 |
| 242 | + * min: ["min", "$1 - 1"] |
| 243 | + * max: ["max", "$1 + 1"] |
| 244 | + * exclusiveMin: min |
| 245 | + * exclusiveMax: max |
| 246 | + * ``` |
| 247 | + */ |
| 248 | + directives?: DirectiveConfig; |
| 249 | +} |
| 250 | +export {}; |
0 commit comments