Validate Models

This guide shows you how to add validation to your CQL database models.

Basic Model Validation

Add validations to your CQL model:

class User
  include CQL::Model(User, Int64)

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

  validate name, presence: true, length: {min: 2, max: 100}
  validate email, presence: true, format: /@/
  validate age, numericality: {greater_than: 0, less_than: 150}, allow_nil: true
end

Validation Rules

Presence

Uniqueness

Ensure a value is unique in the database:

Length

Format

Numericality

Inclusion

Custom Model Validation

Validation Callbacks

Run code before or after validation:

Checking Validity

Skipping Validations

When necessary, skip validations:

Validation Contexts

Use contexts for different validation scenarios:

See Also

Last updated

Was this helpful?