Skip to content

Commit

Permalink
fix: strict() keyword working root interfaces (#134)
Browse files Browse the repository at this point in the history
* test: failing test case

* fix: missing strict() on root schemas

* test: additional test case

---------

Co-authored-by: tvillaren <tvillaren@users.noreply.github.com>
  • Loading branch information
tvillaren and tvillaren committed May 24, 2023
1 parent 1092f10 commit a9f6f03
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
48 changes: 48 additions & 0 deletions src/core/generate.test.ts
Expand Up @@ -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()
});
"
`);
});
});
});
14 changes: 14 additions & 0 deletions src/core/generateZodSchema.ts
Expand Up @@ -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)) {
Expand Down

0 comments on commit a9f6f03

Please sign in to comment.