stronger_parameters 2.11.1 → 2.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +21 -0
- data/lib/stronger_parameters/constraint.rb +30 -0
- data/lib/stronger_parameters/constraints/array_constraint.rb +1 -0
- data/lib/stronger_parameters/constraints/date_time_constraint.rb +0 -2
- data/lib/stronger_parameters/constraints/date_time_iso8601_constraint.rb +0 -2
- data/lib/stronger_parameters/constraints/regexp_constraint.rb +1 -0
- data/lib/stronger_parameters/parameters.rb +13 -2
- data/lib/stronger_parameters/version.rb +1 -1
- metadata +8 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: eb8d8fbb1ffc6da37f90b976befa11fc1bd1b6efc95c66ac60cb05258531edd8
|
4
|
+
data.tar.gz: 336da945ec7950b2ca422527d7c00347bc9ac12ce45aa4197a2f8daf15645a3d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2f14ec3db2cf2b83ff60152057909d236a921ab83d5dd6bf2d7a6d32d95dec905c2502b3ca1055094d7c254a62702a0f4cd94bf7b7b40268658a8fb52a80a740
|
7
|
+
data.tar.gz: 3b83496ec4aed8fe614c7e06dc4e9b8babb5ed96c86462ca28c58a8fc80a1e6bd492119e78b6efa6827c9c5e0f5372c5fc9db022ce160ba869806926788e3458
|
data/README.md
CHANGED
@@ -32,6 +32,27 @@ Rails converts empty arrays to nil, so often `Parameters.array | Parameters.nil`
|
|
32
32
|
It can be convenient to allow nil for all attributes since ActiveRecord converts it to false/0.
|
33
33
|
`ActionController::Parameters.allow_nil_for_everything = true`
|
34
34
|
|
35
|
+
### Rejecting nils
|
36
|
+
|
37
|
+
You can reject a request that fails to supply certain parameters by marking them
|
38
|
+
as required with the `.required` operator:
|
39
|
+
|
40
|
+
```ruby
|
41
|
+
params.permit(
|
42
|
+
name: Parameters.string.required, # will not accept a nil or a non-String
|
43
|
+
email: Parameters.string # optional, may be omitted
|
44
|
+
)
|
45
|
+
```
|
46
|
+
|
47
|
+
This also works in conjunction with the `&` and `|` constraints. For example, to
|
48
|
+
express that a `uid` must be either a string or a number:
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
params.permit(
|
52
|
+
uid: (Parameters.string | Parameters.integer).required
|
53
|
+
)
|
54
|
+
```
|
55
|
+
|
35
56
|
## Nested Parameters
|
36
57
|
|
37
58
|
```ruby
|
@@ -18,6 +18,14 @@ module StrongerParameters
|
|
18
18
|
def ==(other)
|
19
19
|
self.class == other.class
|
20
20
|
end
|
21
|
+
|
22
|
+
def required
|
23
|
+
RequiredConstraint.new(self)
|
24
|
+
end
|
25
|
+
|
26
|
+
def required?
|
27
|
+
false
|
28
|
+
end
|
21
29
|
end
|
22
30
|
|
23
31
|
class OrConstraint < Constraint
|
@@ -50,6 +58,10 @@ module StrongerParameters
|
|
50
58
|
def ==(other)
|
51
59
|
super && constraints == other.constraints
|
52
60
|
end
|
61
|
+
|
62
|
+
def required?
|
63
|
+
constraints.all?(&:required?)
|
64
|
+
end
|
53
65
|
end
|
54
66
|
|
55
67
|
class AndConstraint < Constraint
|
@@ -75,5 +87,23 @@ module StrongerParameters
|
|
75
87
|
def ==(other)
|
76
88
|
super && constraints == other.constraints
|
77
89
|
end
|
90
|
+
|
91
|
+
def required?
|
92
|
+
constraints.any?(&:required?)
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
class RequiredConstraint < Constraint
|
97
|
+
def initialize(other)
|
98
|
+
@other = other
|
99
|
+
end
|
100
|
+
|
101
|
+
def value(v)
|
102
|
+
@other.value(v)
|
103
|
+
end
|
104
|
+
|
105
|
+
def required?
|
106
|
+
true
|
107
|
+
end
|
78
108
|
end
|
79
109
|
end
|
@@ -143,14 +143,25 @@ module StrongerParameters
|
|
143
143
|
|
144
144
|
hash_filter_without_stronger_parameters(params, other_filter)
|
145
145
|
|
146
|
-
|
146
|
+
stronger_filter.keys.each do |key|
|
147
|
+
value = fetch(key, nil)
|
148
|
+
result = nil
|
149
|
+
|
147
150
|
if value.nil? && self.class.allow_nil_for_everything
|
148
151
|
params[key] = nil
|
149
152
|
next
|
150
153
|
end
|
151
154
|
|
152
155
|
constraint = stronger_filter[key]
|
153
|
-
|
156
|
+
|
157
|
+
if key?(key)
|
158
|
+
result = constraint.value(value)
|
159
|
+
elsif constraint.required?
|
160
|
+
result = InvalidValue.new(nil, 'must be present')
|
161
|
+
else
|
162
|
+
next
|
163
|
+
end
|
164
|
+
|
154
165
|
if result.is_a?(InvalidValue)
|
155
166
|
name = "invalid_parameter.action_controller"
|
156
167
|
ActiveSupport::Notifications.publish(name, key: key, value: value, message: result.message)
|
metadata
CHANGED
@@ -1,29 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: stronger_parameters
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.12.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mick Staugaard
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-06-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
17
|
+
- - "<="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '3'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - "
|
24
|
+
- - "<="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
26
|
+
version: '3'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -227,15 +227,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
227
227
|
requirements:
|
228
228
|
- - ">="
|
229
229
|
- !ruby/object:Gem::Version
|
230
|
-
version: 2.
|
230
|
+
version: 2.4.0
|
231
231
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
232
232
|
requirements:
|
233
233
|
- - ">="
|
234
234
|
- !ruby/object:Gem::Version
|
235
235
|
version: '0'
|
236
236
|
requirements: []
|
237
|
-
|
238
|
-
rubygems_version: 2.7.6
|
237
|
+
rubygems_version: 3.0.3
|
239
238
|
signing_key:
|
240
239
|
specification_version: 4
|
241
240
|
summary: Type checking and type casting of parameters for Action Pack
|