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() + }); + " + `); + }); + }); }); diff --git a/src/core/generateZodSchema.ts b/src/core/generateZodSchema.ts index e5925cf3..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)) {