Signature
Description
Accepts arrays of whatever the given decoder accepts. Every element in the array is validated using the provided decoder. If any element fails validation, the entire array decode fails with an error indicating which element (by index) caused the failure.Type Inference
The return type is automatically inferred from the element decoder:Parameters
| Parameter | Type | Description |
|---|---|---|
decoder | Decoder<T> | The decoder to apply to each array element |
Returns
ADecoder<T[]> that validates each element and returns an array of validated values.
Behavior
- Empty arrays: Accepted (returns
[]) - Valid elements: All elements validated and returned
- Invalid element: Decode fails immediately at first error
- Not an array: Decode fails with “Must be an array”
- Error reporting: Includes the index of the failed element
Examples
Basic Usage
Array of Numbers
Array of Objects
Nested Arrays
Union Types
Optional Elements
Custom Decoders
API Response Lists
Error Messages
The decoder provides detailed error messages:- Not an array:
Must be an array - Invalid element:
<element error> (at index N)- Example:
Must be string (at index 3) - Example:
Missing key: 'email' (at index 1)
- Example:
Performance Considerations
- Validation stops at the first error (fail-fast behavior)
- Does not collect all errors from all elements
- For large arrays, this means faster failure detection
- Previously validated elements are discarded when an error occurs
Empty Arrays
Non-Empty Arrays
If you need to ensure an array has at least one element, usenonEmptyArray:
Implementation Notes
- Built on top of the
pojadecoder using.chain() - Uses the element decoder’s
.decode()method directly for performance - Constructs a new array with validated elements
- On error, clears the results array and returns immediately
- Error annotations include a cloned array with the error injected at the failed index
Related Decoders
poja- Accepts any array without element validationtuple- Decode fixed-length arrays with different types per positionnonEmptyArray- Likearray()but requires at least one element
