validate-params 0.5.0 → 0.5.2
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 +6 -2
- data/Gemfile +1 -1
- data/Gemfile.lock +3 -3
- data/README.md +3 -2
- data/lib/validate_params/params_validator.rb +25 -13
- data/lib/validate_params/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f6ab3ca178e0872231216da505527fc494dfa77ec389827c4db303e935ef5ead
|
4
|
+
data.tar.gz: d36f7fcee87464448330e6414e9962214c51295606773a1060460a2fb4926aa5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: af6b5f6b529eb3b848f1da4cbb590255a7122560327a695c22fdcaeaf3689bad84626ea2ecef2c3451c136b8ed64eb58846672d084a84080e25adf4e4c12365c
|
7
|
+
data.tar.gz: 7d86a7ddf3e57603b428b37cc01b5e9cc334348d2bd87a8fa36da108544e9f2a68f61a4cfd7095645e17e2a1c4afcefa91c5702157ab81f8f32d14d095b61fed
|
data/CHANGELOG.md
CHANGED
@@ -1,11 +1,15 @@
|
|
1
1
|
## [Unreleased]
|
2
2
|
|
3
|
+
## [0.5.2] - 2023-05-29
|
4
|
+
|
5
|
+
- Support to handle HTML and JSON response formats
|
6
|
+
|
3
7
|
## [0.5.0] - 2023-05-15
|
4
8
|
|
5
9
|
- Support for Proc as options for default to set param default values
|
6
|
-
|
10
|
+
`- Add :in options to validate param values against a list of values for String and Integer types
|
7
11
|
- Updated JSON formats of error messages to be more consistent
|
8
|
-
|
12
|
+
`
|
9
13
|
## [0.3.0] - 2023-05-12
|
10
14
|
|
11
15
|
- 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.5.
|
4
|
+
validate-params (0.5.2)
|
5
5
|
activesupport (>= 6.1.0)
|
6
6
|
|
7
7
|
GEM
|
@@ -72,7 +72,7 @@ GEM
|
|
72
72
|
diff-lcs (>= 1.2.0, < 2.0)
|
73
73
|
rspec-support (~> 3.12.0)
|
74
74
|
rspec-support (3.12.0)
|
75
|
-
rubocop (1.
|
75
|
+
rubocop (1.51.0)
|
76
76
|
json (~> 2.3)
|
77
77
|
parallel (~> 1.10)
|
78
78
|
parser (>= 3.2.0.0)
|
@@ -98,7 +98,7 @@ DEPENDENCIES
|
|
98
98
|
bundler (~> 2.0)
|
99
99
|
rake (~> 13.0)
|
100
100
|
rspec (~> 3.0)
|
101
|
-
rubocop (~> 1.
|
101
|
+
rubocop (~> 1.51)
|
102
102
|
validate-params!
|
103
103
|
|
104
104
|
BUNDLED WITH
|
data/README.md
CHANGED
@@ -23,8 +23,9 @@ class TestController < ActionController::Base
|
|
23
23
|
include ValidateParams::ParamsValidator
|
24
24
|
|
25
25
|
validate_params :index do |p|
|
26
|
-
p.param :
|
27
|
-
p.param :
|
26
|
+
p.param :name, String, default: "John Doe"
|
27
|
+
p.param :occurred_on, Date, required: true, default: proc { Date.today }
|
28
|
+
p.param :quantity, Integer, required: true, in: [1, 2, 3]
|
28
29
|
p.param :date_of_birth, Hash do |pp|
|
29
30
|
pp.param :gt, Date
|
30
31
|
pp.param :lt, Date
|
@@ -14,12 +14,13 @@ module ValidateParams
|
|
14
14
|
|
15
15
|
def param(field, type, options = {}, &block)
|
16
16
|
@params_validations ||= []
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
17
|
+
|
18
|
+
if block
|
19
|
+
param_builder = ParamBuilder.new(field)
|
20
|
+
@params_validations += yield(param_builder)
|
21
|
+
else
|
22
|
+
@params_validations << ParamBuilder.new.param(field, type, options)
|
23
|
+
end
|
23
24
|
end
|
24
25
|
|
25
26
|
def validate_params_for(request_action, &block)
|
@@ -64,6 +65,8 @@ module ValidateParams
|
|
64
65
|
|
65
66
|
if params_validation[:field].is_a?(Hash)
|
66
67
|
params_validation[:field].each_key do |key|
|
68
|
+
# Skip in case hash is configured and string is passed
|
69
|
+
next if params.dig(key).is_a? Hash
|
67
70
|
next if params.dig(key, params_validation[:field][key])
|
68
71
|
|
69
72
|
value = if params_validation[:options][:default].is_a?(Proc)
|
@@ -91,6 +94,10 @@ module ValidateParams
|
|
91
94
|
errors = []
|
92
95
|
|
93
96
|
for params_validation in params_validations
|
97
|
+
# Skip in case hash is configured and string is passed
|
98
|
+
next if params_validation[:field].is_a?(Hash) &&
|
99
|
+
params.dig(params_validation[:field].keys.first).is_a?(String)
|
100
|
+
|
94
101
|
parameter_value = if params_validation[:field].is_a? Hash
|
95
102
|
params.dig(params_validation[:field].keys.first,
|
96
103
|
params_validation[:field][params_validation[:field].keys.first])
|
@@ -119,7 +126,7 @@ module ValidateParams
|
|
119
126
|
}
|
120
127
|
end
|
121
128
|
when "Integer"
|
122
|
-
|
129
|
+
if invalid_integer?(parameter_value)
|
123
130
|
errors << {
|
124
131
|
message: build_error_message(error_param_name(params_validation[:field]), params_validation[:type])
|
125
132
|
}
|
@@ -146,7 +153,11 @@ module ValidateParams
|
|
146
153
|
|
147
154
|
return if errors.empty?
|
148
155
|
|
149
|
-
|
156
|
+
if request.nil? || request.format.json?
|
157
|
+
render json: { success: false, errors: errors }, status: :bad_request
|
158
|
+
else
|
159
|
+
head :bad_request
|
160
|
+
end
|
150
161
|
end
|
151
162
|
|
152
163
|
def invalid_date?(value)
|
@@ -174,15 +185,16 @@ module ValidateParams
|
|
174
185
|
class ParamBuilder
|
175
186
|
def initialize(parent_field = nil)
|
176
187
|
@parent_field = parent_field
|
188
|
+
@params_validations = []
|
177
189
|
end
|
178
190
|
|
179
191
|
def param(field, type, options = {})
|
180
|
-
|
181
|
-
|
182
|
-
{ field: { @parent_field => field }, type: type, options: options }
|
183
|
-
else
|
184
|
-
{ field: field, type: type, options: options }
|
192
|
+
unless @parent_field
|
193
|
+
return { field: field, type: type, options: options }
|
185
194
|
end
|
195
|
+
|
196
|
+
@params_validations << { field: { @parent_field => field }, type: type, options: options }
|
197
|
+
@params_validations
|
186
198
|
end
|
187
199
|
end
|
188
200
|
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.5.
|
4
|
+
version: 0.5.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- dcherevatenko
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-05-
|
11
|
+
date: 2023-05-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: actionpack
|