Skip to content

Commit ac852b0

Browse files
committed
install zod
1 parent c4615d7 commit ac852b0

File tree

4 files changed

+123
-1
lines changed

4 files changed

+123
-1
lines changed

codegen.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,36 @@ generates:
3737
max: ['max', '$1 + 1']
3838
exclusiveMin: min
3939
exclusiveMax: max
40+
example/zod/schemas.ts:
41+
plugins:
42+
- ./dist/main/index.js:
43+
schema: zod
44+
importFrom: ../types
45+
directives:
46+
required:
47+
msg: required
48+
# This is example using constraint directive.
49+
# see: https://github.com/confuser/graphql-constraint-directive
50+
constraint:
51+
minLength: min # same as ['min', '$1']
52+
maxLength: max
53+
startsWith: ['regex', '/^$1/']
54+
endsWith: ['regex', '/$1$/']
55+
contains: ['regex', '/$1/']
56+
notContains: ['regex', '/^((?!$1).)*$/']
57+
pattern: ['regex', '/$1/']
58+
format:
59+
# For example, `@constraint(format: "uri")`. this case $1 will be "uri".
60+
# Therefore the generator generates yup schema `.url()` followed by `uri: 'url'`
61+
# If $1 does not match anywhere, the generator will ignore.
62+
uri: url
63+
email: email
64+
uuid: uuid
65+
# yup does not have `ipv4` API. If you want to add this,
66+
# you need to add the logic using `yup.addMethod`.
67+
# see: https://github.com/jquense/yup#addmethodschematype-schema-name-string-method--schema-void
68+
ipv4: ipv4
69+
min: ['min', '$1 - 1']
70+
max: ['max', '$1 + 1']
71+
exclusiveMin: min
72+
exclusiveMax: max

example/zod/schemas.ts

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import { z } from 'zod'
2+
import { AttributeInput, ButtonComponentType, ComponentInput, DropDownComponentInput, EventArgumentInput, EventInput, EventOptionType, HttpInput, HttpMethod, LayoutInput, PageInput, PageType } from '../types'
3+
4+
type definedNonNullAny = {};
5+
6+
export const isDefinedNonNullAny = (v: any): v is definedNonNullAny => v !== undefined && v !== null;
7+
8+
export const definedNonNullAnySchema: z.ZodSchema<definedNonNullAny> = z.any().refine((v) => isDefinedNonNullAny(v));
9+
10+
export function AttributeInputSchema(): z.ZodSchema<AttributeInput> {
11+
return z.object({
12+
key: z.string().nullish(),
13+
val: z.string().nullish()
14+
})
15+
}
16+
17+
export const ButtonComponentTypeSchema = z.nativeEnum(ButtonComponentType);
18+
19+
export function ComponentInputSchema(): z.ZodSchema<ComponentInput> {
20+
return z.object({
21+
child: z.lazy(() => ComponentInputSchema().nullish()),
22+
childrens: z.array(z.lazy(() => ComponentInputSchema().nullable())).nullish(),
23+
event: z.lazy(() => EventInputSchema().nullish()),
24+
name: z.string(),
25+
type: ButtonComponentTypeSchema
26+
})
27+
}
28+
29+
export function DropDownComponentInputSchema(): z.ZodSchema<DropDownComponentInput> {
30+
return z.object({
31+
dropdownComponent: z.lazy(() => ComponentInputSchema().nullish()),
32+
getEvent: z.lazy(() => EventInputSchema())
33+
})
34+
}
35+
36+
export function EventArgumentInputSchema(): z.ZodSchema<EventArgumentInput> {
37+
return z.object({
38+
name: z.string().min(5),
39+
value: z.string().regex(/^foo/)
40+
})
41+
}
42+
43+
export function EventInputSchema(): z.ZodSchema<EventInput> {
44+
return z.object({
45+
arguments: z.array(z.lazy(() => EventArgumentInputSchema())),
46+
options: z.array(EventOptionTypeSchema).nullish()
47+
})
48+
}
49+
50+
export const EventOptionTypeSchema = z.nativeEnum(EventOptionType);
51+
52+
export function HttpInputSchema(): z.ZodSchema<HttpInput> {
53+
return z.object({
54+
method: HttpMethodSchema.nullish(),
55+
url: definedNonNullAnySchema
56+
})
57+
}
58+
59+
export const HttpMethodSchema = z.nativeEnum(HttpMethod);
60+
61+
export function LayoutInputSchema(): z.ZodSchema<LayoutInput> {
62+
return z.object({
63+
dropdown: z.lazy(() => DropDownComponentInputSchema().nullish())
64+
})
65+
}
66+
67+
export function PageInputSchema(): z.ZodSchema<PageInput> {
68+
return z.object({
69+
attributes: z.array(z.lazy(() => AttributeInputSchema())).nullish(),
70+
date: definedNonNullAnySchema.nullish(),
71+
height: z.number(),
72+
id: z.string(),
73+
layout: z.lazy(() => LayoutInputSchema()),
74+
pageType: PageTypeSchema,
75+
postIDs: z.array(z.string()).nullish(),
76+
show: z.boolean(),
77+
tags: z.array(z.string().nullable()).nullish(),
78+
title: z.string(),
79+
width: z.number()
80+
})
81+
}
82+
83+
export const PageTypeSchema = z.nativeEnum(PageType);

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@
5656
"prettier": "2.5.1",
5757
"ts-jest": "^27.1.3",
5858
"typescript": "^4.5.4",
59-
"yup": "^0.32.11"
59+
"yup": "^0.32.11",
60+
"zod": "^3.11.6"
6061
},
6162
"dependencies": {
6263
"@graphql-codegen/plugin-helpers": "^2.3.2",

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5975,3 +5975,8 @@ yup@^0.32.11:
59755975
nanoclone "^0.2.1"
59765976
property-expr "^2.0.4"
59775977
toposort "^2.0.2"
5978+
5979+
zod@^3.11.6:
5980+
version "3.11.6"
5981+
resolved "https://registry.yarnpkg.com/zod/-/zod-3.11.6.tgz#e43a5e0c213ae2e02aefe7cb2b1a6fa3d7f1f483"
5982+
integrity sha512-daZ80A81I3/9lIydI44motWe6n59kRBfNzTuS2bfzVh1nAXi667TOTWWtatxyG+fwgNUiagSj/CWZwRRbevJIg==

0 commit comments

Comments
 (0)