Array schema
A function which creates a parsing rule to ensure that a given value is an array.
Arguments
- A schema
- (Optional): A list of modifiers
min
: Learn more.max
: Learn more.custom
: Learn more.
- (Optional): A custom object with error messages to be displayed when validation fails or there are type errors.
Example
import { array, string } from '@nordic-ui/validathor';
const arrayRule = array(string(), undefined, 'Value must be an array');
try {
const parsedValue = arrayRule.parse(['Hello', 'world']);
console.log('Parsed value:', parsedValue);
} catch (error) {
console.error('Parsing failed:', error.message);
}
Future
In one of the next upcoming releases, the Array
schema will support mixed schemas, so you'd be able to for example do the following:
import { array, string, number } from '@nordic-ui/validathor';
const arrayRule = array([string(), number()]);
try {
const parsedValue = arrayRule.parse(['Hello', 42, 'world']);
console.log('Parsed value:', parsedValue);
} catch (error) {
console.error('Parsing failed:', error.message);
}