Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ import { legalizeName } from "../JavaScript/utils.js";

import type { typeScriptZodOptions } from "./language.js";

type TypeScriptZodRendererOptions = Omit<
OptionValues<typeof typeScriptZodOptions>,
"preferUnknown"
> &
Partial<Pick<OptionValues<typeof typeScriptZodOptions>, "preferUnknown">>;

export class TypeScriptZodRenderer extends ConvenienceRenderer {
/** TypeRefs of object types that participate in a reference cycle.
* These must be emitted as z.lazy() schemas with an explicit type
Expand All @@ -42,7 +48,7 @@ export class TypeScriptZodRenderer extends ConvenienceRenderer {
public constructor(
targetLanguage: TargetLanguage,
renderContext: RenderContext,
protected readonly _options: OptionValues<typeof typeScriptZodOptions>,
protected readonly _options: TypeScriptZodRendererOptions,
) {
super(targetLanguage, renderContext);
}
Expand Down Expand Up @@ -106,7 +112,10 @@ export class TypeScriptZodRenderer extends ConvenienceRenderer {

const match = matchType<Sourcelike>(
t,
(_anyType) => "z.any()",
(_anyType) =>
this._options.preferUnknown !== false
? "z.unknown()"
: "z.any()",
(_nullType) => "z.null()",
(_boolType) => "z.boolean()",
(_integerType) => "z.number()",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ import { TypeScriptZodRenderer } from "./TypeScriptZodRenderer.js";

export const typeScriptZodOptions = {
justSchema: new BooleanOption("just-schema", "Schema only", false),
preferUnknown: new BooleanOption(
"prefer-unknown",
"Use unknown instead of any",
false,
),
};

export const typeScriptZodLanguageConfig = {
Expand All @@ -35,8 +40,8 @@ export class TypeScriptZodTargetLanguage extends TargetLanguage<
super(typeScriptZodLanguageConfig);
}

public getOptions(): Record<string, never> {
return {};
public getOptions(): typeof typeScriptZodOptions {
return typeScriptZodOptions;
}

public get stringTypeMapping(): StringTypeMapping {
Expand Down
44 changes: 44 additions & 0 deletions test/unit/typescript-zod-unknown.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import {
InputData,
JSONSchemaInput,
type RendererOptions,
quicktype,
} from "quicktype-core";
import { expect, test } from "vitest";

async function render(rendererOptions: RendererOptions = {}): Promise<string> {
const schemaInput = new JSONSchemaInput(undefined);
await schemaInput.addSource({
name: "TopLevel",
schema: JSON.stringify({
type: "object",
properties: {
value: {},
},
required: ["value"],
}),
});
const inputData = new InputData();
inputData.addInput(schemaInput);

const result = await quicktype({
inputData,
lang: "typescript-zod",
rendererOptions,
});
return result.lines.join("\n");
}

test("TypeScript Zod emits any for unconstrained JSON Schema values by default", async () => {
const output = await render();

expect(output).toContain("z.any()");
expect(output).not.toContain("z.unknown()");
});

test("TypeScript Zod emits unknown when prefer-unknown is enabled", async () => {
const output = await render({ "prefer-unknown": true });

expect(output).toContain("z.unknown()");
expect(output).not.toContain("z.any()");
});