ArraySchemaclass
new ArraySchema<T>({
items,
one = items.one,
many = items.many,
title = "Items",
placeholder = `No ${many}`,
unique = false,
min = 0,
max = Number.POSITIVE_INFINITY,
separator = ",",
value = [],
...options
}: ArraySchemaOptions<T>)| Param | Type | |
|---|---|---|
options | ArraySchemaOptions<T> | Options for ArraySchema. required |
.value | ImmutableArray | Default value used when the input is undefined. Defaults to [] readonly |
.items | Schema<T> | Schema every item in the array must conform to. required readonly |
.min | number | Minimum number of items. Defaults to 0 readonly |
.max | number | Maximum number of items. Defaults to Number.POSITIVE_INFINITY readonly |
.unique | boolean | Whether to deduplicate the items. Defaults to false readonly |
.separator | stringRegExp | String or RegExp used to split a string input into items. Defaults to "," readonly |
| Return | |
|---|---|
ArraySchema<T> | Schema that validates an array and ensures every item matches a specified item schema. |
| Property | Type | |
|---|---|---|
.items | Schema<T> | Schema is an object instance with a validate() method that converts unknown input into a known, valid type T. required readonly |
.unique | boolean | required readonly |
.min | number | required readonly |
.max | number | required readonly |
.separator | stringRegExp | required readonly |
Schema that validates an array and ensures every item matches a specified item schema.
- A string input is split into items using
separator. - Items are optionally deduplicated (
unique), then the count is checked againstminandmax.
Validates a value into a readonly array, validating each element against an items schema. Following the robustness principle, a comma-separated string is split into an array, and a missing value falls back to an empty array.
ARRAY(itemSchema) is the sugar factory that builds an ArraySchema for a given element schema.
Usage
import { ARRAY, STRING, NUMBER } from "shelving/schema";
const TAGS = ARRAY(STRING);
TAGS.validate(["a", "b"]); // ["a", "b"]
TAGS.validate("a,b,c"); // ["a", "b", "c"] (split on comma)
TAGS.validate(undefined); // []
const NUMBERS = ARRAY(NUMBER);
NUMBERS.validate([1, 2]); // [1, 2]
NUMBERS.validate("1,2,3"); // [1, 2, 3] (split on comma, each coerced)
NUMBERS.validate(undefined); // []Examples
const schema = new ArraySchema({ min: 1, max: 2, default: [10,11,12], required: true });
schema.validate([1,2,3], schema); // Returns [1,2,3]
schema.validate(undefined, schema); // Returns [10,11,12] (due to value)
schema.validate([4,5,6,7], schema); // Returns [4,5,6] (due to max)
schema.validate(9999, schema); // Throws Invalid('Must be array')
schema.validate([], schema); // Throws Required
schema.validate([1,2], schema); // Throws Invalid('Needs at least 3 items')const schema = new ArraySchema({ schema: Array });
schema.validate(["a", "a"], schema); // Returns ["a", "a"]
schema.validate(["a", null], schema); // Throws Invalids({ "1": Invalid('Must be a string') });