typed_params 1.2.0 → 1.2.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +8 -0
- data/README.md +6 -4
- data/lib/typed_params/formatters/jsonapi.rb +16 -12
- data/lib/typed_params/schema.rb +3 -4
- data/lib/typed_params/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 160a6e63cc497232a6c33d2cdc06050682c44e71624bee8d70f72e25b3ef3374
|
4
|
+
data.tar.gz: f0e1c30200d4ac4fc555cd5418546ed3ceba69dc54b09a98a02e4b349dcfd014
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 773babee3c280766879e615a654206f1665a3811ac98fe673dcab655ef6e42471335f9cc2605e999eed2aa06a3aa5e6a85ed74fcd4e138b39f97467fc28adef0
|
7
|
+
data.tar.gz: 0ab8544aa6011e99256af88dafe207b27509f78b7db906cc41f47192967fc5b24f7fe8d8b352729908083026804ec39f5aec314b27747c2ea222443e092e4276
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,13 @@
|
|
1
1
|
# Changelog
|
2
2
|
|
3
|
+
## 1.2.2
|
4
|
+
|
5
|
+
- Fix issue where `as:` keyword was not handled in JSONAPI relationship formatter.
|
6
|
+
|
7
|
+
## 1.2.1
|
8
|
+
|
9
|
+
- Update JSONAPI formatter to simplify logic for polymorphic relationships.
|
10
|
+
|
3
11
|
## 1.2.0
|
4
12
|
|
5
13
|
- Add `aliases:` keyword to `#param` to allow the schema to be accessible by different names.
|
data/README.md
CHANGED
@@ -689,8 +689,10 @@ param :invalid, type: :string, validate: -> v {
|
|
689
689
|
|
690
690
|
#### Polymorphic parameter
|
691
691
|
|
692
|
-
|
693
|
-
|
692
|
+
_Note: currently, this option is only utilized by the JSONAPI formatter._
|
693
|
+
|
694
|
+
Define a polymorphic parameter. Actual behavior will vary based on the
|
695
|
+
formatter being used.
|
694
696
|
|
695
697
|
```ruby
|
696
698
|
format :jsonapi
|
@@ -711,8 +713,8 @@ typed_params # => { owner_type: 'User', owner_id: 1 }
|
|
711
713
|
|
712
714
|
In this example, a polymorphic `:owner` relationship is defined. When run
|
713
715
|
through the JSONAPI formatter, instead of formatting the relationship
|
714
|
-
into the `:owner_id` key, it also includes the `:owner_type`
|
715
|
-
for a polymorphic association.
|
716
|
+
into solely the `:owner_id` key, it also includes the `:owner_type`
|
717
|
+
key for a polymorphic association.
|
716
718
|
|
717
719
|
### Shared options
|
718
720
|
|
@@ -58,9 +58,10 @@ module TypedParams
|
|
58
58
|
end
|
59
59
|
|
60
60
|
def self.format_hash_data(data, schema:)
|
61
|
-
|
62
|
-
|
63
|
-
|
61
|
+
relationships = data[:relationships]
|
62
|
+
attributes = data[:attributes]
|
63
|
+
|
64
|
+
res = data.except(
|
64
65
|
:attributes,
|
65
66
|
:links,
|
66
67
|
:meta,
|
@@ -69,25 +70,28 @@ module TypedParams
|
|
69
70
|
)
|
70
71
|
|
71
72
|
# Move attributes over to top-level params
|
72
|
-
|
73
|
-
res[key] =
|
73
|
+
attributes&.each do |key, attribute|
|
74
|
+
res[key] = attribute
|
74
75
|
end
|
75
76
|
|
76
77
|
# Move relationships over. This will use x_id and x_ids when the
|
77
78
|
# relationship data only contains :type and :id, otherwise it
|
78
79
|
# will use the x_attributes key.
|
79
|
-
|
80
|
-
child = schema.children.
|
81
|
-
|
82
|
-
|
80
|
+
relationships&.each do |key, relationship|
|
81
|
+
child = schema.children.fetch(:relationships).then do |rels|
|
82
|
+
rels.children.fetch(key) {
|
83
|
+
rels.children.values.find { _1.as == key || _1.alias == key }
|
84
|
+
}
|
85
|
+
end
|
86
|
+
|
87
|
+
case relationship
|
83
88
|
# FIXME(ezekg) We need https://bugs.ruby-lang.org/issues/18961 to
|
84
89
|
# clean this up (i.e. remove the if guard).
|
85
90
|
in data: [{ type:, id:, **nil }, *] => linkage if linkage.all? { _1 in type:, id:, **nil }
|
86
91
|
res[:"#{key.to_s.singularize}_ids"] = linkage.map { _1[:id] }
|
87
92
|
in data: []
|
88
93
|
res[:"#{key.to_s.singularize}_ids"] = []
|
89
|
-
|
90
|
-
in data: { type:, id:, **nil } if key.to_s.underscore.classify != type.underscore.classify && child.polymorphic?
|
94
|
+
in data: { type:, id:, **nil } if child.polymorphic?
|
91
95
|
res[:"#{key}_type"], res[:"#{key}_id"] = type.underscore.classify, id
|
92
96
|
in data: { type:, id:, **nil }
|
93
97
|
res[:"#{key}_id"] = id
|
@@ -96,7 +100,7 @@ module TypedParams
|
|
96
100
|
else
|
97
101
|
# NOTE(ezekg) Embedded relationships are non-standard as per the
|
98
102
|
# JSONAPI spec, but I don't really care. :)
|
99
|
-
res[:"#{key}_attributes"] = call(
|
103
|
+
res[:"#{key}_attributes"] = call(relationship, schema: child)
|
100
104
|
end
|
101
105
|
end
|
102
106
|
|
data/lib/typed_params/schema.rb
CHANGED
@@ -10,6 +10,7 @@ module TypedParams
|
|
10
10
|
:source,
|
11
11
|
:type,
|
12
12
|
:key,
|
13
|
+
:as,
|
13
14
|
:alias,
|
14
15
|
:if,
|
15
16
|
:unless
|
@@ -80,6 +81,7 @@ module TypedParams
|
|
80
81
|
@strict = strict
|
81
82
|
@parent = parent
|
82
83
|
@key = key
|
84
|
+
@as = as
|
83
85
|
@alias = binding.local_variable_get(:alias)
|
84
86
|
@optional = optional
|
85
87
|
@coerce = coerce && @type.coercable?
|
@@ -272,7 +274,7 @@ module TypedParams
|
|
272
274
|
def allow_nil? = !!@allow_nil
|
273
275
|
def allow_non_scalars? = !!@allow_non_scalars
|
274
276
|
def nilify_blanks? = !!@nilify_blanks
|
275
|
-
def endless?
|
277
|
+
def endless? = !!@endless
|
276
278
|
def indexed? = !endless?
|
277
279
|
def if? = !@if.nil?
|
278
280
|
def unless? = !@unless.nil?
|
@@ -285,9 +287,6 @@ module TypedParams
|
|
285
287
|
"#<#{self.class.name} key=#{key.inspect} type=#{type.inspect} children=#{children.inspect}>"
|
286
288
|
end
|
287
289
|
|
288
|
-
# @private
|
289
|
-
def dig(*keys) = children.dig(*keys)
|
290
|
-
|
291
290
|
private
|
292
291
|
|
293
292
|
attr_reader :controller,
|
data/lib/typed_params/version.rb
CHANGED