request_params_validation 0.1.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 +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +557 -0
- data/Rakefile +32 -0
- data/lib/request_params_validation.rb +346 -0
- data/lib/request_params_validation/definitions.rb +57 -0
- data/lib/request_params_validation/definitions/action.rb +23 -0
- data/lib/request_params_validation/definitions/param.rb +182 -0
- data/lib/request_params_validation/definitions/request.rb +31 -0
- data/lib/request_params_validation/definitions/resource.rb +30 -0
- data/lib/request_params_validation/engine.rb +18 -0
- data/lib/request_params_validation/exceptions/base_errors.rb +10 -0
- data/lib/request_params_validation/exceptions/definitions_errors.rb +31 -0
- data/lib/request_params_validation/exceptions/validator_errors.rb +49 -0
- data/lib/request_params_validation/handler.rb +26 -0
- data/lib/request_params_validation/helpers.rb +17 -0
- data/lib/request_params_validation/params.rb +60 -0
- data/lib/request_params_validation/params/constants.rb +22 -0
- data/lib/request_params_validation/params/converter.rb +33 -0
- data/lib/request_params_validation/params/types/conversions.rb +41 -0
- data/lib/request_params_validation/params/types/validations.rb +57 -0
- data/lib/request_params_validation/params/validator.rb +74 -0
- data/lib/request_params_validation/params/validators/custom.rb +18 -0
- data/lib/request_params_validation/params/validators/format.rb +27 -0
- data/lib/request_params_validation/params/validators/inclusion.rb +27 -0
- data/lib/request_params_validation/params/validators/length.rb +37 -0
- data/lib/request_params_validation/params/validators/presence.rb +13 -0
- data/lib/request_params_validation/params/validators/type.rb +59 -0
- data/lib/request_params_validation/params/validators/value.rb +37 -0
- data/lib/request_params_validation/version.rb +3 -0
- metadata +87 -0
data/Rakefile
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
require 'rdoc/task'
|
8
|
+
|
9
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
+
rdoc.rdoc_dir = 'rdoc'
|
11
|
+
rdoc.title = 'RequestParamsValidation'
|
12
|
+
rdoc.options << '--line-numbers'
|
13
|
+
rdoc.rdoc_files.include('README.md')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
APP_RAKEFILE = File.expand_path("spec/dummy/Rakefile", __dir__)
|
18
|
+
load 'rails/tasks/engine.rake'
|
19
|
+
|
20
|
+
load 'rails/tasks/statistics.rake'
|
21
|
+
|
22
|
+
require 'bundler/gem_tasks'
|
23
|
+
|
24
|
+
require 'rake/testtask'
|
25
|
+
|
26
|
+
Rake::TestTask.new(:test) do |t|
|
27
|
+
t.libs << 'test'
|
28
|
+
t.pattern = 'test/**/*_test.rb'
|
29
|
+
t.verbose = false
|
30
|
+
end
|
31
|
+
|
32
|
+
task default: :test
|
@@ -0,0 +1,346 @@
|
|
1
|
+
require 'request_params_validation/engine'
|
2
|
+
require 'request_params_validation/exceptions/validator_errors'
|
3
|
+
|
4
|
+
module RequestParamsValidation
|
5
|
+
|
6
|
+
# Define helper_method_name for specifing the name for the helper method
|
7
|
+
# in charge of validating the request params.
|
8
|
+
#
|
9
|
+
# The default name is :validate_params!.
|
10
|
+
mattr_accessor :helper_method_name
|
11
|
+
self.helper_method_name = :validate_params!
|
12
|
+
|
13
|
+
|
14
|
+
# Specify the path starting from `Rails.root` where is going
|
15
|
+
# to be the request params definitions files.
|
16
|
+
#
|
17
|
+
# Example:
|
18
|
+
# - config.definitions_path = :definitions
|
19
|
+
# That means the params definitions should be in: "#{Rails.root}/definitions/**/*"
|
20
|
+
#
|
21
|
+
# The default path is :'app/definitions'.
|
22
|
+
mattr_accessor :definitions_path
|
23
|
+
self.definitions_path = :'app/definitions'
|
24
|
+
|
25
|
+
|
26
|
+
# Specify the suffix for your definitions files names.
|
27
|
+
#
|
28
|
+
# Example:
|
29
|
+
# - config.definitions_suffix = ''
|
30
|
+
# That means that the definition file for, i.e., a UsersController will be:
|
31
|
+
# "app/definitions/users.rb" instead of "app/definitions/users_definition.rb"
|
32
|
+
#
|
33
|
+
# The default suffix is :_definition.
|
34
|
+
mattr_accessor :definitions_suffix
|
35
|
+
self.definitions_suffix = :_definition
|
36
|
+
|
37
|
+
|
38
|
+
# The on_definition_not_found option accepts two values: :nothing or :raise.
|
39
|
+
# - :raise means that if the definition for a controller/action can not be found,
|
40
|
+
# it will raise RequestParamsValidation::DefinitionNotFound. This is useful when
|
41
|
+
# you want to validate all requests and be sure that no request will be
|
42
|
+
# executed without running validations.
|
43
|
+
# - :nothing will do nothing and skip the validation for that controller/action.
|
44
|
+
#
|
45
|
+
# The default value is :nothing.
|
46
|
+
mattr_accessor :on_definition_not_found
|
47
|
+
self.on_definition_not_found = :nothing
|
48
|
+
|
49
|
+
|
50
|
+
# Set the filter_params to true if you want to overwrite the params and
|
51
|
+
# stay with only those that are specified in the current controller/action definition.
|
52
|
+
# If params is an ActionController::Parameters, it sets the permitted attribute to true.
|
53
|
+
#
|
54
|
+
# The default value is true.
|
55
|
+
mattr_accessor :filter_params
|
56
|
+
self.filter_params = true
|
57
|
+
|
58
|
+
|
59
|
+
# If you want for some reason to save the original params in an instance variable before
|
60
|
+
# they get filtered, here you can set that variable.
|
61
|
+
#
|
62
|
+
# Example:
|
63
|
+
# - config.save_original_params = :@original_params
|
64
|
+
#
|
65
|
+
# The default value is false.
|
66
|
+
mattr_accessor :save_original_params
|
67
|
+
self.save_original_params = false
|
68
|
+
|
69
|
+
|
70
|
+
# Rails automatically adds the keys 'controller' and 'action' to the params object. The gem
|
71
|
+
# maintains those keys, but if you feel the need to remove them, you can set this option
|
72
|
+
# as follow.
|
73
|
+
#
|
74
|
+
# Example:
|
75
|
+
# - config.remove_keys_from_params = %i(controller action)
|
76
|
+
#
|
77
|
+
# The default value is [].
|
78
|
+
mattr_accessor :remove_keys_from_params
|
79
|
+
self.remove_keys_from_params = []
|
80
|
+
|
81
|
+
|
82
|
+
# Extension configuration goes here. Here you can extend behaviours and
|
83
|
+
# default values of the gem.
|
84
|
+
module ExtensionConfiguration
|
85
|
+
|
86
|
+
# Set a module with all your custom types you want the gem to manage.
|
87
|
+
# For example, if you want to add a custom type named "cellphone", you just need to
|
88
|
+
# create a module which defines the method "valid_cellphone?(value)" and pass that module
|
89
|
+
# to this configuration variable.
|
90
|
+
#
|
91
|
+
# You can also overwrite the existing type validation methods if you feel the
|
92
|
+
# need to change the behaviour of a certain type.
|
93
|
+
#
|
94
|
+
# Note 1: the module should be in Rails autoload_paths or you would need to
|
95
|
+
# require it or autoload it before setting it.
|
96
|
+
# Note 2: the defined methods should be named as `valid_#{type}?(value)`.
|
97
|
+
# Note 3: the defined methods should return true or false.
|
98
|
+
#
|
99
|
+
# Example:
|
100
|
+
# - extend.types = Utils::ValidationTypes
|
101
|
+
#
|
102
|
+
# The default value is false.
|
103
|
+
mattr_accessor :types
|
104
|
+
self.types = false
|
105
|
+
|
106
|
+
|
107
|
+
# Add boolean true values that you want the gem to manage, behalf the default ones,
|
108
|
+
# true and 'true'. For example, you can add the integer 1 or the string 't' or 'y' etc.
|
109
|
+
#
|
110
|
+
# Example:
|
111
|
+
# - extend.boolean_true_values = [1, 't']
|
112
|
+
#
|
113
|
+
# The default value is []
|
114
|
+
mattr_accessor :boolean_true_values
|
115
|
+
self.boolean_true_values = []
|
116
|
+
|
117
|
+
|
118
|
+
# Add boolean false values that you want the gem to manage, behalf the default ones,
|
119
|
+
# false and 'false'. For example, you can add the integer 0 or the string 'f' or 'n' etc.
|
120
|
+
#
|
121
|
+
# Example:
|
122
|
+
# - extend.boolean_false_values = [0, 'f']
|
123
|
+
#
|
124
|
+
# The default value is []
|
125
|
+
mattr_accessor :boolean_false_values
|
126
|
+
self.boolean_false_values = []
|
127
|
+
end
|
128
|
+
|
129
|
+
|
130
|
+
# Formats configuration goes here. Here you can set the formats of different types, so this
|
131
|
+
# configuration apply to all your definitions, avoiding to do it on each parameter.
|
132
|
+
#
|
133
|
+
# Notice that the local configuration of a definition will have more precedence
|
134
|
+
# than this global ones.
|
135
|
+
module FormatsConfiguration
|
136
|
+
|
137
|
+
# Specify the format for the type "date". If not format is specified
|
138
|
+
# then the Date.parse method will be used for the validation. Otherwise, it
|
139
|
+
# will valid the format given using Date.strptime method.
|
140
|
+
#
|
141
|
+
# Note: The option "format" or "format -> strptime" on a date param definition will
|
142
|
+
# locally override this configuration for that parameter.
|
143
|
+
#
|
144
|
+
# Example:
|
145
|
+
# - formats.date = '%Y-%m-%d'
|
146
|
+
#
|
147
|
+
# The default value is nil.
|
148
|
+
mattr_accessor :date
|
149
|
+
self.date = nil
|
150
|
+
|
151
|
+
|
152
|
+
# Specify the format for the type "datetime". If not format is specified
|
153
|
+
# then the DateTime.parse method will be used for the validation. Otherwise, it
|
154
|
+
# will valid the format given using DateTime.strptime method.
|
155
|
+
#
|
156
|
+
# Note: The option "format" or "format -> strptime" on a datetime param definition will
|
157
|
+
# locally override this configuration for that parameter.
|
158
|
+
#
|
159
|
+
# Example:
|
160
|
+
# - formats.datetime = '%Y-%m-%dT%H:%M:%S%z'
|
161
|
+
#
|
162
|
+
# The default value is nil.
|
163
|
+
mattr_accessor :datetime
|
164
|
+
self.datetime = nil
|
165
|
+
|
166
|
+
|
167
|
+
# Specify the precision for the type "decimal". This option will not validate the decimal
|
168
|
+
# precision of the requests, but it will round those decimal parameters to the specified
|
169
|
+
# here when the value get converted to the right type.
|
170
|
+
#
|
171
|
+
# Note: The option "precision" on a decimal param definition will locally override this
|
172
|
+
# configuration for that parameter.
|
173
|
+
#
|
174
|
+
# The default value is nil. That means, no round.
|
175
|
+
mattr_accessor :decimal_precision
|
176
|
+
self.decimal_precision = nil
|
177
|
+
end
|
178
|
+
|
179
|
+
|
180
|
+
# Exceptions configuration go here. You can set your own exception class for each
|
181
|
+
# type of error. Having full control of the exception raised and the message of it.
|
182
|
+
#
|
183
|
+
# Notice that all default exceptions have getters for accesing data related to the
|
184
|
+
# failure. You can also set custom messages details for the validations. See the
|
185
|
+
# documentation for further details.
|
186
|
+
#
|
187
|
+
# Note that the classes set here should be in Rails autoload_paths or you would need to
|
188
|
+
# require it or autoload it before setting it
|
189
|
+
module ExceptionsConfiguration
|
190
|
+
|
191
|
+
# Here you can set your custom exception class to be raisen when a required
|
192
|
+
# parameter is missing.
|
193
|
+
#
|
194
|
+
# Note: The initializer of the class receives a hash argument with
|
195
|
+
# the following key => values:
|
196
|
+
# - param_key => 'The parameter key/name'
|
197
|
+
# - param_value => 'The parameter value'
|
198
|
+
# - param_type => 'The parameter type'
|
199
|
+
#
|
200
|
+
# The default value is RequestParamsValidation::MissingParameterError.
|
201
|
+
mattr_accessor :on_missing_parameter
|
202
|
+
self.on_missing_parameter = RequestParamsValidation::MissingParameterError
|
203
|
+
|
204
|
+
|
205
|
+
# Here you can set your custom exception class to be raisen when the parameter type
|
206
|
+
# option fails.
|
207
|
+
#
|
208
|
+
# Note: The initializer of the class receives a hash argument with
|
209
|
+
# the following key => values:
|
210
|
+
# - param_key => 'The parameter key/name'
|
211
|
+
# - param_value => 'The parameter value'
|
212
|
+
# - param_type => 'The parameter type'
|
213
|
+
# - details => 'The details of the failure'
|
214
|
+
#
|
215
|
+
# The default value is RequestParamsValidation::InvalidParameterValueError.
|
216
|
+
mattr_accessor :on_invalid_parameter_type
|
217
|
+
self.on_invalid_parameter_type = RequestParamsValidation::InvalidParameterValueError
|
218
|
+
|
219
|
+
|
220
|
+
# Here you can set your custom exception class to be raisen when the parameter inclusion
|
221
|
+
# option fails.
|
222
|
+
#
|
223
|
+
# Note: The initializer of the class receives a hash argument with
|
224
|
+
# the following key => values:
|
225
|
+
# - param_key => 'The parameter key/name'
|
226
|
+
# - param_value => 'The parameter value'
|
227
|
+
# - param_type => 'The parameter type'
|
228
|
+
# - include_in => 'The array of the inclusion option'
|
229
|
+
# - details => 'The details of the failure'
|
230
|
+
#
|
231
|
+
# The default value is RequestParamsValidation::InvalidParameterValueError.
|
232
|
+
mattr_accessor :on_invalid_parameter_inclusion
|
233
|
+
self.on_invalid_parameter_inclusion = RequestParamsValidation::InvalidParameterValueError
|
234
|
+
|
235
|
+
|
236
|
+
# Here you can set your custom exception class to be raisen when the parameter length
|
237
|
+
# option fails.
|
238
|
+
#
|
239
|
+
# Note: The initializer of the class receives a hash argument with
|
240
|
+
# the following key => values:
|
241
|
+
# - param_key => 'The parameter key/name'
|
242
|
+
# - param_value => 'The parameter value'
|
243
|
+
# - param_type => 'The parameter type'
|
244
|
+
# - min => 'The min value of the length option'
|
245
|
+
# - max => 'The max value of the length option'
|
246
|
+
# - details => 'The details of the failure'
|
247
|
+
#
|
248
|
+
# The default value is RequestParamsValidation::InvalidParameterValueError.
|
249
|
+
mattr_accessor :on_invalid_parameter_length
|
250
|
+
self.on_invalid_parameter_length = RequestParamsValidation::InvalidParameterValueError
|
251
|
+
|
252
|
+
|
253
|
+
# Here you can set your custom exception class to be raisen when the parameter value
|
254
|
+
# option fails.
|
255
|
+
#
|
256
|
+
# Note: The initializer of the class receives a hash argument with
|
257
|
+
# the following key => values:
|
258
|
+
# - param_key => 'The parameter key/name'
|
259
|
+
# - param_value => 'The parameter value'
|
260
|
+
# - param_type => 'The parameter type'
|
261
|
+
# - min => 'The min value of the value option'
|
262
|
+
# - max => 'The max value of the value option'
|
263
|
+
# - details => 'The details of the failure'
|
264
|
+
#
|
265
|
+
# The default value is RequestParamsValidation::InvalidParameterValueError.
|
266
|
+
mattr_accessor :on_invalid_parameter_value_size
|
267
|
+
self.on_invalid_parameter_value_size = RequestParamsValidation::InvalidParameterValueError
|
268
|
+
|
269
|
+
|
270
|
+
# Here you can set your custom exception class to be raisen when the parameter format
|
271
|
+
# option fails.
|
272
|
+
#
|
273
|
+
# Note: The initializer of the class receives a hash argument with
|
274
|
+
# the following key => values:
|
275
|
+
# - param_key => 'The parameter key/name'
|
276
|
+
# - param_value => 'The parameter value'
|
277
|
+
# - param_type => 'The parameter type'
|
278
|
+
# - regexp => 'The regexp of the format option'
|
279
|
+
# - details => 'The details of the failure'
|
280
|
+
#
|
281
|
+
# The default value is RequestParamsValidation::InvalidParameterValueError.
|
282
|
+
mattr_accessor :on_invalid_parameter_format
|
283
|
+
self.on_invalid_parameter_format = RequestParamsValidation::InvalidParameterValueError
|
284
|
+
|
285
|
+
|
286
|
+
# Here you can set your custom exception class to be raisen when the parameter validate
|
287
|
+
# option fails.
|
288
|
+
#
|
289
|
+
# Note: The initializer of the class receives a hash argument with
|
290
|
+
# the following key => values:
|
291
|
+
# - param_key => 'The parameter key/name'
|
292
|
+
# - param_value => 'The parameter value'
|
293
|
+
# - param_type => 'The parameter type'
|
294
|
+
# - details => 'The details of the failure'
|
295
|
+
#
|
296
|
+
# The default value is RequestParamsValidation::InvalidParameterValueError.
|
297
|
+
mattr_accessor :on_invalid_parameter_custom_validation
|
298
|
+
self.on_invalid_parameter_custom_validation = RequestParamsValidation::InvalidParameterValueError
|
299
|
+
end
|
300
|
+
|
301
|
+
|
302
|
+
# Method for defining a resource. This is the entrypoint for each resource
|
303
|
+
# configuration.
|
304
|
+
def self.define(&block)
|
305
|
+
RequestParamsValidation::Definitions.register_resource(&block)
|
306
|
+
end
|
307
|
+
|
308
|
+
|
309
|
+
# Default way to setup RequestParamsValidation configuration.
|
310
|
+
def self.configure
|
311
|
+
yield self
|
312
|
+
end
|
313
|
+
|
314
|
+
|
315
|
+
# Default way to extend configuration if a block is given, otherwise
|
316
|
+
# it returns the ExtensionConfiguration module.
|
317
|
+
def self.extends
|
318
|
+
if block_given?
|
319
|
+
yield ExtensionConfiguration
|
320
|
+
else
|
321
|
+
ExtensionConfiguration
|
322
|
+
end
|
323
|
+
end
|
324
|
+
|
325
|
+
|
326
|
+
# Default way to setup formats configuration if a block is given, otherwise
|
327
|
+
# it returns the FormatsConfiguration module.
|
328
|
+
def self.formats
|
329
|
+
if block_given?
|
330
|
+
yield FormatsConfiguration
|
331
|
+
else
|
332
|
+
FormatsConfiguration
|
333
|
+
end
|
334
|
+
end
|
335
|
+
|
336
|
+
|
337
|
+
# Default way to setup exceptions configuration if a block is given, otherwise
|
338
|
+
# it returns the ExceptionConfiguration module.
|
339
|
+
def self.exceptions
|
340
|
+
if block_given?
|
341
|
+
yield ExceptionsConfiguration
|
342
|
+
else
|
343
|
+
ExceptionsConfiguration
|
344
|
+
end
|
345
|
+
end
|
346
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'request_params_validation/definitions/resource'
|
2
|
+
require 'request_params_validation/exceptions/definitions_errors'
|
3
|
+
|
4
|
+
module RequestParamsValidation
|
5
|
+
module Definitions
|
6
|
+
@@definitions = {}
|
7
|
+
|
8
|
+
def self.load_all
|
9
|
+
definitions_suffix = RequestParamsValidation.definitions_suffix
|
10
|
+
Dir["#{definitions_path}/**/*#{definitions_suffix}.rb"].each { |file| load file }
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.register_resource(&block)
|
14
|
+
raise DefinitionArgumentError.new('Expecting block for resource definition') unless block_given?
|
15
|
+
|
16
|
+
resource_name = resource_name_from_block(&block)
|
17
|
+
resource = Resource.new(resource_name)
|
18
|
+
|
19
|
+
block.call(resource)
|
20
|
+
|
21
|
+
@@definitions[resource_name] = resource
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.get_request(resource, action)
|
25
|
+
resource = @@definitions[resource]
|
26
|
+
|
27
|
+
return unless resource
|
28
|
+
|
29
|
+
action = resource.actions[action]
|
30
|
+
|
31
|
+
return unless action
|
32
|
+
|
33
|
+
action.request
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.definitions_path
|
37
|
+
definitions_path = RequestParamsValidation.definitions_path.to_s
|
38
|
+
|
39
|
+
definitions_path[0] = '' if definitions_path.start_with?('/')
|
40
|
+
definitions_path[-1] = '' if definitions_path.end_with?('/')
|
41
|
+
|
42
|
+
"#{Rails.root}/#{definitions_path}"
|
43
|
+
end
|
44
|
+
private_class_method :definitions_path
|
45
|
+
|
46
|
+
def self.resource_name_from_block(&block)
|
47
|
+
definitions_suffix = RequestParamsValidation.definitions_suffix
|
48
|
+
|
49
|
+
block_path = block.source_location.first
|
50
|
+
|
51
|
+
block_path.sub("#{definitions_path}/", '')
|
52
|
+
.sub("#{definitions_suffix}", '')
|
53
|
+
.sub('.rb', '')
|
54
|
+
end
|
55
|
+
private_class_method :resource_name_from_block
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'request_params_validation/definitions/request'
|
2
|
+
|
3
|
+
module RequestParamsValidation
|
4
|
+
module Definitions
|
5
|
+
class Action
|
6
|
+
attr_reader :name
|
7
|
+
|
8
|
+
def initialize(name)
|
9
|
+
@name = name
|
10
|
+
end
|
11
|
+
|
12
|
+
def request
|
13
|
+
if block_given?
|
14
|
+
@request = Request.new
|
15
|
+
|
16
|
+
yield @request
|
17
|
+
else
|
18
|
+
@request
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|