module Avram::Validations
Overview
A number of methods for validating Avram::Attributes
This module is included in Avram::Operation
and Avram::SaveOperation
Extended Modules
Direct including types
Defined in:
avram/validations.crInstance Method Summary
-
#validate_acceptance_of(attribute : Avram::Attribute(Bool | Nil), message : Avram::Attribute::ErrorMessage = "must be accepted")
Validate whether an attribute was accepted (
true
) -
#validate_at_most_one_filled(*attributes, message : Avram::Attribute::ErrorMessage = "must be blank")
Validates that at most one attribute is filled
-
#validate_confirmation_of(attribute : Avram::Attribute(T), with confirmation_attribute : Avram::Attribute(T), message : Avram::Attribute::ErrorMessage = "must match") forall T
Validates that the values of two attributes are the same
-
#validate_exactly_one_filled(*attributes, message : Avram::Attribute::ErrorMessage = "at least one must be filled")
Validates that at exactly one attribute is filled
-
#validate_inclusion_of(attribute : Avram::Attribute(T), in allowed_values : Enumerable(T), message : Avram::Attribute::ErrorMessage = "is invalid", allow_nil : Bool = false) forall T
Validates that the attribute value is in a list of allowed values
-
#validate_required(*attributes, message : Avram::Attribute::ErrorMessage = "is required")
Validates that the passed in attributes have values
-
#validate_size_of(attribute : Avram::Attribute, min = nil, max = nil, allow_nil : Bool = false)
Validate the size of the attribute is within a
min
and/ormax
-
#validate_size_of(attribute : Avram::Attribute, *, is exact_size, message : Avram::Attribute::ErrorMessage = "is invalid", allow_nil : Bool = false)
Validate the size of a
String
is exactly a certain size -
#validate_uniqueness_of(attribute : Avram::Attribute, query : Avram::Criteria, message : String = "is already taken")
Validates that the given attribute is unique in the database
-
#validate_uniqueness_of(attribute : Avram::Attribute, message : Avram::Attribute::ErrorMessage = "is already taken")
Validates that the given attribute is unique in the database with a custom query
Instance Method Detail
Validate whether an attribute was accepted (true
)
This validation is only for Boolean Attributes. The attribute will be marked
as invalid for any value other than true
.
Validates that at most one attribute is filled
If more than one attribute is filled it will mark all but the first filled field invalid.
Validates that the values of two attributes are the same
Takes two attributes and if the values are different the second attribute
(with
/confirmation_attribute
) will be marked as invalid
Example:
validate_confirmation_of password, with: password_confirmation
If password_confirmation
does not match, it will be marked invalid.
Validates that at exactly one attribute is filled
This validation is used by Avram::Polymorphic.polymorphic
to ensure
that a required polymorphic association is set.
If more than one attribute is filled it will mark all but the first filled field invalid.
If no field is filled, the first field will be marked as invalid.
Validates that the attribute value is in a list of allowed values
validate_inclusion_of state, in: ["NY", "MA"]
This will mark state
as invalid unless the value is "NY"
, or "MA"
.
Validates that the passed in attributes have values
You can pass in one or more attributes at a time. The attribute will be
marked as invalid if the value is nil
, or "blank" (empty strings or strings with just whitespace)
false
is not considered invalid.
validate_required name, age, email
Validate the size of the attribute is within a min
and/or max
validate_size_of age, min: 18, max: 100
validate_size_of account_balance, min: 500
Validate the size of a String
is exactly a certain size
validate_size_of api_key, is: 32
Validates that the given attribute is unique in the database
This will only work with attributes that correspond to a database column.
validate_uniqueness_of email
If there is another email address with the same value, the attribute will be marked as invalid.
Note that you should also add a unique index when creating the table so that there are no race conditions and you are guaranteed that the value is unique.
This validation is still useful as it will check for uniqueness along with all other validations. If you just have the uniqueness constraint in the database you will not know if there is a collision until all other validations pass and Avram tries to save the record.
Validates that the given attribute is unique in the database with a custom query
The principle is the same as the other #validate_uniqueness_of
method, but
this one allows customizing the query.
This is especially helpful when you want to scope the uniqueness to a subset of records.
For example, if you want to check that a username is unique within a company:
validate_uniqueness_of username, query: UserQuery.new.company_id(123)
So if there is the same username in other companies, this validation will still pass.
Note that you should also add a unique validation constraint in the database This can be done using Avram migrations. For example:
add_index [:username, :company_id], unique: true