validate-params 0.14.1 → 0.16.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: 49a497919ab75580d101c38b3ef6d57a35082d85ae9c63048f672c25745196e0
4
- data.tar.gz: 4c66e68ab065c305742a5d161bb65041eab7bc46c969d02148d4b74a644e7745
3
+ metadata.gz: 77ad49f477340257039775213f6c8ebd6bbaf6eb79ab49b298bd50d6fab88291
4
+ data.tar.gz: 522314281864e41faeea4a67857bd856fe0f02211d0f7ff5c2b84c761b1395da
5
5
  SHA512:
6
- metadata.gz: abec4f999ce02973595d484901448f2420bb0c3742c541516cdebab62facfed8901a8e53711ea9976b248f12f2261fabf4f6b743957b4841d0275d761efb249b
7
- data.tar.gz: e5d117580c45b9fc963304853e2558d0f20d09fe4ea74387bf4867c33383b1851788e1bac94e35c8fc5fde38d4c5387bb2a745b21c9151228228be96b210f0d6
6
+ metadata.gz: dec501d625c7a0ff55f23ce26cfec0b504808a6d4ac5b33e584152fb996905873cce9b443701aa6ef3990de2356d4fe16a2384ef7edeb4a24dd6da5f53168d3e
7
+ data.tar.gz: edd03c3b475a23a5f022aba35edf995df6b332aa7e29fb43bac62e62f25393d238b37e0c7dac24075227cfb84e9e1f2d6aa3844fcb312b4f4153dbeb74fcba61
data/CHANGELOG.md CHANGED
@@ -1,3 +1,15 @@
1
+ ## [0.16.0] - 2025-11-25
2
+
3
+ - Support `:in` options to Array, of: String
4
+
5
+ ## [0.15.1] - 2025-10-14
6
+
7
+ - Fixed issue with DateTime type validation to reject timestamps that result in years greater than 9999
8
+
9
+ ## [0.15.0] - 2025-09-26
10
+
11
+ - Support for boolean type has been added
12
+
1
13
  ## [0.14.1] - 2025-07-15
2
14
 
3
15
  - Fixed issue with `scrub_invalid_utf8` option in String type to ensure it works correctly with non-string inputs
data/Gemfile CHANGED
@@ -5,5 +5,5 @@ source "https://rubygems.org"
5
5
  # Specify your gem's dependencies in validate_params.gemspec
6
6
  gemspec
7
7
 
8
- gem "rake", "~> 13.2"
9
- gem "rubocop", "~> 1.63"
8
+ gem "rake", "~> 13.3"
9
+ gem "rubocop", "~> 1.81"
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- validate-params (0.14.1)
4
+ validate-params (0.16.0)
5
5
  activesupport (>= 6.1.0)
6
6
  i18n (>= 1.6)
7
7
 
@@ -27,7 +27,7 @@ GEM
27
27
  minitest (>= 5.1)
28
28
  tzinfo (~> 2.0)
29
29
  zeitwerk (~> 2.3)
30
- ast (2.4.2)
30
+ ast (2.4.3)
31
31
  builder (3.2.4)
32
32
  concurrent-ruby (1.2.3)
33
33
  crass (1.0.6)
@@ -35,8 +35,9 @@ GEM
35
35
  erubi (1.12.0)
36
36
  i18n (1.14.5)
37
37
  concurrent-ruby (~> 1.0)
38
- json (2.7.2)
39
- language_server-protocol (3.17.0.3)
38
+ json (2.16.0)
39
+ language_server-protocol (3.17.0.5)
40
+ lint_roller (1.1.0)
40
41
  loofah (2.21.1)
41
42
  crass (~> 1.0.2)
42
43
  nokogiri (>= 1.5.9)
@@ -45,12 +46,13 @@ GEM
45
46
  nokogiri (1.15.6)
46
47
  mini_portile2 (~> 2.8.2)
47
48
  racc (~> 1.4)
48
- parallel (1.24.0)
49
- parser (3.3.0.5)
49
+ parallel (1.27.0)
50
+ parser (3.3.10.0)
50
51
  ast (~> 2.4.1)
51
52
  racc
52
- racc (1.7.3)
53
- rack (2.2.9)
53
+ prism (1.6.0)
54
+ racc (1.8.1)
55
+ rack (2.2.20)
54
56
  rack-test (2.1.0)
55
57
  rack (>= 1.3)
