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

Schemas
Enum

Enum schema

A function which creates a parsing rule to ensure that a given value is one of the allowed values from an array or TypeScript enum.

Arguments

  1. An array of values or TypeScript enum
  2. (Optional): A custom object with error messages to be displayed when validation fails or there are type errors.

Examples

import { enum_ } from '@nordic-ui/validathor';
 
const colorRule = enum_(['red', 'green', 'blue']);
 
try {
  const parsedValue = colorRule.parse('red');
  console.log('Parsed value:', parsedValue);
} catch (error) {
  console.error('Parsing failed:', error.message);
}

Custom Error Messages

import { enum_ } from '@nordic-ui/validathor';
 
const priorityRule = enum_(['low', 'medium', 'high'], {
  type_error: 'Expected a string or number',
  error: (value) => `"${value}" is not a valid priority level`
});
 
try {
  const parsedValue = priorityRule.parse('invalid');
} catch (error) {
  console.error(error.message); // "invalid" is not a valid priority level
}

Readonly Arrays

The enum schema also supports readonly arrays for better type safety:

import { enum_ } from '@nordic-ui/validathor';
 
const permissions = ['read', 'write', 'admin'] as const;
const permissionRule = enum_(permissions);
 
try {
  const parsedValue = permissionRule.parse('read');
  console.log('Parsed value:', parsedValue);
} catch (error) {
  console.error('Parsing failed:', error.message);
}