literal
Exactly one specific value: a string, a number or true/false. Strict comparison (===).
Signature
ts
function literal<const T extends string | number | boolean>(value: T, msg?: Message): Schema<T>Parameters
| Parameter | Type | Description |
|---|---|---|
value | `string | number |
msg | Message | Custom message. Replaces the default messages for all errors of this schema |
Error codes
| Code | When | Default message |
|---|---|---|
required | The value is empty | Required |
literal | The value does not match | Must be {expected} |
You can change the texts: see Messages & i18n.
Notes
- The result type is the literal itself:
literal('admin')gives'admin', notstring. literal('')won't work: an empty string is a missing value. See Empty values.
Examples
Accepting terms
ts
const Signup = object({
terms: literal(true, 'You must accept the terms'),
});API version
ts
const Payload = object({ version: literal(2), data: unknown() });