Skip to content

Commit 7632618

Browse files
authored
Merge pull request #2 from Code-Hex/add/README-more
write more description in readme
2 parents b9af2ef + c27a4c5 commit 7632618

File tree

5 files changed

+124
-49
lines changed

5 files changed

+124
-49
lines changed

README.md

Lines changed: 120 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,123 @@
11
# graphql-codegen-typescript-validation-schema
22

3-
GraphQL Code Generator plugin to generate form validation schema from your GraphQL schema
3+
[![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)
44

5-
## THIS IS ALPHA VERSION
5+
[GraphQL code generator](https://github.com/dotansimha/graphql-code-generator) plugin to generate form validation schema from your GraphQL schema.
6+
7+
- [x] support [yup](https://github.com/jquense/yup)
8+
- [ ] support [zod](https://github.com/colinhacks/zod)
9+
10+
## Quick Start
11+
12+
Start by installing this plugin and write simple plugin config;
13+
14+
```sh
15+
$ npm i graphql-codegen-typescript-validation-schema
16+
```
17+
18+
```yml
19+
generates:
20+
path/to/graphql.ts:
21+
plugins:
22+
- typescript
23+
- typescript-validation-schema # specify to use this plugin
24+
config:
25+
# You can put the config for typescript plugin here
26+
# see: https://www.graphql-code-generator.com/plugins/typescript
27+
strictScalars: true
28+
# You can also write the config for this plugin together
29+
schema: yup
30+
```
31+
32+
## Config API Reference
33+
34+
### `schema`
35+
36+
type: `ValidationSchema` default: `'yup'`
37+
38+
Specify generete validation schema you want.
39+
40+
```yml
41+
generates:
42+
path/to/graphql.ts:
43+
plugins:
44+
- typescript
45+
- graphql-codegen-validation-schema
46+
config:
47+
schema: yup
48+
```
49+
50+
### `importFrom`
51+
52+
type: `string`
53+
54+
import types from generated typescript type path. if not given, omit import statement.
55+
56+
```yml
57+
generates:
58+
path/to/graphql.ts:
59+
plugins:
60+
- typescript
61+
path/to/validation.ts:
62+
plugins:
63+
- graphql-codegen-validation-schema
64+
config:
65+
importFrom: ./graphql # path for generated ts code
66+
```
67+
68+
Then the generator generates code with import statement like below.
69+
70+
```ts
71+
import { GeneratedInput } from './graphql'
72+
73+
/* generates validation schema here */
74+
```
75+
76+
### `enumsAsTypes`
77+
78+
type: `boolean` default: `false`
79+
80+
Generates enum as TypeScript `type` instead of `enum`.
81+
82+
### `directives`
83+
84+
type: `DirectiveConfig`
85+
86+
Generates validation schema with more API based on directive schema. For example, yaml config and GraphQL schema is here.
87+
88+
```yml
89+
generates:
90+
path/to/graphql.ts:
91+
plugins:
92+
- typescript
93+
- graphql-codegen-validation-schema
94+
config:
95+
schema: yup
96+
directives:
97+
required:
98+
msg: required
99+
constraint:
100+
minLength: min
101+
# Replace $1 with specified `startsWith` argument value of the constraint directive
102+
startsWith: ["matches", "/^$1/"]
103+
format:
104+
email: email
105+
```
106+
107+
```graphql
108+
input ExampleInput {
109+
email: String! @required(msg: "Hello, World!") @constraint(minLength: 50)
110+
message: String! @constraint(startsWith: "Hello")
111+
}
112+
```
113+
114+
Then generates yup validation schema like below.
115+
116+
```ts
117+
export function ExampleInputSchema(): yup.SchemaOf<ExampleInput> {
118+
return yup.object({
119+
email: yup.string().defined().required("Hello, World!").min(50),
120+
message: yup.string().defined().matches(/^Hello/)
121+
})
122+
}
123+
```

jest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const { resolve } = require("path");
2-
const { pathsToModuleNameMapper } = require("ts-jest/utils");
2+
const { pathsToModuleNameMapper } = require("ts-jest");
33

44
const pkg = require("./package.json");
55
const tsconfig = require("./tsconfig.json");

src/config.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,17 +74,13 @@ export interface ValidationSchemaPluginConfig extends TypeScriptPluginConfig {
7474
*/
7575
enumsAsTypes?: boolean;
7676
/**
77-
* @description this is for yup schema. use this when you specified `schema: yup`
78-
*/
79-
yup?: YupSchemaPluginConfig;
80-
/**
81-
* @description Generates yup schema as strict.
77+
* @description Generates validation schema with more API based on directive schema.
8278
* @exampleMarkdown
8379
* ```yml
8480
* generates:
8581
* path/to/file.ts:
8682
* plugins:
87-
* - graphql-codegen-validation-schema:
83+
* - graphql-codegen-validation-schema
8884
* config:
8985
* schema: yup
9086
* directives:

src/yup/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ const generateNameNodeYupSchema = (
185185
}
186186

187187
const primitive = yup4Scalar(tsVisitor, node.value);
188-
return config.yup?.strict ? `${primitive}.strict(true)` : primitive;
188+
return primitive;
189189
};
190190

191191
const maybeLazy = (type: TypeNode, schema: string): string => {

tests/yup.spec.ts

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -214,43 +214,4 @@ describe("yup", () => {
214214
"export const PageTypeSchema = yup.mixed().oneOf(['PUBLIC', 'BASIC_AUTH'])"
215215
);
216216
});
217-
it("with yup.strict", async () => {
218-
const schema = buildSchema(/* GraphQL */ `
219-
input PrimitiveInput {
220-
a: ID
221-
b: String
222-
c: Boolean
223-
d: Int
224-
e: Float
225-
f: F!
226-
}
227-
228-
input F {
229-
a: String!
230-
}
231-
`);
232-
const result = await plugin(
233-
schema,
234-
[],
235-
{
236-
yup: {
237-
strict: true,
238-
},
239-
},
240-
{}
241-
);
242-
const wantContains = [
243-
"export function PrimitiveInputSchema(): yup.SchemaOf<PrimitiveInput>",
244-
"a: yup.string().strict(true),",
245-
"b: yup.string().strict(true),",
246-
"c: yup.boolean().strict(true),",
247-
"d: yup.number().strict(true),",
248-
"e: yup.number().strict(true),",
249-
"f: FSchema().defined()",
250-
"a: yup.string().strict(true).defined()", // for FSchema
251-
];
252-
for (const wantContain of wantContains) {
253-
expect(result.content).toContain(wantContain);
254-
}
255-
});
256217
});

0 commit comments

Comments
 (0)