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).

Common Constraints (Applicable to Multiple Types)

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.

minLength(int min)

Ensures the string has at least min characters.

Ack.string.minLength(5)

maxLength(int max)

Ensures the string has at most max characters.

Ack.string.maxLength(100)

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)

notEmpty()

Ensures the string is not empty (''). Equivalent to minLength(1).

Ack.string.notEmpty()

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]')

email()

Ensures the string is a valid email address format.

Ack.string.email()

date()

Ensures the string is a valid date format (YYYY-MM-DD).

Ack.string.date()

dateTime()

Ensures the string is a valid date-time format (ISO 8601).

Ack.string.dateTime()

time()

Ensures the string is a valid time format (HH:MM:SS).

Ack.string.time()

uri()

Ensures the string is a valid URI according to RFC 3986.

Ack.string.uri()

uuid()

Ensures the string is a valid UUID according to RFC 4122.

Ack.string.uuid()

ipv4()

Ensures the string is a valid IPv4 address.

Ack.string.ipv4()

ipv6()

Ensures the string is a valid IPv6 address.

Ack.string.ipv6()

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.

minItems(int min)

Ensures the list has at least min items.

Ack.list(Ack.string).minItems(1)

maxItems(int max)

Ensures the list has at most max items.

Ack.list(Ack.int).maxItems(10)

exactItems(int count)

Ensures the list has exactly count items.

Ack.list(Ack.boolean).exactItems(5)

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)