module

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

VERSION = {{ (`shards version \"/tmp/tmp.NALCbL/src/src\"`).chomp.stringify.downcase }}

Class methods

absence?(key : String | Symbol | Number, list : Hash) : Bool

Validates the absence of the value.

  • See also #not_in?.
  • See also #empty?.
Source
absence?(key : String | Symbol, list : NamedTuple) : Bool

Validates the absence of the value.

  • See also #not_in?.
  • See also #empty?.
Source
accepted?(value : String) : Bool

Validates that the value String is the representation of an acceptance.

One of: "yes", "y", "on", "o", "ok", "1", "true"

  • See also #refused?.
Source
accepted?(value) : Bool

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?.
Source
ascii_only?(value : String) : Bool

Validates that the value String is comprised in its entirety by ASCII characters.

Source
ascii_only?(values : Array(String)) : Bool

Validates that all the String in the values Array are comprised in their entirety by ASCII characters.

Source
base64?(value : String) : Bool

Validates that the value has the format base64.

Source
between?(value : String | Array, min : Int, max : Int) : Bool

Validates that the size of the value (String or Array) is between (inclusive) min and max.

  • See also #size?.
Source
between?(value, min, max) : Bool

Validates that the value is between (inclusive) min and max.

Source
domain?(value : String) : Bool
Source
email?(value : String) : Bool

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 !, ...

Source
empty?(value) : Bool

Validates that the value is empty.

  • See also #absence?.
  • See also #not_in?.
Source
ends?(value : String, search : String) : Bool

Validates that the String value ends with search.

  • See also #starts?.
  • See also #match?.
  • See also #in?.
Source
ends?(value : String, search : Char) : Bool

Validates that the String value ends with search.

  • See also #starts?.
  • See also #match?.
  • See also #in?.
Source
ends?(value : String, search : Regex) : Bool

Validates that the String value ends with search.

  • See also #starts?.
  • See also #match?.
  • See also #in?.
Source
eq?(value, another_value) : Bool

Validates that the value is equal to another_value.

Source
gt?(value : String | Array, limit : Int) : Bool

Validates that the size of the value (String or Array) is greater than limit number.

Source
gt?(value, another_value) : Bool

Validates that the value is greater than another_value.

Source
gte?(value : String | Array, min : Int) : Bool

Validates that the size of the value (String or Array) is greater than min number.

Similar to #min.

Source
gte?(value, another_value) : Bool

Validates that the value is equal to or greater than another_value.

Similar to #min.

Source
hex?(value : String) : Bool

Validates that the String value does denote a representation of a hexadecimal string.

Source
hex?(value : Bytes) : Bool

Validates that the Bytes value does denote a representation of a hexadecimal slice of Bytes.

Source
hex_color?(value : String) : Bool

Validates that the String value does denote a representation of a hexadecimal color.

Valid.hex_color? "#fff" => true
Valid.hex_color? "#ffffff" => true
Source
in?(value : String, str : String) : Bool

Validates that the (str) String contains the value.

  • See also #presence?.
  • See also #not_in?.
  • See also #not_empty?.
Source
in?(value, list : Array) : Bool

Validates that the (list) contains the value.

  • See also #presence?.
  • See also #not_in?.
  • See also #not_empty?.
Source
in?(value, list : Tuple) : Bool

Validates that the (list) contains the value.

  • See also #presence?.
  • See also #not_in?.
  • See also #not_empty?.
Source
in?(value, list : Range) : Bool

Validates that the (list) contains the value.

  • See also #presence?.
  • See also #not_in?.
  • See also #not_empty?.
Source
in?(key : Symbol | String, list : NamedTuple) : Bool

Validates that the (list) contains the value.

  • See also #presence?.
  • See also #not_in?.
  • See also #not_empty?.
Source
in?(key : Symbol | String, list : Hash) : Bool

Validates that the (list) contains the value.

  • See also #presence?.
  • See also #not_in?.
  • See also #not_empty?.
Source
ip?(value : String) : Bool

Validates that the value is an IP (IPv4 or IPv6).

Source
ipv4?(value : String) : Bool

Validates that the value is an IPv4.

Source
ipv6?(value : String) : Bool

Validates that the value is an IPv6.

Source
json?(value : String, strict : Bool = true) : Bool

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.

Source
jwt?(value : String) : Bool

Validates that the value is a JSON Web Token.

Source
lat?(value : String | Float) : Bool

Validates that the value is a valid format representation of a geographical latitude.

  • See also #lng?.
  • See also #lat_lng?.
Source
lat_lng?(value : String) : Bool

Validates that the value is a valid format representation of a geographical position (given in latitude and longitude).

  • See also #lat?.
  • See also #lng?.
Source
lng?(value : String | Float) : Bool

Validates that the value is a valid format representation of a geographical longitude.

  • See also #lat?.
  • See also #lat_lng?.
Source
lower?(value : String) : Bool

Validates that the value is in lower case.

Source
lt?(value : String | Array, limit : Int) : Bool

Validates that the size of the value (String or Array) is lesser than limit number.

Source
lt?(value, another_value) : Bool

Validates that the value is lesser than another_value.

Source
lte?(value : String | Array, max : Int) : Bool

