Union schema
A function which creates a parsing rule to ensure that a given unknown value matches one of the provided schemas.
Arguments
schemas
- An array of parser schemas to try in ordermessage?
- An object with a custom error message for type mismatches
Examples
import { union, string, number, parse } from '@nordic-ui/validathor'
const unionRule = union([string(), number()])
try {
const parsedValue = unionRule.parse('Hello world');
console.log('Parsed value:', parsedValue);
} catch (error) {
console.error('Parsing failed:', error.message);
}
TypeScript
To get the type of a schema, you can do the following:
const unionRule = union([string(), number(), boolean()]);
type UnionSchema = ReturnType<typeof unionRule.parse>; // string | number | boolean