Literal schema
A function which ensures that a given input matches an exact value.
Arguments
value
- An exact value the input should be the same asmessage?
- A custom object with error messages to be displayed when validation fails or there are type errors
Examples
import { literal } from '@nordic-ui/validathor';
const literalRule = literal('Hello');
try {
const parsedValue = literalRule.parse('Hello');
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 literalRule = literal('Hello');
type LiteralSchema = ReturnType<typeof literalRule.parse>; // 'Hello'