generated from athena-framework/component-template
/
constraint_violation.cr
98 lines (78 loc) · 2.32 KB
/
constraint_violation.cr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
require "./constraint_violation_interface"
# Basic implementation of `AVD::Violation::ConstraintViolationInterface`.
struct Athena::Validator::Violation::ConstraintViolation(Root)
include Athena::Validator::Violation::ConstraintViolationInterface
protected getter invalid_value_container : AVD::Container
# :inherit:
getter cause : String?
# :inherit:
getter code : String?
# :inherit:
getter! constraint : AVD::Constraint
# :inherit:
getter message : String
# :inherit:
getter message_template : String?
# :inherit:
getter parameters : Hash(String, String)
# :inherit:
getter plural : Int32?
# :inherit:
getter property_path : String
# :inherit:
getter root : Root
def initialize(
@message : String,
@message_template : String?,
@parameters : Hash(String, String),
@root : Root,
@property_path : String,
@invalid_value_container : AVD::Container,
@plural : Int32? = nil,
@code : String? = nil,
@constraint : AVD::Constraint? = nil,
@cause : String? = nil
)
end
# :inherit:
def invalid_value
@invalid_value_container.value
end
# :inherit:
def to_json(builder : JSON::Builder) : Nil
builder.object do
builder.field "property", @property_path
builder.field "message", @message
if (code = @code)
builder.field "code", code
end
end
end
# :inherit:
def to_s(io : IO) : Nil
klass = case @root
when Hash then "Hash"
when AVD::Validatable then "Object(#{@root.class})"
else
@root.to_s
end
klass += '.' if !@property_path.blank? && !@property_path.starts_with?('[') && !klass.blank?
if (c = code) && !c.blank?
code = " (code: #{c})"
end
io.puts "#{klass}#{@property_path}:\n\t#{@message}#{code}"
end
# Returns `true` if *other* is the same as `self`, otherwise `false`.
def ==(other : self) : Bool
@message == other.message &&
@message_template == other.message_template &&
@parameters == other.parameters &&
@root == other.root &&
@property_path == other.property_path &&
@invalid_value_container == other.invalid_value_container &&
@plural == other.plural &&
@code == other.code &&
@constraint == other.constraint? &&
@cause == other.cause
end
end