class

Assert::Exceptions::ValidationError

Inherits Exception / Reference / Object

Represents a validation error. It can be raised manually or via Assert#validate!.

Constructors

new(failed_assertion : Assert::Assertions::Assertion)
Source
new(failed_assertions : Array(Assert::Assertions::Assertion))
Source

Instance methods

to_json(builder : JSON::Builder) : Nil

Returns a JSON/pretty JSON object for self.

Can be overwritten to change the JSON schema.

error = Assert::Exceptions::ValidationError.new([
  Assert::Assertions::NotBlank(String?).new("name", ""),
  Assert::Assertions::GreaterThanOrEqual(Int32).new("age", -1, 0),
])

error.to_pretty_json # =>
{
  "code":    400,
  "message": "Validation tests failed",
  "errors":  [
    "'name' should not be blank",
    "'age' should be greater than or equal to '0'",
  ],
}
Source
to_s

Returns failed validations as a string.

error = Assert::Exceptions::ValidationError.new([
  Assert::Assertions::NotBlank(String?).new("name", ""),
  Assert::Assertions::GreaterThanOrEqual(Int32).new("age", -1, 0),
])

error.to_s # => "Validation tests failed: 'name' should not be blank, 'age' should be greater than or equal to '0'"
Source