validate-params 0.5.2 → 0.6.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/Gemfile.lock +1 -1
- data/README.md +18 -1
- data/lib/validate_params/types/date.rb +19 -0
- data/lib/validate_params/types/date_time.rb +12 -0
- data/lib/validate_params/types/integer.rb +10 -0
- data/lib/validate_params/{params_validator.rb → validatable.rb} +23 -35
- data/lib/validate_params/validators/date.rb +0 -0
- data/lib/validate_params/version.rb +1 -1
- data/lib/validate_params.rb +4 -2
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: '08d564962e2a87e458df54cfaa8682f1495e83b5c17762517a9065ad79021b59'
|
4
|
+
data.tar.gz: 167f2b5dc20f1d028973596a4f365bb5a5c2bbc1b067a6a938d31c8ab906cc9b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 40b1206f8a9fe7e18c54509530186f01143cfef09a8b1eb36853001c55984133b4ddee940238fe98e87b41407e218899d3ad030b7877a3b964b37f1848157076
|
7
|
+
data.tar.gz: db14ee08edf2d3a9f0127053596c2e3e3032817c143a935dc51418139b3d2facf133c76945445a990803258c95ee4da23f2a05007c189c8c23c54ae3ff8fd1e6
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -20,7 +20,7 @@ Definition of the validator with symbols as keys:
|
|
20
20
|
|
21
21
|
```ruby
|
22
22
|
class TestController < ActionController::Base
|
23
|
-
include ValidateParams::
|
23
|
+
include ValidateParams::Validatable
|
24
24
|
|
25
25
|
validate_params :index do |p|
|
26
26
|
p.param :name, String, default: "John Doe"
|
@@ -41,6 +41,23 @@ class TestController < ActionController::Base
|
|
41
41
|
end
|
42
42
|
end
|
43
43
|
```
|
44
|
+
|
45
|
+
## Response
|
46
|
+
|
47
|
+
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.
|
48
|
+
|
49
|
+
## Format
|
50
|
+
|
51
|
+
By default responses are returned in JSON format. To return responses as an empty HTML response, change the :format options in the validate_params methods to :html.
|
52
|
+
|
53
|
+
Example:
|
54
|
+
|
55
|
+
```ruby
|
56
|
+
validate_params :index, format: :html do |p|
|
57
|
+
p.param :name, String, default: "John Doe"
|
58
|
+
end
|
59
|
+
```
|
60
|
+
|
44
61
|
## Development
|
45
62
|
|
46
63
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module ValidateParams
|
2
|
+
class Types
|
3
|
+
class Date
|
4
|
+
def self.valid?(value)
|
5
|
+
value = value.to_s
|
6
|
+
return false if !/\d{4}-\d{2}-\d{2}/.match?(value)
|
7
|
+
|
8
|
+
parsed_date = begin
|
9
|
+
::Date.strptime(value, "%Y-%m-%d")
|
10
|
+
rescue StandardError
|
11
|
+
nil
|
12
|
+
end
|
13
|
+
return false if parsed_date.nil?
|
14
|
+
return false if parsed_date.year > 9999
|
15
|
+
true
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -1,7 +1,11 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "validate_params/types/date"
|
4
|
+
require "validate_params/types/date_time"
|
5
|
+
require "validate_params/types/integer"
|
2
6
|
|
3
7
|
module ValidateParams
|
4
|
-
module
|
8
|
+
module Validatable
|
5
9
|
extend ::ActiveSupport::Concern
|
6
10
|
|
7
11
|
included do
|
@@ -23,8 +27,9 @@ module ValidateParams
|
|
23
27
|
end
|
24
28
|
end
|
25
29
|
|
26
|
-
def validate_params_for(
|
27
|
-
@
|
30
|
+
def validate_params_for(controller_action, options = {}, &block)
|
31
|
+
@controller_action = controller_action
|
32
|
+
@response_format = options[:format] || :html
|
28
33
|
|
29
34
|
yield(self) if block
|
30
35
|
end
|
@@ -34,8 +39,12 @@ module ValidateParams
|
|
34
39
|
self.class.instance_variable_get(:@params_validations) || []
|
35
40
|
end
|
36
41
|
|
37
|
-
def
|
38
|
-
self.class.instance_variable_get(:@
|
42
|
+
def controller_action
|
43
|
+
self.class.instance_variable_get(:@controller_action) || nil
|
44
|
+
end
|
45
|
+
|
46
|
+
def response_format
|
47
|
+
self.class.instance_variable_get(:@response_format) || nil
|
39
48
|
end
|
40
49
|
|
41
50
|
private
|
@@ -89,7 +98,7 @@ module ValidateParams
|
|
89
98
|
end
|
90
99
|
|
91
100
|
def perform_validate_params
|
92
|
-
return unless
|
101
|
+
return unless controller_action.present? && controller_action == action_name.to_sym
|
93
102
|
|
94
103
|
errors = []
|
95
104
|
|
@@ -114,19 +123,19 @@ module ValidateParams
|
|
114
123
|
|
115
124
|
case params_validation[:type].to_s
|
116
125
|
when "Date"
|
117
|
-
|
126
|
+
unless ValidateParams::Types::Date.valid?(parameter_value)
|
118
127
|
errors << {
|
119
128
|
message: build_error_message(error_param_name(params_validation[:field]), params_validation[:type])
|
120
129
|
}
|
121
130
|
end
|
122
131
|
when "DateTime"
|
123
|
-
|
132
|
+
unless ValidateParams::Types::DateTime.valid?(parameter_value)
|
124
133
|
errors << {
|
125
134
|
message: build_error_message(error_param_name(params_validation[:field]), params_validation[:type])
|
126
135
|
}
|
127
136
|
end
|
128
137
|
when "Integer"
|
129
|
-
|
138
|
+
unless ValidateParams::Types::Integer.valid?(parameter_value)
|
130
139
|
errors << {
|
131
140
|
message: build_error_message(error_param_name(params_validation[:field]), params_validation[:type])
|
132
141
|
}
|
@@ -153,35 +162,14 @@ module ValidateParams
|
|
153
162
|
|
154
163
|
return if errors.empty?
|
155
164
|
|
156
|
-
|
157
|
-
|
158
|
-
else
|
165
|
+
case response_format
|
166
|
+
when :html
|
159
167
|
head :bad_request
|
168
|
+
else
|
169
|
+
render json: { success: false, errors: errors }, status: :bad_request
|
160
170
|
end
|
161
171
|
end
|
162
172
|
|
163
|
-
def invalid_date?(value)
|
164
|
-
return true unless /\d{4}-\d{2}-\d{2}/.match?(value)
|
165
|
-
|
166
|
-
parsed_date = begin
|
167
|
-
Date.strptime(value, "%Y-%m-%d")
|
168
|
-
rescue StandardError
|
169
|
-
nil
|
170
|
-
end
|
171
|
-
parsed_date.blank? || parsed_date.year > 9999
|
172
|
-
end
|
173
|
-
|
174
|
-
def invalid_datetime?(value)
|
175
|
-
Time.at(Integer(value))
|
176
|
-
false
|
177
|
-
rescue ArgumentError, TypeError
|
178
|
-
true
|
179
|
-
end
|
180
|
-
|
181
|
-
def invalid_integer?(value)
|
182
|
-
value.to_s !~ /\A[-+]?[0-9]+\z/
|
183
|
-
end
|
184
|
-
|
185
173
|
class ParamBuilder
|
186
174
|
def initialize(parent_field = nil)
|
187
175
|
@parent_field = parent_field
|
File without changes
|
data/lib/validate_params.rb
CHANGED
@@ -1,12 +1,14 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require_relative "validate_params/version"
|
4
|
-
require_relative "validate_params/
|
4
|
+
require_relative "validate_params/validatable"
|
5
|
+
Dir[File.join(__dir__, 'validate_params/types', '*.rb')].each { |file| require file }
|
6
|
+
Dir[File.join(__dir__, 'validate_params/validators', '*.rb')].each { |file| require file }
|
5
7
|
|
6
8
|
module ValidateParams
|
7
9
|
class Error < StandardError; end
|
8
10
|
end
|
9
11
|
|
10
12
|
ActiveSupport.on_load(:action_controller) do
|
11
|
-
include ValidateParams::
|
13
|
+
include ValidateParams::Validatable
|
12
14
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: validate-params
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- dcherevatenko
|
@@ -96,7 +96,11 @@ files:
|
|
96
96
|
- Rakefile
|
97
97
|
- config/locales/en.yml
|
98
98
|
- lib/validate_params.rb
|
99
|
-
- lib/validate_params/
|
99
|
+
- lib/validate_params/types/date.rb
|
100
|
+
- lib/validate_params/types/date_time.rb
|
101
|
+
- lib/validate_params/types/integer.rb
|
102
|
+
- lib/validate_params/validatable.rb
|
103
|
+
- lib/validate_params/validators/date.rb
|
100
104
|
- lib/validate_params/version.rb
|
101
105
|
- sig/validate_params.rbs
|
102
106
|
- validate_params.gemspec
|