Validation Rules
Ack provides a rich set of built-in validation rules (constraints) that you can chain onto schema types to enforce specific requirements. Built-in constraints provide default error messages. For custom error messages, use custom constraints (see Custom Validation Guide).
nullable()
Allows the value to be null
in addition to the type's constraints.
final optionalName = Ack.string.nullable();
final optionalAge = Ack.int.nullable();
constrain(SchemaConstraint constraint)
Applies a custom SchemaConstraint
. See the Custom Validation guide.
String Constraints
Apply these to Ack.string
schemas.
Exact Length
To ensure a string has exactly a specific length, combine minLength()
and maxLength()
:
// String must be exactly 10 characters
Ack.string.minLength(10).maxLength(10)
matches(String pattern, {String? example})
Ensures the string fully matches the given regular expression pattern
. The pattern is automatically anchored with ^
and $
if not already present.
// Simple alphanumeric pattern
Ack.string.matches(r'[a-zA-Z0-9]+')
// UUID pattern
Ack.string.matches(r'[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12}')
contains(String pattern, {String? example})
Ensures the string contains the given pattern anywhere within it.
// Password must contain at least one uppercase letter
Ack.string.contains(r'[A-Z]')
// Password must contain at least one digit
Ack.string.contains(r'[0-9]')
enumValues(List<String> allowedValues)
Ensures the string is one of the allowedValues
.
Ack.string.enumValues(['active', 'inactive', 'pending'])
Number Constraints (Int and Double)
Apply these to Ack.int
and Ack.double
schemas.
min(num limit)
Ensures the number is greater than or equal to the limit
.
Ack.int.min(0) // >= 0
Ack.double.min(0.0) // >= 0.0
max(num limit)
Ensures the number is less than or equal to the limit
.
Ack.int.max(100) // <= 100
Ack.double.max(100.0) // <= 100.0
multipleOf(num factor)
Ensures the number is a multiple of the factor
.
Ack.int.multipleOf(5) // Must be divisible by 5
Ack.double.multipleOf(0.01) // Allows up to 2 decimal places
positive()
Ensures the number is positive (greater than 0).
Ack.int.positive() // > 0
Ack.double.positive() // > 0.0
negative()
Ensures the number is negative (less than 0).
Ack.int.negative() // < 0
Ack.double.negative() // < 0.0
List Constraints
Apply these to Ack.list
schemas.
notEmpty()
Ensures the list is not empty. Equivalent to minItems(1)
.
Ack.list(Ack.object({...})).notEmpty()
uniqueItems()
Ensures all items in the list are unique. Uses Set
comparison.
Ack.list(Ack.string).uniqueItems()
Combining Constraints
You can chain multiple constraints together. They are evaluated in the order they are applied.
final usernameSchema = Ack.string
.minLength(3) // First check min length
.maxLength(20) // Then check max length
.matches(r'[a-z0-9_]+') // Then check pattern (lowercase alphanumeric/underscore)
.notEmpty(); // Redundant if minLength(>0) is used, but illustrates chaining
final quantitySchema = Ack.int
.min(1) // Must be at least 1
.max(100) // Must be at most 100
.multipleOf(1); // Must be an integer (redundant for Ack.int)