stronger_parameters 2.16.0 → 2.19.1

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: ce9ec4b63f79317aa849950dcb6431c4fd0c3a5515abc3eb88aaa170f5680355
4
- data.tar.gz: 85cc4373897928267c81214cdf1ca1207a705ee93f32cc647be65c02c0dcbb56
3
+ metadata.gz: 6b498cb065e3370fff5def0735395b8b396565f62e76364612f7ab101ecc3c63
4
+ data.tar.gz: 7edd672e06906c24efc86aff49783659df49e390918693dcf1c2dab174dfb8b2
5
5
  SHA512:
6
- metadata.gz: 3f4bd6274fdf4a44431fbb1cf87e93ba2393c2d0e78c65d3fe6fe4afdde77f1abd6a7ff24e87216cb622ed43795f1900a10f825915f1bcb04f62ef5bd62eca2d
7
- data.tar.gz: 97d87e0b66db0f5c125b136385b379f72c4af95e17599c1a3f270ea0268af2f27d3532831b83f84b233492131962f9782ef35513963d1dde53ad73ad89e7e818
6
+ metadata.gz: 1d17d12e4b23862fd3dd7d6663a660d26324f29c392e7af6577c4792d0df53c01c20f45a8a99c6b8a57870c43650a580d9f57796528624450e8b59e7482d4cb1
7
+ data.tar.gz: aa06279656cb6ebc52e133e1f7a0d61fe439b0bf88b9e6e6521898cec61986c6799f1b0ce63d70f0a1edb7f3eb7005fc19103606df44fc6f765c17e73bb3bdd9
data/README.md CHANGED
@@ -240,7 +240,6 @@ curl -I 'http://localhost/api/users/1.json' -X POST -d '{ "user": { "id": 1 } }'
240
240
  => X-StrongerParameters-API-Warn: Removed restricted keys ["user.id"] from parameters
