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 code for validation and JSON conversion.

What Ack Does

  • Ensures data conforms to your defined schemas
  • Facilitates seamless conversion between JSON and Dart models
  • Generates JSON Schema specifications from your schemas
  • Delivers detailed and easy-to-understand validation error messages

Quick Start

Add Ack to your project:

dart pub add ack

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.int.min(0).max(120),                // Age must be an integer between 0 and 120.
  'email': Ack.string.isEmail().nullable(),      // Email must be a valid format, but can be null.
}, required: ['name', 'age']); // Name and age are mandatory fields.

// Data to validate.
final dataToValidate = {
  'name': 'John',
  'age': 30,
  'email': 'john@example.com'
};

// Perform the validation against the schema.
final result = userSchema.validate(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.message}'); // Or 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
  • TypeSafe Schemas: Create type-safe schemas with automatic validation
  • Error Handling: Get detailed, structured error messages for validation failures
  • JSON Serialization: Convert between JSON and typed models

TypeSafe Schemas

Turn any Dart class into a validating schema with annotations:

import 'package:ack_generator/ack_generator.dart';
import 'dart:convert';

// Define a model with validation
@Schema()
class User {
  @MinLength(2)
  final String name;

  @IsEmail()
  final String email;

  @Min(18)
  final int age;

  User({required this.name, required this.email, required this.age});
}

// Dynamic payload from API, form, etc.
final Map<String, dynamic> payload = {
  'name': 'John',
  'email': 'john@example.com',
  'age': 30
};

Generated schemas provide three main capabilities:

1. Direct model validation and conversion

// Validate and convert to a type-safe model in one step
final User user = UserSchema.parse(payload);
print('Hello ${user.name}!');  // Type-safe model access

2. Type-safe access through the schema

// Access typed properties directly from the schema
final userSchema = UserSchema(payload);
if (userSchema.isValid) {
  String name = userSchema.name;  // Typed as String
  int age = userSchema.age;       // Typed as int
  print('$name is $age years old');
}

3. JSON Schema generation for documentation

// Generate JSON Schema from the same model definition
final jsonSchema = UserSchema.toJsonSchema();
print(jsonEncode(jsonSchema));  // Use in API docs or client validation

Error Handling

if (result.isFail) {
  // Get formatted error message (or the full error object)
  final error = result.getError();
  print('${error.name}: ${error.message}'); // Specific message
  // print(error); // Full error details including path
}