56
58
  rails-dom-testing (2.0.3)
@@ -59,10 +61,8 @@ GEM
59
61
  rails-html-sanitizer (1.5.0)
60
62
  loofah (~> 2.19, >= 2.19.1)
61
63
  rainbow (3.1.1)
62
- rake (13.2.1)
63
- regexp_parser (2.9.0)
64
- rexml (3.2.8)
65
- strscan (>= 3.0.9)
64
+ rake (13.3.1)
65
+ regexp_parser (2.11.3)
66
66
  rspec (3.13.0)
67
67
  rspec-core (~> 3.13.0)
68
68
  rspec-expectations (~> 3.13.0)
@@ -76,24 +76,26 @@ GEM
76
76
  diff-lcs (>= 1.2.0, < 2.0)
77
77
  rspec-support (~> 3.13.0)
78
78
  rspec-support (3.13.0)
79
- rubocop (1.63.3)
79
+ rubocop (1.81.7)
80
80
  json (~> 2.3)
81
- language_server-protocol (>= 3.17.0)
81
+ language_server-protocol (~> 3.17.0.2)
82
+ lint_roller (~> 1.1.0)
82
83
  parallel (~> 1.10)
83
84
  parser (>= 3.3.0.2)
84
85
  rainbow (>= 2.2.2, < 4.0)
85
- regexp_parser (>= 1.8, < 3.0)
86
- rexml (>= 3.2.5, < 4.0)
87
- rubocop-ast (>= 1.31.1, < 2.0)
86
+ regexp_parser (>= 2.9.3, < 3.0)
87
+ rubocop-ast (>= 1.47.1, < 2.0)
88
88
  ruby-progressbar (~> 1.7)
89
- unicode-display_width (>= 2.4.0, < 3.0)
90
- rubocop-ast (1.31.2)
91
- parser (>= 3.3.0.4)
89
+ unicode-display_width (>= 2.4.0, < 4.0)
90
+ rubocop-ast (1.48.0)
91
+ parser (>= 3.3.7.2)
92
+ prism (~> 1.4)
92
93
  ruby-progressbar (1.13.0)
93
- strscan (3.1.0)
94
94
  tzinfo (2.0.6)
95
95
  concurrent-ruby (~> 1.0)
96
- unicode-display_width (2.5.0)
96
+ unicode-display_width (3.2.0)
97
+ unicode-emoji (~> 4.1)
98
+ unicode-emoji (4.1.0)
97
99
  zeitwerk (2.6.8)
98
100
 
99
101
  PLATFORMS
@@ -102,9 +104,9 @@ PLATFORMS
102
104
  DEPENDENCIES
103
105
  actionpack (>= 6.1.0)
104
106
  bundler (~> 2.0)
105
- rake (~> 13.2)
107
+ rake (~> 13.3)
106
108
  rspec (~> 3.0)
107
- rubocop (~> 1.63)
109
+ rubocop (~> 1.81)
108
110
  validate-params!
109
111
 
110
112
  BUNDLED WITH
data/README.md CHANGED
@@ -50,6 +50,7 @@ class TestController < ActionController::Base
50
50
  p.param :user_ids, Array, of: Integer, default: [1, 2, 3]
51
51
  p.param :states, Array, of: String, default: ["active", "inactive"], reject_blank: true
52
52
  p.param :file, IO, min: 1.byte, max: 1.megabyte
53
+ p.param :active, :boolean, default: true
53
54
  p.param :date_of_birth, Hash do |pp|
54
55
  pp.param :gt, Date, min: Date.new(2020, 1, 1), max: Date.new(2021, 1, 1)
55
56
  pp.param :lt, Date
@@ -78,6 +79,7 @@ Here are the following supported types along with operations supported.
78
79
  - IO (required, min, max)
79
80
  - Array of: (String|Integer|Float) (default, reject_blank)
80
81
  - Hash - Nested block of types
82
+ - Boolean (required, default)
81
83
 
82
84
 
83
85
  ### Params mutation
@@ -3,5 +3,6 @@ en:
3
3
  required: "%{param} is required"
4
4
  invalid_type: "%{param} must be a valid %{type}"
5
5
  invalid_in: "%{param} is an invalid value"
6
+ array_invalid_in: "%{param} has an invalid values: %{values}"
6
7
  more_than_max: "%{param} cannot be more than maximum"
7
8
  less_than_min: "%{param} cannot be less than minimum"
