module Avram::Validations

Overview

A number of methods for validating Avram::Attributes All validation methods return Bool. false if any error is added, otherwise true

This module is included in Avram::Operation, Avram::SaveOperation, and Avram::DeleteOperation

Extended Modules

Direct including types

Defined in:

avram/validations.cr

Instance Method Summary

Macro Summary

Instance Method Detail

def validate_acceptance_of(attribute : Avram::Attribute(Bool), message : Avram::Attribute::ErrorMessage = Avram.settings.i18n_backend.get(:validate_acceptance_of)) : Bool #

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.


[View source]
def validate_at_most_one_filled(*attributes, message : Avram::Attribute::ErrorMessage = Avram.settings.i18n_backend.get(:validate_at_most_one_filled)) : Bool #

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.


[View source]
def validate_confirmation_of(attribute : Avram::Attribute(T), with confirmation_attribute : Avram::Attribute(T), message : Avram::Attribute::ErrorMessage = Avram.settings.i18n_backend.get(:validate_confirmation_of)) : Bool forall T #

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.


[View source]
def validate_exactly_one_filled(*attributes, message : Avram::Attribute::ErrorMessage = Avram.settings.i18n_backend.get(:validate_exactly_one_filled)) : Bool #

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.


[View source]
def validate_format_of(attribute : Avram::Attribute(String), with regex : Regex, match : Bool = true, message : Avram::Attribute::ErrorMessage = Avram.settings.i18n_backend.get(:validate_format_of), allow_nil : Bool = false) : Bool #

Validates that the passed in attributes matches the given regex

validate_format_of email, with: /[^@]+@[^\.]+\..+/

Alternatively, the match argument can be set to false to not match the given regex.


[View source]
def validate_inclusion_of(attribute : Avram::Attribute(T), in allowed_values : Enumerable(T), message : Avram::Attribute::ErrorMessage = Avram.settings.i18n_backend.get(:validate_inclusion_of), allow_nil : Bool = false) : Bool forall T #

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


[View source]
def validate_numeric(attribute : Avram::Attribute(Number), greater_than = nil, less_than = nil, message = nil, allow_nil : Bool = false) : Bool #

DEPRECATED Use validate_numeric with at_least/no_more_than instead of greater_than/less_than


[View source]
def validate_numeric(attribute : Avram::Attribute(Number), *, at_least = nil, no_more_than = nil, message = nil, allow_nil : Bool = false) : Bool #

Validate a number is at_least and/or no_more_than

validate_numeric age, at_least: 18
validate_numeric count, at_least: 0, no_more_than: 1200

ameba:disable Metrics/CyclomaticComplexity


[View source]
def validate_required(*attributes, message : Avram::Attribute::ErrorMessage = Avram.settings.i18n_backend.get(:validate_required)) : Bool #

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

[View source]
def validate_size_of(attribute : Avram::Attribute(String) | Avram::Attribute(Array(T)), min : Number | Nil = nil, max : Number | Nil = nil, message : Avram::Attribute::ErrorMessage | Nil = nil, allow_nil : Bool = false) : Bool forall T #

Validate the size of a String or Array is within a min and/or max

validate_size_of feedback, min: 18, max: 100
validate_size_of password, min: 12
validate_size_of options, max: 10

ameba:disable Metrics/CyclomaticComplexity


[View source]
def validate_size_of(attribute : Avram::Attribute(String) | Avram::Attribute(Array(T)), *, is exact_size : Number, message : Avram::Attribute::ErrorMessage = Avram.settings.i18n_backend.get(:validate_exact_size_of), allow_nil : Bool = false) : Bool forall T #

Validate the size of a String or Array is exactly a certain size

validate_size_of api_key, is: 32
validate_size_of theme_colors, is: 4

[View source]

Macro Detail

macro default_validations #

Defines an instance method that gets called during validation of an operation. Define your default validations inside of the block.

default_validations do
  validate_required some_attribute
end

[View source]