⚠️ This project is still in early alpha. Please report bugs on GitHub.

Schemas
Union

Union schema

A function which creates a parsing rule to ensure that a given unknown value matches one of the provided schemas.

Arguments

  1. schemas - An array of parser schemas to try in order
  2. message? - 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