From baba818892de8f224535b80a1bff3355382cf3a0 Mon Sep 17 00:00:00 2001 From: Kei Kamikawa Date: Wed, 9 Mar 2022 11:15:37 +0900 Subject: [PATCH 1/2] fixed return type to be z.ZodObject --- example/zod/schemas.ts | 22 +++++++++++++--------- src/zod/index.ts | 9 +++++++-- tests/zod.spec.ts | 22 +++++++++++----------- 3 files changed, 31 insertions(+), 22 deletions(-) diff --git a/example/zod/schemas.ts b/example/zod/schemas.ts index 842c45e8..7592f8e0 100644 --- a/example/zod/schemas.ts +++ b/example/zod/schemas.ts @@ -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 = Required<{ + [K in keyof T]: z.ZodType; +}>; + type definedNonNullAny = {}; export const isDefinedNonNullAny = (v: any): v is definedNonNullAny => v !== undefined && v !== null; -export const definedNonNullAnySchema: z.ZodSchema = z.any().refine((v) => isDefinedNonNullAny(v)); +export const definedNonNullAnySchema = z.any().refine((v) => isDefinedNonNullAny(v)); -export function AttributeInputSchema(): z.ZodSchema { +export function AttributeInputSchema(): z.ZodObject> { return z.object({ key: z.string().nullish(), val: z.string().nullish() @@ -16,7 +20,7 @@ export function AttributeInputSchema(): z.ZodSchema { export const ButtonComponentTypeSchema = z.nativeEnum(ButtonComponentType); -export function ComponentInputSchema(): z.ZodSchema { +export function ComponentInputSchema(): z.ZodObject> { return z.object({ child: z.lazy(() => ComponentInputSchema().nullish()), childrens: z.array(z.lazy(() => ComponentInputSchema().nullable())).nullish(), @@ -26,21 +30,21 @@ export function ComponentInputSchema(): z.ZodSchema { }) } -export function DropDownComponentInputSchema(): z.ZodSchema { +export function DropDownComponentInputSchema(): z.ZodObject> { return z.object({ dropdownComponent: z.lazy(() => ComponentInputSchema().nullish()), getEvent: z.lazy(() => EventInputSchema()) }) } -export function EventArgumentInputSchema(): z.ZodSchema { +export function EventArgumentInputSchema(): z.ZodObject> { return z.object({ name: z.string().min(5), value: z.string().regex(/^foo/, "message") }) } -export function EventInputSchema(): z.ZodSchema { +export function EventInputSchema(): z.ZodObject> { return z.object({ arguments: z.array(z.lazy(() => EventArgumentInputSchema())), options: z.array(EventOptionTypeSchema).nullish() @@ -49,7 +53,7 @@ export function EventInputSchema(): z.ZodSchema { export const EventOptionTypeSchema = z.nativeEnum(EventOptionType); -export function HttpInputSchema(): z.ZodSchema { +export function HttpInputSchema(): z.ZodObject> { return z.object({ method: HttpMethodSchema.nullish(), url: definedNonNullAnySchema @@ -58,13 +62,13 @@ export function HttpInputSchema(): z.ZodSchema { export const HttpMethodSchema = z.nativeEnum(HttpMethod); -export function LayoutInputSchema(): z.ZodSchema { +export function LayoutInputSchema(): z.ZodObject> { return z.object({ dropdown: z.lazy(() => DropDownComponentInputSchema().nullish()) }) } -export function PageInputSchema(): z.ZodSchema { +export function PageInputSchema(): z.ZodObject> { return z.object({ attributes: z.array(z.lazy(() => AttributeInputSchema())).nullish(), date: definedNonNullAnySchema.nullish(), diff --git a/src/zod/index.ts b/src/zod/index.ts index c0ab8f7a..69255ec3 100644 --- a/src/zod/index.ts +++ b/src/zod/index.ts @@ -30,6 +30,11 @@ export const ZodSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchema initialEmit: (): string => '\n' + [ + new DeclarationBlock({}).asKind('type').withName('Properties').withContent([ + 'Required<{', + ' [K in keyof T]: z.ZodType;', + '}>', + ].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 @@ -42,7 +47,7 @@ export const ZodSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchema new DeclarationBlock({}) .export() .asKind('const') - .withName(`${anySchema}: z.ZodSchema`) + .withName(`${anySchema}`) .withContent(`z.any().refine((v) => isDefinedNonNullAny(v))`).string, ].join('\n'), InputObjectTypeDefinition: (node: InputObjectTypeDefinitionNode) => { @@ -56,7 +61,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>`) .withBlock([indent(`return z.object({`), shape, indent('})')].join('\n')).string; }, EnumTypeDefinition: (node: EnumTypeDefinitionNode) => { diff --git a/tests/zod.spec.ts b/tests/zod.spec.ts index 37d9787f..ba21e604 100644 --- a/tests/zod.spec.ts +++ b/tests/zod.spec.ts @@ -15,7 +15,7 @@ describe('zod', () => { } `, [ - 'export function PrimitiveInputSchema(): z.ZodSchema', + 'export function PrimitiveInputSchema(): z.ZodObject>', 'a: z.string()', 'b: z.string()', 'c: z.boolean()', @@ -36,7 +36,7 @@ describe('zod', () => { } `, [ - 'export function PrimitiveInputSchema(): z.ZodSchema', + 'export function PrimitiveInputSchema(): z.ZodObject>', // alphabet order 'a: z.string().nullish(),', 'b: z.string().nullish(),', @@ -58,7 +58,7 @@ describe('zod', () => { } `, [ - 'export function ArrayInputSchema(): z.ZodSchema', + 'export function ArrayInputSchema(): z.ZodObject>', 'a: z.array(z.string().nullable()).nullish(),', 'b: z.array(z.string()).nullish(),', 'c: z.array(z.string()),', @@ -81,11 +81,11 @@ describe('zod', () => { } `, [ - 'export function AInputSchema(): z.ZodSchema', + 'export function AInputSchema(): z.ZodObject>', 'b: z.lazy(() => BInputSchema())', - 'export function BInputSchema(): z.ZodSchema', + 'export function BInputSchema(): z.ZodObject>', 'c: z.lazy(() => CInputSchema())', - 'export function CInputSchema(): z.ZodSchema', + 'export function CInputSchema(): z.ZodObject>', 'a: z.lazy(() => AInputSchema())', ], ], @@ -98,7 +98,7 @@ describe('zod', () => { } `, [ - 'export function NestedInputSchema(): z.ZodSchema', + 'export function NestedInputSchema(): z.ZodObject>', 'child: z.lazy(() => NestedInputSchema().nullish()),', 'childrens: z.array(z.lazy(() => NestedInputSchema().nullable())).nullish()', ], @@ -116,7 +116,7 @@ describe('zod', () => { `, [ 'export const PageTypeSchema = z.nativeEnum(PageType)', - 'export function PageInputSchema(): z.ZodSchema', + 'export function PageInputSchema(): z.ZodObject>', 'pageType: PageTypeSchema', ], ], @@ -136,7 +136,7 @@ describe('zod', () => { scalar URL # unknown scalar, should be any (definedNonNullAnySchema) `, [ - 'export function HttpInputSchema(): z.ZodSchema', + 'export function HttpInputSchema(): z.ZodObject>', 'export const HttpMethodSchema = z.nativeEnum(HttpMethod)', 'method: HttpMethodSchema', 'url: definedNonNullAnySchema', @@ -236,7 +236,7 @@ describe('zod', () => { {} ); const wantContains = [ - 'export function PrimitiveInputSchema(): z.ZodSchema', + 'export function PrimitiveInputSchema(): z.ZodObject>', 'a: z.string().min(1),', 'b: z.string().min(1),', 'c: z.boolean(),', @@ -271,7 +271,7 @@ describe('zod', () => { {} ); const wantContains = [ - 'export function ScalarsInputSchema(): z.ZodSchema', + 'export function ScalarsInputSchema(): z.ZodObject>', 'date: z.date(),', 'email: z.string().email().nullish(),', 'str: z.string()', From 0dcfdbf6492aa260f2e71aa1111e88f5e631fbb1 Mon Sep 17 00:00:00 2001 From: Code-Hex Date: Wed, 9 Mar 2022 02:17:09 +0000 Subject: [PATCH 2/2] Apply auto lint-fix changes --- src/zod/index.ts | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/zod/index.ts b/src/zod/index.ts index 69255ec3..1e1d030f 100644 --- a/src/zod/index.ts +++ b/src/zod/index.ts @@ -30,11 +30,10 @@ export const ZodSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchema initialEmit: (): string => '\n' + [ - new DeclarationBlock({}).asKind('type').withName('Properties').withContent([ - 'Required<{', - ' [K in keyof T]: z.ZodType;', - '}>', - ].join('\n')).string, + new DeclarationBlock({}) + .asKind('type') + .withName('Properties') + .withContent(['Required<{', ' [K in keyof T]: z.ZodType;', '}>'].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