@@ -38,9 +38,12 @@ module ValidateParams
38
38
  end
39
39
 
40
40
  def array
41
- return if Types::Array.valid?(@value, **@options)
41
+ unless Types::Array.valid?(@value, **@options)
42
+ @errors << { message: error_message }
43
+ return
44
+ end
42
45
 
43
- @errors << { message: error_message }
46
+ array_validate_inclusion if @options[:in].present?
44
47
  end
45
48
 
46
49
  def date
@@ -109,6 +112,12 @@ module ValidateParams
109
112
  validate_max(io_size) if @options[:max].present?
110
113
  end
111
114
 
115
+ def boolean
116
+ return if Types::Boolean.valid?(@value)
117
+
118
+ @errors << { message: error_message }
119
+ end
120
+
112
121
  def validate_inclusion
113
122
  return if @options[:in].include?(@value)
114
123
 
@@ -118,6 +127,16 @@ module ValidateParams
118
127
  }
119
128
  end
120
129
 
130
+ def array_validate_inclusion
131
+ extra_values = Types::Array.cast(@value, **@options) - @options[:in]
132
+ return if extra_values.empty?
133
+
134
+ @errors << {
135
+ message: I18n.t("validate_params.array_invalid_in", param: error_param_name, values: extra_values),
136
+ valid_values: @options[:in]
137
+ }
138
+ end
139
+
121
140
  def validate_min(value)
122
141
  min_value = @options[:min].is_a?(Proc) ? @options[:min].call : @options[:min]
123
142
  return if min_value <= value
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ValidateParams
4
+ class Types
5
+ class Boolean
6
+ TRUE_VALUES = [true, 1, "1", :"1", "t", :t, "T", :T, "true", :true, "TRUE", :TRUE, "on", :on, "ON", :ON].to_set.freeze
7
+ FALSE_VALUES = [false, 0, "0", :"0", "f", :f, "F", :F, "false", :false, "FALSE", :FALSE, "off", :off, "OFF", :OFF].to_set.freeze
8
+
9
+ def self.valid?(value)
10
+ value.is_a?(::TrueClass) || value.is_a?(::FalseClass)
11
+ end
12
+
13
+ def self.cast(raw_value, **)
14
+ return true if TRUE_VALUES.include?(raw_value)
15
+ return false if FALSE_VALUES.include?(raw_value)
16
+
17
+ raw_value
18
+ end
19
+ end
20
+ end
21
+ end
@@ -4,7 +4,9 @@ module ValidateParams
4
4
  class Types
5
5
  class DateTime
6
6
  def self.valid?(value)
7
- Time.at(Integer(value))
7
+ parsed_time = Time.at(Integer(value))
8
+ return false if parsed_time.year > 9999
9
+
8
10
  true
9
11
  rescue ArgumentError, TypeError
10
12
  false
@@ -9,6 +9,7 @@ require "validate_params/types/float"
9
9
  require "validate_params/types/array"
10
10
  require "validate_params/types/string"
11
11
  require "validate_params/types/i_o"
12
+ require "validate_params/types/boolean"
12
13
  require_relative "param_builder"
13
14
  require_relative "param_validator"
14
15
 
@@ -129,7 +130,8 @@ module ValidateParams
129
130
  return if value.blank?
130
131
 
131
132
  options = validation.options.presence || {}
132
- params[validation.field] = Types.const_get(validation.type.name).cast(value, **options)
133
+ type_name = validation.type.is_a?(Symbol) ? validation.type.to_s.classify : validation.type.name
134
+ params[validation.field] = Types.const_get(type_name).cast(value, **options)
133
135
  end
134
136
  end
135
137
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ValidateParams
4
- VERSION = "0.14.1"
4
+ VERSION = "0.16.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: validate-params
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.14.1
4
+ version: 0.16.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - dcherevatenko
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2025-07-14 00:00:00.000000000 Z
11
+ date: 2025-11-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: actionpack
@@ -117,6 +117,7 @@ files:
117
117
  - lib/validate_params/param_builder.rb
118
118
  - lib/validate_params/param_validator.rb
119
119
  - lib/validate_params/types/array.rb
120
+ - lib/validate_params/types/boolean.rb
120
121
  - lib/validate_params/types/date.rb
121
122
  - lib/validate_params/types/date_time.rb
122
123
  - lib/validate_params/types/float.rb