Declarative form validation with composable rules and JSON schemas
dart pub add philiprehberger_form_validatorDeclarative form validation with composable rules and JSON schemas
Add to your pubspec.yaml:
dependencies:
philiprehberger_form_validator: ^0.1.0
Then run:
dart pub get
import 'package:philiprehberger_form_validator/form_validator.dart';
final schema = FormSchema({
'name': [Rules.required(), Rules.minLength(2)],
'email': [Rules.required(), Rules.email()],
});
final result = schema.validate({
'name': 'Alice',
'email': 'alice@example.com',
});
print(result.isValid); // true
Rules.required()
Rules.email()
Rules.url()
Rules.minLength(3)
Rules.maxLength(100)
Rules.pattern(RegExp(r'^\d+$'))
Rules.numeric()
Rules.between(1, 100)
Rules.equals('password') // cross-field comparison
Rules.oneOf(['a', 'b', 'c'])
Rules.custom((v) => v != null, message: 'Required')
final schema = FormSchema.fromJson({
'email': ['required', 'email'],
'name': ['required', 'minLength:3', 'maxLength:100'],
'age': ['numeric', 'between:18,120'],
});
final schema = FormSchema({
'password': [Rules.required(), Rules.minLength(8)],
'confirm': [Rules.required(), Rules.equals('password')],
});
final result = schema.validate({
'password': 'secret123',
'confirm': 'secret123',
});
print(result.isValid); // true
final result = schema.validate(data);
result.isValid; // true if no errors
result.hasError('email'); // check specific field
result.errorsFor('email'); // list of error messages
result.allErrors; // flat list of all errors
result.errorCount; // total error count
| Class | Description |
|---|---|
FieldValidator | Single validation rule with message and test function |
Rules | Static factory methods for built-in validators |
FormSchema | Schema defining validators per field, validates form data maps |
FormSchema.fromJson() | Create schema from JSON-like rule descriptor map |
ValidationResult | Result object with errors, field queries, and counts |
CrossFieldValidator | Validator that compares against another field's value |
dart pub get
dart analyze --fatal-infos
dart test
If you find this project useful: