rails_simple_params 2.1.0 → 2.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8dc9967120c78ee7a3ad10512c15f4f4a18a5351565f66cc18b134b358fbadbb
4
- data.tar.gz: f4458365c870abc53c4523ee6876e165ba3194ff934c63ab728a31ce8327c390
3
+ metadata.gz: 5d83fd444ad9c327d6e322ef3c671893c833552cf640954972ce64bb7b111a7d
4
+ data.tar.gz: c9a5c1d155ef8feac0fac786d02392aef5618641be1b29e7d7abb3b86cafd45f
5
5
  SHA512:
6
- metadata.gz: f952eb67a354af6dee3f9a6e618b0d625e0f8106e81d9d789339b799564a8d1105ebea888195a0ae68d02e38c000cf8f717671a965024e6f79e09c579d6a15b1
7
- data.tar.gz: 4fa21104284ef27cf297c83f86218d96c3889aac225fcecbe781ef4cf92dae62e303c9c15ec3a209b269d5cc01a6d835e85a22f41fb4d983acdadfd397d3ec0d
6
+ metadata.gz: 28c30554c5e272213224eb7d1f5144e898a174f9e3ad7832224321b7e93fb996d22a3ab14a80a67f3b7e37e58d1b906baaf56260c1884a57dc26346743e166e2
7
+ data.tar.gz: e37b558d09fef07223b6a4a37f2d45223a537318c34229fe0fd86553d8b3ddcd9c362b2081a0f4920250a657eb5a7f1f270806e191e365a2ee480ce96ef2a63a
data/README.md CHANGED
@@ -1,4 +1,5 @@
1
1
  # rails_simple_params
2
+
2
3
  _Parameter Validation & Type Coercion for Rails_
3
4
 
4
5
  ## Introduction
@@ -39,7 +40,7 @@ As usual, in your Gemfile...
39
40
  ### Migrating from `rails_param`
40
41
 
41
42
  Change any code you have rescuing `RailsParam::InvalidParameterError` to instead
42
- rescue `RailsSimpleParam::InvalidParameter` so you will continue to provide any
43
+ rescue `RailsSimpleParams::InvalidParameter` so you will continue to provide any
43
44
  customized HTTP `4xx` responses appropriate for your app.
44
45
 
45
46
  If you want to handle different types of validations separately — especially if
@@ -47,17 +48,18 @@ you want to handle your own I18n translations for the default English error
47
48
  messages provided by this gem — you can rescue from any/all of the sub-classed
48
49
  exception classes:
49
50
 
50
- - `RailsSimpleParam::EmptyParameter`
51
- - `RailsSimpleParam::InvalidFormat`
52
- - `RailsSimpleParam::InvalidIdentity`
53
- - `RailsSimpleParam::InvalidOption`
54
- - `RailsSimpleParam::InvalidType`
55
- - `RailsSimpleParam::MissingParameter`
56
- - `RailsSimpleParam::OutOfRange`
57
- - `RailsSimpleParam::TooLarge`
58
- - `RailsSimpleParam::TooLong`
59
- - `RailsSimpleParam::TooShort`
60
- - `RailsSimpleParam::TooSmall`
51
+ - `RailsSimpleParams::EmptyParameter`
52
+ - `RailsSimpleParams::FileUpload`
53
+ - `RailsSimpleParams::InvalidFormat`
54
+ - `RailsSimpleParams::InvalidIdentity`
55
+ - `RailsSimpleParams::InvalidOption`
56
+ - `RailsSimpleParams::InvalidType`
57
+ - `RailsSimpleParams::MissingParameter`
58
+ - `RailsSimpleParams::OutOfRange`
59
+ - `RailsSimpleParams::TooLarge`
60
+ - `RailsSimpleParams::TooLong`
61
+ - `RailsSimpleParams::TooShort`
62
+ - `RailsSimpleParams::TooSmall`
61
63
 
62
64
  ## Using this Gem
63
65
 
@@ -112,6 +114,11 @@ this kind of exception.
112
114
  - `min_length` / `max_length`
113
115
  - `required`
114
116
 
117
+ File uploads are always rejected: if a declared parameter (including one nested
118
+ in a `Hash` or `Array` block) receives an uploaded file, `RailsSimpleParams::FileUpload`
119
+ is raised regardless of the declared type. Parameters you don't declare with
120
+ `param!` are not inspected.
121
+
115
122
  Customize exception message with option `:message`
116
123
 
117
124
  ```ruby
@@ -177,7 +184,7 @@ param! :books_array, Array, required: true do |b|
177
184
  end
178
185
  ```
179
186
 
180
- ## Many thanks to:
187
+ ## Many thanks to
181
188
 
