rails_simple_params 2.2.0 → 2.3.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: 5d83fd444ad9c327d6e322ef3c671893c833552cf640954972ce64bb7b111a7d
4
- data.tar.gz: c9a5c1d155ef8feac0fac786d02392aef5618641be1b29e7d7abb3b86cafd45f
3
+ metadata.gz: a845fd6ca29aa63c8cd45df789ed1f6a0c63d6966e30bc9823b65eab62897962
4
+ data.tar.gz: 732e164119a5a2a156c4d8491b7298cf7e5b229a37d5c2fe11805be74adecedc
5
5
  SHA512:
6
- metadata.gz: 28c30554c5e272213224eb7d1f5144e898a174f9e3ad7832224321b7e93fb996d22a3ab14a80a67f3b7e37e58d1b906baaf56260c1884a57dc26346743e166e2
7
- data.tar.gz: e37b558d09fef07223b6a4a37f2d45223a537318c34229fe0fd86553d8b3ddcd9c362b2081a0f4920250a657eb5a7f1f270806e191e365a2ee480ce96ef2a63a
6
+ metadata.gz: 0e40cf00cc6a3e5b67f12a7b6153e6bdee1178707e2110712683d0d4a33004a86543879ee11dc7d591dc11e6e21ea96676d77286708a63f221a5158c9740e389
7
+ data.tar.gz: cda73e87a60e99aa261eefe9f2b6afdd1d71177079cc3a6fef01183fec19a11a894fc4acca66144a5115d6fa6607af134247ac0de9d53ef9fbef7845e5a1d897
data/README.md CHANGED
@@ -72,7 +72,7 @@ exception classes:
72
72
  param! :categories, Array
73
73
  param! :sort, String, default: 'title'
74
74
  param! :order, String, in: %w(asc desc), transform: :downcase, default: 'asc'
75
- param! :price, String, format: /[<\=>]\s*\$\d+/
75
+ param! :price, String, format: /\A[<\=>]\s*\$\d+\z/
76
76
  param! :results, Integer, in: (10..100)
77
77
 
78
78
  # Access the parameters using the params object (e.g. `params[:q]`) as you usually do...
@@ -114,6 +114,8 @@ this kind of exception.
114
114
  - `min_length` / `max_length`
115
115
  - `required`
116
116
 
117
+ Format regular expressions should always use `\A` and `\z` anchors.
118
+
117
119
  File uploads are always rejected: if a declared parameter (including one nested
118
120
  in a `Hash` or `Array` block) receives an uploaded file, `RailsSimpleParams::FileUpload`
119
121
  is raised regardless of the declared type. Parameters you don't declare with
@@ -8,13 +8,18 @@ module RailsSimpleParams
8
8
  def coerce
9
9
  return nil if param == '' # e.g. from an empty field in an HTML form
10
10
 
11
- stripped_param = if param.is_a?(String)
12
- param.delete('$,').strip.to_f
13
- else
14
- param
15
- end
11
+ # Parse from a String (never via Float) so invalid input raises ArgumentError and no precision is lost.
12
+ # Kernel#BigDecimal ignores its precision argument for Strings, so round to significant digits explicitly.
13
+ value = BigDecimal(normalized_param).mult(1, options[:precision] || DEFAULT_PRECISION)
14
+ raise ArgumentError unless value.finite?
16
15
 
17
- BigDecimal(stripped_param, options[:precision] || DEFAULT_PRECISION)
16
+ value
17
+ end
18
+
19
+ private
20
+
21
+ def normalized_param
22
+ param.is_a?(String) ? param.delete('$,').strip : param.to_s
18
23
  end
19
24
  end
20
25
  end
@@ -3,8 +3,8 @@
3
3
  module RailsSimpleParams
4
4
  class Coercion
5
5
  class BooleanParam < Base
6
- FALSEY = /^(false|f|no|n|0)$/i
7
- TRUTHY = /^(true|t|yes|y|1)$/i
6
+ FALSEY = /\A(false|f|no|n|0)\z/i
7
+ TRUTHY = /\A(true|t|yes|y|1)\z/i
8
8
 
9
9
  def coerce
10
10
  return false if FALSEY === param.to_s
@@ -13,7 +13,7 @@ module RailsSimpleParams
13
13
  private
14
14
 
15
15
  def argument_validation
16
- raise InvalidConfiguration unless type == Hash
16
+ raise ArgumentError unless type == Hash
17
17
  end
18
18
  end
19
19
  end
@@ -14,7 +14,7 @@ module RailsSimpleParams
14
14
  private
15
15
 
16
16
  def argument_validation
17
- raise InvalidConfiguration unless type.respond_to?(:parse)
17
+ raise ArgumentError unless type.respond_to?(:parse)
18
18
  end
19
19
  end
20
20
  end
