/
error_set.cr
74 lines (64 loc) · 2.52 KB
/
error_set.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
module Marten
module Core
module Validation
# Represents a set of validation errors.
class ErrorSet
include Indexable(Error)
DEFAULT_ERROR_TYPE = :invalid
def initialize
@errors = [] of Error
end
# Returns all the errors associated with a specific field.
def [](field : Nil | String | Symbol)
@errors.select { |e| e.field == field.try(&.to_s) }
end
# Adds a new global error to the error set.
#
# This method can be used to add a new global error (that is not associated with a specific field) to an
# existing error set. At least a `message` must be specified:
#
# ```
# record.errors.add("This record is invalid!")
# ```
#
# Optionnaly, an error `type` can be explicitely specified, otherwise it defaults to `:invalid`:
#
# ```
# record.errors.add("This record is invalid!", type: :invalid_record)
# ```
def add(message : String, *, type : Nil | String | Symbol = nil)
@errors << Error.new(type: type || DEFAULT_ERROR_TYPE, field: nil, message: message)
end
# Adds a new field error to the error set.
#
# This method can be used to add a new error associated with a specific field to an existing error set. At least
# a `field` and a `message` must be specified:
#
# ```
# record.errors.add(:attribute, "This record attribute is invalid")
# ```
#
# Optionnaly, an error `type` can be explicitely specified, otherwise it defaults to `:invalid`:
#
# ```
# records.errors.add(:attribute, "This record attribute is invalid!", type: :invalid_attribute)
# ```
def add(field : String | Symbol, message : String, *, type : Nil | String | Symbol = nil)
@errors << Error.new(type: type || DEFAULT_ERROR_TYPE, field: field, message: message)
end
# Returns the global errors (errors that are not associated with a specific field).
def global
self[nil]
end
# Iterates over all the `Marten::Core::Validation::Error` objects in this error set.
delegate each, to: @errors
# Clears all the `Marten::Core::Validation::Error` objects from this error set.
delegate clear, to: @errors
# Returns the number of errors.
delegate size, to: @errors
# :nodoc:
delegate unsafe_fetch, to: @errors
end
end
end
end