Repository: fabien0102/ts-to-zod
Pull request #20 (2021-05-03 14:28:37)

Fix optional array
# Why

Fix optional key on `Array<T>` syntax generation

## Related issue

#18


codecov-commenter: # [Codecov](https://codecov.io/gh/fabien0102/ts-to-zod/pull/20?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Fabien+BERNARD) Report
> Merging [#20](https://codecov.io/gh/fabien0102/ts-to-zod/pull/20?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Fabien+BERNARD) (7c9b4b6) into [main](https://codecov.io/gh/fabien0102/ts-to-zod/commit/e5e49615d832129cae8a23cbf5fa0b35024e15e2?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Fabien+BERNARD) (e5e4961) will **not change** coverage.
> The diff coverage is `n/a`.

[![Impacted file tree graph](https://codecov.io/gh/fabien0102/ts-to-zod/pull/20/graphs/tree.svg?width=650&height=150&src=pr&token=W5M8UIJ59C&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Fabien+BERNARD)](https://codecov.io/gh/fabien0102/ts-to-zod/pull/20?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Fabien+BERNARD)

```diff
@@           Coverage Diff           @@
##             main      #20   +/-   ##
=======================================
  Coverage   98.49%   98.49%           
=======================================
  Files           8        8           
  Lines         332      332           
  Branches      102      102           
=======================================
  Hits          327      327           
  Misses          5        5           
```


| [Impacted Files](https://codecov.io/gh/fabien0102/ts-to-zod/pull/20?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Fabien+BERNARD) | Coverage Δ | |
|---|---|---|
| [src/core/generateZodSchema.ts](https://codecov.io/gh/fabien0102/ts-to-zod/pull/20/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Fabien+BERNARD#diff-c3JjL2NvcmUvZ2VuZXJhdGVab2RTY2hlbWEudHM=) | `97.20% <ø> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/fabien0102/ts-to-zod/pull/20?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Fabien+BERNARD).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Fabien+BERNARD)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/fabien0102/ts-to-zod/pull/20?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Fabien+BERNARD). Last update [6cf4a32...7c9b4b6](https://codecov.io/gh/fabien0102/ts-to-zod/pull/20?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Fabien+BERNARD). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Fabien+BERNARD).


Patch:
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,


Test patch:
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<string, Power>;`;
+    const source = `export type EnemiesPowers = Record<string, Power>;`;
     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<string>
+    }`;
+    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(