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 +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +12 -0
- data/lib/validate_params/types/date.rb +1 -1
- data/lib/validate_params/types/date_time.rb +1 -1
- data/lib/validate_params/types/integer.rb +1 -1
- data/lib/validate_params/types/string.rb +1 -1
- data/lib/validate_params/validatable.rb +12 -19
- data/lib/validate_params/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c8a3bd014c84bdbaf778f5fc1e3c5b1e666fcf1ff9ab95107b509defffb42dd8
|
4
|
+
data.tar.gz: e2e3a30f811d7551df3f863ca54c5347f6f3c8c729e494d3649e67b71580e8ea
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 45b0c42c93eea9f538c52a1fd90df22d6b0dab828eec6f6be89359f8f52d4c287507547247c5f05c4859099c4dab73b04085a269c44972190fa5b2ab12235d0f
|
7
|
+
data.tar.gz: 86250638fe0a6963d71ee7b6842cd754f95ddccff639e5caed9f6acdc4eeb1caaed13d3ccedfc361220b6afc755cc005942cf98f3ca5809e27e86d29a1d31b94
|
data/Gemfile.lock
CHANGED
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.
|
@@ -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]
|
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
|
-
|
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]
|
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.
|
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]] =
|
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
|