Skip to content

Commit edc3d87

Browse files
committed
fixed to build validation schema using directives
1 parent 7840255 commit edc3d87

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

src/directive.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ import {
33
ConstDirectiveNode,
44
ConstValueNode,
55
Kind,
6-
NameNode,
76
valueFromASTUntyped,
87
} from "graphql";
98
import { DirectiveConfig, DirectiveObjectArguments } from "./config";
9+
import { isConvertableRegexp } from "./regexp";
1010

1111
export interface FormattedDirectiveConfig {
1212
[directive: string]: FormattedDirectiveArguments;
@@ -22,7 +22,8 @@ export interface FormattedDirectiveObjectArguments {
2222

2323
const isFormattedDirectiveObjectArguments = (
2424
arg: FormattedDirectiveArguments[keyof FormattedDirectiveArguments]
25-
): arg is FormattedDirectiveObjectArguments => arg !== undefined && !Array.isArray(arg);
25+
): arg is FormattedDirectiveObjectArguments =>
26+
arg !== undefined && !Array.isArray(arg);
2627

2728
// ```yml
2829
// directives:
@@ -205,8 +206,13 @@ const stringify = (arg: any, quoteString?: boolean): string => {
205206
if (Array.isArray(arg)) {
206207
return arg.map((v) => stringify(v, true)).join(",");
207208
}
208-
if (quoteString && typeof arg === "string") {
209-
return JSON.stringify(arg);
209+
if (typeof arg === "string") {
210+
if (isConvertableRegexp(arg)) {
211+
return arg;
212+
}
213+
if (quoteString) {
214+
return JSON.stringify(arg);
215+
}
210216
}
211217
if (
212218
typeof arg === "boolean" ||

src/yup/index.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
indent,
1414
} from "@graphql-codegen/visitor-plugin-common";
1515
import { TsVisitor } from "@graphql-codegen/typescript";
16+
import { buildApi, formatDirectiveConfig } from "../directive";
1617

1718
const importYup = `import * as yup from 'yup'`;
1819

@@ -112,13 +113,16 @@ const generateInputObjectFieldYupSchema = (
112113
field: InputValueDefinitionNode,
113114
indentCount: number
114115
): string => {
115-
// TOOD(codehex): handle directive
116-
const gen = generateInputObjectFieldTypeYupSchema(
116+
let gen = generateInputObjectFieldTypeYupSchema(
117117
config,
118118
tsVisitor,
119119
schema,
120120
field.type
121121
);
122+
if (config.directives && field.directives) {
123+
const formatted = formatDirectiveConfig(config.directives);
124+
gen += buildApi(formatted, field.directives);
125+
}
122126
return indent(
123127
`${field.name.value}: ${maybeLazy(field.type, gen)}`,
124128
indentCount

0 commit comments

Comments
 (0)