Exception: TSJSON::ObjectValidationError
- Inherits:
-
ValidationError
- Object
- StandardError
- ValidationError
- TSJSON::ObjectValidationError
- Defined in:
- lib/errors/object_validation_error.rb
Instance Method Summary collapse
-
#initialize(errors:) ⇒ ObjectValidationError
constructor
A new instance of ObjectValidationError.
- #to_human_json ⇒ Object
- #to_json ⇒ Object
Constructor Details
#initialize(errors:) ⇒ ObjectValidationError
Returns a new instance of ObjectValidationError.
3 4 5 |
# File 'lib/errors/object_validation_error.rb', line 3 def initialize(errors:) super end |
Instance Method Details
#to_human_json ⇒ Object
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 |
# File 'lib/errors/object_validation_error.rb', line 19 def to_human_json required_fields = [] unexpected_fields = [] other_errors = [] @details[:errors].each do |err| if (err[:error].is_a?(RequiredFieldError)) required_fields.push(err[:field]) elsif (err[:error].is_a?(UnexpectedFieldError)) unexpected_fields.push(err[:field]) else err_human_json = err[:error].to_human_json other_errors.push( { message: "#{err[:field]}: #{err_human_json[:message]}", details: err_human_json[:details] }.compact ) end end details = [] if required_fields.length > 0 details.push( { message: "Required fields: #{required_fields.join(', ')}" } ) end if unexpected_fields.length > 0 details.push( { message: "Unexpected fields: #{unexpected_fields.join(', ')}" } ) end details.concat(other_errors) { message: 'Invalid object', details: details } end |
#to_json ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/errors/object_validation_error.rb', line 7 def to_json { code: self.class.name.split('::').last, details: { errors: @details[:errors].map do |e| { field: e[:field], error: e[:error].to_json } end } } end |