module Check::CheckableStatic

Overview

Mixin that adds .check method to be used with a Hash. The idea is to check a Hash against rules defined with Check.rules plus executing custom checkers defined with Checker annotation.

Defined in:

checkable.cr

Instance Method Summary

Instance Method Detail

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

Lifecycle method triggered after each call of .check.

This method (in static call) must returns the cleaned Hash which is provided in the third argument. You can update this cleaned hash but you have to return it.

# Triggered on a static call: `User.check(h)` (with a `Hash` or `JSON::Any`)
def self.after_check(v : Check::Validation, h, cleaned_h, required : Bool = true, format : Bool = true) : Hash
  # Code...
  pp cleaned_h
  cleaned_h # <= returns cleaned_h!
end

[View source]
def before_check(v : Check::Validation, h : Hash, required : Bool = true, format : Bool = true) #

Lifecycle method triggered before each call of .check.

# Triggered on a static call: `User.check(h)` (with a `Hash` or `JSON::Any`)
def self.before_check(v : Check::Validation, h, required : Bool = true, format : Bool = true)
  # Code...
  pp h
end

[View source]
def check(v : Check::Validation, h : Hash, required : Bool = true, format : Bool = true) #

Checks and clean the Hash for its fields corresponding to class variables that have a .check_{{field}} method.

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


[View source]
def check(h : Hash, required : Bool = true, format : Bool = true) #

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

cleaned_h = MyCheckable.check!(h)

[View source]
def check!(v : Check::Validation, h : Hash, required : Bool = true, format : Bool = true) #

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

cleaned_h = MyCheckable.check!(v, h)

[View source]
def check!(h : Hash, required : Bool = true, format : Bool = true) #

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

cleaned_h = MyCheckable.check!(h)

[View source]
def h_from_json(json : String | IO) #

Returns a json Hash from a JSON input. The return type is a tuple with a bool as a first argument indicating that the JSON.parse has been processed successfully or not and the 2nd argument is the json Hash.

ok, user_h = User.h_from_json(json) # => true, {"username" => "Bob", "email" => "[email protected]"}

[View source]
def h_from_json!(json : String | IO) #

Returns a json Hash from a JSON input. Same as #h_from_json, except that this method raises a JSON::ParseException if the conversion fails.

user_h = User.h_from_json!(json) # => {"username" => "Bob", "email" => "[email protected]"}

[View source]
def map_json_keys : Hash(String, String) #

Macro that returns the mapping of the JSON fields


[View source]
def to_crystal_h(h : Hash) : Hash #

Returns a new Hash with all JSON keys converted to Crystal keys.


[View source]
def to_json_h(h : Hash) : Hash #

Returns a new Hash with all Crystal keys converted to JSON keys.


[View source]