module Assert

Overview

Annotation based object validation library.

See Assert::Assertions for the full list, and each assertion for more detailed information/examples.

See Assert::Assertions::Assertion for assertion usage documentation.

Example Usage

require "assert"

class User
  include Assert

  def initialize(@name : String, @age : Int32?, @email : String, @password : String); end

  # Assert their name is not blank
  @[Assert::NotBlank]
  property name : String

  # Asserts that their age is >= 0 AND not nil
  @[Assert::NotNil]
  @[Assert::GreaterThanOrEqual(value: 0)]
  property age : Int32?

  # Assert their email is not blank AND is a valid format
  @[Assert::Email(message: "'{{actual}}' is not a proper email")]
  @[Assert::NotBlank]
  property email : String

  # Assert their password is between 7 and 25 characters
  @[Assert::Size(Range(Int32, Int32), range: 7..25)]
  property password : String
end

user = User.new "Jim", 19, "[email protected]", "monkey123"

# #valid? returns `true` if `self` is valid, otherwise `false`
user.valid? # => true

user.email = "foobar"
user.password = "hi"

# #valid? returns `true` if `self` is valid, otherwise `false`
user.valid? # => false

# #validate returns an array of assertions that were not valid
user.validate.empty? # => false

begin
  # #validate! raises an exception if `self` is not valid
  user.validate!
rescue ex : Assert::Exceptions::ValidationError
  ex.to_s    # => Validation tests failed:  'foobar' is not a proper email, 'password' is too short.  It should have 7 character(s) or more
  ex.to_json # => {"code":400,"message":"Validation tests failed","errors":["'foobar' is not a proper email","'password' is too short.  It should have 7 character(s) or more"]}
end

Defined in:

assert.cr
assertion.cr

Instance Method Summary

Instance Method Detail

def valid?(groups : Array(String) = Array(String).new) : Bool #

Returns true if self is valid, otherwise false. Optionally only run assertions a part of the provided groups.


[View source]
def valid?(*groups : String) : Bool #

Returns true if self is valid, otherwise false. Optionally only run assertions a part of the provided groups.


[View source]
def validate(groups : Array(String) = Array(String).new) : Array(Assert::Assertions::Assertion) #

Runs the assertions on self, returning the assertions that are not valid. Optionally only run assertions a part of the provided groups.


[View source]
def validate(*groups : String) : Array(Assert::Assertions::Assertion) #

Runs the assertions on self, returning the assertions that are not valid. Optionally only run assertions a part of the provided groups.


[View source]
def validate!(groups : Array(String) = Array(String).new) : Nil #

Runs the assertions on self, raises an Assert::Exceptions::ValidationError if self is not valid.

Optionally only run assertions a part of the provided groups.


[View source]
def validate!(*groups : String) : Nil #

Runs the assertions on self, raises an Assert::Exceptions::ValidationError if self is not valid.

Optionally only run assertions a part of the provided groups.


[View source]