Skip to content

fixed return type to be z.ZodObject #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions example/zod/schemas.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import { z } from 'zod'
import { AttributeInput, ButtonComponentType, ComponentInput, DropDownComponentInput, EventArgumentInput, EventInput, EventOptionType, HttpInput, HttpMethod, LayoutInput, PageInput, PageType } from '../types'

type Properties<T> = Required<{
[K in keyof T]: z.ZodType<T[K], any, T[K]>;
}>;

type definedNonNullAny = {};

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

export const definedNonNullAnySchema: z.ZodSchema<definedNonNullAny> = z.any().refine((v) => isDefinedNonNullAny(v));
export const definedNonNullAnySchema = z.any().refine((v) => isDefinedNonNullAny(v));

export function AttributeInputSchema(): z.ZodSchema<AttributeInput> {
export function AttributeInputSchema(): z.ZodObject<Properties<AttributeInput>> {
return z.object({
key: z.string().nullish(),
val: z.string().nullish()
Expand All @@ -16,7 +20,7 @@ export function AttributeInputSchema(): z.ZodSchema<AttributeInput> {

export const ButtonComponentTypeSchema = z.nativeEnum(ButtonComponentType);

export function ComponentInputSchema(): z.ZodSchema<ComponentInput> {
export function ComponentInputSchema(): z.ZodObject<Properties<ComponentInput>> {
return z.object({
child: z.lazy(() => ComponentInputSchema().nullish()),
childrens: z.array(z.lazy(() => ComponentInputSchema().nullable())).nullish(),
Expand All @@ -26,21 +30,21 @@ export function ComponentInputSchema(): z.ZodSchema<ComponentInput> {
})
}

export function DropDownComponentInputSchema(): z.ZodSchema<DropDownComponentInput> {
export function DropDownComponentInputSchema(): z.ZodObject<Properties<DropDownComponentInput>> {
return z.object({
dropdownComponent: z.lazy(() => ComponentInputSchema().nullish()),
getEvent: z.lazy(() => EventInputSchema())
})
}

export function EventArgumentInputSchema(): z.ZodSchema<EventArgumentInput> {
export function EventArgumentInputSchema(): z.ZodObject<Properties<EventArgumentInput>> {
return z.object({
name: z.string().min(5),
value: z.string().regex(/^foo/, "message")
})
}

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

export const EventOptionTypeSchema = z.nativeEnum(EventOptionType);

export function HttpInputSchema(): z.ZodSchema<HttpInput> {
export function HttpInputSchema(): z.ZodObject<Properties<HttpInput>> {
return z.object({
method: HttpMethodSchema.nullish(),
url: definedNonNullAnySchema
Expand All @@ -58,13 +62,13 @@ export function HttpInputSchema(): z.ZodSchema<HttpInput> {

export const HttpMethodSchema = z.nativeEnum(HttpMethod);

export function LayoutInputSchema(): z.ZodSchema<LayoutInput> {
export function LayoutInputSchema(): z.ZodObject<Properties<LayoutInput>> {
return z.object({
dropdown: z.lazy(() => DropDownComponentInputSchema().nullish())
})
}

export function PageInputSchema(): z.ZodSchema<PageInput> {
export function PageInputSchema(): z.ZodObject<Properties<PageInput>> {
return z.object({
attributes: z.array(z.lazy(() => AttributeInputSchema())).nullish(),
date: definedNonNullAnySchema.nullish(),
Expand Down
8 changes: 6 additions & 2 deletions src/zod/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ export const ZodSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchema
initialEmit: (): string =>
'\n' +
[
new DeclarationBlock({})
.asKind('type')
.withName('Properties<T>')
.withContent(['Required<{', ' [K in keyof T]: z.ZodType<T[K], any, T[K]>;', '}>'].join('\n')).string,
// Unfortunately, zod doesn’t provide non-null defined any schema.
// This is a temporary hack until it is fixed.
// see: https://github.com/colinhacks/zod/issues/884
Expand All @@ -42,7 +46,7 @@ export const ZodSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchema
new DeclarationBlock({})
.export()
.asKind('const')
.withName(`${anySchema}: z.ZodSchema<definedNonNullAny>`)
.withName(`${anySchema}`)
.withContent(`z.any().refine((v) => isDefinedNonNullAny(v))`).string,
].join('\n'),
InputObjectTypeDefinition: (node: InputObjectTypeDefinitionNode) => {
Expand All @@ -56,7 +60,7 @@ export const ZodSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchema
return new DeclarationBlock({})
.export()
.asKind('function')
.withName(`${name}Schema(): z.ZodSchema<${name}>`)
.withName(`${name}Schema(): z.ZodObject<Properties<${name}>>`)
.withBlock([indent(`return z.object({`), shape, indent('})')].join('\n')).string;
},
EnumTypeDefinition: (node: EnumTypeDefinitionNode) => {
Expand Down
22 changes: 11 additions & 11 deletions tests/zod.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('zod', () => {
}
`,
[
'export function PrimitiveInputSchema(): z.ZodSchema<PrimitiveInput>',
'export function PrimitiveInputSchema(): z.ZodObject<Properties<PrimitiveInput>>',
'a: z.string()',
'b: z.string()',
'c: z.boolean()',
Expand All @@ -36,7 +36,7 @@ describe('zod', () => {
}
`,
[
'export function PrimitiveInputSchema(): z.ZodSchema<PrimitiveInput>',
'export function PrimitiveInputSchema(): z.ZodObject<Properties<PrimitiveInput>>',
// alphabet order
'a: z.string().nullish(),',
'b: z.string().nullish(),',
Expand All @@ -58,7 +58,7 @@ describe('zod', () => {
}
`,
[
'export function ArrayInputSchema(): z.ZodSchema<ArrayInput>',
'export function ArrayInputSchema(): z.ZodObject<Properties<ArrayInput>>',
'a: z.array(z.string().nullable()).nullish(),',
'b: z.array(z.string()).nullish(),',
'c: z.array(z.string()),',
Expand All @@ -81,11 +81,11 @@ describe('zod', () => {
}
`,
[
'export function AInputSchema(): z.ZodSchema<AInput>',
'export function AInputSchema(): z.ZodObject<Properties<AInput>>',
'b: z.lazy(() => BInputSchema())',
'export function BInputSchema(): z.ZodSchema<BInput>',
'export function BInputSchema(): z.ZodObject<Properties<BInput>>',
'c: z.lazy(() => CInputSchema())',
'export function CInputSchema(): z.ZodSchema<CInput>',
'export function CInputSchema(): z.ZodObject<Properties<CInput>>',
'a: z.lazy(() => AInputSchema())',
],
],
Expand All @@ -98,7 +98,7 @@ describe('zod', () => {
}
`,
[
'export function NestedInputSchema(): z.ZodSchema<NestedInput>',
'export function NestedInputSchema(): z.ZodObject<Properties<NestedInput>>',
'child: z.lazy(() => NestedInputSchema().nullish()),',
'childrens: z.array(z.lazy(() => NestedInputSchema().nullable())).nullish()',
],
Expand All @@ -116,7 +116,7 @@ describe('zod', () => {
`,
[
'export const PageTypeSchema = z.nativeEnum(PageType)',
'export function PageInputSchema(): z.ZodSchema<PageInput>',
'export function PageInputSchema(): z.ZodObject<Properties<PageInput>>',
'pageType: PageTypeSchema',
],
],
Expand All @@ -136,7 +136,7 @@ describe('zod', () => {
scalar URL # unknown scalar, should be any (definedNonNullAnySchema)
`,
[
'export function HttpInputSchema(): z.ZodSchema<HttpInput>',
'export function HttpInputSchema(): z.ZodObject<Properties<HttpInput>>',
'export const HttpMethodSchema = z.nativeEnum(HttpMethod)',
'method: HttpMethodSchema',
'url: definedNonNullAnySchema',
Expand Down Expand Up @@ -236,7 +236,7 @@ describe('zod', () => {
{}
);
const wantContains = [
'export function PrimitiveInputSchema(): z.ZodSchema<PrimitiveInput>',
'export function PrimitiveInputSchema(): z.ZodObject<Properties<PrimitiveInput>>',
'a: z.string().min(1),',
'b: z.string().min(1),',
'c: z.boolean(),',
Expand Down Expand Up @@ -271,7 +271,7 @@ describe('zod', () => {
{}
);
const wantContains = [
'export function ScalarsInputSchema(): z.ZodSchema<ScalarsInput>',
'export function ScalarsInputSchema(): z.ZodObject<Properties<ScalarsInput>>',
'date: z.date(),',
'email: z.string().email().nullish(),',
'str: z.string()',
Expand Down