class Assert::Exceptions::ValidationError
- Assert::Exceptions::ValidationError
- Exception
- Reference
- Object
Overview
Represents a validation error. It can be raised manually or via Assert#validate!
.
Defined in:
exceptions/validation_error.crConstructors
- .new(failed_assertion : Assert::Assertions::Assertion)
- .new(failed_assertions : Array(Assert::Assertions::Assertion))
Instance Method Summary
-
#to_json(builder : JSON::Builder) : Nil
Returns a JSON/pretty JSON object for
self
. -
#to_s : String
Returns failed validations as a string.
Constructor Detail
Instance Method Detail
def 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'",
],
}
def to_s : String
#
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'"