Overview
Ack is a schema validation library for Dart and Flutter that helps you validate data with a simple, fluent API. Ack is short for "acknowledgment".
Why Use Ack?
- Simplify Validation: Easily handle complex data validation logic.
- Ensure Data Integrity: Guarantee data consistency from external sources (APIs, user input).
- Single Source of Truth: Define data structures and rules in one place.
- Reduce Boilerplate: Minimize repetitive validation code.
What Ack Does
- Validates data against defined schemas
- Provides detailed error messages for validation failures
- Generates JSON Schema specifications
- Works seamlessly with JSON data from APIs
Basic Usage
import 'package:ack/ack.dart';
// Define the structure and rules for a user object.
final userSchema = Ack.object({
'name': Ack.string().minLength(2).maxLength(50), // Name must be a string between 2 and 50 chars.
'age': Ack.integer().min(0).max(120), // Age must be an integer between 0 and 120.
'email': Ack.string().email().nullable(), // Email must be a valid format, but can be null.
}); // All fields are required by default
// Data to validate.
final dataToValidate = {
'name': 'John',
'age': 30,
'email': 'john@example.com'
};
// Perform the validation against the schema.
final result = userSchema.safeParse(dataToValidate);
// Check the validation outcome.
if (result.isOk) {
// Validation passed, safely access the validated data.
final validData = result.getOrThrow();
print('Valid data: $validData');
} else {
// Validation failed, access the detailed error.
final error = result.getError();
print('Validation Error: $error'); // Use error.toString() for full details.
}
Core Features
Ack provides a comprehensive set of features for data validation and transformation:
- Schema Types: Define data structures with strings, numbers, booleans, lists, objects, and more
- Validation Rules: Apply constraints like length, range, pattern matching, and custom validators
- Error Handling: Get detailed, structured error messages for validation failures
- JSON Serialization: Convert between JSON and typed models
- Schema Validation: Comprehensive schema validation with fluent API
Next Steps
Ready to dive deeper? Start with the Quickstart Tutorial for a step-by-step guide, or explore specific topics:
- Schema Types - Learn about all available schema types
- Validation Rules - Master validation constraints
- Error Handling - Handle validation errors effectively
- Flutter Integration - Validate Flutter forms
Advanced Features
Ack provides powerful features for complex validation scenarios:
- Nested validation: Validate deeply nested objects and lists
- Custom validation: Add custom logic with
.refine()
and constraints - Union types: Handle polymorphic data with
anyOf()
and discriminated unions - Data transformation: Transform validated data with
.transform()
- JSON Schema generation: Generate JSON Schema specs for API documentation
For detailed examples and usage patterns, see the Common Recipes guide.