module Check::Checkable

Overview

Mixin that adds #check method to be used with variables of an instance of the class including it. The idea is to check the instance variables of the class extending it against rules defined with Check.rules plus executing custom checkers defined with Checker.

Defined in:

checkable.cr

Instance Method Summary

Instance Method Detail

def after_check(v : Check::Validation, required : Bool = true, format : Bool = true) #

Lifecycle method triggered after each call of #check.

# Triggered on instance: `user.check`
def after_check(v : Check::Validation, required : Bool = true, format : Bool = true)
  # Code...
end

def before_check(v : Check::Validation, required : Bool = true, format : Bool = true) #

Lifecycle method triggered before each call of #check.

# Triggered on instance: `user.check`
def before_check(v : Check::Validation, required : Bool = true, format : Bool = true)
  # Code...
end

def check(required : Bool = true, format : Bool = true) : Validation #

Same as #check but this method raises a Check::ValidationError if the validation fails or if the clean has not been processed successfully.

myCheckable.check! # => Returns the current instance.

This method returns self, so it chainable :)

user.email = "me@example.org"
user.check!.save

def check(v : Check::Validation, required : Bool = true, format : Bool = true) : Validation #

Checks the instance fields and clean them.

It instantiates a Check::Validation (if not provided) and calls all methods related to rules and then methods defined with annotation Checker.

Lifecycle methods #before_check and #after_check that are triggered respectively at the beginning and at the end of the process.

format is used to tell cleaners generated by Check.rules to execute format method if it has been defined.


def check!(required : Bool = true, format : Bool = true) : self #

Same as #check but this method raises a Check::ValidationError if the validation fails or if the clean has not been processed successfully.

myCheckable.check! # => Returns the current instance.

This method returns self, so it chainable :)

user.email = "me@example.org"
user.check!.save

def check!(v : Check::Validation, required : Bool = true, format : Bool = true) : self #

Same as #check but this method raises a Check::ValidationError if the validation fails or if the clean has not been processed successfully.

v = Validation.new_validation
myCheckable.check!(v) # => Returns the current instance.

This method returns self, so it chainable :)

v = Validation.new_validation
user.email = "me@example.org"
user.check!(v).save