Validator
∠(・.-)―〉 →◎ validator is a Crystal data validation module.
Very simple and efficient, all validations return true or false.
Also validator/check (not exposed by default) provides error message handling intended for the end user.
There are 2 main ways to use validator:
- As a simple validator to check rules (eg: email, url, min, max, presence, in, ...) which return a boolean.
- As a more advanced validation system which will check a series of rules and returns all validation errors encountered with custom or standard messages.
By default the validator module expose only Validator and Valid (alias) in the scope:
require "validator"
Valid.email? "contact@example.org" # => true
Valid.url? "https://github.com/Nicolab/crystal-validator" # => true
Valid.my_validator? "value to validate", "hello", 42 # => true
An (optional) expressive validation flavor, is available as an alternative.
Not exposed by default, it must be imported:
require "validator/is"
is :email?, "contact@example.org" # => true
is :url?, "https://github.com/Nicolab/crystal-validator" # => true
is :my_validator?, "value to validate", "hello", 42 # => true
# raises an error if the email is not valid
is! :email?, "contact@@example..org" # => Validator::Error
is is a macro, no overhead during the runtime 🚀
By the nature of the macros, you can't pass the validator name dynamically
with a variable like that is(validator_name, "my value to validate", arg).
But of course you can pass arguments with variables is(:validator_name?, arg1, arg2).
Check
Make a series of checks, with a customized error message for each case.
require "validator/check"
check = Check.new
check("email", "The email is required.", is :absence?, "email", user)
Custom validator
Just add your own method to register a custom validator or to overload an existing validator.
module Validator
# My custom validator
def self.my_validator?(value, arg : String, another_arg : Int32) : Bool
# write here the logic of your validator...
return true
end
end
# Call it
puts Valid.my_validator?("value to validate", "hello", 42) # => true
# or with the `is` flavor
puts is :my_validator?, "value to validate", "hello", 42 # => true
Check is a simple and lightweight wrapper, let your imagination run wild to add your logic around it.
Using the custom validator with the validation rules:
require "validator/check"
class Article
# Mixin
Check.checkable
property title : String
property content : String
Check.rules(
content: {
# Now the custom validator is available
check: {
my_validator: {"My validator error message"},
between: {"The article content must be between 10 and 20 000 characters", 10, 20_000},
# ...
},
},
)
end
# Triggered with all data
v, article = Article.check(input_data)
# Triggered with one value
v, content = Article.check_content(input_data["content"]?)
Constants
Class methods
Validates the absence of the value.
- See also
#not_in?. - See also
#empty?.
Validates the absence of the value.
- See also
#not_in?. - See also
#empty?.
Validates that the value String is the representation of an acceptance.
One of: "yes", "y", "on", "o", "ok", "1", "true"
- See also
#refused?.
Validates that the value is the representation of an acceptance.
One of: "yes", "y", "on", "o", "ok", "1", "true"
value must implements #to_s method.
- See also
#refused?.
Validates that the value String
is comprised in its entirety by ASCII characters.
Validates that all the String in the values Array
are comprised in their entirety by ASCII characters.
Validates that the size of the value (String or Array) is between
(inclusive) min and max.
- See also
#size?.
Validates that the value is an email. This method is stricter than the standard allows. It is subjectively based on the common addresses of organisations (@enterprise.ltd, ...) and mail services suck as Gmail, Hotmail, Yahoo !, ...
Validates that the value is empty.
- See also
#absence?. - See also
#not_in?.
Validates that the String value ends with search.
- See also
#starts?. - See also
#match?. - See also
#in?.
Validates that the String value ends with search.
- See also
#starts?. - See also
#match?. - See also
#in?.
Validates that the String value ends with search.
- See also
#starts?. - See also
#match?. - See also
#in?.
Validates that the size of the value (String or Array)
is greater than limit number.
Validates that the size of the value (String or Array)
is greater than min number.
Similar to
#min.
Validates that the value is equal to or greater than another_value.
Similar to
#min.
Validates that the String value does denote
a representation of a hexadecimal string.
Validates that the Bytes value does denote
a representation of a hexadecimal slice of Bytes.
Validates that the String value does denote
a representation of a hexadecimal color.
Valid.hex_color? "#fff" => true
Valid.hex_color? "#ffffff" => true
Validates that the (str) String contains the value.
- See also
#presence?. - See also
#not_in?. - See also
#not_empty?.
Validates that the (list) contains the value.
- See also
#presence?. - See also
#not_in?. - See also
#not_empty?.
Validates that the (list) contains the value.
- See also
#presence?. - See also
#not_in?. - See also
#not_empty?.
Validates that the (list) contains the value.
- See also
#presence?. - See also
#not_in?. - See also
#not_empty?.
Validates that the (list) contains the value.
- See also
#presence?. - See also
#not_in?. - See also
#not_empty?.
Validates that the (list) contains the value.
- See also
#presence?. - See also
#not_in?. - See also
#not_empty?.
Validates that the value represents a JSON string.
strict to true (default) to try to parse the JSON,
returns false if the parsing fails.
If strict is false, only the first char and the last char are checked.
Validates that the value is a valid format representation of a geographical latitude.
- See also
#lng?. - See also
#lat_lng?.
Validates that the value is a valid format representation of a geographical position (given in latitude and longitude).
- See also
#lat?. - See also
#lng?.
Validates that the value is a valid format representation of a geographical longitude.
- See also
#lat?. - See also
#lat_lng?.
Validates that the size of the value (String or Array)
is lesser than limit number.
Validates that the size of the value (String or Array)
is equal or lesser than max number.
Similar to
#max.
Validates that the value is equal to or lesser than another_value.
Similar to
#max.
Validates that the value is a MAC address.
Validates that the value is equal to or lesser than max (inclusive).
Similar to
#lte.
Based on the size of the
String.
Validates that the value is equal to or lesser than max (inclusive).
Similar to
#lte.
Validates that the value is equal to or greater than min (inclusive).
Similar to
#gte.
Based on the size of the
String.
Validates that the value is equal to or greater than min (inclusive).
Similar to
#gte.
Validates that the String value does denote
a representation of a MongoID string.
Validates that the Bytes value does denote
a representation of a MongoID slice of Bytes.
Validates that the value is not empty.
- See also
#presence?. - See also
#in?.
Validates that the (str) String does not contain the value.
- See also
#absence?. - See also
#in?. - See also
#empty?.
Validates that the (list) does not contain the value.
- See also
#absence?. - See also
#in?. - See also
#empty?.
Validates that the (list) does not contain the value.
- See also
#absence?. - See also
#in?. - See also
#empty?.
Validates that the (list) does not contain the value.
- See also
#absence?. - See also
#in?. - See also
#empty?.
Validates that the (list) does not contain the value.
- See also
#absence?. - See also
#in?. - See also
#empty?.
Validates that the (list) does not contain the value.
- See also
#absence?. - See also
#in?. - See also
#empty?.
Validates that the value is not null (nil).
- See also
#null?. - See also
#not_empty?.
Validates that the value is null (nil).
- See also
#empty?. - See also
#not_null?.
Validates that the value is in a valid port range, between (inclusive) 1 / min and 65535 / max.
Validates that the value is in a valid port range, between (inclusive) 1 / min and 65535 / max.
Validates the presence of the value.
- See also
#in?. - See also
#not_empty?.
Validates the presence of the value.
- See also
#in?. - See also
#not_empty?.
Validates that the value String is the representation of a refusal.
One of: "no", "n", "off", "0", "false"
- See also
#accepted?.
Returns true if the value is the representation of a refusal.
One of: "no", "n", "off", "0", "false"
value must implements #to_s method.
- See also
#accepted?.
Validates that the value is in the size array.
- See also
#between?.
Validates that the value is in the size array.
- See also
#between?.
Validates that the String value starts with search.
- See also
#ends?. - See also
#match?. - See also
#in?.
Validates that the String value starts with search.
- See also
#ends?. - See also
#match?. - See also
#in?.
Validates that the String value starts with search.
- See also
#ends?. - See also
#match?. - See also
#in?.