stronger_parameters 2.11.1 → 2.12.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 46343e25829800aec85852ba31fbbf040dce9b6071ff53acb04475e524f64cef
4
- data.tar.gz: c4fd41f9cfef6bcfa16d350ca41e00981f583d92697b081afa121e12114a8454
3
+ metadata.gz: eb8d8fbb1ffc6da37f90b976befa11fc1bd1b6efc95c66ac60cb05258531edd8
4
+ data.tar.gz: 336da945ec7950b2ca422527d7c00347bc9ac12ce45aa4197a2f8daf15645a3d
5
5
  SHA512:
6
- metadata.gz: 5d51dcdbfbaf4a60af24536aedce0f0be8a2ba8da5f8d7cf9b517bc25113645623302a630688bd8d20f82cf1a34a42a8fabd1d9a75982451324bbb83a81a2bd7
7
- data.tar.gz: 7332fac5079c83ac64df9e08b3a6b1abaceba349c99cb4b0c6936089d7928f39f0181c1c444316f8b6577b3c6b267d3e68c60a5a6385680beaebda09d34544ee
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
@@ -14,6 +14,7 @@ module StrongerParameters
14
14
  return v.map do |item|
15
15
  result = item_constraint.value(item)
16
16
  return result if result.is_a?(InvalidValue)
17
+
17
18
  result
18
19
  end
19
20
  end
@@ -1,5 +1,4 @@
1
1
  # frozen_string_literal: true
2
- # rubocop:disable Style/DateTime
3
2
  require 'stronger_parameters/constraint'
4
3
 
5
4
  module StrongerParameters
@@ -15,4 +14,3 @@ module StrongerParameters
15
14
  end
16
15
  end
17
16
  end
18
- # rubocop:enable Style/DateTime
@@ -1,5 +1,4 @@
1
1
  # frozen_string_literal: true
2
- # rubocop:disable Style/DateTime
3
2
  require 'stronger_parameters/constraint'
4
3
 
5
4
  module StrongerParameters
@@ -11,4 +10,3 @@ module StrongerParameters
11
10
  end
12
11
  end
13
12
  end
14
- # rubocop:enable Style/DateTime
@@ -13,6 +13,7 @@ module StrongerParameters
13
13
  def value(v)
14
14
  v = @string.value(v)
15
15
  return v if v.is_a?(InvalidValue)
16
+
16
17
  if v =~ regexp
17
18
  v
18
19
  else
@@ -143,14 +143,25 @@ module StrongerParameters
143
143
 
144
144
  hash_filter_without_stronger_parameters(params, other_filter)
145
145
 
146
- slice(*stronger_filter.keys).each do |key, value|
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
- result = constraint.value(value)
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)
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module StrongerParameters
3
- VERSION = '2.11.1'
3
+ VERSION = '2.12.0'
4
4
  end
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.11.1
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-05-29 00:00:00.000000000 Z
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: '1.3'
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: '1.3'
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.2.0
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
- rubyforge_project:
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