Skip to content

x-to-zod / Builder

Interface: Builder<Z, T>

Defined in: src/Builder/index.ts:20

Builder — output contract for all schema parsers.

Extracted from the concrete ZodBuilder class so that:

  • Parser return types are statically verifiable against the interface, not a class.
  • Alternative output targets (AST, dry-run, Arktype) can be implemented without touching core parsing code.
  • Custom parsers registered via registerParser() are type-checked to return a Builder, eliminating the untyped string escape hatch.

All modifier methods return Builder (not this) to keep the interface simple and implementable by third parties. The concrete ZodBuilder class retains this return types for its own fluent chaining — TypeScript accepts this structural assignment.

Type Parameters

Z

Z extends ZodType = ZodType

T

T = Z extends ZodStringFormat ? Z["format"] : Z["type"]

Properties

typeKind

ts
readonly typeKind: T;

Defined in: src/Builder/index.ts:28

Parser type discriminator (e.g. 'string', 'object', 'anyOf'). Set by the parser that creates this builder.

Methods

brand()

ts
brand(brand): this;

Defined in: src/Builder/index.ts:58

Apply .brand(brand) modifier for nominal typing.

Parameters

brand

string

Brand string literal.

Returns

this


catch()

ts
catch(value): this;

Defined in: src/Builder/index.ts:67

Apply .catch(fallback) modifier for parse-failure recovery.

Parameters

value

unknown

Fallback value serialised as JSON in the generated code.

Returns

this


default()

ts
default(value): this;

Defined in: src/Builder/index.ts:46

Apply .default(value) modifier.

Parameters

value

unknown

Serialised as JSON in the generated code.

Returns

this


describe()

ts
describe(description): this;

Defined in: src/Builder/index.ts:52

Apply .describe(description) modifier.

Parameters

description

string

JSDoc description string.

Returns

this


meta()

ts
meta(metadata): this;

Defined in: src/Builder/index.ts:86

Apply .meta(obj) metadata (Zod v4+).

Parameters

metadata

Record<string, unknown>

Arbitrary key/value metadata object.

Returns

this


nullable()

ts
nullable(): this;

Defined in: src/Builder/index.ts:40

Apply .nullable() modifier to the generated schema.

Returns

this


optional()

ts
optional(): this;

Defined in: src/Builder/index.ts:37

Apply .optional() modifier to the generated schema.

Returns

this


readonly()

ts
readonly(): this;

Defined in: src/Builder/index.ts:61

Apply .readonly() modifier to the generated schema.

Returns

this


refine()

ts
refine(refineFn, message?): this;

Defined in: src/Builder/index.ts:74

Apply .refine(fn, message?) constraint.

Parameters

refineFn

string

Refine predicate as a code string (e.g. "(val) => val > 0").

message?

string

Optional error message string.

Returns

this


superRefine()

ts
superRefine(superRefineFn): this;

Defined in: src/Builder/index.ts:80

Apply .superRefine(fn) constraint.

Parameters

superRefineFn

string

SuperRefine function as a code string.

Returns

this


text()

ts
text(): string;

Defined in: src/Builder/index.ts:34

Emit the final Zod expression as a TypeScript code string. Example: z.string().optional().describe("name")

Returns

string


transform()

ts
transform(transformFn): this;

Defined in: src/Builder/index.ts:92

Apply .transform(fn) mapping.

Parameters

transformFn

string

Transform function as a code string.

Returns

this

Released under the ISC License.