@@ -17,16 +17,13 @@ module RailsSimpleParams
17
17
  boolean: BooleanParam
18
18
  }.freeze
19
19
 
20
- attr_reader :coercion, :param
20
+ attr_reader :coercion
21
21
 
22
22
  def initialize(param, type, options)
23
- @param = param
24
23
  @coercion = klass_for(type).new(param: param, options: options, type: type)
25
24
  end
26
25
 
27
26
  def coerce
28
- return nil if param.nil?
29
-
30
27
  coercion.coerce
31
28
  end
32
29
 
@@ -18,7 +18,7 @@ module RailsSimpleParams
18
18
  return unless (allowed = options[:in])
19
19
 
20
20
  allowed = allowed.call if allowed.respond_to?(:call)
21
- allowed = [allowed.begin, allowed.end] if allowed.is_a?(Range) && type.is_a?(Numeric)
21
+ allowed = [allowed.begin, allowed.end].compact if allowed.is_a?(Range)
22
22
  allowed
23
23
  end
24
24
 
@@ -57,9 +57,9 @@ module RailsSimpleParams
57
57
  validate_length :max_length
58
58
  validate_length :min_length
59
59
  return unless options.key?(:min_length) && options.key?(:max_length)
60
- return if options[:min_length] < options[:max_length]
60
+ return if options[:min_length] <= options[:max_length]
61
61
 
62
- raise_error ":max_length on #{param} must be larger than :min_length"
62
+ raise_error ":max_length on #{param} cannot be smaller than :min_length"
63
63
  end
64
64
 
65
65
  def validate_limit(key)
@@ -73,9 +73,9 @@ module RailsSimpleParams
73
73
  validate_limit :max
74
74
  validate_limit :min
75
75
  return unless options.key?(:min) && options.key?(:max)
76
- return if options[:min] < options[:max]
76
+ return if options[:min] <= options[:max]
77
77
 
78
- raise_error ":max on #{param} must be larger than :min"
78
+ raise_error ":max on #{param} cannot be smaller than :min"
79
79
  end
80
80
  end
81
81
  end
@@ -18,16 +18,15 @@ module RailsSimpleParams
18
18
  }.freeze
19
19
 
20
20
  def initialize(param, type, options)
21
- klass_for(type).new(param: param, options: options, type: type)
21
+ klass_for(param, type).new(param: param, options: options, type: type)
22
22
  end
23
23
 
24
24
  private
25
25
 
26
- def klass_for(type)
27
- klass = PARAM_TYPE_MAPPING[type]
28
- return klass if klass
29
-
30
- raise TypeError
26
+ def klass_for(param, type)
27
+ PARAM_TYPE_MAPPING.fetch(type) do
28
+ raise InvalidConfiguration.new("#{type.inspect} is not a supported type for #{param}", param:)
29
+ end
31
30
  end
32
31
  end
33
32
  end
@@ -26,7 +26,7 @@ module RailsSimpleParams
26
26
  parameter.set_default
27
27
  recurse_on_parameter(parameter, &) if block_given?
28
28
  parameter.transform
29
- validate!(parameter)
29
+ parameter.validate
30
30
 
31
31
  # set params value
32
32
  params[name] = parameter.value
@@ -73,16 +73,15 @@ module RailsSimpleParams
73
73
  if element.is_a?(Hash) || element.is_a?(ActionController::Parameters)
74
74
  recurse(element, "#{parameter.name}[#{i}]", &)
75
75
  else
76
- parameter.value[i] = recurse({ i => element }, parameter.name, i, &) # supply index as key unless value is hash
76
+ # do not rely on the block's return value
77
+ wrapper = { i => element }
78
+ recurse(wrapper, parameter.name, i, &)
79
+ parameter.value[i] = wrapper[i]
77
80
  end
78
81
  end
79
82
  else
80
83
  recurse(parameter.value, parameter.name, &)
81
84
  end
82
85
  end
83
-
84
- def validate!(parameter)
85
- parameter.validate
86
- end
87
86
  end
88
87
  end
@@ -4,9 +4,6 @@ module RailsSimpleParams
4
4
  class Parameter
5
5
  attr_accessor :name, :value, :options, :type
6
6
 
7
- TIME_TYPES = [Date, DateTime, Time].freeze
8
- STRING_OR_TIME_TYPES = ([String] + TIME_TYPES).freeze
9
-
10
7
  def initialize(name:, value:, options: {}, type: nil)
11
8
  @name = name
12
9
  @value = value
@@ -15,7 +12,7 @@ module RailsSimpleParams
15
12
  end
16
13
 
17
14
  def should_set_default?
18
- value.blank? && check_param_presence?(options[:default])
15
+ value.blank? && !options[:default].nil?
19
16
  end
20
17
 
21
18
  def set_default
