File tree Expand file tree Collapse file tree
source/guides/getting_started Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -42,8 +42,57 @@ renderer.render(posts, relationship: :comments
4242## Rendering errors
4343
4444```ruby
45+ class SerializableError < JSONAPI::Serializable::Error
46+ title do
47+ "Invalid #{@field}" unless @field.nil?
48+ end
49+
50+ detail do
51+ @message
52+ end
53+
54+ source do
55+ pointer "/data/attributes/#{@field}" unless @field.nil?
56+ end
57+ end
58+
59+ class SerializableErrors
60+ def initialize(exposures)
61+ @errors = exposures[:object]
62+ freeze
63+ end
64+
65+ def as_jsonapi
66+ @errors.keys.flat_map do |key|
67+ @errors[key].map do |message|
68+ SerializableError.new(
69+ field: key,
70+ message: message
71+ ).as_jsonapi
72+ end
73+ end
74+ end
75+ end
76+
77+
4578renderer = JSONAPI::Serializable::Renderer.new
4679
80+ # errors input signature
81+ # errors = [
82+ # {
83+ # title: [
84+ # "must be filled",
85+ # "size cannot be greater than 100"
86+ # ]
87+ # },
88+ # {
89+ # body: [
90+ # "must be filled",
91+ # "size cannot be less than 3"
92+ # ]
93+ # }
94+ # ]
95+
4796renderer.render_errors(errors,
48- class: { Hash: SerializableErrorHash })
97+ class: { Hash: SerializableErrors })
4998```
You can’t perform that action at this time.
0 commit comments