FunctionRule<T, E> class

A rule defined by a validation function.

Use this for more complex validation logic that needs to return different errors based on the value.

final ageRule = FunctionRule<int, AgeError>(
  validator: (value) {
    if (value < 0) return const Invalid(AgeError.negative);
    if (value > 150) return const Invalid(AgeError.tooHigh);
    return Valid(value);
  },
);
Inheritance
Available extensions

Constructors

FunctionRule({required ValidationResult<T, E> validator(T value)})
Creates a rule from a validator function.
const

Properties

hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
validator ValidationResult<T, E> Function(T value)
The validation function.
final

Methods

andThen(Formix<T, E> next) Formix<T, E>

Available on Formix<T, E>, provided by the FormixExtensions extension

Chains this validator with next, running next only if this passes.
cached() CachedFormix<T, E>

Available on Formix<T, E>, provided by the FormixExtensions extension

Wraps this validator with single-value caching.
lazy() LazyFormix<T, E>

Available on Formix<T, E>, provided by the FormixExtensions extension

Creates a lazy validator that defers instantiation until first use.
lruCached({int maxSize = 10}) LruCachedFormix<T, E>

Available on Formix<T, E>, provided by the FormixExtensions extension

Wraps this validator with LRU (Least Recently Used) caching.
mapError<E2>(E2 mapper(E error)) Formix<T, E2>

Available on Formix<T, E>, provided by the FormixExtensions extension

Maps errors from type E to type E2.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
optional({bool isEmpty(T value)?}) Formix<T, E>

Available on Formix<T, E>, provided by the FormixExtensions extension

Makes this validator optional - skips validation when empty.
recover(T onError(List<E> errors)) Formix<T, E>

Available on Formix<T, E>, provided by the FormixExtensions extension

Recovers from validation errors with a default value.
tap({void onValid(T value)?, void onInvalid(List<E> errors)?}) Formix<T, E>

Available on Formix<T, E>, provided by the FormixExtensions extension

Applies side effects without affecting validation.
toString() String
A string representation of this object.
inherited
validate(T value) ValidationResult<T, E>
Validates value and returns a ValidationResult.
override
when(bool condition(T value)) Formix<T, E>

Available on Formix<T, E>, provided by the FormixExtensions extension

Conditionally applies this validator based on condition.
whenNot(bool condition(T value)) Formix<T, E>

Available on Formix<T, E>, provided by the FormixExtensions extension

Conditionally skips this validator based on condition.
withMessageFormatter(ErrorMessageFormatter<E> formatter) Formix<T, String>

Available on Formix<T, E>, provided by the FormixExtensions extension

Maps errors to user-facing messages using formatter.

Operators

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