From ae610410b1a6d8caeaa4caa614bf2d69613a6f36 Mon Sep 17 00:00:00 2001 From: Fabien BERNARD Date: Mon, 3 May 2021 16:31:01 +0200 Subject: [PATCH] fix: Fix optional array (#20) closed #18 --- src/core/generateZodSchema.test.ts | 19 +++++++++++++++---- src/core/generateZodSchema.ts | 2 +- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/core/generateZodSchema.test.ts b/src/core/generateZodSchema.test.ts index 45cd242..08a02d7 100644 --- a/src/core/generateZodSchema.test.ts +++ b/src/core/generateZodSchema.test.ts @@ -68,9 +68,9 @@ describe("generateZodSchema", () => { }); it("should generate a literal schema (number)", () => { - const source = `export type IdentiesCount = 2;`; + const source = `export type IdentitiesCount = 2;`; expect(generate(source)).toMatchInlineSnapshot( - `"export const identiesCountSchema = z.literal(2);"` + `"export const identitiesCountSchema = z.literal(2);"` ); }); @@ -164,9 +164,9 @@ describe("generateZodSchema", () => { }); it("should generate a record schema", () => { - const source = `export type EnnemiesPowers = Record;`; + const source = `export type EnemiesPowers = Record;`; expect(generate(source)).toMatchInlineSnapshot( - `"export const ennemiesPowersSchema = z.record(powerSchema);"` + `"export const enemiesPowersSchema = z.record(powerSchema);"` ); }); @@ -368,6 +368,17 @@ describe("generateZodSchema", () => { `); }); + it("should deal with optional array", () => { + const source = `export interface Collection { + movies?: Array + }`; + expect(generate(source)).toMatchInlineSnapshot(` + "export const collectionSchema = z.object({ + movies: z.array(z.string()).optional() + });" + `); + }); + it("should generate an empty object schema", () => { const source = `export type Empty = {};`; expect(generate(source)).toMatchInlineSnapshot( diff --git a/src/core/generateZodSchema.ts b/src/core/generateZodSchema.ts index ff2139e..bb7dd5c 100644 --- a/src/core/generateZodSchema.ts +++ b/src/core/generateZodSchema.ts @@ -212,7 +212,7 @@ function buildZodPrimitive({ return buildZodPrimitive({ z, typeNode: f.createArrayTypeNode(typeNode.typeArguments[0]), - isOptional: false, + isOptional, jsDocTags: {}, sourceFile, dependencies,