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:
- Consistency across versions
- Clarity in error messages
- 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
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
Properties
_brandText?
optional _brandText?: string = undefined;Defined in: src/ZodBuilder/BaseBuilder.ts:125
Inherited from
_defaultValue?
optional _defaultValue?: unknown = undefined;Defined in: src/ZodBuilder/BaseBuilder.ts:122
Inherited from
_describeText?
optional _describeText?: string = undefined;Defined in: src/ZodBuilder/BaseBuilder.ts:124
Inherited from
_exactLength?
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?
optional params?:
| string
| {
abort?: boolean;
error?: | string
| $ZodErrorMap<NonNullable<$ZodIssueTooSmall<HasLength> | $ZodIssueTooBig<HasLength>>>;
message?: string;
};Union Members
string
Type Literal
{
abort?: boolean;
error?: | string
| $ZodErrorMap<NonNullable<$ZodIssueTooSmall<HasLength> | $ZodIssueTooBig<HasLength>>>;
message?: string;
}abort?
optional abort?: boolean;If true, no later checks will be executed if this check fails. Default false.
error?
optional error?:
| string
| $ZodErrorMap<NonNullable<$ZodIssueTooSmall<HasLength> | $ZodIssueTooBig<HasLength>>>;message?
optional message?: string;Deprecated
This parameter is deprecated. Use error instead.
value
value: number;_fallbackText?
optional _fallbackText?: unknown = undefined;Defined in: src/ZodBuilder/BaseBuilder.ts:126
Inherited from
_maxItems?
optional _maxItems?: {
params?: | string
| {
abort?: boolean;
error?: string | $ZodErrorMap<$ZodIssueTooBig<HasLength>>;
message?: string;
};
value: number;
} = undefined;Defined in: src/ZodBuilder/array.ts:39
params?
optional params?:
| string
| {
abort?: boolean;
error?: string | $ZodErrorMap<$ZodIssueTooBig<HasLength>>;
message?: string;
};Union Members
string
Type Literal
{
abort?: boolean;
error?: string | $ZodErrorMap<$ZodIssueTooBig<HasLength>>;
message?: string;
}abort?
optional abort?: boolean;If true, no later checks will be executed if this check fails. Default false.
error?
optional error?: string | $ZodErrorMap<$ZodIssueTooBig<HasLength>>;message?
optional message?: string;Deprecated
This parameter is deprecated. Use error instead.
value
value: number;_metaData?
optional _metaData?: Record<string, unknown> = undefined;Defined in: src/ZodBuilder/BaseBuilder.ts:130
Inherited from
_minItems?
optional _minItems?: {
params?: | string
| {
abort?: boolean;
error?: string | $ZodErrorMap<$ZodIssueTooSmall<HasLength>>;
message?: string;
};
value: number;
} = undefined;Defined in: src/ZodBuilder/array.ts:38
params?
optional params?:
| string
| {
abort?: boolean;
error?: string | $ZodErrorMap<$ZodIssueTooSmall<HasLength>>;
message?: string;
};Union Members
string
Type Literal
{
abort?: boolean;
error?: string | $ZodErrorMap<$ZodIssueTooSmall<HasLength>>;
message?: string;
}abort?
optional abort?: boolean;If true, no later checks will be executed if this check fails. Default false.
error?
optional error?: string | $ZodErrorMap<$ZodIssueTooSmall<HasLength>>;message?
optional message?: string;Deprecated
This parameter is deprecated. Use error instead.
value
value: number;_nullable
_nullable: boolean = false;Defined in: src/ZodBuilder/BaseBuilder.ts:120
Inherited from
_optional
_optional: boolean = false;Defined in: src/ZodBuilder/BaseBuilder.ts:119
Inherited from
_params?
protected optional _params?: [BuilderFor<Z> | BuilderFor<Z>[],
| string
| {
error?: string | $ZodErrorMap<$ZodIssueInvalidType<unknown>>;
message?: string;
}];Defined in: src/ZodBuilder/BaseBuilder.ts:118
Inherited from
_readonly
_readonly: boolean = false;Defined in: src/ZodBuilder/BaseBuilder.ts:121
Inherited from
_refineFn?
optional _refineFn?: string = undefined;Defined in: src/ZodBuilder/BaseBuilder.ts:127
Inherited from
_refineMessage?
optional _refineMessage?: string = undefined;Defined in: src/ZodBuilder/BaseBuilder.ts:128
Inherited from
_superRefineFns
_superRefineFns: string[] = [];Defined in: src/ZodBuilder/BaseBuilder.ts:129
Inherited from
_transformFn?
optional _transformFn?: string = undefined;Defined in: src/ZodBuilder/BaseBuilder.ts:131
Inherited from
_version?
protected optional _version?: "v3" | "v4";Defined in: src/ZodBuilder/BaseBuilder.ts:133
Inherited from
typeKind
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
BuilderFor.typeKindOverrides
Accessors
zodVersion
Get Signature
get protected zodVersion(): ZodVersion;Defined in: src/ZodBuilder/BaseBuilder.ts:144
Get the target Zod version for code generation.
Returns
'v3' or 'v4' (default: 'v4')
Inherited from
Methods
base()
protected base(): string;Defined in: src/ZodBuilder/array.ts:89
Compute the base array schema.
Returns
string
Overrides
brand()
brand(brand): this;Defined in: src/ZodBuilder/BaseBuilder.ts:232
Apply brand modifier.
Parameters
brand
string
Returns
this
Implementation of
BuilderFor.brandInherited from
catch()
catch(fallback): this;Defined in: src/ZodBuilder/BaseBuilder.ts:248
Apply catch modifier.
Parameters
fallback
unknown
Returns
this
Implementation of
BuilderFor.catchInherited from
default()
default(value): this;Defined in: src/ZodBuilder/BaseBuilder.ts:212
Apply default value.
Parameters
value
unknown
Returns
this
Implementation of
BuilderFor.defaultInherited from
describe()
describe(description): this;Defined in: src/ZodBuilder/BaseBuilder.ts:220
Apply describe modifier.
Parameters
description
string
Returns
this
Implementation of
BuilderFor.describeInherited from
is()
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
isV3()
protected isV3(): boolean;Defined in: src/ZodBuilder/BaseBuilder.ts:158
Check if targeting Zod v3.
Returns
boolean
Inherited from
isV4()
protected isV4(): boolean;Defined in: src/ZodBuilder/BaseBuilder.ts:151
Check if targeting Zod v4.
Returns
boolean
Inherited from
length()
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
BuilderFor.lengthmax()
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
BuilderFor.maxmeta()
meta(metadata): this;Defined in: src/ZodBuilder/BaseBuilder.ts:277
Apply meta modifier.
Parameters
metadata
Record<string, unknown>
Returns
this
Implementation of
BuilderFor.metaInherited from
min()
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
BuilderFor.minmodify()
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
nonempty()
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
BuilderFor.nonemptynullable()
nullable(): this;Defined in: src/ZodBuilder/BaseBuilder.ts:204
Apply nullable constraint.
Returns
this
Implementation of
BuilderFor.nullableInherited from
optional()
optional(): this;Defined in: src/ZodBuilder/BaseBuilder.ts:191
Apply optional constraint.
Returns
this
Implementation of
BuilderFor.optionalInherited from
readonly()
readonly(): this;Defined in: src/ZodBuilder/BaseBuilder.ts:240
Apply readonly modifier.
Returns
this
Implementation of
BuilderFor.readonlyInherited from
refine()
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
BuilderFor.refineInherited from
required()
required(): this;Defined in: src/ZodBuilder/BaseBuilder.ts:196
Returns
this
Inherited from
serializeParams()
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
superRefine()
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
BuilderFor.superRefineInherited from
text()
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
BuilderFor.textInherited from
toString()
toString(): string;Defined in: src/ZodBuilder/BaseBuilder.ts:370
Returns a string representation of an object.
Returns
string
Inherited from
transform()
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
BuilderFor.transformInherited from
unwrap()
unwrap(): Z;Defined in: src/ZodBuilder/array.ts:82
Returns
Z
Implementation of
BuilderFor.unwrap