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
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