validate-params 0.7.0 → 0.7.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7626ebe2f24591c008739bce05f28938e39094dc057ce65b227e7f99130c21cc
4
- data.tar.gz: 10f86e031e06e0c8723792d399881d4cc9baa64d9841eca1a1123c13606519ea
3
+ metadata.gz: c8a3bd014c84bdbaf778f5fc1e3c5b1e666fcf1ff9ab95107b509defffb42dd8
4
+ data.tar.gz: e2e3a30f811d7551df3f863ca54c5347f6f3c8c729e494d3649e67b71580e8ea
5
5
  SHA512:
6
- metadata.gz: b90773a69bdb7d7a8f65df932f78c3c0da3b4937540d4da8ca89695a0113548dffbe45760ff49ee0d6e89b718fb1d689c3f778a0887562290d2e5092956e13a7
7
- data.tar.gz: 18a1f5f2d8b0aec4210e8ad50f9d1a715e6f1aa01ab7cf74af77c4fffdbbe4b1f0c16a38b9b57a1df2a83cb3834bb7a9d908ff1b981a71206131e2c4ffaa7726
6
+ metadata.gz: 45b0c42c93eea9f538c52a1fd90df22d6b0dab828eec6f6be89359f8f52d4c287507547247c5f05c4859099c4dab73b04085a269c44972190fa5b2ab12235d0f
7
+ data.tar.gz: 86250638fe0a6963d71ee7b6842cd754f95ddccff639e5caed9f6acdc4eeb1caaed13d3ccedfc361220b6afc755cc005942cf98f3ca5809e27e86d29a1d31b94
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- validate-params (0.7.0)
4
+ validate-params (0.7.1)
5
5
  activesupport (>= 6.1.0)
6
6
  i18n (>= 1.6)
7
7
 
data/README.md CHANGED
@@ -48,6 +48,18 @@ end
48
48
 
49
49
  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
50
 
51
+ ```json
52
+ {
53
+ "success": false,
54
+ "errors": [
55
+ {
56
+ "message": "hired_on must be a valid Date"
57
+ },
58
+
59
+ ]
60
+ }
61
+ ```
62
+
51
63
  ## Format
52
64
 
53
65
  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.
@@ -20,7 +20,7 @@ module ValidateParams
20
20
  true
21
21
  end
22
22
 
23
- def self.cast(raw_value)
23
+ def self.cast(raw_value, **)
24
24
  return raw_value if raw_value.is_a?(::Date)
25
25
 
26
26
  ::Date.strptime(raw_value.to_s, FORMAT)
@@ -10,7 +10,7 @@ module ValidateParams
10
10
  false
11
11
  end
12
12
 
13
- def self.cast(raw_value)
13
+ def self.cast(raw_value, **)
14
14
  return raw_value if raw_value.is_a?(::Time)
15
15
 
16
16
  Time.at(Integer(raw_value))
@@ -7,7 +7,7 @@ module ValidateParams
7
7
  /\A[-+]?\d+\z/ === value.to_s
8
8
  end
9
9
 
10
- def self.cast(raw_value)
10
+ def self.cast(raw_value, **)
11
11
  raw_value.to_i
12
12
  end
13
13
  end
@@ -3,7 +3,7 @@
3
3
  module ValidateParams
4
4
  class Types
5
5
  class String
6
- def self.cast(raw_value)
6
+ def self.cast(raw_value, **)
7
7
  raw_value.to_s
8
8
  end
9
9
  end
@@ -60,7 +60,7 @@ module ValidateParams
60
60
  if params_validation[:field].is_a?(Hash)
61
61
  params_validation[:field].each_key do |key|
62
62
  # Skip in case hash is configured and string is passed
63
- next if params[key].is_a? Hash
63
+ next if hashlike?(params[key])
64
64
  next if params.dig(key, params_validation[:field][key])
65
65
 
66
66
  value = if params_validation[:options][:default].is_a?(Proc)
@@ -68,7 +68,9 @@ module ValidateParams
68
68
  else
69
69
  params_validation[:options][:default]
70
70
  end
71
- params.deep_merge!(key => { params_validation[:field][key] => value })
71
+
72
+ params[key] ||= {}
73
+ params[key][params_validation[:field][key]] = value
72
74
  end
73
75
  else
74
76
  value = if params_validation[:options][:default].is_a?(Proc)
@@ -78,40 +80,27 @@ module ValidateParams
78
80
  end
79
81
 
80
82
  params[params_validation[:field]] ||= value
81
-
82
83
  end
83
84
  end
84
85
  end
85
86
 
86
87
  def cast_param_values
88
+ ActionController::Parameters
87
89
  params_validations.each do |params_validation|
88
90
  if params_validation[:field].is_a?(Hash)
89
91
  params_validation[:field].each_key do |key|
90
- next unless params[key].is_a?(Hash)
92
+ next unless hashlike?(params[key])
91
93
 
92
94
  value = params.dig(key, params_validation[:field][key])
93
95
  next if value.blank?
94
96
 
95
- params.deep_merge!(
96
- key => {
97
- params_validation[:field][key] => if params_validation[:type].name == "Array"
98
- Types.const_get(params_validation[:type].name).cast(value,
99
- of: params_validation[:options][:of])
100
- else
101
- Types.const_get(params_validation[:type].name).cast(value)
102
- end
103
- }
104
- )
97
+ params[key][params_validation[:field][key]] = Types.const_get(params_validation[:type].name).cast(value, of: params_validation.dig(:options, :of))
105
98
  end
106
99
  else
107
100
  value = params[params_validation[:field]]
108
101
  next if value.blank?
109
102
 
110
- params[params_validation[:field]] = if params_validation[:type].name == "Array"
111
- Types.const_get(params_validation[:type].name).cast(value, of: params_validation[:options][:of])
112
- else
113
- Types.const_get(params_validation[:type].name).cast(value)
114
- end
103
+ params[params_validation[:field]] = Types.const_get(params_validation[:type].name).cast(value, of: params_validation.dig(:options, :of))
115
104
  end
116
105
  end
117
106
  end
@@ -154,5 +143,9 @@ module ValidateParams
154
143
  render json: { success: false, errors: errors }, status: :bad_request
155
144
  end
156
145
  end
146
+
147
+ def hashlike?(obj)
148
+ obj.is_a?(Hash) || obj.is_a?(ActionController::Parameters)
149
+ end
157
150
  end
158
151
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ValidateParams
4
- VERSION = "0.7.0"
4
+ VERSION = "0.7.1"
5
5
  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.7.0
4
+ version: 0.7.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - dcherevatenko