Validator class

The Validator Class.

The heart of Magic's validation system. Runs rules against data and provides a clean API for validation in controllers.

Usage

final validator = Validator.make({
  'email': 'user@example.com',
  'password': 'secret',
}, {
  'email': [Required(), Email()],
  'password': [Required(), Min(8)],
});

if (validator.fails()) {
  print(validator.errors());
}

// Or throw on failure:
validator.validate(); // Throws ValidationException if fails

Message Resolution

When a rule fails, the Validator resolves the message:

  1. Calls rule.message() to get the key/raw message
  2. If Lang.has(key), uses Lang.get(key, params) for translation
  3. Otherwise uses the message as a raw string
  4. Replaces :attribute with the humanized field name

Properties

hashCode → int
The hash code for this object.
no setterinherited
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

Methods

errors() → Map<String, String>
Get all validation errors.
fails() → bool
Run the validation and check if it failed.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
passes() → bool
Run the validation and check if it passed.
toString() → String
A string representation of this object.
inherited
validate() → Map<String, dynamic>
Run validation and throw if it fails.
validateAsync() → Future<Map<String, dynamic>>
Run validation including any AsyncRules and throw if it fails.

Operators

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

Static Methods

make(Map<String, dynamic> data, Map<String, List<Rule>> rules) → Validator
Create a new Validator instance.