Skip to content

Commit 2fbb44b

Browse files
authored
Merge pull request #23 from Code-Hex/feature/fix-zod-return-schema
fixed return type to be z.ZodObject
2 parents b61f4ea + 0dcfdbf commit 2fbb44b

File tree

3 files changed

+30
-22
lines changed

3 files changed

+30
-22
lines changed

example/zod/schemas.ts

+13-9
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
import { z } from 'zod'
22
import { AttributeInput, ButtonComponentType, ComponentInput, DropDownComponentInput, EventArgumentInput, EventInput, EventOptionType, HttpInput, HttpMethod, LayoutInput, PageInput, PageType } from '../types'
33

4+
type Properties<T> = Required<{
5+
[K in keyof T]: z.ZodType<T[K], any, T[K]>;
6+
}>;
7+
48
type definedNonNullAny = {};
59

610
export const isDefinedNonNullAny = (v: any): v is definedNonNullAny => v !== undefined && v !== null;
711

8-
export const definedNonNullAnySchema: z.ZodSchema<definedNonNullAny> = z.any().refine((v) => isDefinedNonNullAny(v));
12+
export const definedNonNullAnySchema = z.any().refine((v) => isDefinedNonNullAny(v));
913

10-
export function AttributeInputSchema(): z.ZodSchema<AttributeInput> {
14+
export function AttributeInputSchema(): z.ZodObject<Properties<AttributeInput>> {
1115
return z.object({
1216
key: z.string().nullish(),
1317
val: z.string().nullish()
@@ -16,7 +20,7 @@ export function AttributeInputSchema(): z.ZodSchema<AttributeInput> {
1620

1721
export const ButtonComponentTypeSchema = z.nativeEnum(ButtonComponentType);
1822

19-
export function ComponentInputSchema(): z.ZodSchema<ComponentInput> {
23+
export function ComponentInputSchema(): z.ZodObject<Properties<ComponentInput>> {
2024
return z.object({
2125
child: z.lazy(() => ComponentInputSchema().nullish()),
2226
childrens: z.array(z.lazy(() => ComponentInputSchema().nullable())).nullish(),
@@ -26,21 +30,21 @@ export function ComponentInputSchema(): z.ZodSchema<ComponentInput> {
2630
})
2731
}
2832

29-
export function DropDownComponentInputSchema(): z.ZodSchema<DropDownComponentInput> {
33+
export function DropDownComponentInputSchema(): z.ZodObject<Properties<DropDownComponentInput>> {
3034
return z.object({
3135
dropdownComponent: z.lazy(() => ComponentInputSchema().nullish()),
3236
getEvent: z.lazy(() => EventInputSchema())
3337
})
3438
}
3539

36-
export function EventArgumentInputSchema(): z.ZodSchema<EventArgumentInput> {
40+
export function EventArgumentInputSchema(): z.ZodObject<Properties<EventArgumentInput>> {
3741
return z.object({
3842
name: z.string().min(5),
3943
value: z.string().regex(/^foo/, "message")
4044
})
4145
}
4246

43-
export function EventInputSchema(): z.ZodSchema<EventInput> {
47+
export function EventInputSchema(): z.ZodObject<Properties<EventInput>> {
4448
return z.object({
4549
arguments: z.array(z.lazy(() => EventArgumentInputSchema())),
4650
options: z.array(EventOptionTypeSchema).nullish()
@@ -49,7 +53,7 @@ export function EventInputSchema(): z.ZodSchema<EventInput> {
4953

5054
export const EventOptionTypeSchema = z.nativeEnum(EventOptionType);
5155

52-
export function HttpInputSchema(): z.ZodSchema<HttpInput> {
56+
export function HttpInputSchema(): z.ZodObject<Properties<HttpInput>> {
5357
return z.object({
5458
method: HttpMethodSchema.nullish(),
5559
url: definedNonNullAnySchema
@@ -58,13 +62,13 @@ export function HttpInputSchema(): z.ZodSchema<HttpInput> {
5862

5963
export const HttpMethodSchema = z.nativeEnum(HttpMethod);
6064

61-
export function LayoutInputSchema(): z.ZodSchema<LayoutInput> {
65+
export function LayoutInputSchema(): z.ZodObject<Properties<LayoutInput>> {
6266
return z.object({
6367
dropdown: z.lazy(() => DropDownComponentInputSchema().nullish())
6468
})
6569
}
6670

67-
export function PageInputSchema(): z.ZodSchema<PageInput> {
71+
export function PageInputSchema(): z.ZodObject<Properties<PageInput>> {
6872
return z.object({
6973
attributes: z.array(z.lazy(() => AttributeInputSchema())).nullish(),
7074
date: definedNonNullAnySchema.nullish(),

src/zod/index.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ export const ZodSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchema
3030
initialEmit: (): string =>
3131
'\n' +
3232
[
33+
new DeclarationBlock({})
34+
.asKind('type')
35+
.withName('Properties<T>')
36+
.withContent(['Required<{', ' [K in keyof T]: z.ZodType<T[K], any, T[K]>;', '}>'].join('\n')).string,
3337
// Unfortunately, zod doesn’t provide non-null defined any schema.
3438
// This is a temporary hack until it is fixed.
3539
// see: https://github.com/colinhacks/zod/issues/884
@@ -42,7 +46,7 @@ export const ZodSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchema
4246
new DeclarationBlock({})
4347
.export()
4448
.asKind('const')
45-
.withName(`${anySchema}: z.ZodSchema<definedNonNullAny>`)
49+
.withName(`${anySchema}`)
4650
.withContent(`z.any().refine((v) => isDefinedNonNullAny(v))`).string,
4751
].join('\n'),
4852
InputObjectTypeDefinition: (node: InputObjectTypeDefinitionNode) => {
@@ -56,7 +60,7 @@ export const ZodSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchema
5660
return new DeclarationBlock({})
5761
.export()
5862
.asKind('function')
59-
.withName(`${name}Schema(): z.ZodSchema<${name}>`)
63+
.withName(`${name}Schema(): z.ZodObject<Properties<${name}>>`)
6064
.withBlock([indent(`return z.object({`), shape, indent('})')].join('\n')).string;
6165
},
6266
EnumTypeDefinition: (node: EnumTypeDefinitionNode) => {

tests/zod.spec.ts

+11-11
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ describe('zod', () => {
1515
}
1616
`,
1717
[
18-
'export function PrimitiveInputSchema(): z.ZodSchema<PrimitiveInput>',
18+
'export function PrimitiveInputSchema(): z.ZodObject<Properties<PrimitiveInput>>',
1919
'a: z.string()',
2020
'b: z.string()',
2121
'c: z.boolean()',
@@ -36,7 +36,7 @@ describe('zod', () => {
3636
}
3737
`,
3838
[
39-
'export function PrimitiveInputSchema(): z.ZodSchema<PrimitiveInput>',
39+
'export function PrimitiveInputSchema(): z.ZodObject<Properties<PrimitiveInput>>',
4040
// alphabet order
4141
'a: z.string().nullish(),',
4242
'b: z.string().nullish(),',
@@ -58,7 +58,7 @@ describe('zod', () => {
5858
}
5959
`,
6060
[
61-
'export function ArrayInputSchema(): z.ZodSchema<ArrayInput>',
61+
'export function ArrayInputSchema(): z.ZodObject<Properties<ArrayInput>>',
6262
'a: z.array(z.string().nullable()).nullish(),',
6363
'b: z.array(z.string()).nullish(),',
6464
'c: z.array(z.string()),',
@@ -81,11 +81,11 @@ describe('zod', () => {
8181
}
8282
`,
8383
[
84-
'export function AInputSchema(): z.ZodSchema<AInput>',
84+
'export function AInputSchema(): z.ZodObject<Properties<AInput>>',
8585
'b: z.lazy(() => BInputSchema())',
86-
'export function BInputSchema(): z.ZodSchema<BInput>',
86+
'export function BInputSchema(): z.ZodObject<Properties<BInput>>',
8787
'c: z.lazy(() => CInputSchema())',
88-
'export function CInputSchema(): z.ZodSchema<CInput>',
88+
'export function CInputSchema(): z.ZodObject<Properties<CInput>>',
8989
'a: z.lazy(() => AInputSchema())',
9090
],
9191
],
@@ -98,7 +98,7 @@ describe('zod', () => {
9898
}
9999
`,
100100
[
101-
'export function NestedInputSchema(): z.ZodSchema<NestedInput>',
101+
'export function NestedInputSchema(): z.ZodObject<Properties<NestedInput>>',
102102
'child: z.lazy(() => NestedInputSchema().nullish()),',
103103
'childrens: z.array(z.lazy(() => NestedInputSchema().nullable())).nullish()',
104104
],
@@ -116,7 +116,7 @@ describe('zod', () => {
116116
`,
117117
[
118118
'export const PageTypeSchema = z.nativeEnum(PageType)',
119-
'export function PageInputSchema(): z.ZodSchema<PageInput>',
119+
'export function PageInputSchema(): z.ZodObject<Properties<PageInput>>',
120120
'pageType: PageTypeSchema',
121121
],
122122
],
@@ -136,7 +136,7 @@ describe('zod', () => {
136136
scalar URL # unknown scalar, should be any (definedNonNullAnySchema)
137137
`,
138138
[
139-
'export function HttpInputSchema(): z.ZodSchema<HttpInput>',
139+
'export function HttpInputSchema(): z.ZodObject<Properties<HttpInput>>',
140140
'export const HttpMethodSchema = z.nativeEnum(HttpMethod)',
141141
'method: HttpMethodSchema',
142142
'url: definedNonNullAnySchema',
@@ -236,7 +236,7 @@ describe('zod', () => {
236236
{}
237237
);
238238
const wantContains = [
239-
'export function PrimitiveInputSchema(): z.ZodSchema<PrimitiveInput>',
239+
'export function PrimitiveInputSchema(): z.ZodObject<Properties<PrimitiveInput>>',
240240
'a: z.string().min(1),',
241241
'b: z.string().min(1),',
242242
'c: z.boolean(),',
@@ -271,7 +271,7 @@ describe('zod', () => {
271271
{}
272272
);
273273
const wantContains = [
274-
'export function ScalarsInputSchema(): z.ZodSchema<ScalarsInput>',
274+
'export function ScalarsInputSchema(): z.ZodObject<Properties<ScalarsInput>>',
275275
'date: z.date(),',
276276
'email: z.string().email().nullish(),',
277277
'str: z.string()',

0 commit comments

Comments
 (0)