validate-params 0.12.5 → 0.13.1

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: a0efef56bcd7c8b05e33859d21078422a2a772d3dbb8241a9d0a0e5818c085a6
4
- data.tar.gz: 23936b451c39e8b6425498c75383b4742ab8e43850f83960aeb200846d59dd32
3
+ metadata.gz: c7e15b3464314531ae9f982d69feb1fd6ad5f57ee9964cf37e83ba7fa9fe6939
4
+ data.tar.gz: f45cfcdaceaa64d3883165e0e8c9b7d4f86d8e065e8cc144e104006c7619c0b0
5
5
  SHA512:
6
- metadata.gz: 963d02b70917dfb1c81dbe672c62ff750c3ed76ae39943eb719e8385b90cf53e588c0d4599cff3551b367650f43a3f0b88cc975a7147e3a3f6f934670f99092f
7
- data.tar.gz: 1ef4c60aba7a23cff17ad899ebe6996b77312203a0a5c11509dcc53bf66c6cd4801eb03d4af63e5fefcae4ce086de115e919c2e11b15390bd81d9a15c409d7e6
6
+ metadata.gz: 0266c77d4a5ac354cd2056f2c9da2db95fc41566c0c02d83023d983c9b13c40a09fd8aabe5cb9a8bf39cc9a300a8f21d79e1c06db1a18f95d5eea8a115f314b3
7
+ data.tar.gz: 77d490ec23d2b849e4be9e45e11cdef57e76f125f1dc0a10f59de38f1b53ebe274b0215779e91661f09b77cc94e1d8506c9477ff9763bf27952b276e7a46213a
data/CHANGELOG.md CHANGED
@@ -1,3 +1,12 @@
1
+ ## [0.13.1] - 2024-04-22
2
+
3
+ - Improved checking for empty strings
4
+
5
+ ## [0.13.0] - 2024-04-16
6
+
7
+ - Added `:scrub_invalid_utf8` and `:scrub_invalid_utf8_replacement` options to String type to handle invalid UTF-8 characters
8
+ - Added [configurator](README.md#configuration) to change default behaviour for certain options
9
+
1
10
  ## [0.12.5] - 2024-04-15
2
11
 
3
12
  - Restrict validation rules for Hash type objects
@@ -10,6 +19,19 @@
10
19
 
11
20
  - Default option for Integer type should support empty string as well
12
21
 
22
+ ## [0.12.2] - 2024-03-11
23
+
24
+ - Added inheritance of validate params rule between child and parent controllers
25
+
26
+ ## [0.12.1] - 2024-03-05
27
+
28
+ - Fixed support of ActionController::Parameters along with Hash
29
+
30
+ ## [0.12.0] - 2024-03-05
31
+
32
+ - Added support of array of hashes
33
+ - Improved nested attributes validation
34
+
13
35
  ## [0.11.0] - 2023-11-04
14
36
 
15
37
  - Added support for Float type
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.0"
9
- gem "rubocop", "~> 1.57"
8
+ gem "rake", "~> 13.2"
9
+ gem "rubocop", "~> 1.63"
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- validate-params (0.12.5)
4
+ validate-params (0.13.1)
5
5
  activesupport (>= 6.1.0)
6
6
  i18n (>= 1.6)
7
7
 
@@ -31,26 +31,26 @@ GEM
31
31
  builder (3.2.4)
32
32
  concurrent-ruby (1.2.2)
33
33
  crass (1.0.6)
34
- diff-lcs (1.5.0)
34
+ diff-lcs (1.5.1)
35
35
  erubi (1.12.0)
36
36
  i18n (1.13.0)
37
37
  concurrent-ruby (~> 1.0)
38
- json (2.6.3)
38
+ json (2.7.2)
39
39
  language_server-protocol (3.17.0.3)
40
40
  loofah (2.21.1)
41
41
  crass (~> 1.0.2)
42
42
  nokogiri (>= 1.5.9)
43
- mini_portile2 (2.8.2)
43
+ mini_portile2 (2.8.6)
44
44
  minitest (5.18.0)
45
- nokogiri (1.14.3)
46
- mini_portile2 (~> 2.8.0)
45
+ nokogiri (1.15.6)
46
+ mini_portile2 (~> 2.8.2)
47
47
  racc (~> 1.4)
48
- parallel (1.23.0)
49
- parser (3.2.2.4)
48
+ parallel (1.24.0)
49
+ parser (3.3.0.5)
50
50
  ast (~> 2.4.1)
51
51
  racc
52
- racc (1.7.1)
53
- rack (2.2.7)
52
+ racc (1.7.3)
53
+ rack (2.2.9)
54
54
  rack-test (2.1.0)
55
55
  rack (>= 1.3)
56
56
  rails-dom-testing (2.0.3)
@@ -59,35 +59,35 @@ GEM
59
59
  rails-html-sanitizer (1.5.0)
60
60
  loofah (~> 2.19, >= 2.19.1)
61
61
  rainbow (3.1.1)
62
- rake (13.0.6)
63
- regexp_parser (2.8.2)
62
+ rake (13.2.1)
63
+ regexp_parser (2.9.0)
64
64
  rexml (3.2.6)
65
- rspec (3.12.0)
66
- rspec-core (~> 3.12.0)
67
- rspec-expectations (~> 3.12.0)
68
- rspec-mocks (~> 3.12.0)
69
- rspec-core (3.12.2)
70
- rspec-support (~> 3.12.0)
71
- rspec-expectations (3.12.3)
65
+ rspec (3.13.0)
66
+ rspec-core (~> 3.13.0)
67
+ rspec-expectations (~> 3.13.0)
68
+ rspec-mocks (~> 3.13.0)
69
+ rspec-core (3.13.0)
70
+ rspec-support (~> 3.13.0)
71
+ rspec-expectations (3.13.0)
72
72
  diff-lcs (>= 1.2.0, < 2.0)
73
- rspec-support (~> 3.12.0)
74
- rspec-mocks (3.12.5)
73
+ rspec-support (~> 3.13.0)
74
+ rspec-mocks (3.13.0)
75
75
  diff-lcs (>= 1.2.0, < 2.0)
76
- rspec-support (~> 3.12.0)
77
- rspec-support (3.12.0)
78
- rubocop (1.57.2)
76
+ rspec-support (~> 3.13.0)
77
+ rspec-support (3.13.0)
78
+ rubocop (1.63.3)
79
79
  json (~> 2.3)
80
80
  language_server-protocol (>= 3.17.0)
81
81
  parallel (~> 1.10)
82
- parser (>= 3.2.2.4)
82
+ parser (>= 3.3.0.2)
83
83
  rainbow (>= 2.2.2, < 4.0)
84
84
  regexp_parser (>= 1.8, < 3.0)
85
85
  rexml (>= 3.2.5, < 4.0)
86
- rubocop-ast (>= 1.28.1, < 2.0)
86
+ rubocop-ast (>= 1.31.1, < 2.0)
87
87
  ruby-progressbar (~> 1.7)
88
88
  unicode-display_width (>= 2.4.0, < 3.0)
89
- rubocop-ast (1.30.0)
90
- parser (>= 3.2.1.0)
89
+ rubocop-ast (1.31.2)
90
+ parser (>= 3.3.0.4)
91
91
  ruby-progressbar (1.13.0)
92
92
  tzinfo (2.0.6)
93
93
  concurrent-ruby (~> 1.0)
@@ -100,9 +100,9 @@ PLATFORMS
100
100
  DEPENDENCIES
101
101
  actionpack (>= 6.1.0)
102
102
  bundler (~> 2.0)
103
- rake (~> 13.0)
103
+ rake (~> 13.2)
104
104
  rspec (~> 3.0)
105
- rubocop (~> 1.57)
105
+ rubocop (~> 1.63)
106
106
  validate-params!
107
107
 
108
108
  BUNDLED WITH
data/README.md CHANGED
@@ -8,11 +8,30 @@ ValidateParams is a lightweight, robust Ruby on Rails gem that introduces a simp
8
8
 
9
9
  Install the gem and add to the application's Gemfile by executing:
10
10
 
11
- $ bundle add validate_params
11
+ $ bundle add validate-params
12
12
 
13
13
  If bundler is not being used to manage dependencies, install the gem by executing:
14
14
 
15
- $ gem install validate_params
15
+ $ gem install validate-params
16
+
17
+
18
+ ## Configuration
19
+
20
+ To configure default options, create `config/initializers/validate_params.rb` file. Configuration example:
21
+
22
+ ```ruby
23
+ require "validate_params/validatable"
24
+
25
+ Rails.application.config.before_initialize do
26
+ ValidateParams::Validatable.configure do |config|
27
+ config.scrub_invalid_utf8 = true # Default: false
28
+ config.scrub_invalid_utf8_replacement = "�" # Default: empty string
29
+ end
30
+ end
31
+ ```
32
+ Currently only these options are supported in configuration. If you need more options, please create an issue.
33
+
34
+ **Be aware**: `scrub_invalid_utf8` mutates parameters value passed into controller. Learn more in [params mutation](#params-mutation) section.
16
35
 
17
36
  ## Usage
18
37
 
@@ -51,7 +70,7 @@ end
51
70
 
52
71
  Here are the following supported types along with operations supported.
53
72
 
54
- - String (required, default)
73
+ - String (required, default, scrub_invalid_utf8, scrub_invalid_utf8_replacement)
55
74
  - Integer (required, default, min, max, in)
56
75
  - Float (required, default, min, max, in)
57
76
  - Date (required, default, min, max)
@@ -61,6 +80,14 @@ Here are the following supported types along with operations supported.
61
80
  - Hash - Nested block of types
62
81
 
63
82
 
83
+ ### Params mutation
84
+
85
+ String type supports `scrub_invalid_utf8` and `scrub_invalid_utf8_replacement` options to handle invalid UTF-8 characters.
86
+ If `scrub_invalid_utf8` is set to true, it will replace invalid UTF-8 characters with the value of `scrub_invalid_utf8_replacement`.
87
+
88
+ This modified value will be passed to the controller parameters.
89
+
90
+
64
91
  ## Response
65
92
 
66
93
  If the parameters are valid, the controller action will be executed as normal. If the parameters are invalid, a **400 Bad Request** response will be returned with a JSON body containing the errors, or an empty HTML response.
@@ -81,8 +108,7 @@ If the parameters are valid, the controller action will be executed as normal. I
81
108
  {
82
109
  "message": "states is an invalid value",
83
110
  "valid_values": ["active", "inactive"]
84
- },
85
-
111
+ }
86
112
  ]