Validates that the size of the value (String or Array) is equal or lesser than max number.

Similar to #max.

Source
lte?(value, another_value) : Bool

Validates that the value is equal to or lesser than another_value.

Similar to #max.

Source
mac_addr?(value : String, no_colons : Bool = false) : Bool

Validates that the value is a MAC address.

Source
magnet_uri?(value : String) : Bool

Validates that the value is a Magnet URI.

Source
match?(value : String, pattern : Regex) : Bool

Validates that the value matches the pattern.

Source
match?(value : Number, pattern : Regex) : Bool

Validates that the value matches the pattern.

Source
max?(value : String | Array, max : Int) : Bool

Validates that the value is equal to or lesser than max (inclusive).

Similar to #lte.

Based on the size of the String.

Source
max?(value, max) : Bool

Validates that the value is equal to or lesser than max (inclusive).

Similar to #lte.

Source
md5?(value : String) : Bool

Validates that the value has the format md5.

Source
min?(value : String | Array, min : Int) : Bool

Validates that the value is equal to or greater than min (inclusive).

Similar to #gte.

Based on the size of the String.

Source
min?(value, min) : Bool

Validates that the value is equal to or greater than min (inclusive).

Similar to #gte.

Source
mongo_id?(value : String) : Bool

Validates that the String value does denote a representation of a MongoID string.

Source
mongo_id?(value : Bytes) : Bool

Validates that the Bytes value does denote a representation of a MongoID slice of Bytes.

Source
not_empty?(value) : Bool

Validates that the value is not empty.

  • See also #presence?.
  • See also #in?.
Source
not_in?(value : String, str : String) : Bool

Validates that the (str) String does not contain the value.

  • See also #absence?.
  • See also #in?.
  • See also #empty?.
Source
not_in?(value, list : Array) : Bool

Validates that the (list) does not contain the value.

  • See also #absence?.
  • See also #in?.
  • See also #empty?.
Source
not_in?(value, list : Tuple) : Bool

Validates that the (list) does not contain the value.

  • See also #absence?.
  • See also #in?.
  • See also #empty?.
Source
not_in?(value, list : Range) : Bool

Validates that the (list) does not contain the value.

  • See also #absence?.
  • See also #in?.
  • See also #empty?.
Source
not_in?(key : Symbol | String, list : NamedTuple) : Bool

Validates that the (list) does not contain the value.

  • See also #absence?.
  • See also #in?.
  • See also #empty?.
Source
not_in?(key : Symbol | String, list : Hash) : Bool

Validates that the (list) does not contain the value.

  • See also #absence?.
  • See also #in?.
  • See also #empty?.
Source
not_null?(value) : Bool

Validates that the value is not null (nil).

  • See also #null?.
  • See also #not_empty?.
Source
null?(value) : Bool

Validates that the value is null (nil).

  • See also #empty?.
  • See also #not_null?.
Source
number?(value : String) : Bool

Validates that the value is a numeric String representation.

Source
port?(value : String = "0", min : String = "1", max : String = "65535") : Bool

Validates that the value is in a valid port range, between (inclusive) 1 / min and 65535 / max.

Source
port?(value = 0, min = 1, max = 65535) : Bool

Validates that the value is in a valid port range, between (inclusive) 1 / min and 65535 / max.

Source
presence?(key : String | Symbol | Number, list : Hash) : Bool

Validates the presence of the value.

  • See also #in?.
  • See also #not_empty?.
Source
presence?(key : String | Symbol, list : NamedTuple) : Bool

Validates the presence of the value.

  • See also #in?.
  • See also #not_empty?.
Source
refused?(value : String) : Bool

Validates that the value String is the representation of a refusal.

One of: "no", "n", "off", "0", "false"

  • See also #accepted?.
Source
refused?(value) : Bool

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?.
Source
size?(value, size : Int)

Validates that the value is equal to the size.

Source
size?(value, size : String)

Validates that the value is equal to the size.

Source
size?(value, size : Range)

Validates that the value is in the size range.

  • See also #between?.
Source
size?(value, size : Array(Int))

Validates that the value is in the size array.

  • See also #between?.
Source
size?(value, size : Array(String))

Validates that the value is in the size array.

  • See also #between?.
Source
slug?(value : String, min = 1, max = 100) : Bool

Validates that the value is a slug.

Source
starts?(value : String, search : String) : Bool

Validates that the String value starts with search.

  • See also #ends?.
  • See also #match?.
  • See also #in?.
Source
starts?(value : String, search : Char) : Bool

Validates that the String value starts with search.

  • See also #ends?.
  • See also #match?.
  • See also #in?.
Source
starts?(value : String, search : Regex) : Bool

Validates that the String value starts with search.

  • See also #ends?.
  • See also #match?.
  • See also #in?.
Source
time?(value : String) : Bool

Validates that the value is a time String representation.

Source
upper?(value : String) : Bool

Validates that the value is in upper case.

Source
url?(value : String) : Bool

Validates that the value is a URL.

Source
uuid?(value : String, version = 0) : Bool

Validates that the value is a UUID Versions: 0 (all), 3, 4 and 5. version by default is 0 (all).

Source

Nested types