Assert
Annotation based object validation library.
See the Assert::Assertions namespace for the full assertion list as well as each assertion class for more detailed information/examples.
See Assert::Assertions::Assertion for common/high level assertion usage documentation.
Example Usage
Assert supports both object based validations via annotations as well as ad hoc value validations via class methods.
Object Validation
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, "test@email.com", "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
Ad Hoc Validation
# Each assertion automatically defines a shortcut class method for ad hoc validations.
Assert.not_blank "foo" # => true
Assert.not_blank "" # => false
begin
# The bang version will raise if the value is invalid.
Assert.not_blank! " "
rescue ex
ex.to_s # => Validation tests failed: 'actual' should not be blank
end
begin
# Optional arguments can be used just like the annotation versions.
Assert.equal_to! 15, 20, message: "%{actual} does not equal %{value}"
rescue ex
ex.to_s # => Validation tests failed: 15 does not equal 20
end
Class methods
Assert::Assertions::Choice assertion shortcut method.
Can be used for ad hoc validations when applying annotations is not possible.
Assert::Assertions::Choice assertion shortcut method.
Can be used for ad hoc validations when applying annotations is not possible.
Raises an Assert::Exceptions::ValidationError if the value is not valid.
Assert::Assertions::DivisibleBy assertion shortcut method.
Can be used for ad hoc validations when applying annotations is not possible.
Assert::Assertions::DivisibleBy assertion shortcut method.
Can be used for ad hoc validations when applying annotations is not possible.
Raises an Assert::Exceptions::ValidationError if the value is not valid.
Assert::Assertions::Email assertion shortcut method.
Can be used for ad hoc validations when applying annotations is not possible.
Assert::Assertions::Email assertion shortcut method.
Can be used for ad hoc validations when applying annotations is not possible.
Raises an Assert::Exceptions::ValidationError if the value is not valid.
Assert::Assertions::EqualTo assertion shortcut method.
Can be used for ad hoc validations when applying annotations is not possible.
Assert::Assertions::EqualTo assertion shortcut method.
Can be used for ad hoc validations when applying annotations is not possible.
Raises an Assert::Exceptions::ValidationError if the value is not valid.
Assert::Assertions::GreaterThan assertion shortcut method.
Can be used for ad hoc validations when applying annotations is not possible.
Assert::Assertions::GreaterThan assertion shortcut method.
Can be used for ad hoc validations when applying annotations is not possible.
Raises an Assert::Exceptions::ValidationError if the value is not valid.
Assert::Assertions::GreaterThanOrEqual assertion shortcut method.
Can be used for ad hoc validations when applying annotations is not possible.
Assert::Assertions::GreaterThanOrEqual assertion shortcut method.
Can be used for ad hoc validations when applying annotations is not possible.
Raises an Assert::Exceptions::ValidationError if the value is not valid.
Assert::Assertions::InRange assertion shortcut method.
Can be used for ad hoc validations when applying annotations is not possible.
Assert::Assertions::InRange assertion shortcut method.
Can be used for ad hoc validations when applying annotations is not possible.
Raises an Assert::Exceptions::ValidationError if the value is not valid.
Assert::Assertions::Ip assertion shortcut method.
Can be used for ad hoc validations when applying annotations is not possible.
Assert::Assertions::Ip assertion shortcut method.
Can be used for ad hoc validations when applying annotations is not possible.
Raises an Assert::Exceptions::ValidationError if the value is not valid.
Assert::Assertions::IsBlank assertion shortcut method.
Can be used for ad hoc validations when applying annotations is not possible.
Assert::Assertions::IsBlank assertion shortcut method.
Can be used for ad hoc validations when applying annotations is not possible.
Raises an Assert::Exceptions::ValidationError if the value is not valid.
Assert::Assertions::IsFalse assertion shortcut method.
Can be used for ad hoc validations when applying annotations is not possible.
Assert::Assertions::IsFalse assertion shortcut method.
Can be used for ad hoc validations when applying annotations is not possible.
Raises an Assert::Exceptions::ValidationError if the value is not valid.
Assert::Assertions::IsNil assertion shortcut method.
Can be used for ad hoc validations when applying annotations is not possible.
Assert::Assertions::IsNil assertion shortcut method.
Can be used for ad hoc validations when applying annotations is not possible.
Raises an Assert::Exceptions::ValidationError if the value is not valid.
Assert::Assertions::IsTrue assertion shortcut method.
Can be used for ad hoc validations when applying annotations is not possible.
Assert::Assertions::IsTrue assertion shortcut method.
Can be used for ad hoc validations when applying annotations is not possible.
Raises an Assert::Exceptions::ValidationError if the value is not valid.
Assert::Assertions::LessThan assertion shortcut method.
Can be used for ad hoc validations when applying annotations is not possible.
Assert::Assertions::LessThan assertion shortcut method.
Can be used for ad hoc validations when applying annotations is not possible.
Raises an Assert::Exceptions::ValidationError if the value is not valid.
Assert::Assertions::LessThanOrEqual assertion shortcut method.
Can be used for ad hoc validations when applying annotations is not possible.
Assert::Assertions::LessThanOrEqual assertion shortcut method.
Can be used for ad hoc validations when applying annotations is not possible.
Raises an Assert::Exceptions::ValidationError if the value is not valid.
Assert::Assertions::NotBlank assertion shortcut method.
Can be used for ad hoc validations when applying annotations is not possible.
Assert::Assertions::NotBlank assertion shortcut method.
Can be used for ad hoc validations when applying annotations is not possible.
Raises an Assert::Exceptions::ValidationError if the value is not valid.
Assert::Assertions::NotEqualTo assertion shortcut method.
Can be used for ad hoc validations when applying annotations is not possible.
Assert::Assertions::NotEqualTo assertion shortcut method.
Can be used for ad hoc validations when applying annotations is not possible.
Raises an Assert::Exceptions::ValidationError if the value is not valid.
Assert::Assertions::NotNil assertion shortcut method.
Can be used for ad hoc validations when applying annotations is not possible.
Assert::Assertions::NotNil assertion shortcut method.
Can be used for ad hoc validations when applying annotations is not possible.
Raises an Assert::Exceptions::ValidationError if the value is not valid.
Assert::Assertions::RegexMatch assertion shortcut method.
Can be used for ad hoc validations when applying annotations is not possible.
Assert::Assertions::RegexMatch assertion shortcut method.
Can be used for ad hoc validations when applying annotations is not possible.
Raises an Assert::Exceptions::ValidationError if the value is not valid.
Assert::Assertions::Size assertion shortcut method.
Can be used for ad hoc validations when applying annotations is not possible.
Assert::Assertions::Size assertion shortcut method.
Can be used for ad hoc validations when applying annotations is not possible.
Raises an Assert::Exceptions::ValidationError if the value is not valid.
Assert::Assertions::Url assertion shortcut method.
Can be used for ad hoc validations when applying annotations is not possible.
Assert::Assertions::Url assertion shortcut method.
Can be used for ad hoc validations when applying annotations is not possible.
Raises an Assert::Exceptions::ValidationError if the value is not valid.
Assert::Assertions::Uuid assertion shortcut method.
Can be used for ad hoc validations when applying annotations is not possible.
Assert::Assertions::Uuid assertion shortcut method.
Can be used for ad hoc validations when applying annotations is not possible.
Raises an Assert::Exceptions::ValidationError if the value is not valid.
Assert::Assertions::Valid assertion shortcut method.
Can be used for ad hoc validations when applying annotations is not possible.
Assert::Assertions::Valid assertion shortcut method.
Can be used for ad hoc validations when applying annotations is not possible.
Raises an Assert::Exceptions::ValidationError if the value is not valid.
Instance methods
Returns true if self is valid, otherwise false.
Optionally only run assertions a part of the provided groups.
Returns true if self is valid, otherwise false.
Optionally only run assertions a part of the provided groups.
Runs the assertions on self, returning the assertions that are not valid.
Optionally only run assertions a part of the provided groups.
Runs the assertions on self, returning the assertions that are not valid.
Optionally only run assertions a part of the provided groups.
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.
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.