literal
Creates a literal schema.
const Schema = v.literal<TLiteral, TMessage>(literal, message);
Generics
TLiteralextends LiteralTMessageextends ErrorMessage<LiteralIssue> | undefined
Parameters
literalTLiteralmessageTMessage
Explanation
With literal you can validate that the input matches a specified value. If the input is invalid, you can use message to customize the error message.
Returns
SchemaLiteralSchema<TLiteral, TMessage>
Examples
The following examples show how literal can be used.
String literal
Schema to validate a string literal.
const StringLiteralSchema = v.literal('foo');
Number literal
Schema to validate a number literal.
const NumberLiteralSchema = v.literal(26);
Boolean literal
Schema to validate a boolean literal.
const BooleanLiteralSchema = v.literal(true);
Related
The following APIs can be combined with literal.
Schemas
array, exactOptional, intersect, lazy, looseObject, looseTuple, map, nonNullable, nonNullish, nonOptional, nullable, nullish, object, objectWithRest, optional, record, set, strictObject, strictTuple, tuple, tupleWithRest, undefinedable, union
Methods
assert, config, fallback, getDefault, getDefaults, getFallback, getFallbacks, is, message, parse, parser, pipe, safeParse, safeParser
Actions
check, brand, description, flavor, guard, metadata, rawCheck, rawTransform, readonly, title, transform
Utils
entriesFromList, isOfKind, isOfType
© Fabian Hiller
Licensed under the MIT License.
https://valibot.dev/api/literal/