class Assert::Exceptions::ValidationError

Overview

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

Defined in:

exceptions/validation_error.cr

Constructors

Instance Method Summary

Constructor Detail

def self.new(failed_assertion : Assert::Assertions::Assertion) #

[View source]
def self.new(failed_assertions : Array(Assert::Assertions::Assertion)) #

[View source]

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'",
  ],
}

[View source]
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'"

[View source]