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? "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"]?)

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 \"/media/data/lab/dev/work/projects/nicolab/crystal/crystal-validator/src\"`).chomp.stringify.downcase }}

Class Method Summary

Class Method Detail

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

Validates the absence of the value.


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

Validates the absence of the value.


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"

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.


def self.ascii_only?(value : String) : Bool #

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


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.


def self.base64?(value : String) : Bool #

Validates that the value has the format base64.


def self.between?(value, min, max) : Bool #

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


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.


def self.domain?(value : String) : Bool #

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


def self.empty?(value) : Bool #

Validates that the value is empty.


def self.ends?(value : String, search : String) : Bool #

Validates that the String value ends with search.


def self.ends?(value : String, search : Char) : Bool #

Validates that the String value ends with search.


def self.ends?(value : String, search : Regex) : Bool #

Validates that the String value ends with search.


def self.eq?(value, another_value) : Bool #

Validates that the value is equal to another_value.


def self.gt?(value, another_value) : Bool #

Validates that the value is greater than another_value.


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

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


def self.gte?(value, another_value) : Bool #

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

Similar to #min.

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.

def self.hex?(value : String) : Bool #

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


def self.hex?(value : Bytes) : Bool #

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


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

def self.in?(value, list : Array) : Bool #

Validates that the (list) contains the value.


def self.in?(value : String, str : String) : Bool #

Validates that the (str) String contains the value.


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

Validates that the (list) contains the value.


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

Validates that the (list) contains the value.


def self.in?(value, list : Range) : Bool #

Validates that the (list) contains the value.


def self.in?(value, list : Tuple) : Bool #

Validates that the (list) contains the value.


def self.ip?(value : String) : Bool #

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


def self.ipv4?(value : String) : Bool #

Validates that the value is an IPv4.


def self.ipv6?(value : String) : Bool #

Validates that the value is an IPv6.


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.


def self.jwt?(value : String) : Bool #

Validates that the value is a JSON Web Token.


def self.lat?(value : String | Float) : Bool #

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


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


def self.lng?(value : String | Float) : Bool #

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


def self.lower?(value : String) : Bool #

Validates that the value is in lower case.


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

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


def self.lt?(value, another_value) : Bool #

Validates that the value is lesser than another_value.


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.

def self.lte?(value, another_value) : Bool #

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

Similar to #max.

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

Validates that the value is a MAC address.


def self.magnet_uri?(value : String) : Bool #

Validates that the value is a Magnet URI.


def self.match?(value : Number, pattern : Regex) : Bool #

Validates that the value matches the pattern.


def self.match?(value : String, pattern : Regex) : Bool #

Validates that the value matches the pattern.


def self.max?(value, max) : Bool #

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

Similar to #lte.

def self.max?(value : String | Array, max : Int) : Bool #

:ditto:

Based on the size of the String.

def self.md5?(value : String) : Bool #

Validates that the value has the format md5.


def self.min?(value, min) : Bool #

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

Similar to #gte.

def self.min?(value : String | Array, min : Int) : Bool #

:ditto:

Based on the size of the String.

def self.mongo_id?(value : Bytes) : Bool #

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


def self.mongo_id?(value : String) : Bool #

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


def self.not_empty?(value) : Bool #

Validates that the value is not empty.


def self.not_in?(value : String, str : String) : Bool #

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


def self.not_in?(value, list : Array) : Bool #

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


def self.not_in?(value, list : Tuple) : Bool #

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


def self.not_in?(value, list : Range) : Bool #

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


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

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


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

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


def self.not_null?(value) : Bool #

Validates that the value is not null (nil).


def self.null?(value) : Bool #

Validates that the value is null (nil).


def self.number?(value : String) : Bool #

Validates that the value is a numeric String representation.


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.


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.


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

Validates the presence of the value.


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

Validates the presence of the value.


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.


def self.refused?(value : String) : Bool #

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

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

def self.size?(value, size : Array(String)) #

Validates that the value is in the size array.


def self.size?(value, size : Array(Int)) #

Validates that the value is in the size array.


def self.size?(value, size : Range) #

Validates that the value is in the size range.


def self.size?(value, size : String) #

Validates that the value is equal to the size.


def self.size?(value, size : Int) #

Validates that the value is equal to the size.


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

Validates that the value is a slug.


def self.starts?(value : String, search : Char) : Bool #

Validates that the String value starts with search.


def self.starts?(value : String, search : Regex) : Bool #

Validates that the String value starts with search.


def self.starts?(value : String, search : String) : Bool #

Validates that the String value starts with search.


def self.time?(value : String) : Bool #

Validates that the value is a time String representation.


def self.upper?(value : String) : Bool #

Validates that the value is in upper case.


def self.url?(value : String) : Bool #

Validates that the value is a URL.


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