Validation Options

Complete reference for all validation options available in CQL models.

Validation Macro

validate :field_name, option: value, option2: value2

Common Options

presence

Validates that a field is not nil and not empty.

validate :name, presence: true
validate :email, presence: true

Error: "field_name is required"

format / match

Validates that a field matches a regular expression.

validate :email, match: /@/
validate :phone, match: /^\d{10}$/
validate :slug, match: /^[a-z0-9-]+$/

Error: "field_name format is invalid"

size

Validates the length of a string or size of a collection.

Error: "field_name must be between X and Y characters"

gt / gte / lt / lte

Validates numeric values are within bounds.

Error: "field_name must be greater than X" / "field_name must be less than or equal to X"

in

Validates that a value is in a list of allowed values.

Error: "field_name must be one of: X, Y, Z"

exclude

Validates that a value is not in a list of excluded values.

Error: "field_name must not be included in [X, Y, Z]"

Combining Validations

Multiple validations can be applied to a single field:

Model Example

Checking Validation

Custom Validations

For complex validations, use callbacks:

See Also

Last updated

Was this helpful?