Exception: Common::Exceptions::DetailedSchemaErrors
- Inherits:
-
BaseError
- Object
- StandardError
- BaseError
- Common::Exceptions::DetailedSchemaErrors
show all
- Defined in:
- lib/common/exceptions/detailed_schema_errors.rb
Instance Method Summary
collapse
-
#array_items(error) ⇒ Object
(also: #minitems, #maxitems, #uniqueitems)
private
-
#const(error) ⇒ Object
private
-
#data_type(_error) ⇒ Object
(also: #boolean, #integer, #number, #string, #object, #array)
private
-
#enum(error) ⇒ Object
private
-
#errors ⇒ Object
-
#format(error) ⇒ Object
private
-
#i18n_interpolated(path, options = {}) ⇒ Object
private
-
#initialize(raw_schema_errors) ⇒ DetailedSchemaErrors
constructor
Expects array of JSONSchemer errors.
-
#length(error) ⇒ Object
(also: #maxlength, #minlength)
private
-
#pattern(error) ⇒ Object
private
-
#range(error) ⇒ Object
(also: #maximum, #minimum)
private
-
#required(error) ⇒ Object
private
-
#schema(_error) ⇒ Object
private
Methods inherited from BaseError
#i18n_data, #i18n_field, #i18n_key, #log_to_sentry?, #message, #sentry_type, #status_code
Constructor Details
Expects array of JSONSchemer errors
10
11
12
13
|
# File 'lib/common/exceptions/detailed_schema_errors.rb', line 10
def initialize(raw_schema_errors)
@raw_errors = raw_schema_errors
raise TypeError, 'the resource provided has no errors' if raw_schema_errors.blank?
end
|
Instance Method Details
#array_items(error) ⇒ Object
Also known as:
minitems, maxitems, uniqueitems
97
98
99
100
101
102
103
104
105
106
|
# File 'lib/common/exceptions/detailed_schema_errors.rb', line 97
def array_items(error)
data = i18n_interpolated :array_items, { detail: { size: error['data'].size } }
data[:meta] ||= {}
data[:meta][:received_size] = error['data'].size
data[:meta][:received_unique_items] = error['data'].size == error['data'].uniq.size
data[:meta].merge! max_items: error['schema']['maxItems'] if error['schema']['maxItems']
data[:meta].merge! min_items: error['schema']['minItems'] if error['schema']['minItems']
data[:meta].merge! unique_items: error['schema']['uniqueItems'] if error['schema']['uniqueItems']
data
end
|
#const(error) ⇒ Object
53
54
55
56
57
58
|
# File 'lib/common/exceptions/detailed_schema_errors.rb', line 53
def const(error)
data = i18n_interpolated :const, detail: { value: error['data'] }
data[:meta] ||= {}
data.merge! meta: { required_value: error.dig('schema', 'const') }
data
end
|
#data_type(_error) ⇒ Object
Also known as:
boolean, integer, number, string, object, array
35
36
37
|
# File 'lib/common/exceptions/detailed_schema_errors.rb', line 35
def data_type(_error)
i18n_interpolated :data_type, detail: { data_type: __callee__ }
end
|
#enum(error) ⇒ Object
45
46
47
48
49
50
51
|
# File 'lib/common/exceptions/detailed_schema_errors.rb', line 45
def enum(error)
opts = error.dig 'schema', 'enum'
data = i18n_interpolated :enum, detail: { value: error['data'], enum: opts }
data[:meta] ||= {}
data.merge! meta: { available_options: opts }
data
end
|
#errors ⇒ Object
15
16
17
18
19
20
21
22
23
24
25
26
|
# File 'lib/common/exceptions/detailed_schema_errors.rb', line 15
def errors
@errors ||= @raw_errors.map do |raw_error|
type = raw_error['type'].downcase
pointer = raw_error['data_pointer'].presence || '/'
error = if respond_to?(type, true)
send type, raw_error
else
I18n.t('common.exceptions.validation_errors')
end
SerializableError.new error.merge source: { pointer: }
end
end
|
111
112
113
114
115
116
117
|
# File 'lib/common/exceptions/detailed_schema_errors.rb', line 111
def format(error)
format = error.dig 'schema', 'format'
data = i18n_interpolated :format, detail: { value: error['data'] }
data[:meta] ||= {}
data.merge! meta: { format: }
data
end
|
#i18n_interpolated(path, options = {}) ⇒ Object
30
31
32
33
|
# File 'lib/common/exceptions/detailed_schema_errors.rb', line 30
def i18n_interpolated(path, options = {})
merge_values = options.map { |attr, opts| [attr, i18n_field("#{path}.#{attr}", opts)] }.to_h
i18n_data[path].merge(merge_values)
end
|
#length(error) ⇒ Object
Also known as:
maxlength, minlength
60
61
62
63
64
65
66
|
# File 'lib/common/exceptions/detailed_schema_errors.rb', line 60
def length(error)
data = i18n_interpolated :length, detail: { value: error['data'] }
data[:meta] ||= {}
data[:meta].merge! max_length: error['schema']['maxLength'] if error['schema']['maxLength']
data[:meta].merge! min_length: error['schema']['minLength'] if error['schema']['minLength']
data
end
|
#pattern(error) ⇒ Object
80
81
82
83
84
85
|
# File 'lib/common/exceptions/detailed_schema_errors.rb', line 80
def pattern(error)
regex = error.dig 'schema', 'pattern'
data = i18n_interpolated :pattern, detail: { value: error['data'], regex: }
data.merge! meta: { regex: }
data
end
|
#range(error) ⇒ Object
Also known as:
maximum, minimum
70
71
72
73
74
75
76
|
# File 'lib/common/exceptions/detailed_schema_errors.rb', line 70
def range(error)
data = i18n_interpolated :range, detail: { value: error['data'] }
data[:meta] ||= {}
data[:meta].merge! maximum: error['schema']['maximum'] if error['schema']['maximum']
data[:meta].merge! minimum: error['schema']['minimum'] if error['schema']['minimum']
data
end
|
#required(error) ⇒ Object
87
88
89
90
91
|
# File 'lib/common/exceptions/detailed_schema_errors.rb', line 87
def required(error)
data = i18n_interpolated :required
data.merge! meta: { missing_fields: error['details']['missing_keys'] }
data
end
|
#schema(_error) ⇒ Object
93
94
95
|
# File 'lib/common/exceptions/detailed_schema_errors.rb', line 93
def schema(_error)
i18n_interpolated :schema
end
|