You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

32 lines
651 B

  1. # frozen_string_literal: true
  2. class ValidationErrorFormatter
  3. def initialize(error, aliases = {})
  4. @error = error
  5. @aliases = aliases
  6. end
  7. def as_json
  8. { error: @error.to_s, details: details }
  9. end
  10. private
  11. def details
  12. h = {}
  13. errors.details.each_pair do |attribute_name, attribute_errors|
  14. messages = errors.messages[attribute_name]
  15. h[@aliases[attribute_name] || attribute_name] = attribute_errors.map.with_index do |error, index|
  16. { error: 'ERR_' + error[:error].to_s.upcase, description: messages[index] }
  17. end
  18. end
  19. h
  20. end
  21. def errors
  22. @errors ||= @error.record.errors
  23. end
  24. end