validate-params 0.5.1 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1cc4a4179e78c2c12842ddac40a4745028bbbf7f5f90cc0031fcaa3ae5db1422
4
- data.tar.gz: 33e7a943d361d305a14e1a4c6f0d97f8f20f078d89ee872857238db5b947ab90
3
+ metadata.gz: '08d564962e2a87e458df54cfaa8682f1495e83b5c17762517a9065ad79021b59'
4
+ data.tar.gz: 167f2b5dc20f1d028973596a4f365bb5a5c2bbc1b067a6a938d31c8ab906cc9b
5
5
  SHA512:
6
- metadata.gz: 2d081f19216d1a04527a13366857f37a25c818398dffa4703540b4f806593942d75d6759b4a8892fc2512dd72fa6a882d3a5ef7f9b57ac178b6c160b5d26896f
7
- data.tar.gz: c484a35d91d3b3d3ff7c96b301063bb497e25dfea5621b3db3ade98b36a5981cae8555b451eff9d9b8240a579826b762f91025a5f86859d4e33476e9325ccd86
6
+ metadata.gz: 40b1206f8a9fe7e18c54509530186f01143cfef09a8b1eb36853001c55984133b4ddee940238fe98e87b41407e218899d3ad030b7877a3b964b37f1848157076
7
+ data.tar.gz: db14ee08edf2d3a9f0127053596c2e3e3032817c143a935dc51418139b3d2facf133c76945445a990803258c95ee4da23f2a05007c189c8c23c54ae3ff8fd1e6
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
- - Add :in options to validate param values against a list of values for String and Integer types
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.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- validate-params (0.5.1)
4
+ validate-params (0.6.0)
5
5
  activesupport (>= 6.1.0)
6
6
 
7
7
  GEM
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::ParamsValidator
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
@@ -0,0 +1,12 @@
1
+ module ValidateParams
2
+ class Types
3
+ class DateTime
4
+ def self.valid?(value)
5
+ Time.at(Integer(value))
6
+ true
7
+ rescue ArgumentError, TypeError
8
+ false
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,10 @@
1
+ module ValidateParams
2
+ class Types
3
+ class Integer
4
+ def self.valid?(value)
5
+ value = value.to_s
6
+ /\A[-+]?\d+\z/ === value
7
+ end
8
+ end
9
+ end
10
+ end
@@ -1,7 +1,11 @@
1
- require "active_support/concern"
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 ParamsValidator
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(request_action, &block)
27
- @request_action = request_action
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 request_action
38
- self.class.instance_variable_get(:@request_action) || nil
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 request_action.present? && request_action == action_name.to_sym
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
- if invalid_date?(parameter_value)
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
- if invalid_datetime?(parameter_value)
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
- if invalid_integer?(parameter_value)
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,29 +162,12 @@ module ValidateParams
153
162
 
154
163
  return if errors.empty?
155
164
 
156
- render json: { success: false, errors: errors }, status: :unprocessable_entity
157
- end
158
-
159
- def invalid_date?(value)
160
- return true unless /\d{4}-\d{2}-\d{2}/.match?(value)
161
-
162
- parsed_date = begin
163
- Date.strptime(value, "%Y-%m-%d")
164
- rescue StandardError
165
- nil
166
- end
167
- parsed_date.blank? || parsed_date.year > 9999
168
- end
169
-
170
- def invalid_datetime?(value)
171
- Time.at(Integer(value))
172
- false
173
- rescue ArgumentError, TypeError
174
- true
175
- end
176
-
177
- def invalid_integer?(value)
178
- value.to_s !~ /\A[-+]?[0-9]+\z/
165
+ case response_format
166
+ when :html
167
+ head :bad_request
168
+ else
169
+ render json: { success: false, errors: errors }, status: :bad_request
170
+ end
179
171
  end
180
172
 
181
173
  class ParamBuilder
File without changes
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ValidateParams
4
- VERSION = "0.5.1"
4
+ VERSION = "0.6.0"
5
5
  end
@@ -1,12 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "validate_params/version"
4
- require_relative "validate_params/params_validator"
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::ParamsValidator
13
+ include ValidateParams::Validatable
12
14
  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.1
4
+ version: 0.6.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-05-15 00:00:00.000000000 Z
11
+ date: 2023-05-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: actionpack
@@ -96,15 +96,19 @@ files:
96
96
  - Rakefile
97
97
  - config/locales/en.yml
98
98
  - lib/validate_params.rb
99
- - lib/validate_params/params_validator.rb
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
103
- homepage:
107
+ homepage:
104
108
  licenses:
105
109
  - MIT
106
110
  metadata: {}
107
- post_install_message:
111
+ post_install_message:
108
112
  rdoc_options: []
109
113
  require_paths:
110
114
  - lib
@@ -119,8 +123,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
119
123
  - !ruby/object:Gem::Version
120
124
  version: '0'
121
125
  requirements: []
122
- rubygems_version: 3.2.3
123
- signing_key:
126
+ rubygems_version: 3.4.10
127
+ signing_key:
124
128
  specification_version: 4
125
129
  summary: Gem to validate params in controllers
126
130
  test_files: []