parseNonEmptyArray
Parses a value into a non-empty array. This parser uses the validateNonEmptyArray structural validator to ensure that the input is an actual array instance containing at least one element.
The parser never throws and never mutates input. It returns a structured ParseResult<unknown[]> describing success or failure.
Signature
function parseNonEmptyArray(
value: unknown,
field?: string
): ParseResult<unknown[]>
Behavior
- Accepts actual array instances with at least one element.
- Rejects:
- Empty arrays.
- Objects.
- Functions.
- Primitives.
nullandundefined.- Never mutates input.
- Never throws.
Returns
ok: true: And the original array when parsing succeeds.ok: false, value: null: And a list of issues when parsing fails.
Examples
parseNonEmptyArray([1])
// { ok: true, value: [1], issues: [] }
parseNonEmptyArray([])
// { ok: false, value: null, issues: ["Value must be a non-empty array"] }
Notes
- This parser is a foundational building block for tuple parsing, schema-based array parsing, and any scenario requiring guaranteed non-empty collections.
- The helper is pure, side-effect-free, and safe for all runtime environments.