Exception: Grape::Exceptions::ValidationErrors
- Includes:
- Enumerable
- Defined in:
- lib/grape/exceptions/validation_errors.rb
Constant Summary
Constants inherited from Base
Instance Attribute Summary collapse
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
Attributes inherited from Base
Instance Method Summary collapse
- #as_json(**_opts) ⇒ Object
- #each ⇒ Object
- #full_messages ⇒ Object
-
#initialize(errors: [], headers: {}) ⇒ ValidationErrors
constructor
A new instance of ValidationErrors.
- #to_json(*_opts) ⇒ Object
Methods inherited from Base
Constructor Details
#initialize(errors: [], headers: {}) ⇒ ValidationErrors
Returns a new instance of ValidationErrors.
10 11 12 13 |
# File 'lib/grape/exceptions/validation_errors.rb', line 10 def initialize(errors: [], headers: {}) @errors = errors.group_by(&:params) super(message: .join(', '), status: 400, headers:) end |
Instance Attribute Details
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
8 9 10 |
# File 'lib/grape/exceptions/validation_errors.rb', line 8 def errors @errors end |
Instance Method Details
#as_json(**_opts) ⇒ Object
23 24 25 26 27 28 29 30 |
# File 'lib/grape/exceptions/validation_errors.rb', line 23 def as_json(**_opts) errors.map do |k, v| { params: k, messages: v.map(&:to_s) } end end |
#each ⇒ Object
15 16 17 18 19 20 21 |
# File 'lib/grape/exceptions/validation_errors.rb', line 15 def each errors.each_pair do |attribute, errors| errors.each do |error| yield attribute, error end end end |
#full_messages ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/grape/exceptions/validation_errors.rb', line 36 def = map do |attributes, error| translate( :format, scope: 'grape.errors', default: '%<attributes>s %<message>s', attributes: translate_attributes(attributes), message: error. ) end .uniq! end |
#to_json(*_opts) ⇒ Object
32 33 34 |
# File 'lib/grape/exceptions/validation_errors.rb', line 32 def to_json(*_opts) as_json.to_json end |