87
113
  }
88
114
  ```
data/docker-compose.yml CHANGED
@@ -10,3 +10,4 @@ services:
10
10
 
11
11
  volumes:
12
12
  bundler:
13
+
@@ -0,0 +1,17 @@
1
+ module ValidateParams
2
+ class Configuration
3
+ attr_accessor :scrub_invalid_utf8, :scrub_invalid_utf8_replacement
4
+
5
+ def initialize
6
+ @scrub_invalid_utf8 = false
7
+ @scrub_invalid_utf8_replacement = ""
8
+ end
9
+
10
+ def to_h
11
+ {
12
+ scrub_invalid_utf8: scrub_invalid_utf8,
13
+ scrub_invalid_utf8_replacement: scrub_invalid_utf8_replacement
14
+ }
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,7 @@
1
+ class String
2
+ alias original_blank? blank?
3
+
4
+ def blank?
5
+ self.scrub.original_blank?
6
+ end
7
+ end
@@ -43,6 +43,7 @@ module ValidateParams
43
43
  end
44
44
 
45
45
  def param(field, type, options = {})
46
+ options = ValidateParams::Validatable.configuration.to_h.merge(options)
46
47
  validation = Validation.new(field, type, options, [], @parent)
47
48
 
48
49
  if block_given?
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "active_support/inflector"
4
+ require "validate_params/extenstions/string"
4
5
 
5
6
  module ValidateParams
6
7
  module Validatable
@@ -19,7 +20,7 @@ module ValidateParams
19
20
  end
20
21
 
21
22
  def call
22
- return if @value.blank? && !@options[:required]
23
+ return if @value.nil? && !@options[:required]
23
24
 
24
25
  if @value.blank? && @options[:required]
25
26
  @errors << { message: required_error_message }
@@ -1,10 +1,18 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "validate_params/utilities/scrubber"
4
+
3
5
  module ValidateParams
4
6
  class Types
5
7
  class String
6
- def self.cast(raw_value, **)
7
- raw_value.to_s
8
+ def self.cast(raw_value, scrub_invalid_utf8: false, **)
9
+ value = raw_value.to_s
10
+
11
+ if scrub_invalid_utf8
12
+ value = Validatable::Utilities::Scrubber.scrub(raw_value)
13
+ end
14
+
15
+ value
8
16
  end
9
17
  end
10
18
  end
@@ -0,0 +1,13 @@
1
+ module ValidateParams
2
+ module Validatable
3
+ module Utilities
4
+ class Scrubber
5
+ def self.scrub(input_string, replacement: Validatable.configuration.scrub_invalid_utf8_replacement)
6
+ input_string
7
+ .scrub(replacement)
8
+ .tr("\u0000", replacement)
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "validate_params/configuration"
4
+
3
5
  require "validate_params/types/date"
4
6
  require "validate_params/types/date_time"
5
7
  require "validate_params/types/integer"
@@ -16,6 +18,14 @@ module ValidateParams
16
18
  module Validatable
17
19
  extend ::ActiveSupport::Concern
18
20
 
21
+ def self.configure
22
+ yield(configuration)
23
+ end
24
+
25
+ def self.configuration
26
+ @configuration ||= Configuration.new
27
+ end
28
+
19
29
  included do
20
30
  before_action :perform_validate_params
21
31
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ValidateParams
4
- VERSION = "0.12.5"
4
+ VERSION = "0.13.1"
5
5
  end
@@ -26,7 +26,7 @@ Gem::Specification.new do |spec|
26
26
 
27
27
  spec.add_development_dependency "actionpack", ">= 6.1.0"
28
28
  spec.add_development_dependency "bundler", "~> 2.0"
29
- spec.add_development_dependency "rake", "~> 10.0"
29
+ spec.add_development_dependency "rake", "~> 13.2"
30
30
  spec.add_development_dependency "rspec", "~> 3.0"
31
31
 
32
32
  spec.add_dependency "activesupport", ">= 6.1.0"
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.12.5
4
+ version: 0.13.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - dcherevatenko
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-04-17 00:00:00.000000000 Z
11
+ date: 2024-04-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: actionpack
@@ -44,14 +44,14 @@ dependencies:
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '10.0'
47
+ version: '13.2'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '10.0'
54
+ version: '13.2'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: rspec
57
57
  requirement: !ruby/object:Gem::Requirement
@@ -111,6 +111,8 @@ files:
111
111
  - config/locales/en.yml
112
112
  - docker-compose.yml
113
113
  - lib/validate_params.rb
114
+ - lib/validate_params/configuration.rb
115
+ - lib/validate_params/extenstions/string.rb
114
116
  - lib/validate_params/param_builder.rb
115
117
  - lib/validate_params/param_validator.rb
116
118
  - lib/validate_params/types/array.rb
@@ -120,6 +122,7 @@ files:
120
122
  - lib/validate_params/types/i_o.rb
121
123
  - lib/validate_params/types/integer.rb
122
124
  - lib/validate_params/types/string.rb
125
+ - lib/validate_params/utilities/scrubber.rb
123
126
  - lib/validate_params/validatable.rb
124
127
  - lib/validate_params/version.rb
125
128
  - sig/validate_params.rbs