ValidationMiddleware class

Built-in schema validation middleware.

Validates data before it is written to a box. If validation fails, the put operation is cancelled and an error message is logged.

The middleware supports:

  • Required field checks
  • Type checking
  • Min/max constraints for numeric values or string lengths
  • Custom validation functions
  • Extra field detection

Usage:

rift.use(ValidationMiddleware(schemas: [
  ValidationSchema(
    boxName: 'users',
    rules: [
      ValidationRule(field: 'name', required: true, type: String),
      ValidationRule(field: 'age', type: int, min: 0, max: 150),
      ValidationRule(
        field: 'email',
        required: true,
        validator: (value) {
          if (value is! String || !value.contains('@')) {
            return 'Invalid email format';
          }
          return null;
        },
      ),
    ],
  ),
]));
Inheritance

Constructors

ValidationMiddleware({List<ValidationSchema> schemas = const [], void onError(String boxName, dynamic key, String error)?})

Properties

hashCode int
The hash code for this object.
no setterinherited
name String
Optional name for debugging purposes.
no setteroverride
onError → void Function(String boxName, dynamic key, String error)?
Callback for validation errors.
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

addSchema(ValidationSchema schema) → void
Adds a schema for a box.
afterClear(String boxName) Future<void>
Called after a box is cleared successfully.
override
afterDelete(String boxName, dynamic key) Future<void>
Called after a delete operation completes successfully.
override
afterPut(String boxName, dynamic key, dynamic value) Future<void>
Called after a put operation completes successfully.
override
beforeClear(String boxName) Future<bool>
Called before a box is cleared.
override
beforeDelete(String boxName, dynamic key) Future<bool>
Called before a delete operation.
override
beforePut(String boxName, dynamic key, dynamic value) Future<bool>
Called before a put operation.
override
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
removeSchema(String boxName) → void
Removes the schema for a box.
toString() String
A string representation of this object.
inherited

Operators

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