validate-params 0.12.5 → 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: a0efef56bcd7c8b05e33859d21078422a2a772d3dbb8241a9d0a0e5818c085a6
4
- data.tar.gz: 23936b451c39e8b6425498c75383b4742ab8e43850f83960aeb200846d59dd32
3
+ metadata.gz: 77ee095dc2316a90b3784532a9d0586777c187b99c63b489129e4c0d6c4c6b2b
4
+ data.tar.gz: 17f55ad85601e32c1218f76c0b3e804b83cd5159b41308c0a448431f7a360dfc
5
5
  SHA512:
6
- metadata.gz: 963d02b70917dfb1c81dbe672c62ff750c3ed76ae39943eb719e8385b90cf53e588c0d4599cff3551b367650f43a3f0b88cc975a7147e3a3f6f934670f99092f
7
- data.tar.gz: 1ef4c60aba7a23cff17ad899ebe6996b77312203a0a5c11509dcc53bf66c6cd4801eb03d4af63e5fefcae4ce086de115e919c2e11b15390bd81d9a15c409d7e6
6
+ metadata.gz: 8a6b277ff7d5d03c8926a9ccf3b6b730bed2527de0e70d44fdc1853e749c6701a0072d39aa3e1679be0a22ae93d72569cff7986ff1b71f1d424c80f2b3831c1e
7
+ data.tar.gz: a5001cd005f065d12ffaa16a6f0f97b1148bf5934ea37e2804d6c5b749f7b666116fdb6b060a49babfa84b354dd3f4ce1685e73bd2f56670dcf579338cb1ae42
data/CHANGELOG.md CHANGED
@@ -1,3 +1,9 @@
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
+
1
7
  ## [0.12.5] - 2024-04-15
2
8
 
3
9
  - Restrict validation rules for Hash type objects
@@ -10,6 +16,19 @@
10
16
 
11
17
  - Default option for Integer type should support empty string as well
12
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
+
13
32
  ## [0.11.0] - 2023-11-04
14
33
 
15
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.5)
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
@@ -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
@@ -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.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.5
4
+ version: 0.13.0
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-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,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