This repository has been archived on 2024-06-09. You can view files and clone it, but cannot push or open issues/pull-requests.
2021-03-01 04:59:13 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class ValidationErrorFormatter
|
|
|
|
def initialize(error, aliases = {})
|
|
|
|
@error = error
|
|
|
|
@aliases = aliases
|
|
|
|
end
|
|
|
|
|
|
|
|
def as_json
|
|
|
|
{ error: @error.to_s, details: details }
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def details
|
|
|
|
h = {}
|
|
|
|
|
|
|
|
errors.details.each_pair do |attribute_name, attribute_errors|
|
|
|
|
messages = errors.messages[attribute_name]
|
|
|
|
|
|
|
|
h[@aliases[attribute_name] || attribute_name] = attribute_errors.map.with_index do |error, index|
|
2023-02-22 01:54:36 +01:00
|
|
|
{ error: "ERR_#{error[:error].to_s.upcase}", description: messages[index] }
|
2021-03-01 04:59:13 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
h
|
|
|
|
end
|
|
|
|
|
|
|
|
def errors
|
|
|
|
@errors ||= @error.record.errors
|
|
|
|
end
|
|
|
|
end
|