Repository: fabien0102/ts-to-zod
Pull request #134 (2023-05-18 07:32:46)

[Fix] strict() keyword working root interfaces
# Why

This finally solves https://github.com/fabien0102/ts-to-zod/issues/78 using a new @strict JSDoc tag key, amending PR https://github.com/fabien0102/ts-to-zod/pull/131


codecov-commenter: ## [Codecov](https://app.codecov.io/gh/fabien0102/ts-to-zod/pull/134?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Fabien+BERNARD) Report
> Merging [#134](https://app.codecov.io/gh/fabien0102/ts-to-zod/pull/134?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Fabien+BERNARD) (1fc4314) into [main](https://app.codecov.io/gh/fabien0102/ts-to-zod/commit/1092f10359c3830cdeb29689321ddaa945fce7b9?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Fabien+BERNARD) (1092f10) will **increase** coverage by `0.02%`.
> The diff coverage is `100.00%`.

:exclamation: Your organization is not using the GitHub App Integration. As a result you may experience degraded service beginning May 15th. Please [install the Github App Integration](https://github.com/apps/codecov) for your organization. [Read more](https://about.codecov.io/blog/codecov-is-updating-its-github-integration/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Fabien+BERNARD).

```diff
@@            Coverage Diff             @@
##             main     #134      +/-   ##
==========================================
+ Coverage   96.01%   96.04%   +0.02%     
==========================================
  Files          13       13              
  Lines         653      657       +4     
  Branches      263      265       +2     
==========================================
+ Hits          627      631       +4     
  Misses         26       26              
```


| [Impacted Files](https://app.codecov.io/gh/fabien0102/ts-to-zod/pull/134?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://app.codecov.io/gh/fabien0102/ts-to-zod/pull/134?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Fabien+BERNARD#diff-c3JjL2NvcmUvZ2VuZXJhdGVab2RTY2hlbWEudHM=) | `96.19% <100.00%> (+0.05%)` | :arrow_up: |


tvillaren: @fabien0102 : you may have missed this, but my previous PR was missing a big chunk 🙈 so it would be preferable not to wait too long before shipping the fix 🙏 😉 

Patch:
diff --git a/src/core/generateZodSchema.ts b/src/core/generateZodSchema.ts
index e5925cf..6c780f8 100644
--- a/src/core/generateZodSchema.ts
+++ b/src/core/generateZodSchema.ts
@@ -108,6 +108,20 @@ export function generateZodSchemaVariableStatement({
       schemaExtensionClauses,
       skipParseJSDoc,
     });
+
+    if (!skipParseJSDoc) {
+      const jsDocTags = getJSDocTags(node, sourceFile);
+      if (jsDocTags.strict) {
+        schema = f.createCallExpression(
+          f.createPropertyAccessExpression(
+            schema,
+            f.createIdentifier("strict")
+          ),
+          undefined,
+          undefined
+        );
+      }
+    }
   }
 
   if (ts.isTypeAliasDeclaration(node)) {


Test patch:
diff --git a/src/core/generate.test.ts b/src/core/generate.test.ts
index 5a810a3..6da4cfd 100644
--- a/src/core/generate.test.ts
+++ b/src/core/generate.test.ts
@@ -478,4 +478,52 @@ describe("generate", () => {
       expect(errors).toEqual([]);
     });
   });
+
+  describe("with @strict tag", () => {
+    it("should generate strict keyword", () => {
+      const sourceText = `
+      /**
+       * @strict
+       */
+      export interface Superman {
+        name: string;
+        age: number;
+      }
+
+      export interface Villain {
+        name: string;
+        /**
+         * @strict
+         */
+        nemesis: {
+          name: string;
+          ref: Superman
+        };
+      }
+      `;
+
+      const { getZodSchemasFile } = generate({
+        sourceText,
+      });
+
+      expect(getZodSchemasFile("./source")).toMatchInlineSnapshot(`
+      "// Generated by ts-to-zod
+      import { z } from \\"zod\\";
+
+      export const supermanSchema = z.object({
+          name: z.string(),
+          age: z.number()
+      }).strict();
+
+      export const villainSchema = z.object({
+          name: z.string(),
+          nemesis: z.object({
+              name: z.string(),
+              ref: supermanSchema
+          }).strict()
+      });
+      "
+      `);
+    });
+  });
 });