241
241
  ```
242
242
 
243
-
244
243
  ## Types
245
244
 
246
245
  | Syntax | (Simplified) Definition |
@@ -248,6 +247,10 @@ curl -I 'http://localhost/api/users/1.json' -X POST -d '{ "user": { "id": 1 } }'
248
247
  | Parameters.string | value.is_a?(String) |
249
248
  | Parameters.integer | value.is_a?(Fixnum) or '-1' |
250
249
  | Parameters.float | value.is_a?(Float) or '-1.2' |
250
+ | Parameters.date | value.is_a?(Date) or '2014-05-13' or '13.05.2014' |
251
+ | Parameters.date_iso8601 | value is a date that conforms to ISO8601: '2014-05-13' |
252
+ | Parameters.time | value.is_a?(Time) or '2014-05-13' or '2015-03-31 14:34:56 +0000' |
253
+ | Parameters.time_iso8601 | value is a time that conforms to ISO8601: '2014-05-13' or '2015-03-31T14:34:56Z' |
251
254
  | Parameters.datetime | value.is_a?(DateTime) or '2014-05-13' or '2015-03-31T14:34:56Z' |
252
255
  | Parameters.datetime_iso8601 | value is a date that conforms to ISO8601: '2014-05-13' or '2015-03-31T14:34:56Z' |
253
256
  | Parameters.regexp(/foo/) | value =~ regexp |
@@ -268,3 +271,16 @@ curl -I 'http://localhost/api/users/1.json' -X POST -d '{ "user": { "id": 1 } }'
268
271
  | Parameters.file | File, StringIO, Rack::Test::UploadedFile, ActionDispatch::Http::UploadedFile or subclasses |
269
272
  | Parameters.decimal(8,2) | value is a String, Integer or Float with a precision of 9 and scale of 2 |
270
273
  | Parameters.hex | value is a String that matches the hexadecimal format |
274
+
275
+ ## Development
276
+
277
+ ### Releasing a new version
278
+
279
+ ```
280
+ git checkout master && git fetch origin && git reset --hard origin/master
281
+ bundle exec rake bump:<patch|minor|major>
282
+ git tag v<tag>
283
+ git push --tags
284
+ ```
285
+
286
+ [github action](.github/workflows/ruby-gem-publication.yml) will release a new version to rubygems.org
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+ require 'stronger_parameters/constraint'
3
+
4
+ module StrongerParameters
5
+ class DateConstraint < Constraint
6
+ def value(v)
7
+ return v if v.is_a?(Date)
8
+
9
+ begin
10
+ Date.parse v
11
+ rescue ArgumentError, TypeError
12
+ StrongerParameters::InvalidValue.new(v, "must be a date")
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+ require 'stronger_parameters/constraint'
3
+
4
+ module StrongerParameters
5
+ class DateIso8601Constraint < Constraint
6
+ def value(v)
7
+ Date.iso8601 v
8
+ rescue ArgumentError, TypeError
9
+ StrongerParameters::InvalidValue.new(v, "must be an iso8601 date")
10
+ end
11
+ end
12
+ end
@@ -9,7 +9,7 @@ module StrongerParameters
9
9
  begin
10
10
  DateTime.parse v
11
11
  rescue ArgumentError, TypeError
12
- StrongerParameters::InvalidValue.new(v, "must be a date")
12
+ StrongerParameters::InvalidValue.new(v, "must be a datetime")
13
13
  end
14
14
  end
15
15
  end
@@ -6,7 +6,7 @@ module StrongerParameters
6
6
  def value(v)
7
7
  DateTime.iso8601 v
8
8
  rescue ArgumentError, TypeError
9
- StrongerParameters::InvalidValue.new(v, "must be an iso8601 date")
9
+ StrongerParameters::InvalidValue.new(v, "must be an iso8601 datetime")
10
10
  end
11
11
  end
12
12
  end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+ require 'stronger_parameters/constraint'
3
+
4
+ module StrongerParameters
5
+ class TimeConstraint < Constraint
6
+ def value(v)
7
+ return v if v.is_a?(Time)
8
+
9
+ begin
10
+ Time.parse v
11
+ rescue ArgumentError, TypeError
12
+ StrongerParameters::InvalidValue.new(v, "must be a time")
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+ require 'stronger_parameters/constraint'
3
+
4
+ module StrongerParameters
5
+ class TimeIso8601Constraint < Constraint
6
+ def value(v)
7
+ Time.iso8601 v
8
+ rescue ArgumentError, TypeError
9
+ StrongerParameters::InvalidValue.new(v, "must be an iso8601 time")
10
+ end
11
+ end
12
+ end
@@ -2,6 +2,10 @@
2
2
  require 'stronger_parameters/constraint'
3
3
  require 'stronger_parameters/constraints/string_constraint'
4
4
  require 'stronger_parameters/constraints/float_constraint'
5
+ require 'stronger_parameters/constraints/date_constraint'
6
+ require 'stronger_parameters/constraints/date_iso8601_constraint'
7
+ require 'stronger_parameters/constraints/time_constraint'
8
+ require 'stronger_parameters/constraints/time_iso8601_constraint'
5
9
  require 'stronger_parameters/constraints/date_time_constraint'
6
10
  require 'stronger_parameters/constraints/date_time_iso8601_constraint'
7
11
  require 'stronger_parameters/constraints/regexp_constraint'
@@ -2,6 +2,7 @@
2
2
  require 'action_pack'
3
3
 
4
4
  require 'action_controller/base'
5
+ require 'action_controller/api'
5
6
  require 'action_controller/metal/strong_parameters'
6
7
 
7
8
  require 'stronger_parameters/constraints'
@@ -104,6 +105,22 @@ module StrongerParameters
104
105
  NilStringConstraint.new
105
106
  end
106
107
 
108
+ def date
109
+ DateConstraint.new
110
+ end
111
+
112
+ def date_iso8601
113
+ DateIso8601Constraint.new
114
+ end
115
+
116
+ def time
117
+ TimeConstraint.new
118
+ end
119
+
120
+ def time_iso8601
121
+ TimeIso8601Constraint.new
122
+ end
123
+
107
124
  def datetime
108
125
  DateTimeConstraint.new
109
126
  end
@@ -159,7 +176,7 @@ module StrongerParameters
159
176
 
160
177
  if result.is_a?(InvalidValue)
161
178
  name = "invalid_parameter.action_controller"
162
- ActiveSupport::Notifications.publish(name, key: key, value: value, message: result.message)
179
+ ActiveSupport::Notifications.instrument(name, key: key, value: value, message: result.message)
163
180
 
164
181
  action = self.class.action_on_invalid_parameters
165
182
  case action
@@ -190,7 +207,11 @@ module StrongerParameters
190
207
  # TODO: this is not consistent with the behavior of raising ActionController::UnpermittedParameters
191
208
  # should have the same render vs raise behavior in test/dev ... see permitted_parameters_test.rb
192
209
  rescue_from(StrongerParameters::InvalidParameter) do |e|
193
- render plain: e.message, status: :bad_request # uncovered
210
+ if request.format.to_s.include?('json')
211
+ render json: { error: e.message }, status: :bad_request
212
+ else
213
+ render plain: e.message, status: :bad_request
214
+ end
194
215
  end
195
216
  end
196
217
  end
@@ -198,3 +219,4 @@ end
198
219
 
199
220
  ActionController::Parameters.include StrongerParameters::Parameters
200
221
  ActionController::Base.include StrongerParameters::ControllerSupport
222
+ ActionController::API.include StrongerParameters::ControllerSupport
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module StrongerParameters
3
- VERSION = '2.16.0'
3
+ VERSION = '2.19.1'
4
4
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stronger_parameters
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.16.0
4
+ version: 2.19.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mick Staugaard
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-02-14 00:00:00.000000000 Z
11
+ date: 2022-11-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -170,6 +170,8 @@ files:
170
170
  - lib/stronger_parameters/constraints/array_constraint.rb
171
171
  - lib/stronger_parameters/constraints/boolean_constraint.rb
172
172
  - lib/stronger_parameters/constraints/comparison_constraints.rb
173
+ - lib/stronger_parameters/constraints/date_constraint.rb
174
+ - lib/stronger_parameters/constraints/date_iso8601_constraint.rb
173
175
  - lib/stronger_parameters/constraints/date_time_constraint.rb
174
176
  - lib/stronger_parameters/constraints/date_time_iso8601_constraint.rb
175
177
  - lib/stronger_parameters/constraints/decimal_constraint.rb
@@ -183,6 +185,8 @@ files:
183
185
  - lib/stronger_parameters/constraints/nil_string_constraint.rb
184
186
  - lib/stronger_parameters/constraints/regexp_constraint.rb
185
187
  - lib/stronger_parameters/constraints/string_constraint.rb
188
+ - lib/stronger_parameters/constraints/time_constraint.rb
189
+ - lib/stronger_parameters/constraints/time_iso8601_constraint.rb
186
190
  - lib/stronger_parameters/controller_support/permitted_parameters.rb
187
191
  - lib/stronger_parameters/errors.rb
188
192
  - lib/stronger_parameters/parameters.rb
@@ -200,14 +204,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
200
204
  requirements:
201
205
  - - ">="
202
206
  - !ruby/object:Gem::Version
203
- version: 2.5.0
207
+ version: 2.7.0
204
208
  required_rubygems_version: !ruby/object:Gem::Requirement
205
209
  requirements:
206
210
  - - ">="
207
211
  - !ruby/object:Gem::Version
208
212
  version: '0'
209
213
  requirements: []
210
- rubygems_version: 3.1.6
214
+ rubygems_version: 3.0.3.1
211
215
  signing_key:
212
216
  specification_version: 4
213
217
  summary: Type checking and type casting of parameters for Action Pack