validate-params 0.8.1 → 0.10.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 +4 -4
- data/CHANGELOG.md +10 -2
- data/Gemfile +1 -1
- data/Gemfile.lock +15 -10
- data/README.md +32 -7
- data/config/locales/en.yml +2 -0
- data/lib/validate_params/param_validator.rb +50 -4
- data/lib/validate_params/types/i_o.rb +15 -0
- data/lib/validate_params/validatable.rb +3 -1
- data/lib/validate_params/version.rb +1 -1
- metadata +8 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4e686013f02bbb7e5fd50eeabb70a7b3abdd3fdcc8458b3e2230d1a288435988
|
4
|
+
data.tar.gz: e2434d12a8b52721b930b426133189db03a84a7dbcb86ed57c1ca98c4b4b06b3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 809c060682bd2e8b9e49781589f5fe45bd91abfdfdba7d41b1285b12fef5e8d27fb8476acdd8e1ffee73ec1a16c3f5017acb8348faa0e5b9c8735d499496c094
|
7
|
+
data.tar.gz: 34ee15be73ac94a817b144f246b43e89623c4a81261035448e432bce589b51971d3b8022ac6732a8e27197f0f21efb918b85535866a4109d63582a0a7d12da9a
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,13 @@
|
|
1
1
|
## [Unreleased]
|
2
2
|
|
3
|
+
## [0.10.0] - 2023-08-28
|
4
|
+
|
5
|
+
- Added support for IO type to validate file uploads
|
6
|
+
|
7
|
+
## [0.9.0] - 2023-08-02
|
8
|
+
|
9
|
+
- Add :min and :max options to validate param values for Integer types
|
10
|
+
|
3
11
|
## [0.8.0] - 2023-05-30
|
4
12
|
|
5
13
|
- Added support for passing array of actions into validate_params_for to run on multiple actions
|
@@ -17,9 +25,9 @@
|
|
17
25
|
## [0.5.0] - 2023-05-15
|
18
26
|
|
19
27
|
- Support for Proc as options for default to set param default values
|
20
|
-
|
28
|
+
- Add :in options to validate param values against a list of values for String and Integer types
|
21
29
|
- Updated JSON formats of error messages to be more consistent
|
22
|
-
|
30
|
+
|
23
31
|
## [0.3.0] - 2023-05-12
|
24
32
|
|
25
33
|
- Add required attribute support
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
validate-params (0.
|
4
|
+
validate-params (0.10.0)
|
5
5
|
activesupport (>= 6.1.0)
|
6
6
|
i18n (>= 1.6)
|
7
7
|
|
@@ -28,6 +28,7 @@ GEM
|
|
28
28
|
tzinfo (~> 2.0)
|
29
29
|
zeitwerk (~> 2.3)
|
30
30
|
ast (2.4.2)
|
31
|
+
base64 (0.1.1)
|
31
32
|
builder (3.2.4)
|
32
33
|
concurrent-ruby (1.2.2)
|
33
34
|
crass (1.0.6)
|
@@ -36,6 +37,7 @@ GEM
|
|
36
37
|
i18n (1.13.0)
|
37
38
|
concurrent-ruby (~> 1.0)
|
38
39
|
json (2.6.3)
|
40
|
+
language_server-protocol (3.17.0.3)
|
39
41
|
loofah (2.21.1)
|
40
42
|
crass (~> 1.0.2)
|
41
43
|
nokogiri (>= 1.5.9)
|
@@ -45,9 +47,10 @@ GEM
|
|
45
47
|
mini_portile2 (~> 2.8.0)
|
46
48
|
racc (~> 1.4)
|
47
49
|
parallel (1.23.0)
|
48
|
-
parser (3.2.2.
|
50
|
+
parser (3.2.2.3)
|
49
51
|
ast (~> 2.4.1)
|
50
|
-
|
52
|
+
racc
|
53
|
+
racc (1.7.1)
|
51
54
|
rack (2.2.7)
|
52
55
|
rack-test (2.1.0)
|
53
56
|
rack (>= 1.3)
|
@@ -58,8 +61,8 @@ GEM
|
|
58
61
|
loofah (~> 2.19, >= 2.19.1)
|
59
62
|
rainbow (3.1.1)
|
60
63
|
rake (13.0.6)
|
61
|
-
regexp_parser (2.8.
|
62
|
-
rexml (3.2.
|
64
|
+
regexp_parser (2.8.1)
|
65
|
+
rexml (3.2.6)
|
63
66
|
rspec (3.12.0)
|
64
67
|
rspec-core (~> 3.12.0)
|
65
68
|
rspec-expectations (~> 3.12.0)
|
@@ -73,17 +76,19 @@ GEM
|
|
73
76
|
diff-lcs (>= 1.2.0, < 2.0)
|
74
77
|
rspec-support (~> 3.12.0)
|
75
78
|
rspec-support (3.12.0)
|
76
|
-
rubocop (1.
|
79
|
+
rubocop (1.56.0)
|
80
|
+
base64 (~> 0.1.1)
|
77
81
|
json (~> 2.3)
|
82
|
+
language_server-protocol (>= 3.17.0)
|
78
83
|
parallel (~> 1.10)
|
79
|
-
parser (>= 3.2.
|
84
|
+
parser (>= 3.2.2.3)
|
80
85
|
rainbow (>= 2.2.2, < 4.0)
|
81
86
|
regexp_parser (>= 1.8, < 3.0)
|
82
87
|
rexml (>= 3.2.5, < 4.0)
|
83
|
-
rubocop-ast (>= 1.28.
|
88
|
+
rubocop-ast (>= 1.28.1, < 2.0)
|
84
89
|
ruby-progressbar (~> 1.7)
|
85
90
|
unicode-display_width (>= 2.4.0, < 3.0)
|
86
|
-
rubocop-ast (1.
|
91
|
+
rubocop-ast (1.29.0)
|
87
92
|
parser (>= 3.2.1.0)
|
88
93
|
ruby-progressbar (1.13.0)
|
89
94
|
tzinfo (2.0.6)
|
@@ -99,7 +104,7 @@ DEPENDENCIES
|
|
99
104
|
bundler (~> 2.0)
|
100
105
|
rake (~> 13.0)
|
101
106
|
rspec (~> 3.0)
|
102
|
-
rubocop (~> 1.
|
107
|
+
rubocop (~> 1.56)
|
103
108
|
validate-params!
|
104
109
|
|
105
110
|
BUNDLED WITH
|
data/README.md
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
[](https://github.com/peopleforce/validate_params/actions/workflows/rspec.yml)
|
4
4
|
|
5
|
-
ValidateParams is a lightweight, robust Ruby on Rails gem that introduces a simple yet powerful DSL (Domain Specific Language) to validate parameters for your controller actions. It is designed to make your code cleaner, more maintainable, and ensures that your application handles invalid or unexpected parameters gracefully.
|
5
|
+
ValidateParams is a lightweight, robust Ruby on Rails gem that introduces a simple yet powerful DSL (Domain Specific Language) to validate and type cast the parameters for your controller actions. It is designed to make your code cleaner, more maintainable, and ensures that your application handles invalid or unexpected parameters gracefully.
|
6
6
|
|
7
7
|
## Installation
|
8
8
|
|
@@ -25,15 +25,17 @@ class TestController < ActionController::Base
|
|
25
25
|
validate_params :index do |p|
|
26
26
|
p.param :name, String, default: "John Doe"
|
27
27
|
p.param :occurred_on, Date, required: true, default: proc { Date.today }
|
28
|
+
p.param :per_page, Integer, default: 50, min: 1, max: 50
|
28
29
|
p.param :quantity, Integer, required: true, in: [1, 2, 3]
|
29
30
|
p.param :user_ids, Array, of: Integer, default: [1, 2, 3]
|
30
31
|
p.param :states, Array, of: String, default: ["active", "inactive"], reject_blank: true
|
32
|
+
p.param :file, IO, min: 1.byte, max: 1.megabyte
|
31
33
|
p.param :date_of_birth, Hash do |pp|
|
32
|
-
pp.param :gt, Date
|
34
|
+
pp.param :gt, Date, min: Date.new(2020, 1, 1), max: Date.new(2021, 1, 1)
|
33
35
|
pp.param :lt, Date
|
34
36
|
end
|
35
37
|
p.param :created_at, Hash do |pp|
|
36
|
-
pp.param :lt, DateTime
|
38
|
+
pp.param :lt, DateTime, min: DateTime.new(2020, 1, 1), max: DateTime.new(2021, 1, 1)
|
37
39
|
pp.param :gt, DateTime
|
38
40
|
end
|
39
41
|
end
|
@@ -44,10 +46,25 @@ class TestController < ActionController::Base
|
|
44
46
|
end
|
45
47
|
```
|
46
48
|
|
49
|
+
### Types
|
50
|
+
|
51
|
+
Here are the following supported types along with operations supported.
|
52
|
+
|
53
|
+
- String (required, default)
|
54
|
+
- Integer (required, default, min, max, in)
|
55
|
+
- Date (required, default, min, max)
|
56
|
+
- DateTime (required, default, min, max)
|
57
|
+
- IO (required, min, max)
|
58
|
+
- Array of: (String|Integer) (default, reject_blank)
|
59
|
+
- Hash - Nested block of types
|
60
|
+
|
61
|
+
|
47
62
|
## Response
|
48
63
|
|
49
64
|
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.
|
50
65
|
|
66
|
+
### JSON (Default)
|
67
|
+
|
51
68
|
```json
|
52
69
|
{
|
53
70
|
"success": false,
|
@@ -55,16 +72,24 @@ If the parameters are valid, the controller action will be executed as normal. I
|
|
55
72
|
{
|
56
73
|
"message": "hired_on must be a valid Date"
|
57
74
|
},
|
58
|
-
|
75
|
+
{
|
76
|
+
"message": "per_page cannot be more than maximum",
|
77
|
+
"max": 50
|
78
|
+
},
|
79
|
+
{
|
80
|
+
"message": "states is an invalid value",
|
81
|
+
"valid_values": ["active", "inactive"]
|
82
|
+
},
|
83
|
+
|
59
84
|
]
|
60
85
|
}
|
61
86
|
```
|
62
87
|
|
63
|
-
|
88
|
+
### HTML
|
64
89
|
|
65
|
-
By default responses are returned in JSON format. To return responses as an empty HTML response, change the :format
|
90
|
+
By default responses are returned in JSON format. To return responses as an empty HTML response with a **400 Bad Request** status, change the :format option in the validate_params methods to **:html**.
|
66
91
|
|
67
|
-
Example:
|
92
|
+
Example:
|
68
93
|
|
69
94
|
```ruby
|
70
95
|
validate_params :index, format: :html do |p|
|
data/config/locales/en.yml
CHANGED
@@ -36,15 +36,27 @@ module ValidateParams
|
|
36
36
|
end
|
37
37
|
|
38
38
|
def date
|
39
|
-
|
39
|
+
if !Types::Date.valid?(@value)
|
40
|
+
@errors << { message: error_message }
|
41
|
+
return
|
42
|
+
end
|
40
43
|
|
41
|
-
|
44
|
+
formatted_value = Types::Date.cast(@value)
|
45
|
+
|
46
|
+
validate_min(formatted_value) if @options[:min].present?
|
47
|
+
validate_max(formatted_value) if @options[:max].present?
|
42
48
|
end
|
43
49
|
|
44
50
|
def date_time
|
45
|
-
|
51
|
+
if !Types::DateTime.valid?(@value)
|
52
|
+
@errors << { message: error_message }
|
53
|
+
return
|
54
|
+
end
|
46
55
|
|
47
|
-
|
56
|
+
formatted_value = Types::DateTime.cast(@value)
|
57
|
+
|
58
|
+
validate_min(formatted_value) if @options[:min].present?
|
59
|
+
validate_max(formatted_value) if @options[:max].present?
|
48
60
|
end
|
49
61
|
|
50
62
|
def integer
|
@@ -53,13 +65,29 @@ module ValidateParams
|
|
53
65
|
return
|
54
66
|
end
|
55
67
|
|
68
|
+
formatted_value = Types::Integer.cast(@value)
|
69
|
+
|
56
70
|
validate_inclusion if @options[:in].present?
|
71
|
+
validate_min(formatted_value) if @options[:min].present?
|
72
|
+
validate_max(formatted_value) if @options[:max].present?
|
57
73
|
end
|
58
74
|
|
59
75
|
def string
|
60
76
|
validate_inclusion if @options[:in].present?
|
61
77
|
end
|
62
78
|
|
79
|
+
def io
|
80
|
+
unless Types::IO.valid?(@value)
|
81
|
+
@errors << { message: error_message }
|
82
|
+
return
|
83
|
+
end
|
84
|
+
|
85
|
+
formatted_value = Types::IO.cast(@value)
|
86
|
+
|
87
|
+
validate_min(formatted_value) if @options[:min].present?
|
88
|
+
validate_max(formatted_value) if @options[:max].present?
|
89
|
+
end
|
90
|
+
|
63
91
|
def validate_inclusion
|
64
92
|
return if @options[:in].include?(@value)
|
65
93
|
|
@@ -69,6 +97,24 @@ module ValidateParams
|
|
69
97
|
}
|
70
98
|
end
|
71
99
|
|
100
|
+
def validate_min(value)
|
101
|
+
return if @options[:min] <= value
|
102
|
+
|
103
|
+
@errors << {
|
104
|
+
message: I18n.t("validate_params.less_than_min", param: error_param_name),
|
105
|
+
min: @options[:min]
|
106
|
+
}
|
107
|
+
end
|
108
|
+
|
109
|
+
def validate_max(value)
|
110
|
+
return if @options[:max] >= value
|
111
|
+
|
112
|
+
@errors << {
|
113
|
+
message: I18n.t("validate_params.more_than_max", param: error_param_name),
|
114
|
+
max: @options[:max]
|
115
|
+
}
|
116
|
+
end
|
117
|
+
|
72
118
|
def error_param_name
|
73
119
|
case @field
|
74
120
|
when Array
|
@@ -4,6 +4,8 @@ require "validate_params/types/date"
|
|
4
4
|
require "validate_params/types/date_time"
|
5
5
|
require "validate_params/types/integer"
|
6
6
|
require "validate_params/types/array"
|
7
|
+
require "validate_params/types/string"
|
8
|
+
require "validate_params/types/i_o"
|
7
9
|
require_relative "param_builder"
|
8
10
|
require_relative "param_validator"
|
9
11
|
|
@@ -25,7 +27,7 @@ module ValidateParams
|
|
25
27
|
@params_validations ||= {}
|
26
28
|
|
27
29
|
Array(action).each do |act|
|
28
|
-
@params_validations[act] ||= {options: options, validations: []}
|
30
|
+
@params_validations[act] ||= { options: options, validations: [] }
|
29
31
|
|
30
32
|
yield(ParamBuilder.new(validations: @params_validations[act][:validations])) if block
|
31
33
|
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.
|
4
|
+
version: 0.10.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: 2023-
|
11
|
+
date: 2023-08-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: actionpack
|
@@ -116,17 +116,18 @@ files:
|
|
116
116
|
- lib/validate_params/types/array.rb
|
117
117
|
- lib/validate_params/types/date.rb
|
118
118
|
- lib/validate_params/types/date_time.rb
|
119
|
+
- lib/validate_params/types/i_o.rb
|
119
120
|
- lib/validate_params/types/integer.rb
|
120
121
|
- lib/validate_params/types/string.rb
|
121
122
|
- lib/validate_params/validatable.rb
|
122
123
|
- lib/validate_params/version.rb
|
123
124
|
- sig/validate_params.rbs
|
124
125
|
- validate_params.gemspec
|
125
|
-
homepage:
|
126
|
+
homepage:
|
126
127
|
licenses:
|
127
128
|
- MIT
|
128
129
|
metadata: {}
|
129
|
-
post_install_message:
|
130
|
+
post_install_message:
|
130
131
|
rdoc_options: []
|
131
132
|
require_paths:
|
132
133
|
- lib
|
@@ -141,8 +142,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
141
142
|
- !ruby/object:Gem::Version
|
142
143
|
version: '0'
|
143
144
|
requirements: []
|
144
|
-
rubygems_version: 3.
|
145
|
-
signing_key:
|
145
|
+
rubygems_version: 3.2.3
|
146
|
+
signing_key:
|
146
147
|
specification_version: 4
|
147
148
|
summary: Gem to validate params in controllers
|
148
149
|
test_files: []
|