Module: DataModel::Errors
- Extended by:
- Errors
- Included in:
- Builtin::Array, Builtin::BigDecimal, Builtin::Boolean, Builtin::Date, Builtin::Float, Builtin::Hash, Builtin::Integer, Builtin::Numeric, Builtin::Object, Builtin::Or, Builtin::String, Builtin::Symbol, Builtin::Time, Error, Errors
- Defined in:
- lib/data_model/errors.rb
Overview
Provide Error building functionality as a mixin
Instance Method Summary collapse
-
#blank_error ⇒ Array(Symbol, untyped)
Blank error applies when a value is blank.
-
#blank_error_message ⇒ String
Generate a message for a blank error.
-
#coerce_error(cls, value) ⇒ Array(Symbol, untyped)
Coerce error applies when a value cannot be coerced to the expected type.
-
#coerce_error_message(cls, value) ⇒ String
Generate a message for a coerce error.
-
#earliest_error(earliest, val) ⇒ Array(Symbol, untyped)
Earliest applies when value is earlier then earliest.
-
#early_error_message(earliest, val) ⇒ String
Generate a message for a value that occurs earlier then the specified earliest point.
-
#error_messages ⇒ Hash{Symbol => Proc}
Get the error message builders.
-
#exclusion_error(set) ⇒ Array(Symbol, untyped)
Exclusive error applies when a value is in a set of disallowed values.
-
#exclusion_error_message(set) ⇒ String
Generate a message for an exclusion error.
-
#extra_keys_error(keys) ⇒ Array(Symbol, untyped)
Extra keys error applies when a hash has extra keys.
-
#extra_keys_error_message(keys) ⇒ String
Generate a message for an extra keys error.
-
#format_error(format, val) ⇒ Array(Symbol, untyped)
Format applies when value does not match a format.
-
#format_error_message(format, val) ⇒ String
Generate a message for a value that does not match the format.
-
#inclusion_error(set) ⇒ Array(Symbol, untyped)
Inclusion error applies when a value is not in a set of allowed values.
-
#inclusion_error_message(set) ⇒ String
Generate a message for an inclusion error.
-
#late_error_message(latest, val) ⇒ String
Generate a message for a value that occurs later then the specified latest point.
-
#latest_error(latest, val) ⇒ Array(Symbol, untyped)
Latest applies when value is earlier then earliest.
-
#max_error(min, val) ⇒ Array(Symbol, untyped)
Max applies when value is less then the minimum.
-
#max_error_message(max, val) ⇒ String
Generate a message for a min error.
-
#min_error(min, val) ⇒ Array(Symbol, untyped)
Min applies when value is less then the minimum.
-
#min_error_message(min, val) ⇒ String
Generate a message for a min error.
-
#missing_error(cls) ⇒ Array(Symbol, untyped)
Missing error applies when a value is missing.
-
#missing_error_message(cls) ⇒ String
Generate a message for a missing error.
-
#type_error(cls, value) ⇒ Array(Symbol, untyped)
Type error applies when a value is not of the expected type.
-
#type_error_message(cls, value) ⇒ String
Generate a message for a type error.
Instance Method Details
#blank_error ⇒ Array(Symbol, untyped)
Blank error applies when a value is blank
47 48 49 |
# File 'lib/data_model/errors.rb', line 47 def blank_error [:blank, nil] end |
#blank_error_message ⇒ String
Generate a message for a blank error
142 143 144 |
# File 'lib/data_model/errors.rb', line 142 def "cannot be blank" end |
#coerce_error(cls, value) ⇒ Array(Symbol, untyped)
Coerce error applies when a value cannot be coerced to the expected type
20 21 22 |
# File 'lib/data_model/errors.rb', line 20 def coerce_error(cls, value) [:coerce, [cls, value]] end |
#coerce_error_message(cls, value) ⇒ String
Generate a message for a coerce error
113 114 115 116 |
# File 'lib/data_model/errors.rb', line 113 def (cls, value) names = Array(cls).join(" or ") "cannot be coerced to #{names}, it is a #{value.class.name}" end |
#earliest_error(earliest, val) ⇒ Array(Symbol, untyped)
Earliest applies when value is earlier then earliest
78 79 80 |
# File 'lib/data_model/errors.rb', line 78 def earliest_error(earliest, val) [:earliest, [earliest, val]] end |
#early_error_message(earliest, val) ⇒ String
Generate a message for a value that occurs earlier then the specified earliest point
173 174 175 |
# File 'lib/data_model/errors.rb', line 173 def (earliest, val) "value #{val} is before #{earliest}" end |
#error_messages ⇒ Hash{Symbol => Proc}
Get the error message builders
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/data_model/errors.rb', line 197 def return { type: lambda do |ctx| cls, val = ctx (cls, val) end, coerce: lambda do |ctx| cls, val = ctx (cls, val) end, missing: lambda do |ctx| cls = ctx (cls) end, inclusion: lambda do |ctx| set = ctx (set) end, exclusion: lambda do |ctx| set = ctx (set) end, extra_keys: lambda do |ctx| set = ctx (set) end, min: lambda do |ctx| min, val = ctx (min, val) end, max: lambda do |ctx| max, val = ctx (max, val) end, earliest: lambda do |ctx| earliest, val = ctx (earliest, val) end, latest: lambda do |ctx| latest, val = ctx (latest, val) end, blank: lambda do end, format: lambda do |ctx| format, val = ctx (format, val) end } end |
#exclusion_error(set) ⇒ Array(Symbol, untyped)
Exclusive error applies when a value is in a set of disallowed values
41 42 43 |
# File 'lib/data_model/errors.rb', line 41 def exclusion_error(set) [:exclusion, set] end |
#exclusion_error_message(set) ⇒ String
Generate a message for an exclusion error
136 137 138 |
# File 'lib/data_model/errors.rb', line 136 def (set) "must not be one of #{set.join(', ')}" end |
#extra_keys_error(keys) ⇒ Array(Symbol, untyped)
Extra keys error applies when a hash has extra keys
54 55 56 |
# File 'lib/data_model/errors.rb', line 54 def extra_keys_error(keys) [:extra_keys, keys] end |
#extra_keys_error_message(keys) ⇒ String
Generate a message for an extra keys error
149 150 151 |
# File 'lib/data_model/errors.rb', line 149 def (keys) "more elements found in closed hash then specified children: #{keys.join(', ')}" end |
#format_error(format, val) ⇒ Array(Symbol, untyped)
Format applies when value does not match a format
94 95 96 |
# File 'lib/data_model/errors.rb', line 94 def format_error(format, val) [:format, [format, val]] end |
#format_error_message(format, val) ⇒ String
Generate a message for a value that does not match the format
189 190 191 |
# File 'lib/data_model/errors.rb', line 189 def (format, val) "value #{val} does not match format #{format}" end |
#inclusion_error(set) ⇒ Array(Symbol, untyped)
Inclusion error applies when a value is not in a set of allowed values
34 35 36 |
# File 'lib/data_model/errors.rb', line 34 def inclusion_error(set) [:inclusion, set] end |
#inclusion_error_message(set) ⇒ String
Generate a message for an inclusion error
129 130 131 |
# File 'lib/data_model/errors.rb', line 129 def (set) "must be one of #{set.join(', ')}" end |
#late_error_message(latest, val) ⇒ String
Generate a message for a value that occurs later then the specified latest point
181 182 183 |
# File 'lib/data_model/errors.rb', line 181 def (latest, val) "value #{val} is after #{latest}" end |
#latest_error(latest, val) ⇒ Array(Symbol, untyped)
Latest applies when value is earlier then earliest
86 87 88 |
# File 'lib/data_model/errors.rb', line 86 def latest_error(latest, val) [:latest, [latest, val]] end |
#max_error(min, val) ⇒ Array(Symbol, untyped)
Max applies when value is less then the minimum
70 71 72 |
# File 'lib/data_model/errors.rb', line 70 def max_error(min, val) [:max, [min, val]] end |
#max_error_message(max, val) ⇒ String
Generate a message for a min error
165 166 167 |
# File 'lib/data_model/errors.rb', line 165 def (max, val) "value is more than the maximum of #{max}, it is #{val}" end |
#min_error(min, val) ⇒ Array(Symbol, untyped)
Min applies when value is less then the minimum
62 63 64 |
# File 'lib/data_model/errors.rb', line 62 def min_error(min, val) [:min, [min, val]] end |
#min_error_message(min, val) ⇒ String
Generate a message for a min error
157 158 159 |
# File 'lib/data_model/errors.rb', line 157 def (min, val) "value is less than the minimum of #{min}, it is #{val}" end |
#missing_error(cls) ⇒ Array(Symbol, untyped)
Missing error applies when a value is missing
27 28 29 |
# File 'lib/data_model/errors.rb', line 27 def missing_error(cls) [:missing, cls] end |
#missing_error_message(cls) ⇒ String
Generate a message for a missing error
121 122 123 124 |
# File 'lib/data_model/errors.rb', line 121 def (cls) names = Array(cls).join(" or ") "missing value, expected a #{names}" end |
#type_error(cls, value) ⇒ Array(Symbol, untyped)
Type error applies when a value is not of the expected type
12 13 14 |
# File 'lib/data_model/errors.rb', line 12 def type_error(cls, value) [:type, [cls, value]] end |
#type_error_message(cls, value) ⇒ String
Generate a message for a type error
104 105 106 107 |
# File 'lib/data_model/errors.rb', line 104 def (cls, value) names = Array(cls).join(" or ") "#{value.inspect} is not a #{names}, it is a #{value.class.name}" end |