A flexible, composeable, and type-safe validation library for Dart & Flutter.
Documentation API Documentation
Caution
This is the complete rewrite of the Validasi library into a completely new API (with modifier support).
To use this package, add validasi as a dependency in your pubspec.yaml file:
dependencies:
validasi: ^1.0.0-rc.1Check the pub.dev page for the latest pre-release version.
To use this library, simply import package:validasi/validasi.dart and the rules from package:validasi/rules.dart. Here's a basic example:
import 'package:validasi/validasi.dart';
import 'package:validasi/rules.dart';
void main() {
final schema = Validasi.string([
Rules.nullable<String>(),
Rules.transform<String>((input) => input?.trim()),
Rules.string.minLength(3),
Rules.string.maxLength(16)
]);
final result = schema.validate(' Hello World! ');
print("isValid: ${result.isValid}, errors: ${result.errors.map((e) => e.message).join(', ')}, value: ${result.data}");
}Map Validation:
final schema = Validasi.map<dynamic>([
Rules.map.hasFields({
'name': Validasi.string([Rules.string.minLength(1)]),
'age': Validasi.number<int>([Rules.number.moreThan(0)]),
}),
]);
final result = schema.validate({'name': 'John', 'age': 30});List Validation:
final schema = Validasi.list<String>([
Rules.iterable.forEach<String>(
Validasi.string([Rules.string.minLength(1)]),
),
]);
final result = schema.validate(['item1', 'item2', 'item3']);Refer to the examples folder to see more usage samples or see the documentation.
Validasi provides type-safe validation schemas for various data types:
Validasi.string()- String validationValidasi.number<T>()- Numeric validation (int, double, num)Validasi.list<T>()- List/Iterable validationValidasi.map<T>()- Map validationValidasi.any<T>()- Generic type validation
The library comes with comprehensive built-in rules organized by data type. See the documentation for the full list:
- String Rules -
alpha,email,url,uuid,regex, and more - Number Rules -
finite,lessThan,moreThan, and more - List Rules -
minLength,maxLength,unique,contains,forEach, and more - Map Rules -
hasFields,hasFieldKeys,allowedKeys,requiredAny,matchesField, and more - Generic Rules -
required,nullable,transform,equals,anyOf,inline, and more
Use withPreprocess to transform input data before validation:
final schema = Validasi.string([Rules.string.minLength(3)])
.withPreprocess((value) => value.toString());
final result = schema.validate(123); // Converts to "123" then validatesAll validation returns a ValidasiResult object that contains:
isValid- Boolean indicating validation successdata- The validated (and potentially transformed) dataerrors- List of validation errors with messages and paths
Validasi tracks error paths for nested structures, making it easy to identify exactly where validation fails:
final result = schema.validate(complexNestedData);
result.errors.forEach((error) {
print("Error at ${error.path?.join('.')}: ${error.message}");
});The Validasi Library is licensed under MIT License.
We welcome contributions! Here's how to set up the development environment:
# Clone the repository
git clone https://github.com/albetnov/validasi
cd validasi
# Install root dependencies (includes melos)
dart pub get
# Install workspace dependencies
dart run melos bootstrapThe test structure in packages/validasi/test/ mirrors packages/validasi/lib/src.
# Run all tests
dart run melos run test
# Run tests for root package only
dart run melos run test:validasi
# Run tests for MCP package only
dart run melos run test:mcp
# Run tests with coverage
pushd packages/validasi
dart test --coverage=coverage
# Format coverage report
dart pub global activate coverage
dart pub global run coverage:format_coverage --lcov --in=coverage --out=coverage/lcov.info --report-on=lib
popd