VLiteral<T> class Types

Validates that a value is exactly equal to the expected literal.

final schema = V.literal('active');
schema.parse('active');   // 'active'
schema.parse('inactive'); // throws VException

See also:

  • V.literal, the factory that creates this schema.
  • VLiteralCode, the error codes it emits.
  • VEnum, for a fixed set drawn from an enum.
Inheritance
Available extensions

Constructors

VLiteral(T _expected, {String? message, String? invalidTypeMessage})
Creates a literal validator that only accepts _expected.

Properties

coercer ↔ T Function(Object value)?
Optional coercion function to convert input to the expected type.
getter/setter pairinherited
defaultValueOrNull → T?
The configured default value, or null when none was set. Check hasDefault first to distinguish "no default" from defaultValue(null) (which is nonsensical but allowed by the type).
no setterinherited
hasAsync → bool
Returns true if the pipeline contains any async step. When true, the synchronous consumers (parse, validate, safeParse, errors) throw VAsyncRequiredException and the caller must use the *Async variants instead.
no setterinherited
hasDefault → bool
Returns true if a default value was configured via defaultValue. When true, defaultValueOrNull holds the configured value.
no setterinherited
hashCode → int
The hash code for this object.
no setterinherited
hasPreprocessors → bool
Returns true if at least one preprocessor (sync or async) was registered via preprocess / preprocessAsync. Useful for consumers that want to short-circuit a snapshot/preprocess step when there is nothing to run — e.g. valiform's per-field validator only mounts the container preprocess closure when this is true.
no setterinherited
isNullable → bool
Returns true if null is accepted by this schema (set via nullable).
no setterinherited
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
typeName → String
Type-specific prefix used to build error codes for required and invalid_type. Concrete subclasses return literals like 'string', 'int', 'bool'. Wrappers delegate to the inner schema.
no setteroverride

Methods

add(Validator<T> validator, {String? message, List<Object>? path, Set<String>? dependsOn}) → VType<T>
Adds a Validator to the validation phase of the pipeline.
inherited
addAsync(AsyncValidator<T> validator, {String? message, List<Object>? path, Set<String>? dependsOn}) → VType<T>
Adds an AsyncValidator to the validation phase.
inherited
addRaw(Validator<T> validator, {String? message, List<Object>? path}) → VType<T>
Adds a Validator that runs in the raw validation phase — before any per-field iteration in container schemas (VMap / VObject). The callback inside validator sees the input as it arrived (after container preprocess and type check), not the post-pipeline parsed value that add sees.
inherited
applyIf(bool condition, V builder(V schema)) → V

Available on V, provided by the VTypeApplyIf extension

Returns builder(this) when condition is true; returns this unchanged otherwise.
defaultValue(T value) → VType<T>
Sets a default value to use when the input is null.
inherited
errors(Object? value) → List<VError>?
Returns the list of VErrors for value, or null if valid.
inherited
errorsAsync(Object? value) → Future<List<VError>?>
Async variant of errors. Returns the list of errors, or null when valid.
inherited
mapType<R>(R fn<U>(VType<U> type)) → R
Maps this type through a generic function, preserving the inner type.
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
nullable() → VType<T>
Marks this schema as nullable, allowing null to pass validation.
inherited
parse(Object? value) → T?
Parses value and returns the result, or throws a VException on failure.
inherited
parseAsync(Object? value) → Future<T?>
Async variant of parse. Throws VException on failure.
inherited
preprocess(Object? fn(Object? value)) → VType<T>
Applies a function to the raw input before type checking.
inherited
preprocessAsync(Future<Object?> fn(Object? value)) → VType<T>
Async variant of preprocess. The function can return a Future — the raw input is transformed before type checking.
inherited
refine(bool check(T value), {String? message, String? code, Set<String>? dependsOn}) → VType<T>
Adds a custom validation check.
inherited
refineAsync(Future<bool> check(T value), {String? message, String? code, Duration? timeout, Set<String>? dependsOn}) → VType<T>
Adds an async custom validation check.
inherited
runPreprocessors(Object? value) → Object?
Runs the synchronous preprocessor chain registered via preprocess against value and returns the result. Does NOT run _resolveNull, validators, or transforms — only the preprocess stage.
inherited
runPreprocessorsAsync(Object? value) → Future<Object?>
Async variant of runPreprocessors: runs sync preprocessors first, then async preprocessors, in the order they were registered.
inherited
safeParse(Object? value) → VResult<T?>
Parses value and returns a VResult without throwing.
override
safeParseAsync(Object? value) → Future<VResult<T?>>
Async variant of safeParse. Use this when the schema has async validators (added via refineAsync).
inherited
toString() → String
A string representation of this object.
inherited
transform<O>(O fn(T value)) → VTransformed<T, O>
Creates a VTransformed that converts the validated value to type O.
inherited
transformAsync<O>(Future<O> fn(T value)) → VTransformedAsync<T, O>
Async variant of transform. The transform function returns a Future — the schema becomes async-only.
inherited
validate(Object? value) → bool
Returns true if value passes all validations.
inherited
validateAsync(Object? value) → Future<bool>
Async variant of validate. Returns true if the value passes.
inherited

Operators

operator ==(Object other) → bool
The equality operator.
inherited