Skip to content

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 ​

ParameterTypeDescription
value`stringnumber
msgMessageCustom message. Replaces the default messages for all errors of this schema

Error codes ​

CodeWhenDefault message
requiredThe value is emptyRequired
literalThe value does not matchMust be {expected}

You can change the texts: see Messages & i18n.

Notes ​

  • The result type is the literal itself: literal('admin') gives 'admin', not string.
  • 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() });

See also ​