Validations

CQL provides model-level validations that run before saving records to the database.

Model vs Request Validations

Azu applications have two validation layers:

Layer
Purpose
When to Use

Request Validations (Azu::Request)

Validate incoming HTTP data

Input sanitization, format checks

Model Validations (CQL::ActiveRecord)

Validate business rules

Uniqueness, relationships, complex rules

Both layers work together for defense in depth.

Basic Validations

struct User
  include CQL::ActiveRecord::Model(Int64)
  db_context AppDB, :users

  getter id : Int64?
  getter name : String
  getter email : String
  getter age : Int32?

  # Required field
  validates :name, presence: true

  # Length constraints
  validates :name, size: 2..50

  # Format validation
  validates :email, match: /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i

  # Numeric validation
  validates :age, gt: 0, lt: 150
end

Available Validators

Presence

Size/Length

Format

Numeric

Inclusion

Exclusion

Confirmation

Combining Validators

Custom Validations

Validation Errors

Saving with Validations

Integration with Azu Endpoints

Next Steps

  • Models - Full model definition with validations

  • Queries - Query validated data

Last updated

Was this helpful?