SchemaValidator class

Validate form payloads with explicit unknown-field and custom-rule policies.

Validation is not authorization or network verification. A custom rule returns null on success; sanitized maps and snapshots remain shallow.

Gallery walkthrough · browser route /#/validation. These examples use the current checkout; published 1.x APIs can differ.

Validate and sanitize

import 'package:activity/activity.dart';

void main() {
  final validator = SchemaValidator({
    'age': {'type': int, 'min': 18},
  }, allowUnknownKeys: false);
  final result = validator.validate({'age': 21, 'extra': 'omitted'});
  assert(result.valid && !result.schema.containsKey('extra'));
}

Explain a recoverable field error

import 'package:activity/activity.dart';

void main() {
  final validator = SchemaValidator({
    'name': {
      'type': String,
      'custom':
          (dynamic value, Map<String, dynamic> data) =>
              value == 'admin' ? 'Reserved name' : null,
    },
  });
  final result = validator.validate({'name': 'admin'});
  assert(result.errors['name'] == 'Reserved name');
  // Keep the form draft and display result.errors beside its fields.
}

Constructors

SchemaValidator(Map<String, dynamic> schema, {Map<String, String>? customErrors, bool allowUnknownKeys = true})

Properties

allowUnknownKeys → bool
final
customErrors → Map<String, String>?
Optional custom error messages.
final
hashCode → int
The hash code for this object.
no setterinherited
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() → String
A string representation of this object.
inherited
validate(Map<String, dynamic> data) → SchemaResponse
Validate data against this schema.

Operators

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