MapValidator<T extends Map> class
abstract
Abstract class from which to create class based validator schemes. EskMap is a utility class designed to facilitate the validation and transformation of map-like data structures in Dart. It provides a flexible way to define validation rules and apply them to input data, ensuring that the data conforms to expected formats or constraints.
This is particularly useful when working with dynamic data sources such as JSON, form inputs, or external APIs, where the structure and types of the data may not be guaranteed.
Example:
class UserValidator extends EskMap {
final name = EskField(
id: 'name',
validators: [ $isString ],
);
final age = EskField(
id: 'age',
validators: [ $isInt, $isGt(0) ],
);
@override
get fields => [name, age];
}
final userValidator = UserValidator();
final result = userValidator.validate({
'name': 'Alice',
'age': 30
});
if (result.isValid) {
print('Validation passed!');
} else {
print(result.toString());
}
In this example, EskMap
is used to define a schema where name
is a required
string and age
is an integer with a minimum value of 0. The validate
method
checks the input data against these rules and returns the result.
- Inheritance
-
- Object
- IValidator
- Validator<
Result> - IdValidator<
Result> - MapValidator
- Available extensions
Constructors
- MapValidator.new({String id = '', bool nullable = false})
Properties
-
fields
→ List<
IdValidator< Result> > -
List of IdValidators used to validate a dynamic
Map
. Each field represents a value in the map,id
is used to identify the key from the map.no setter - hashCode → int
-
The hash code for this object.
no setterinherited
- id → String
-
Identifier for this particular validator.
finalinherited
- isNullable → bool
-
Marks the validator as nullable. This means that if the value being checked is null,
the validation is considered valid.
finalinherited
- isOptional → bool
-
Marks the validator as optional. This means that if the value being checked is null or missing,
the validation is considered valid.
finalinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
copyWith(
{bool? nullable, bool? optional}) → IValidator -
Creates a copy of the validator with the given parameters.
override
-
isNotValid(
dynamic value) → bool -
Checks if the given value is not valid.
inherited
-
isValid(
dynamic value) → bool -
Checks if the given value is valid.
inherited
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
nullable<
T> () → IValidator -
Creates a nullable copy of the validator.
inherited
-
optional<
T> () → IValidator -
Creates a optional copy of the validator.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
-
validate(
dynamic value, {bool exists = true}) → Result -
Main validation method. Use this method if you want to validate
that a dynamic value is valid, and get an error message if not.
inherited
-
validateAsync(
dynamic value, {bool exists = true}) → Future< Result> -
Always returns a Future, allowing async + sync validators to compose.
inherited
-
validateOrThrow(
dynamic value) → Result -
Works the same as validate, validates that a given value is valid,
but throws instead if it's not.
inherited
-
validator(
dynamic value) → FutureOr< Result> -
Core validation function (may return a Result or Future<Result>).
Don't call directly, use validate or validateAsync.
override
Operators
-
operator &(
IValidator other) → IValidator -
Available on IValidator, provided by the EskemaEskValidatorOperations extension
Combines two validators with a logical AND, same as using all -
operator ==(
Object other) → bool -
The equality operator.
inherited
-
operator >(
Expectation error) → IValidator -
Available on IValidator, provided by the EskemaEskValidatorOperations extension
Returns a new validator that will return theerror
message if the validation fails -
operator |(
IValidator other) → IValidator -
Available on IValidator, provided by the EskemaEskValidatorOperations extension
Combines two validators with a logical OR, same as using any