Skip to content

x-to-zod / ZodBuilder / ArrayBuilder

Class: ArrayBuilder<Z> ​

Defined in: src/ZodBuilder/array.ts:28

Fluent ArrayBuilder: wraps a Zod array schema string and provides chainable methods.

NONEMPTY() TYPE INFERENCE - Version Notes: Both Zod v3 and v4 support .nonempty() with identical validation behavior. However, type inference differs:

  • .nonempty() infers [T, ...T[]] (tuple-like with at least one element)
  • .min(1) infers T[] (regular array)

Implementation: ArrayBuilder uses .min(1) instead of .nonempty() for:

  1. Consistency across versions
  2. Clarity in error messages
  3. Alignment with JSON Schema constraints (which don't express tuple constraints)

The validation is functionally identical in both v3 and v4. See ARRAY-NONEMPTY-NOTES.md for details.

Extends ​

  • BaseBuilder<ZodArray<Z>, "array", [BuilderFor<Z> | BuilderFor<Z>[], ArrayCreateParams]>

Type Parameters ​

Z ​

Z extends ZodType

Implements ​

  • BuilderFor<ZodArray>

Constructors ​

Constructor ​

ts
new ArrayBuilder<Z>(
   version?, 
   itemSchema, 
   params?
): ArrayBuilder<Z>;

Defined in: src/ZodBuilder/array.ts:42

Parameters ​

version? ​

"v3" | "v4"

itemSchema ​

BuilderFor<Z> | BuilderFor<Z>[]

params? ​

| string | { error?: string | $ZodErrorMap<$ZodIssueInvalidType<unknown>>; message?: string; }

string


Type Literal ​

{ error?: string | $ZodErrorMap<$ZodIssueInvalidType<unknown>>; message?: string; }

error? ​

string | $ZodErrorMap<$ZodIssueInvalidType<unknown>>

message? ​

string

Deprecated

This parameter is deprecated. Use error instead.

Returns ​

ArrayBuilder<Z>

Overrides ​

BaseBuilder.constructor

Properties ​

_brandText? ​

ts
optional _brandText?: string = undefined;

Defined in: src/ZodBuilder/BaseBuilder.ts:125

Inherited from ​

NumberBuilder._brandText


_defaultValue? ​

ts
optional _defaultValue?: unknown = undefined;

Defined in: src/ZodBuilder/BaseBuilder.ts:122

Inherited from ​

NumberBuilder._defaultValue


_describeText? ​

ts
optional _describeText?: string = undefined;

Defined in: src/ZodBuilder/BaseBuilder.ts:124

Inherited from ​

NumberBuilder._describeText


_exactLength? ​

ts
optional _exactLength?: {
  params?:   | string
     | {
     abort?: boolean;
     error?:   | string
        | $ZodErrorMap<NonNullable<$ZodIssueTooSmall<HasLength> | $ZodIssueTooBig<HasLength>>>;
     message?: string;
   };
  value: number;
} = undefined;

Defined in: src/ZodBuilder/array.ts:40

params? ​

ts
optional params?: 
  | string
  | {
  abort?: boolean;
  error?:   | string
     | $ZodErrorMap<NonNullable<$ZodIssueTooSmall<HasLength> | $ZodIssueTooBig<HasLength>>>;
  message?: string;
};
Union Members ​

string


Type Literal ​
ts
{
  abort?: boolean;
  error?:   | string
     | $ZodErrorMap<NonNullable<$ZodIssueTooSmall<HasLength> | $ZodIssueTooBig<HasLength>>>;
  message?: string;
}
abort? ​
ts
optional abort?: boolean;

If true, no later checks will be executed if this check fails. Default false.

error? ​
ts
optional error?: 
  | string
| $ZodErrorMap<NonNullable<$ZodIssueTooSmall<HasLength> | $ZodIssueTooBig<HasLength>>>;
message? ​
ts
optional message?: string;
Deprecated ​

This parameter is deprecated. Use error instead.

value ​

ts
value: number;

_fallbackText? ​

ts
optional _fallbackText?: unknown = undefined;

Defined in: src/ZodBuilder/BaseBuilder.ts:126

Inherited from ​

NumberBuilder._fallbackText


_maxItems? ​

ts
optional _maxItems?: {
  params?:   | string
     | {
     abort?: boolean;
     error?: string | $ZodErrorMap<$ZodIssueTooBig<HasLength>>;
     message?: string;
   };
  value: number;
} = undefined;

Defined in: src/ZodBuilder/array.ts:39

params? ​

ts
optional params?: 
  | string
  | {
  abort?: boolean;
  error?: string | $ZodErrorMap<$ZodIssueTooBig<HasLength>>;
  message?: string;
};
Union Members ​

string


Type Literal ​
ts
{
  abort?: boolean;
  error?: string | $ZodErrorMap<$ZodIssueTooBig<HasLength>>;
  message?: string;
}
abort? ​
ts
optional abort?: boolean;

If true, no later checks will be executed if this check fails. Default false.

error? ​
ts
optional error?: string | $ZodErrorMap<$ZodIssueTooBig<HasLength>>;
message? ​
ts
optional message?: string;
Deprecated ​

This parameter is deprecated. Use error instead.

value ​

ts
value: number;

_metaData? ​

ts
optional _metaData?: Record<string, unknown> = undefined;

Defined in: src/ZodBuilder/BaseBuilder.ts:130

Inherited from ​

NumberBuilder._metaData


_minItems? ​

ts
optional _minItems?: {
  params?:   | string
     | {
     abort?: boolean;
     error?: string | $ZodErrorMap<$ZodIssueTooSmall<HasLength>>;
     message?: string;
   };
  value: number;
} = undefined;

Defined in: src/ZodBuilder/array.ts:38

params? ​

ts
optional params?: 
  | string
  | {
  abort?: boolean;
  error?: string | $ZodErrorMap<$ZodIssueTooSmall<HasLength>>;
  message?: string;
};
Union Members ​

string


Type Literal ​
ts
{
  abort?: boolean;
  error?: string | $ZodErrorMap<$ZodIssueTooSmall<HasLength>>;
  message?: string;
}
abort? ​
ts
optional abort?: boolean;

If true, no later checks will be executed if this check fails. Default false.

error? ​
ts
optional error?: string | $ZodErrorMap<$ZodIssueTooSmall<HasLength>>;
message? ​
ts
optional message?: string;
Deprecated ​

This parameter is deprecated. Use error instead.

value ​

ts
value: number;

_nullable ​

ts
_nullable: boolean = false;

Defined in: src/ZodBuilder/BaseBuilder.ts:120

Inherited from ​

NumberBuilder._nullable


_optional ​

ts
_optional: boolean = false;

Defined in: src/ZodBuilder/BaseBuilder.ts:119

Inherited from ​

NumberBuilder._optional


_params? ​

ts
protected optional _params?: [BuilderFor<Z> | BuilderFor<Z>[], 
  | string
  | {
  error?: string | $ZodErrorMap<$ZodIssueInvalidType<unknown>>;
  message?: string;
}];

Defined in: src/ZodBuilder/BaseBuilder.ts:118

Inherited from ​

BaseBuilder._params


_readonly ​

ts
_readonly: boolean = false;

Defined in: src/ZodBuilder/BaseBuilder.ts:121

Inherited from ​

NumberBuilder._readonly


_refineFn? ​

ts
optional _refineFn?: string = undefined;

Defined in: src/ZodBuilder/BaseBuilder.ts:127

Inherited from ​

NumberBuilder._refineFn


_refineMessage? ​

ts
optional _refineMessage?: string = undefined;

Defined in: src/ZodBuilder/BaseBuilder.ts:128

Inherited from ​

NumberBuilder._refineMessage


_superRefineFns ​

ts
_superRefineFns: string[] = [];

Defined in: src/ZodBuilder/BaseBuilder.ts:129

Inherited from ​

NumberBuilder._superRefineFns


_transformFn? ​

ts
optional _transformFn?: string = undefined;

Defined in: src/ZodBuilder/BaseBuilder.ts:131

Inherited from ​

NumberBuilder._transformFn


_version? ​

ts
protected optional _version?: "v3" | "v4";

Defined in: src/ZodBuilder/BaseBuilder.ts:133

Inherited from ​

BaseBuilder._version


typeKind ​

ts
readonly typeKind: "array";

Defined in: src/ZodBuilder/array.ts:36

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

Implementation of ​

ts
BuilderFor.typeKind

Overrides ​

BaseBuilder.typeKind

Accessors ​

zodVersion ​

Get Signature ​

ts
get protected zodVersion(): ZodVersion;

Defined in: src/ZodBuilder/BaseBuilder.ts:144

Get the target Zod version for code generation.

Returns ​

ZodVersion

'v3' or 'v4' (default: 'v4')

Inherited from ​

BaseBuilder.zodVersion

Methods ​

base() ​

ts
protected base(): string;

Defined in: src/ZodBuilder/array.ts:89

Compute the base array schema.

Returns ​

string

Overrides ​

BaseBuilder.base


brand() ​

ts
brand(brand): this;

Defined in: src/ZodBuilder/BaseBuilder.ts:232

Apply brand modifier.

Parameters ​

brand ​

string

Returns ​

this

Implementation of ​

ts
BuilderFor.brand

Inherited from ​

BaseBuilder.brand


catch() ​

ts
catch(fallback): this;

Defined in: src/ZodBuilder/BaseBuilder.ts:248

Apply catch modifier.

Parameters ​

fallback ​

unknown

Returns ​

this

Implementation of ​

ts
BuilderFor.catch

Inherited from ​

BaseBuilder.catch


default() ​

ts
default(value): this;

Defined in: src/ZodBuilder/BaseBuilder.ts:212

Apply default value.

Parameters ​

value ​

unknown

Returns ​

this

Implementation of ​

ts
BuilderFor.default

Inherited from ​

BaseBuilder.default


describe() ​

ts
describe(description): this;

Defined in: src/ZodBuilder/BaseBuilder.ts:220

Apply describe modifier.

Parameters ​

description ​

string

Returns ​

this

Implementation of ​

ts
BuilderFor.describe

Inherited from ​

BaseBuilder.describe


is() ​

ts
is<K>(type): this is TypeKindOf<K>;

Defined in: src/ZodBuilder/BaseBuilder.ts:308

Type Parameters ​

K ​

K extends | "symbol" | "undefined" | "null" | "any" | "void" | "never" | "unknown" | "nan"

Parameters ​

type ​

K

Returns ​

this is TypeKindOf<K>

Inherited from ​

BaseBuilder.is


isV3() ​

ts
protected isV3(): boolean;

Defined in: src/ZodBuilder/BaseBuilder.ts:158

Check if targeting Zod v3.

Returns ​

boolean

Inherited from ​

BaseBuilder.isV3


isV4() ​

ts
protected isV4(): boolean;

Defined in: src/ZodBuilder/BaseBuilder.ts:151

Check if targeting Zod v4.

Returns ​

boolean

Inherited from ​

BaseBuilder.isV4


length() ​

ts
length(len, params?): this;

Defined in: src/ZodBuilder/array.ts:77

Require exactly len elements.

Parameters ​

len ​

number

params? ​

| string | { abort?: boolean; error?: | string | $ZodErrorMap<NonNullable<$ZodIssueTooSmall<HasLength> | $ZodIssueTooBig<HasLength>>>; message?: string; }

string


Type Literal ​

{ abort?: boolean; error?: | string | $ZodErrorMap<NonNullable<$ZodIssueTooSmall<HasLength> | $ZodIssueTooBig<HasLength>>>; message?: string; }

abort? ​

boolean

If true, no later checks will be executed if this check fails. Default false.

error? ​

| string | $ZodErrorMap<NonNullable<$ZodIssueTooSmall<HasLength> | $ZodIssueTooBig<HasLength>>>

message? ​

string

Deprecated

This parameter is deprecated. Use error instead.

Returns ​

this

Implementation of ​

ts
BuilderFor.length

max() ​

ts
max(value, params?): this;

Defined in: src/ZodBuilder/array.ts:64

Apply maxItems constraint.

Parameters ​

value ​

number

params? ​

| string | { abort?: boolean; error?: string | $ZodErrorMap<$ZodIssueTooBig<HasLength>>; message?: string; }

string


Type Literal ​

{ abort?: boolean; error?: string | $ZodErrorMap<$ZodIssueTooBig<HasLength>>; message?: string; }

abort? ​

boolean

If true, no later checks will be executed if this check fails. Default false.

error? ​

string | $ZodErrorMap<$ZodIssueTooBig<HasLength>>

message? ​

string

Deprecated

This parameter is deprecated. Use error instead.

Returns ​

this

Implementation of ​

ts
BuilderFor.max

meta() ​

ts
meta(metadata): this;

Defined in: src/ZodBuilder/BaseBuilder.ts:277

Apply meta modifier.

Parameters ​

metadata ​

Record<string, unknown>

Returns ​

this

Implementation of ​

ts
BuilderFor.meta

Inherited from ​

BaseBuilder.meta


min() ​

ts
min(value, params?): this;

Defined in: src/ZodBuilder/array.ts:54

Apply minItems constraint.

Parameters ​

value ​

number

params? ​

| string | { abort?: boolean; error?: string | $ZodErrorMap<$ZodIssueTooSmall<HasLength>>; message?: string; }

string


Type Literal ​

{ abort?: boolean; error?: string | $ZodErrorMap<$ZodIssueTooSmall<HasLength>>; message?: string; }

abort? ​

boolean

If true, no later checks will be executed if this check fails. Default false.

error? ​

string | $ZodErrorMap<$ZodIssueTooSmall<HasLength>>

message? ​

string

Deprecated

This parameter is deprecated. Use error instead.

Returns ​

this

Implementation of ​

ts
BuilderFor.min

modify() ​

ts
protected modify(baseText): string;

Defined in: src/ZodBuilder/array.ts:105

Apply all shared modifiers to the base schema string. This method is called by text() and applies modifiers in a stable order.

Parameters ​

baseText ​

string

Returns ​

string

Overrides ​

BaseBuilder.modify


nonempty() ​

ts
nonempty(params?): this;

Defined in: src/ZodBuilder/array.ts:72

Require at least one element (delegates to min(1)).

Parameters ​

params? ​

| string | { abort?: boolean; error?: string | $ZodErrorMap<$ZodIssueTooSmall<HasLength>>; message?: string; }

string


Type Literal ​

{ abort?: boolean; error?: string | $ZodErrorMap<$ZodIssueTooSmall<HasLength>>; message?: string; }

abort? ​

boolean

If true, no later checks will be executed if this check fails. Default false.

error? ​

string | $ZodErrorMap<$ZodIssueTooSmall<HasLength>>

message? ​

string

Deprecated

This parameter is deprecated. Use error instead.

Returns ​

this

Implementation of ​

ts
BuilderFor.nonempty

nullable() ​

ts
nullable(): this;

Defined in: src/ZodBuilder/BaseBuilder.ts:204

Apply nullable constraint.

Returns ​

this

Implementation of ​

ts
BuilderFor.nullable

Inherited from ​

BaseBuilder.nullable


optional() ​

ts
optional(): this;

Defined in: src/ZodBuilder/BaseBuilder.ts:191

Apply optional constraint.

Returns ​

this

Implementation of ​

ts
BuilderFor.optional

Inherited from ​

BaseBuilder.optional


readonly() ​

ts
readonly(): this;

Defined in: src/ZodBuilder/BaseBuilder.ts:240

Apply readonly modifier.

Returns ​

this

Implementation of ​

ts
BuilderFor.readonly

Inherited from ​

BaseBuilder.readonly


refine() ​

ts
refine(refineFn, message?): this;

Defined in: src/ZodBuilder/BaseBuilder.ts:258

Apply refine modifier.

Note: function is provided as raw code string e.g. (val) => val > 0.

Parameters ​

refineFn ​

string

message? ​

string

Returns ​

this

Implementation of ​

ts
BuilderFor.refine

Inherited from ​

BaseBuilder.refine


required() ​

ts
required(): this;

Defined in: src/ZodBuilder/BaseBuilder.ts:196

Returns ​

this

Inherited from ​

BaseBuilder.required


serializeParams() ​

ts
protected serializeParams(): string;

Defined in: src/ZodBuilder/BaseBuilder.ts:167

Serialize params to a string representation for code generation. Handles objects, strings, primitives, and undefined.

Returns ​

string

String representation of params or empty string if no params

Inherited from ​

BaseBuilder.serializeParams


superRefine() ​

ts
superRefine(superRefineFn): this;

Defined in: src/ZodBuilder/BaseBuilder.ts:269

Apply superRefine modifier.

Note: function is provided as raw code string e.g. (val, ctx) => { ... }.

Parameters ​

superRefineFn ​

string

Returns ​

this

Implementation of ​

ts
BuilderFor.superRefine

Inherited from ​

BaseBuilder.superRefine


text() ​

ts
text(): string;

Defined in: src/ZodBuilder/BaseBuilder.ts:366

Unwrap and return the final Zod code string. This orchestrates the template method pattern: text() = modify(base())

Returns ​

string

Implementation of ​

ts
BuilderFor.text

Inherited from ​

BaseBuilder.text


toString() ​

ts
toString(): string;

Defined in: src/ZodBuilder/BaseBuilder.ts:370

Returns a string representation of an object.

Returns ​

string

Inherited from ​

BaseBuilder.toString


transform() ​

ts
transform(transformFn): this;

Defined in: src/ZodBuilder/BaseBuilder.ts:287

Apply transform modifier.

Note: function is provided as raw code string e.g. (val) => transformedVal.

Parameters ​

transformFn ​

string

Returns ​

this

Implementation of ​

ts
BuilderFor.transform

Inherited from ​

BaseBuilder.transform


unwrap() ​

ts
unwrap(): Z;

Defined in: src/ZodBuilder/array.ts:82

Returns ​

Z

Implementation of ​

ts
BuilderFor.unwrap

Released under the ISC License.