validate-params 0.12.4 → 0.13.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: 49259451d9cfaab7442c2efbebcf863fd95ffaa5939d20bfda60f74a790ebcde
4
- data.tar.gz: 93b889e507e6a14faa28715ce53fc0a8609dd4903a7b1400a4398fad401d9105
3
+ metadata.gz: 77ee095dc2316a90b3784532a9d0586777c187b99c63b489129e4c0d6c4c6b2b
4
+ data.tar.gz: 17f55ad85601e32c1218f76c0b3e804b83cd5159b41308c0a448431f7a360dfc
5
5
  SHA512:
6
- metadata.gz: feb837f5b782d6aa7715b8cc4f508a4c5280221ce4760e818c7b39e10955340db62aaad82ac512257ac01071ca66f6b7246af7de8f4570e3f130acf78a74a7c3
7
- data.tar.gz: 80276c1107d9fd4395b4e3e9cd9bc0fa472b5ea22eedd3303e26521650e7bb25ae56d37285641ace765c6d9f8765345e9b0117538c070be71a83dac64e425b77
6
+ metadata.gz: 8a6b277ff7d5d03c8926a9ccf3b6b730bed2527de0e70d44fdc1853e749c6701a0072d39aa3e1679be0a22ae93d72569cff7986ff1b71f1d424c80f2b3831c1e
7
+ data.tar.gz: a5001cd005f065d12ffaa16a6f0f97b1148bf5934ea37e2804d6c5b749f7b666116fdb6b060a49babfa84b354dd3f4ce1685e73bd2f56670dcf579338cb1ae42
data/CHANGELOG.md CHANGED
@@ -1,4 +1,14 @@
1
- ## [0.12.3] - 2024-04-14
1
+ ## [0.13.0] - 2024-04-16
2
+
3
+ - Added `:scrub_invalid_utf8` and `:scrub_invalid_utf8_replacement` options to String type to handle invalid UTF-8 characters
4
+ - Added [configurator](README.md#configuration) to change default behaviour for certain options
5
+
6
+
7
+ ## [0.12.5] - 2024-04-15
8
+
9
+ - Restrict validation rules for Hash type objects
10
+
11
+ ## [0.12.4] - 2024-04-14
2
12
 
3
13
  - Revert 0.12.2 changes
4
14
 
@@ -6,6 +16,19 @@
6
16
 
7
17
  - Default option for Integer type should support empty string as well
8
18
 
19
+ ## [0.12.2] - 2024-03-11
20
+
21
+ - Added inheritance of validate params rule between child and parent controllers
22
+
23
+ ## [0.12.1] - 2024-03-05
24
+
25
+ - Fixed support of ActionController::Parameters along with Hash
26
+
27
+ ## [0.12.0] - 2024-03-05
28
+
29
+ - Added support of array of hashes
30
+ - Improved nested attributes validation
31
+
9
32
  ## [0.11.0] - 2023-11-04
10
33
 
11
34
  - Added support for Float type
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- validate-params (0.12.4)
4
+ validate-params (0.13.0)
5
5
  activesupport (>= 6.1.0)
6
6
  i18n (>= 1.6)
7
7
 
data/README.md CHANGED
@@ -8,11 +8,28 @@ 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
+ Rails.application.config.after_initialize do
24
+ ValidateParams::Validatable.configure do |config|
25
+ config.scrub_invalid_utf8 = true # Default: false
26
+ config.scrub_invalid_utf8_replacement = "�" # Default: empty string
27
+ end
28
+ end
29
+ ```
30
+ Currently only these options are supported in configuration. If you need more options, please create an issue.
31
+
32
+ **Be aware**: `scrub_invalid_utf8` mutates parameters value passed into controller. Learn more in [params mutation](#params-mutation) section.
16
33
 
17
34
  ## Usage
18
35
 
@@ -51,7 +68,7 @@ end
51
68
 
52
69
  Here are the following supported types along with operations supported.
53
70
 
54
- - String (required, default)
71
+ - String (required, default, scrub_invalid_utf8, scrub_invalid_utf8_replacement)
55
72
  - Integer (required, default, min, max, in)
56
73
  - Float (required, default, min, max, in)
57
74
  - Date (required, default, min, max)
@@ -61,6 +78,14 @@ Here are the following supported types along with operations supported.
61
78
  - Hash - Nested block of types
62
79
 
63
80
 
81
+ ### Params mutation
82
+
83
+ String type supports `scrub_invalid_utf8` and `scrub_invalid_utf8_replacement` options to handle invalid UTF-8 characters.
84
+ If `scrub_invalid_utf8` is set to true, it will replace invalid UTF-8 characters with the value of `scrub_invalid_utf8_replacement`.
85
+
86
+ This modified value will be passed to the controller parameters.
87
+
88
+
64
89
  ## Response
65
90
 
66
91
  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 +106,7 @@ If the parameters are valid, the controller action will be executed as normal. I
81
106
  {
82
107
  "message": "states is an invalid value",
83
108
  "valid_values": ["active", "inactive"]
84
- },
85
-
109
+ }
86
110
  ]
87
111
  }
88
112
  ```
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
@@ -14,9 +14,9 @@ module ValidateParams
14
14
  if children.any?
15
15
  case type.to_s
16
16
  when "Hash"
17
- # Skip in case hash is configured and string is passed
18
- if !value.is_a?(String)
19
- children.each { |c| c.valid?(value&.[](c.field), errors) }
17
+ children.each do |child|
18
+ child_value = value[child.field] if value.is_a?(Hash) || value.is_a?(ActionController::Parameters)
19
+ child.valid?(child_value, errors)
20
20
  end
21
21
  when "Array"
22
22
  values = value ? Array.wrap(value) : [nil]
@@ -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
@@ -30,6 +31,12 @@ module ValidateParams
30
31
  end
31
32
 
32
33
  private
34
+ def hash
35
+ return if @value.is_a?(Hash) || @value.is_a?(ActionController::Parameters)
36
+
37
+ @errors << { message: error_message }
38
+ end
39
+
33
40
  def array
34
41
  return if Types::Array.valid?(@value, **@options)
35
42
 
@@ -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
@@ -79,9 +89,6 @@ module ValidateParams
79
89
  apply_default_values(sub_params, sub_validation)
80
90
  end
81
91
  elsif validation.type == Hash
82
- # Skip in case hash is configured and string is passed
83
- next if params[validation.field].is_a?(String)
84
-
85
92
  params[validation.field] ||= {}
86
93
  apply_default_values(params[validation.field], sub_validation)
87
94
  else
@@ -108,9 +115,6 @@ module ValidateParams
108
115
 
109
116
  validation.children.each do |sub_validation|
110
117
  if validation.type == Hash
111
- # Skip in case hash is configured and string is passed
112
- next if params[validation.field].is_a?(String)
113
-
114
118
  cast_param_values(params[validation.field], sub_validation)
115
119
  elsif validation.type == Array
116
120
  params[validation.field].each do |sub_params|
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ValidateParams
4
- VERSION = "0.12.4"
4
+ VERSION = "0.13.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.12.4
4
+ version: 0.13.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - dcherevatenko
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-04-14 00:00:00.000000000 Z
11
+ date: 2024-04-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: actionpack
@@ -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,15 +122,16 @@ 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
126
129
  - validate_params.gemspec
127
- homepage:
130
+ homepage:
128
131
  licenses:
129
132
  - MIT
130
133
  metadata: {}
131
- post_install_message:
134
+ post_install_message:
132
135
  rdoc_options: []
133
136
  require_paths:
134
137
  - lib
@@ -143,8 +146,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
143
146
  - !ruby/object:Gem::Version
144
147
  version: '0'
145
148
  requirements: []
146
- rubygems_version: 3.4.10
147
- signing_key:
149
+ rubygems_version: 3.3.26
150
+ signing_key:
148
151
  specification_version: 4
149
152
  summary: Gem to validate params in controllers
150
153
  test_files: []