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.crInstance Method Summary
-
#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. -
#before_check(v : Check::Validation, h : Hash, required : Bool = true, format : Bool = true)
Lifecycle method triggered before each call of
.check. -
#check(v : Check::Validation, h : Hash, required : Bool = true, format : Bool = true)
Checks and clean the
Hashfor its fields corresponding to class variables that have a.check_{{field}}method. -
#check(h : Hash, required : Bool = true, format : Bool = true)
Same as
#checkbut this method raises aCheck::ValidationErrorif the validation fails or if the clean has not been processed successfully. -
#check!(v : Check::Validation, h : Hash, required : Bool = true, format : Bool = true)
Same as
#checkbut this method raises aCheck::ValidationErrorif the validation fails or if the clean has not been processed successfully. -
#check!(h : Hash, required : Bool = true, format : Bool = true)
Same as
#checkbut this method raises aCheck::ValidationErrorif the validation fails or if the clean has not been processed successfully. -
#h_from_json(json : String | IO)
Returns a json
Hashfrom a JSON input. -
#h_from_json!(json : String | IO)
Returns a json
Hashfrom a JSON input. -
#map_json_keys : Hash(String, String)
Macro that returns the mapping of the JSON fields
-
#to_crystal_h(h : Hash) : Hash
Returns a new
Hashwith all JSON keys converted to Crystal keys. -
#to_json_h(h : Hash) : Hash
Returns a new
Hashwith all Crystal keys converted to JSON keys.
Instance Method Detail
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
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
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.
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)
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)
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)
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" => "user@example.com"}
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" => "user@example.com"}
Returns a new Hash with all JSON keys converted to Crystal keys.