Validations
CQL's Active Record integration provides a simple yet effective way to ensure data integrity through model-level validations. By defining validation logic within your models, you can prevent invalid data from being persisted to the database.
The CQL::ActiveRecord::Validations
module is automatically included when you use CQL::ActiveRecord::Model(Pk)
.
Defining Validations
Validations are typically implemented by overriding the validate
instance method in your model. This method is called automatically before saving a record (create or update).
Inside the validate
method, you should check the model's attributes. If an attribute is invalid, you add a message to the errors
collection for that attribute.
Model Example:
Checking Validity
You can explicitly check if a model instance is valid using the valid?
and validate!
methods.
valid?
valid?
The valid?
method runs the validate
method and returns true
if the errors
collection is empty, and false
otherwise. It does not raise an exception.
validate!
validate!
The validate!
method also runs the validate
method. If the record is invalid, it raises a CQL::Errors::RecordInvalid
exception containing the validation errors. If the record is valid, it returns true
.
The errors
Object
errors
ObjectThe errors
object is an instance of CQL::Errors::Collection
. It provides methods to add and inspect error messages.
errors.add(attribute : Symbol, message : String)
: Adds an error message for the specified attribute.errors.empty?
: Returnstrue
if there are no errors.errors.clear
: Removes all error messages.errors.on(attribute : Symbol)
: Returns an array of error messages for a specific attribute, ornil
if none.errors.full_messages
: Returns an array of user-friendly error messages, typically in the format "Attribute_name message" (e.g., "Name cannot be blank").errors.to_h
: Returns a hash where keys are attribute names (Symbols) and values are arrays of error messages for that attribute.
Validations and Persistence Methods
Validation is automatically integrated with the persistence methods:
save
/save!
:save
runs validations. If they fail (valid?
returnsfalse
),save
returnsfalse
and the record is not persisted.save!
also runs validations. If they fail, it raisesCQL::Errors::RecordInvalid
.
create
/create!
:create(attributes)
is likenew(attributes).save
. It returns the instance (which may be invalid and not persisted) orfalse
if using abefore_create
callback that halts.create!(attributes)
is likenew(attributes).save!
. It raisesCQL::Errors::RecordInvalid
if validations fail.
update(attributes)
/update!(attributes)
:update
assigns attributes and then callssave
. Returnstrue
orfalse
.update!
assigns attributes and then callssave!
. RaisesCQL::Errors::RecordInvalid
on failure.
Example with save
:
Example with create!
:
Custom Validation Helpers (Optional)
For more complex or reusable validation logic, you can define private helper methods within your model.
By using these validation features, you can maintain data consistency and provide clear feedback about data entry issues.
Remember that CQL::ActiveRecord::Validations
provides a basic framework. For very complex validation scenarios or integrating with external validation libraries, you might need to extend this functionality further.
Last updated
Was this helpful?