|
1 | 1 | import { DeclarationBlock, indent } from '@graphql-codegen/visitor-plugin-common';
|
| 2 | +import type { |
| 3 | + EnumTypeDefinitionNode, |
| 4 | + FieldDefinitionNode, |
| 5 | + GraphQLSchema, |
| 6 | + InputObjectTypeDefinitionNode, |
| 7 | + InputValueDefinitionNode, |
| 8 | + NameNode, |
| 9 | + TypeNode, |
| 10 | +} from 'graphql'; |
2 | 11 | import {
|
3 | 12 | Kind,
|
4 |
| - type EnumTypeDefinitionNode, |
5 |
| - type FieldDefinitionNode, |
6 |
| - type GraphQLSchema, |
7 |
| - type InputObjectTypeDefinitionNode, |
8 |
| - type InputValueDefinitionNode, |
9 |
| - type NameNode, |
10 |
| - type TypeNode, |
11 | 13 | } from 'graphql';
|
12 | 14 |
|
13 | 15 | import type { ValidationSchemaPluginConfig } from '../config';
|
| 16 | +import { buildApiForValibot, formatDirectiveConfig } from '../directive'; |
14 | 17 | import { BaseSchemaVisitor } from '../schema_visitor';
|
15 | 18 | import type { Visitor } from '../visitor';
|
16 | 19 | import {
|
@@ -118,28 +121,38 @@ function generateFieldTypeValibotSchema(config: ValidationSchemaPluginConfig, vi
|
118 | 121 | if (isListType(parentType))
|
119 | 122 | return `v.nullable(${gen})`;
|
120 | 123 |
|
| 124 | + let appliedDirectivesGen = applyDirectives(config, field, gen); |
| 125 | + |
121 | 126 | if (field.kind === Kind.INPUT_VALUE_DEFINITION) {
|
122 | 127 | const { defaultValue } = field;
|
123 | 128 | if (defaultValue?.kind === Kind.INT || defaultValue?.kind === Kind.FLOAT || defaultValue?.kind === Kind.BOOLEAN)
|
124 |
| - gen = `v.optional(${gen}, ${defaultValue.value})`; |
| 129 | + appliedDirectivesGen = `v.optional(${appliedDirectivesGen}, ${defaultValue.value})`; |
125 | 130 |
|
126 | 131 | if (defaultValue?.kind === Kind.STRING || defaultValue?.kind === Kind.ENUM)
|
127 |
| - gen = `v.optional(${gen}, "${defaultValue.value}")`; |
| 132 | + appliedDirectivesGen = `v.optional(${appliedDirectivesGen}, "${defaultValue.value}")`; |
128 | 133 |
|
129 | 134 | }
|
130 | 135 | if (isNonNullType(parentType)) {
|
131 | 136 | if (visitor.shouldEmitAsNotAllowEmptyString(type.name.value))
|
132 | 137 | return "v.string([v.minLength(1)])"; // TODO
|
133 | 138 |
|
134 |
| - return gen; |
| 139 | + return appliedDirectivesGen; |
135 | 140 | }
|
136 | 141 |
|
137 |
| - return `v.nullish(${gen})`; |
| 142 | + return `v.nullish(${appliedDirectivesGen})`; |
138 | 143 | }
|
139 | 144 | console.warn('unhandled type:', type);
|
140 | 145 | return '';
|
141 | 146 | }
|
142 | 147 |
|
| 148 | +function applyDirectives(config: ValidationSchemaPluginConfig, field: InputValueDefinitionNode | FieldDefinitionNode, gen: string): string { |
| 149 | + if (config.directives && field.directives) { |
| 150 | + const formatted = formatDirectiveConfig(config.directives); |
| 151 | + return `v.pipe(${gen}, ${buildApiForValibot(formatted, field.directives).join(', ')})`; |
| 152 | + } |
| 153 | + return gen; |
| 154 | +} |
| 155 | + |
143 | 156 | function generateNameNodeValibotSchema(config: ValidationSchemaPluginConfig, visitor: Visitor, node: NameNode): string {
|
144 | 157 | const converter = visitor.getNameNodeConverter(node);
|
145 | 158 |
|
|
0 commit comments