File tree Expand file tree Collapse file tree 1 file changed +50
-1
lines changed
source/guides/getting_started Expand file tree Collapse file tree 1 file changed +50
-1
lines changed Original file line number Diff line number Diff line change @@ -42,8 +42,57 @@ renderer.render(posts, relationship: :comments
42
42
## Rendering errors
43
43
44
44
```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
+
45
78
renderer = JSONAPI::Serializable::Renderer.new
46
79
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
+
47
96
renderer.render_errors(errors,
48
- class: { Hash: SerializableErrorHash })
97
+ class: { Hash: SerializableErrors })
49
98
```
You can’t perform that action at this time.
0 commit comments