validate-params 0.4.1 → 0.5.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 +4 -4
- data/CHANGELOG.md +6 -0
- data/Gemfile +1 -1
- data/Gemfile.lock +3 -7
- data/README.md +4 -3
- data/config/locales/en.yml +3 -2
- data/lib/validate_params/params_validator.rb +74 -43
- data/lib/validate_params/version.rb +1 -1
- metadata +7 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1cc4a4179e78c2c12842ddac40a4745028bbbf7f5f90cc0031fcaa3ae5db1422
|
4
|
+
data.tar.gz: 33e7a943d361d305a14e1a4c6f0d97f8f20f078d89ee872857238db5b947ab90
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2d081f19216d1a04527a13366857f37a25c818398dffa4703540b4f806593942d75d6759b4a8892fc2512dd72fa6a882d3a5ef7f9b57ac178b6c160b5d26896f
|
7
|
+
data.tar.gz: c484a35d91d3b3d3ff7c96b301063bb497e25dfea5621b3db3ade98b36a5981cae8555b451eff9d9b8240a579826b762f91025a5f86859d4e33476e9325ccd86
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,11 @@
|
|
1
1
|
## [Unreleased]
|
2
2
|
|
3
|
+
## [0.5.0] - 2023-05-15
|
4
|
+
|
5
|
+
- Support for Proc as options for default to set param default values
|
6
|
+
- Add :in options to validate param values against a list of values for String and Integer types
|
7
|
+
- Updated JSON formats of error messages to be more consistent
|
8
|
+
|
3
9
|
## [0.3.0] - 2023-05-12
|
4
10
|
|
5
11
|
- 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.5.1)
|
5
5
|
activesupport (>= 6.1.0)
|
6
6
|
|
7
7
|
GEM
|
@@ -43,8 +43,6 @@ GEM
|
|
43
43
|
nokogiri (1.14.3)
|
44
44
|
mini_portile2 (~> 2.8.0)
|
45
45
|
racc (~> 1.4)
|
46
|
-
nokogiri (1.14.3-arm64-darwin)
|
47
|
-
racc (~> 1.4)
|
48
46
|
parallel (1.23.0)
|
49
47
|
parser (3.2.2.1)
|
50
48
|
ast (~> 2.4.1)
|
@@ -74,7 +72,7 @@ GEM
|
|
74
72
|
diff-lcs (>= 1.2.0, < 2.0)
|
75
73
|
rspec-support (~> 3.12.0)
|
76
74
|
rspec-support (3.12.0)
|
77
|
-
rubocop (1.
|
75
|
+
rubocop (1.51.0)
|
78
76
|
json (~> 2.3)
|
79
77
|
parallel (~> 1.10)
|
80
78
|
parser (>= 3.2.0.0)
|
@@ -93,8 +91,6 @@ GEM
|
|
93
91
|
zeitwerk (2.6.8)
|
94
92
|
|
95
93
|
PLATFORMS
|
96
|
-
arm64-darwin-21
|
97
|
-
arm64-darwin-22
|
98
94
|
ruby
|
99
95
|
|
100
96
|
DEPENDENCIES
|
@@ -102,7 +98,7 @@ DEPENDENCIES
|
|
102
98
|
bundler (~> 2.0)
|
103
99
|
rake (~> 13.0)
|
104
100
|
rspec (~> 3.0)
|
105
|
-
rubocop (~> 1.
|
101
|
+
rubocop (~> 1.51)
|
106
102
|
validate-params!
|
107
103
|
|
108
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
|
@@ -48,7 +49,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
48
49
|
|
49
50
|
## Contributing
|
50
51
|
|
51
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
52
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/peopleforce/validate_params.
|
52
53
|
|
53
54
|
## Credits
|
54
55
|
|
data/config/locales/en.yml
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
en:
|
2
2
|
validate_params:
|
3
|
-
required: "
|
4
|
-
|
3
|
+
required: "%{param} is required"
|
4
|
+
invalid_type: "%{param} must be a valid %{type}"
|
5
|
+
invalid_in: "%{param} is an invalid value"
|
@@ -12,14 +12,15 @@ module ValidateParams
|
|
12
12
|
class_methods do
|
13
13
|
attr_accessor :params_validations, :method
|
14
14
|
|
15
|
-
def param(field, type,
|
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)
|
@@ -39,12 +40,12 @@ module ValidateParams
|
|
39
40
|
|
40
41
|
private
|
41
42
|
|
42
|
-
def build_error_message(
|
43
|
-
I18n.t("validate_params.
|
43
|
+
def build_error_message(param, type)
|
44
|
+
I18n.t("validate_params.invalid_type", param: param, type: type)
|
44
45
|
end
|
45
46
|
|
46
|
-
def build_required_message(
|
47
|
-
I18n.t("validate_params.required",
|
47
|
+
def build_required_message(param)
|
48
|
+
I18n.t("validate_params.required", param: param)
|
48
49
|
end
|
49
50
|
|
50
51
|
def error_param_name(field)
|
@@ -60,16 +61,29 @@ module ValidateParams
|
|
60
61
|
|
61
62
|
def set_params_defaults
|
62
63
|
params_validations.each do |params_validation|
|
63
|
-
next if params_validation[:default].blank?
|
64
|
+
next if params_validation[:options][:default].blank?
|
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)
|
73
|
+
params_validation[:options][:default].call
|
74
|
+
else
|
75
|
+
params_validation[:options][:default]
|
76
|
+
end
|
77
|
+
params.merge!(key => { params_validation[:field][key] => value })
|
70
78
|
end
|
71
79
|
else
|
72
|
-
|
80
|
+
value = if params_validation[:options][:default].is_a?(Proc)
|
81
|
+
params_validation[:options][:default].call
|
82
|
+
else
|
83
|
+
params_validation[:options][:default]
|
84
|
+
end
|
85
|
+
|
86
|
+
params[params_validation[:field]] ||= value
|
73
87
|
end
|
74
88
|
end
|
75
89
|
end
|
@@ -80,6 +94,10 @@ module ValidateParams
|
|
80
94
|
errors = []
|
81
95
|
|
82
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
|
+
|
83
101
|
parameter_value = if params_validation[:field].is_a? Hash
|
84
102
|
params.dig(params_validation[:field].keys.first,
|
85
103
|
params_validation[:field][params_validation[:field].keys.first])
|
@@ -87,37 +105,48 @@ module ValidateParams
|
|
87
105
|
params[params_validation[:field]]
|
88
106
|
end
|
89
107
|
|
90
|
-
next if parameter_value.blank? && !params_validation[:required]
|
108
|
+
next if parameter_value.blank? && !params_validation[:options][:required]
|
91
109
|
|
92
|
-
if parameter_value.blank? && params_validation[:required]
|
93
|
-
errors << build_required_message(error_param_name(params_validation[:field]))
|
110
|
+
if parameter_value.blank? && params_validation[:options][:required]
|
111
|
+
errors << { message: build_required_message(error_param_name(params_validation[:field])) }
|
94
112
|
next
|
95
113
|
end
|
96
114
|
|
97
115
|
case params_validation[:type].to_s
|
98
116
|
when "Date"
|
99
117
|
if invalid_date?(parameter_value)
|
100
|
-
errors <<
|
101
|
-
error_param_name(params_validation[:field]),
|
102
|
-
|
103
|
-
parameter_value
|
104
|
-
)
|
118
|
+
errors << {
|
119
|
+
message: build_error_message(error_param_name(params_validation[:field]), params_validation[:type])
|
120
|
+
}
|
105
121
|
end
|
106
122
|
when "DateTime"
|
107
123
|
if invalid_datetime?(parameter_value)
|
108
|
-
errors <<
|
109
|
-
error_param_name(params_validation[:field]),
|
110
|
-
|
111
|
-
parameter_value
|
112
|
-
)
|
124
|
+
errors << {
|
125
|
+
message: build_error_message(error_param_name(params_validation[:field]), params_validation[:type])
|
126
|
+
}
|
113
127
|
end
|
114
128
|
when "Integer"
|
115
|
-
|
116
|
-
errors <<
|
117
|
-
error_param_name(params_validation[:field]),
|
118
|
-
|
119
|
-
|
120
|
-
|
129
|
+
if invalid_integer?(parameter_value)
|
130
|
+
errors << {
|
131
|
+
message: build_error_message(error_param_name(params_validation[:field]), params_validation[:type])
|
132
|
+
}
|
133
|
+
next
|
134
|
+
end
|
135
|
+
|
136
|
+
parameter_value = parameter_value.to_i
|
137
|
+
if params_validation[:options][:in].present? && !params_validation[:options][:in].include?(parameter_value)
|
138
|
+
errors << {
|
139
|
+
message: I18n.t("validate_params.invalid_in", param: error_param_name(params_validation[:field])),
|
140
|
+
valid_values: params_validation[:options][:in]
|
141
|
+
}
|
142
|
+
end
|
143
|
+
when "String"
|
144
|
+
parameter_value = parameter_value.to_s
|
145
|
+
if params_validation[:options][:in].present? && !params_validation[:options][:in].include?(parameter_value)
|
146
|
+
errors << {
|
147
|
+
message: I18n.t("validate_params.invalid_in", param: error_param_name(params_validation[:field])),
|
148
|
+
valid_values: params_validation[:options][:in]
|
149
|
+
}
|
121
150
|
end
|
122
151
|
end
|
123
152
|
end
|
@@ -131,10 +160,10 @@ module ValidateParams
|
|
131
160
|
return true unless /\d{4}-\d{2}-\d{2}/.match?(value)
|
132
161
|
|
133
162
|
parsed_date = begin
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
163
|
+
Date.strptime(value, "%Y-%m-%d")
|
164
|
+
rescue StandardError
|
165
|
+
nil
|
166
|
+
end
|
138
167
|
parsed_date.blank? || parsed_date.year > 9999
|
139
168
|
end
|
140
169
|
|
@@ -146,20 +175,22 @@ module ValidateParams
|
|
146
175
|
end
|
147
176
|
|
148
177
|
def invalid_integer?(value)
|
149
|
-
value !~ /\A[-+]?[0-9]+\z/
|
178
|
+
value.to_s !~ /\A[-+]?[0-9]+\z/
|
150
179
|
end
|
151
180
|
|
152
181
|
class ParamBuilder
|
153
182
|
def initialize(parent_field = nil)
|
154
183
|
@parent_field = parent_field
|
184
|
+
@params_validations = []
|
155
185
|
end
|
156
186
|
|
157
|
-
def param(field, type,
|
158
|
-
|
159
|
-
{ field:
|
160
|
-
else
|
161
|
-
{ field: field, type: type, required: required, default: default }
|
187
|
+
def param(field, type, options = {})
|
188
|
+
unless @parent_field
|
189
|
+
return { field: field, type: type, options: options }
|
162
190
|
end
|
191
|
+
|
192
|
+
@params_validations << { field: { @parent_field => field }, type: type, options: options }
|
193
|
+
@params_validations
|
163
194
|
end
|
164
195
|
end
|
165
196
|
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.5.1
|
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-05-
|
11
|
+
date: 2023-05-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: actionpack
|
@@ -100,11 +100,11 @@ files:
|
|
100
100
|
- lib/validate_params/version.rb
|
101
101
|
- sig/validate_params.rbs
|
102
102
|
- validate_params.gemspec
|
103
|
-
homepage:
|
103
|
+
homepage:
|
104
104
|
licenses:
|
105
105
|
- MIT
|
106
106
|
metadata: {}
|
107
|
-
post_install_message:
|
107
|
+
post_install_message:
|
108
108
|
rdoc_options: []
|
109
109
|
require_paths:
|
110
110
|
- lib
|
@@ -119,8 +119,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
119
119
|
- !ruby/object:Gem::Version
|
120
120
|
version: '0'
|
121
121
|
requirements: []
|
122
|
-
rubygems_version: 3.
|
123
|
-
signing_key:
|
122
|
+
rubygems_version: 3.2.3
|
123
|
+
signing_key:
|
124
124
|
specification_version: 4
|
125
125
|
summary: Gem to validate params in controllers
|
126
126
|
test_files: []
|