@@ -34,11 +31,5 @@ module RailsSimpleParams
34
31
  def validate
35
32
  Validator.new(self).validate!
36
33
  end
37
-
38
- private
39
-
40
- def check_param_presence?(param)
41
- !param.nil?
42
- end
43
34
  end
44
35
  end
@@ -4,11 +4,11 @@ module RailsSimpleParams
4
4
  class Validator
5
5
  class Blank < Validator
6
6
  def valid_value?
7
- return false if parameter.options[:blank]
7
+ return true if options[:blank] # blank: true explicitly allows blank values
8
8
 
9
9
  case value
10
10
  when String
11
- (/\S/ === value) # rubocop:disable Style/CaseEquality
11
+ (/\S/ === value)
12
12
  when Array, Hash, ActionController::Parameters
13
13
  !value.empty?
14
14
  else
@@ -4,32 +4,27 @@ module RailsSimpleParams
4
4
  class Validator
5
5
  class Format < Validator
6
6
  TIME_TYPES = [Date, DateTime, Time].freeze
7
- STRING_OR_TIME_TYPES = ([String] + TIME_TYPES).freeze
8
7
 
8
+ # ConfigCheck only allows :format on String and time types, so after coercion the value is a
9
+ # String, a Date/DateTime/Time (already parsed with the format), or nil (absent or null input).
9
10
  def valid_value?
10
- matches_time_types? || string_in_format?
11
+ value.nil? || matches_time_types? || string_in_format?
11
12
  end
12
13
 
13
14
  private
14
15
 
15
16
  def error_message
16
- return "#{name} must be a string if using the format validation" unless matches_string_or_time_types?
17
-
18
- "#{name} must match format #{options[:format]}" unless string_in_format?
17
+ "#{name} must match format #{options[:format]}"
19
18
  end
20
19
 
21
20
  def exception_class
22
- matches_string_or_time_types? ? InvalidFormat : InvalidConfiguration
21
+ InvalidFormat
23
22
  end
24
23
 
25
24
  def matches_time_types?
26
25
  TIME_TYPES.any? { |cls| value.is_a? cls }
27
26
  end
28
27
 
29
- def matches_string_or_time_types?
30
- STRING_OR_TIME_TYPES.any? { |cls| value.is_a? cls }
31
- end
32
-
33
28
  def string_in_format?
34
29
  value.is_a?(String) && (value == '' || value =~ options[:format])
35
30
  end
@@ -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.empty?
12
+ return false if value.respond_to?(:empty?) && value.empty?
13
13
 
14
14
  true
15
15
  end
@@ -10,8 +10,8 @@ module RailsSimpleParams
10
10
 
11
11
  def_delegators :parameter, :name, :options, :value
12
12
 
13
- VALIDATABLE_OPTIONS = %i[
14
- blank
13
+ PRESENCE_OPTIONS = %i[required blank].freeze # these run first
14
+ VALUE_OPTIONS = %i[
15
15
  custom
16
16
  format
17
17
  in
@@ -20,7 +20,6 @@ module RailsSimpleParams
20
20
  max
21
21
  min_length
22
22
  min
23
- required
24
23
  ].freeze
25
24
 
26
25
  def initialize(parameter)
@@ -28,11 +27,13 @@ module RailsSimpleParams
28
27
  end
29
28
 
30
29
  def validate!
30
+ PRESENCE_OPTIONS.each { |key| run_validator(key) if options.key?(key) }
31
+ return if value.nil?
32
+
31
33
  options.each_key do |key|
32
- next unless VALIDATABLE_OPTIONS.include? key
34
+ next unless VALUE_OPTIONS.include?(key)
33
35
 
34
- class_name = camelize(key)
35
- Validator.const_get(class_name).new(parameter).valid!
36
+ run_validator(key)
36
37
  end
37
38
  end
38
39
 
@@ -48,6 +49,10 @@ module RailsSimpleParams
48
49
 
49
50
  private
50
51
 
52
+ def run_validator(key)
53
+ Validator.const_get(camelize(key)).new(parameter).valid!
54
+ end
55
+
51
56
  def camelize(term)
52
57
  string = term.to_s
53
58
  string.split('_').collect(&:capitalize).join
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RailsSimpleParams
4
- VERSION = '2.2.0'
4
+ VERSION = '2.3.0'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_simple_params
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.0
4
+ version: 2.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeremy Weathers
@@ -211,7 +211,7 @@ files:
211
211
  - lib/rails_simple_params/validator/min_length.rb
212
212
  - lib/rails_simple_params/validator/required.rb
213
213
  - lib/rails_simple_params/version.rb
214
- homepage: http://github.com/jlw/rails-simple-param
214
+ homepage: https://github.com/jlw/rails_simple_params
215
215
  licenses:
216
216
  - MIT
217
217
  metadata: