module Validator

Overview

∠(・.-)―〉 →◎ 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:

By default the validator module expose only Validator and Valid (alias) in the scope:

require "validator"

Valid.email? "[email protected]"                        # => 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?, "[email protected]"                        # => 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"]?)

Defined in:

validator.cr
validators/alpha_num.cr
validators/case_sensitive.cr
validators/comparisons.cr
validators/format.cr
validators/geo.cr
validators/presence.cr
validators/uri.cr

Constant Summary

VERSION = {{ (`shards version \"/srv/crystaldoc.info/github-Nicolab-crystal-validator-v1.11.0/src\"`).chomp.stringify.downcase }}

Class Method Summary

Class Method Detail

def self.absence?(key : String | Symbol | Number, list : Hash) : Bool #

Validates the absence of the value.


[View source]
def self.absence?(key : String | Symbol, list : NamedTuple) : Bool #

Validates the absence of the value.


[View source]
def self.accepted?(value : String) : Bool #

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

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


[View source]
def self.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.


[View source]
def self.ascii_only?(value : String) : Bool #

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


[View source]
def self.ascii_only?(values : Array(String)) : Bool #

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


[View source]
def self.base64?(value : String) : Bool #

Validates that the value has the format base64.


[View source]
def self.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.


[View source]
def self.between?(value, min, max) : Bool #

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


[View source]
def self.domain?(value : String) : Bool #

[View source]
def self.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 !, ...


[View source]
def self.empty?(value) : Bool #

Validates that the value is empty.


[View source]
def self.ends?(value : String, search : String) : Bool #

Validates that the String value ends with search.


[View source]
def self.ends?(value : String, search : Char) : Bool #

Validates that the String value ends with search.


[View source]
def self.ends?(value : String, search : Regex) : Bool #

Validates that the String value ends with search.


[View source]
def self.eq?(value, another_value) : Bool #

Validates that the value is equal to another_value.


[View source]
def self.gt?(value : String | Array, limit : Int) : Bool #

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


[View source]
def self.gt?(value, another_value) : Bool #

Validates that the value is greater than another_value.


[View source]
def self.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.


[View source]
def self.gte?(value, another_value) : Bool #

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

Similar to #min.


[View source]
def self.hex?(value : String) : Bool #

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


[View source]
def self.hex?(value : Bytes) : Bool #

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


[View source]
def self.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

[View source]
def self.in?(value : String, str : String) : Bool #

Validates that the (str) String contains the value.


[View source]
def self.in?(value, list : Array) : Bool #

Validates that the (list) contains the value.


[View source]
def self.in?(value, list : Tuple) : Bool #

Validates that the (list) contains the value.


[View source]
def self.in?(value, list : Range) : Bool #

Validates that the (list) contains the value.


[View source]
def self.in?(key : Symbol | String, list : NamedTuple) : Bool #

Validates that the (list) contains the value.


[View source]
def self.in?(key : Symbol | String, list : Hash) : Bool #

Validates that the (list) contains the value.


[View source]
def self.ip?(value : String) : Bool #

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


[View source]
def self.ipv4?(value : String) : Bool #

Validates that the value is an IPv4.


[View source]
def self.ipv6?(value : String) : Bool #

Validates that the value is an IPv6.


[View source]
def self.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.


[View source]
def self.jwt?(value : String) : Bool #

Validates that the value is a JSON Web Token.


[View source]
def self.lat?(value : String | Float) : Bool #

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


[View source]
def self.lat_lng?(value : String) : Bool #

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


[View source]
def self.lng?(value : String | Float) : Bool #

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


[View source]
def self.lower?(value : String) : Bool #

Validates that the value is in lower case.


[View source]
def self.lt?(value : String | Array, limit : Int) : Bool #

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


[View source]
def self.lt?(value, another_value) : Bool #

Validates that the value is lesser than another_value.


[View source]
def self.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.


[View source]
def self.lte?(value, another_value) : Bool #

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

Similar to #max.


[View source]
def self.mac_addr?(value : String, no_colons : Bool = false) : Bool #

Validates that the value is a MAC address.


[View source]
def self.magnet_uri?(value : String) : Bool #

Validates that the value is a Magnet URI.


[View source]
def self.match?(value : String, pattern : Regex) : Bool #

Validates that the value matches the pattern.


[View source]
def self.match?(value : Number, pattern : Regex) : Bool #

Validates that the value matches the pattern.


[View source]
def self.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.


[View source]
def self.max?(value, max) : Bool #

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

Similar to #lte.


[View source]
def self.md5?(value : String) : Bool #

Validates that the value has the format md5.


[View source]
def self.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.


[View source]
def self.min?(value, min) : Bool #

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

Similar to #gte.


[View source]
def self.mongo_id?(value : String) : Bool #

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


[View source]
def self.mongo_id?(value : Bytes) : Bool #

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


[View source]
def self.not_empty?(value) : Bool #

Validates that the value is not empty.


[View source]
def self.not_in?(value : String, str : String) : Bool #

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


[View source]
def self.not_in?(value, list : Array) : Bool #

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


[View source]
def self.not_in?(value, list : Tuple) : Bool #

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


[View source]
def self.not_in?(value, list : Range) : Bool #

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


[View source]
def self.not_in?(key : Symbol | String, list : NamedTuple) : Bool #

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


[View source]
def self.not_in?(key : Symbol | String, list : Hash) : Bool #

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


[View source]
def self.not_null?(value) : Bool #

Validates that the value is not null (nil).


[View source]
def self.null?(value) : Bool #

Validates that the value is null (nil).


[View source]
def self.number?(value : String) : Bool #

Validates that the value is a numeric String representation.


[View source]
def self.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.


[View source]
def self.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.


[View source]
def self.presence?(key : String | Symbol | Number, list : Hash) : Bool #

Validates the presence of the value.


[View source]
def self.presence?(key : String | Symbol, list : NamedTuple) : Bool #

Validates the presence of the value.


[View source]
def self.refused?(value : String) : Bool #

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

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


[View source]
def self.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.


[View source]
def self.size?(value, size : Int) #

Validates that the value is equal to the size.


[View source]
def self.size?(value, size : String) #

Validates that the value is equal to the size.


[View source]
def self.size?(value, size : Range) #

Validates that the value is in the size range.


[View source]
def self.size?(value, size : Array(Int)) #

Validates that the value is in the size array.


[View source]
def self.size?(value, size : Array(String)) #

Validates that the value is in the size array.


[View source]
def self.slug?(value : String, min = 1, max = 100) : Bool #

Validates that the value is a slug.


[View source]
def self.starts?(value : String, search : String) : Bool #

Validates that the String value starts with search.


[View source]
def self.starts?(value : String, search : Char) : Bool #

Validates that the String value starts with search.


[View source]
def self.starts?(value : String, search : Regex) : Bool #

Validates that the String value starts with search.


[View source]
def self.time?(value : String) : Bool #

Validates that the value is a time String representation.


[View source]
def self.upper?(value : String) : Bool #

Validates that the value is in upper case.


[View source]
def self.url?(value : String) : Bool #

Validates that the value is a URL.


[View source]
def self.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).


[View source]