182
189
  - [Nicolas Blanco](http://twitter.com/nblanco_fr)
183
190
  - [Mattt Thompson](https://twitter.com/mattt)
@@ -18,6 +18,7 @@ module RailsSimpleParams
18
18
  end
19
19
 
20
20
  class EmptyParameter < InvalidParameter; end
21
+ class FileUpload < InvalidParameter; end
21
22
  class InvalidFormat < InvalidParameter; end
22
23
  class InvalidIdentity < InvalidParameter; end
23
24
  class InvalidOption < InvalidParameter; end
@@ -9,39 +9,23 @@ module RailsSimpleParams
9
9
  @context = context
10
10
  end
11
11
 
12
- def param!(name, type, options = {}, &) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
12
+ def param!(name, type, options = {}, &) # rubocop:disable Metrics/MethodLength
13
13
  name = name.to_s unless name.is_a?(Integer)
14
- return unless params.include?(name) || check_param_presence?(options[:default]) || options[:required]
14
+ return unless evaluate?(name, options)
15
15
 
16
16
  parameter_name = @context ? "#{@context}[#{name}]" : name
17
- check_config(parameter_name, type, options)
18
- coerced_value = coerce(parameter_name, params[name], type, options)
19
-
17
+ ConfigCheck.new(parameter_name, type, options)
20
18
  parameter = RailsSimpleParams::Parameter.new(
21
19
  name: parameter_name,
22
- value: coerced_value,
20
+ value: coerce(parameter_name, params[name], type, options),
23
21
  type: type,
24
22
  options: options,
25
23
  &
26
24
  )
27
25
 
28
- parameter.set_default if parameter.should_set_default?
29
-
30
- # validate presence
31
- if params[name].nil? && options[:required]
32
- raise MissingParameter.new(
33
- "#{parameter_name} is required",
34
- param: parameter_name,
35
- options: options
36
- )
37
- end
38
-
26
+ parameter.set_default
39
27
  recurse_on_parameter(parameter, &) if block_given?
40
-
41
- # apply transformation
42
- parameter.transform if options[:transform]
43
-
44
- # validate
28
+ parameter.transform
45
29
  validate!(parameter)
46
30
 
47
31
  # set params value
@@ -52,6 +36,8 @@ module RailsSimpleParams
52
36
 
53
37
  def coerce(param_name, param, type, options = {})
54
38
  return nil if param.nil?
39
+
40
+ reject_file_upload!(param_name, param, options)
55
41
  return param if begin
56
42
  param.is_a?(type)
57
43
  rescue StandardError
@@ -63,12 +49,14 @@ module RailsSimpleParams
63
49
  raise InvalidType.new("'#{param}' is not a valid #{type}", param: param_name)
64
50
  end
65
51
 
66
- def check_config(param_name, type, options = {})
67
- ConfigCheck.new(param_name, type, options)
52
+ def evaluate?(name, options)
53
+ params.include?(name) || !options[:default].nil? || options[:required]
68
54
  end
69
55
 
70
- def check_param_presence?(param)
71
- !param.nil?
56
+ def reject_file_upload!(param_name, param, options)
57
+ return unless param.respond_to?(:original_filename) && param.respond_to?(:tempfile)
58
+
59
+ raise FileUpload.new("#{param_name} does not allow file uploads", param: param_name, options: options)
72
60
  end
73
61
 
74
62
  def recurse(element, context, index = nil)
@@ -93,8 +81,8 @@ module RailsSimpleParams
93
81
  end
94
82
  end
95
83
 
96
- def validate!(param)
97
- param.validate
84
+ def validate!(parameter)
85
+ parameter.validate
98
86
  end
99
87
  end
100
88
  end
@@ -15,14 +15,19 @@ module RailsSimpleParams
15
15
  end
16
16
 
17
17
  def should_set_default?
18
- value.nil? && check_param_presence?(options[:default])
18
+ value.blank? && check_param_presence?(options[:default])
19
19
  end
20
20
 
21
21
  def set_default
22
+ return unless should_set_default?
23
+
22
24
  self.value = options[:default].respond_to?(:call) ? options[:default].call : options[:default]
23
25
  end
24
26
 
25
27
  def transform
28
+ return if value.nil?
29
+ return unless options[:transform]
30
+
26
31
  self.value = options[:transform].to_proc.call(value)
27
32
  end
28
33
 
@@ -9,7 +9,7 @@ module RailsSimpleParams
9
9
  return true unless options[:required]
10
10
  return false if value.nil?
11
11
  return false if value == ''
12
- return false if value.is_a?(Enumerable) && value.size.zero?
12
+ return false if value.is_a?(Enumerable) && value.empty?
13
13
 
14
14
  true
15
15
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RailsSimpleParams
4
- VERSION = '2.1.0'
4
+ VERSION = '2.2.0'
5
5
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_simple_params
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.0
4
+ version: 2.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeremy Weathers
8
8
  bindir: bin
9
9
  cert_chain: []
10
- date: 2026-01-28 00:00:00.000000000 Z
10
+ date: 2026-11-22 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: actionpack