Skip to content

parseArray

Parses a value into an array. This parser uses the validateArray structural validator to ensure that the input is an actual array instance. It does not attempt to coerce or transform the input.

The parser never throws and never mutates input. It returns a structured ParseResult<unknown[]> describing success or failure.

Signature

function parseArray(
    value: unknown,
    field?: string
): ParseResult<unknown[]>

Behavior

  • Accepts actual array instances (Array.isArray).
  • Rejects:
  • Objects.
  • Functions.
  • Primitives.
  • null and undefined.
  • Never throws and never mutates input.

Returns

  • ok: true: And the original array when parsing succeeds.
  • ok: false, value: null: And a list of issues when parsing fails.

Examples

parseArray([1, 2, 3])
// { ok: true, value: [1, 2, 3], issues: [] }

parseArray("not-an-array")
// { ok: false, value: null, issues: ["Value must be an array"] }

Notes

  • This parser is a foundational building block for higher-level structural parsers such as parseTuple, parseNonEmptyArray, and schema-based array parsing.
  • The helper is pure, side-effect-free, and safe for all runtime environments.