params_checker 0.2.2 → 0.2.3

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.
@@ -1,312 +1,326 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ParamsChecker
2
- module ParamChecker
3
- class BaseParamChecker
4
- def initialize(key = '', fields = {}, opts = {})
5
- @key = key
6
- @fields = fields
7
- @opts = opts
8
- end
9
-
10
- def call; end
11
-
12
- def add_error(message = '')
13
- errors.add(key, message)
14
- end
15
-
16
- attr_accessor :key, :fields, :opts
17
- end
18
-
19
- class NumParamChecker < BaseParamChecker
20
- prepend SimpleCommand
21
-
22
- def call
23
- return nil if fields[key][:allow_nil] && opts[key].nil?
24
-
25
- check_type && check_param
26
- opts[key]
27
- end
28
-
29
- def check_type
30
- valid = opts[key].is_a? Numeric
31
- add_error("This field's type must be numeric.") unless valid
32
- valid
33
- end
34
-
35
- def check_param
36
- min = fields[key][:min]
37
- max = fields[key][:max]
38
- valid =(min..max).include? opts[key]
39
- add_error("This numeric field's value must be in range from #{min} to #{max}.") unless valid
40
- valid
41
- end
42
- end
4
+ module ParamChecker
5
+ class BaseParamChecker
6
+ def initialize(key = '', schema = {}, params = {})
7
+ @key = key
8
+ @schema = schema
9
+ @params = params
10
+ end
43
11
 
44
- class IntParamChecker < BaseParamChecker
45
- prepend SimpleCommand
46
-
47
- def call
48
- return nil if fields[key][:allow_nil] && opts[key].nil?
49
-
50
- check_type && check_param
51
- opts[key]
52
- end
53
-
54
- def check_type
55
- valid = opts[key].is_a? Integer
56
- add_error("This field's type must be integer.") unless valid
57
- valid
58
- end
59
-
60
- def check_param
61
- min = fields[key][:min]
62
- max = fields[key][:max]
63
- valid = (min..max).include? opts[key]
64
- add_error("This integer field's value must be in range from #{min} to #{max}.") unless valid
65
- valid
66
- end
67
- end
68
-
69
- class CharParamChecker < BaseParamChecker
70
- prepend SimpleCommand
71
-
72
- def call
73
- return nil if fields[key][:allow_nil] && opts[key].nil?
74
-
75
- check_type && check_allow_blank && check_length && opts[key]
76
- end
77
-
78
- def check_type
79
- valid = opts[key].is_a?(String)
80
- add_error("This field's type must be string.") unless valid
81
- valid
82
- end
83
-
84
- def check_allow_blank
85
- valid = !(!fields[key][:allow_blank] && opts[key].blank?)
86
- add_error('This field cannot be blank.') unless valid
87
- valid
88
- end
89
-
90
- def check_length
91
- min_length = fields[key][:min_length]
92
- max_length = fields[key][:max_length]
93
- valid = (min_length..max_length).include? opts[key].length
94
- add_error("This string field's length must be in range from #{min_length} to #{max_length}.") unless valid
95
- valid
96
- end
97
- end
98
-
99
- class ArrParamChecker < BaseParamChecker
100
- prepend SimpleCommand
101
-
102
- def call
103
- return nil if fields[key][:allow_nil] && opts[key].nil?
104
-
105
- check_type && check_allow_empty && opts[key]
106
- end
107
-
108
- def check_type
109
- valid =opts[key].is_a? Array
110
- add_error("This field's type must be array.") unless valid
111
- valid
112
- end
113
-
114
- def check_allow_empty
115
- valid =!(!fields[key][:allow_empty] && opts[key].empty?)
116
- add_error('This field cannot be empty.') unless valid
117
- valid
118
- end
119
- end
12
+ def call; end
120
13
 
121
- class NestedHashChecker
122
- prepend SimpleCommand
123
- def initialize(key = '', fields = {}, opts = {}, context = {})
124
- @key = key
125
- @fields = fields
126
- @opts = opts
127
- @context = context
128
- end
129
-
130
- def call
131
- return nil if fields[key][:allow_nil] && opts[key].nil?
132
-
133
- check_type && formatted_nested_hash
134
- end
135
-
136
- def formatted_nested_hash
137
- cmd = fields[key][:class].call(params:opts[key], context: context, is_outest_hash: false)
138
- if cmd.success?
139
- cmd.result
140
- else
141
- add_error cmd.errors
142
- end
143
- end
144
-
145
- def check_type
146
- valid = opts[key].is_a?(ActionController::Parameters) || opts[key].is_a?(Hash)
147
- add_error("This field's type must be object or ActionController::Parameters.") unless valid
148
- valid
149
- end
150
-
151
- def add_error(message = '')
152
- errors.add(key, message)
153
- end
154
-
155
- attr_accessor :key, :fields, :opts, :class, :context
156
- end
14
+ def add_field_error(message = '')
15
+ errors.add(key, message)
16
+ end
157
17
 
158
- class NestedHashsChecker
159
- prepend SimpleCommand
160
- def initialize(key = '', fields = {}, opts = {}, context = {})
161
- @key = key
162
- @fields = fields
163
- @opts = opts
164
- @context = context
165
- end
166
-
167
- def call
168
- return nil if fields[key][:allow_nil] && opts[key].nil?
169
-
170
- check_type && formatted_nested_hashs
171
- end
172
-
173
- def formatted_nested_hashs
174
- opts[key].map do |nested_hash|
175
- formatted_nested_hash(nested_hash)
176
- end
177
- end
178
-
179
- def formatted_nested_hash nested_hash
180
- cmd = fields[key][:class].call(params: nested_hash, context: context, is_outest_hash: false)
181
- if cmd.success?
182
- cmd.result
183
- else
184
- add_error cmd.errors
185
- end
186
- end
187
-
188
- def check_type
189
- valid = opts[key].is_a?(Array)
190
- add_error("This field's type must be array.") unless valid
191
- valid
192
- end
193
-
194
- def add_error(message = '')
195
- errors.add(key, message)
196
- end
197
-
198
- attr_accessor :key, :fields, :opts, :class, :context
199
- end
200
-
201
- class DateParamChecker < BaseParamChecker
202
- prepend SimpleCommand
203
-
204
- def call
205
- return nil if fields[key][:allow_nil] && opts[key].nil?
206
-
207
- formatted_date
208
- end
209
-
210
- def formatted_date
211
- Date.parse opts[key]
212
- rescue => e
213
- add_error 'Invalid date.'
214
- end
215
- end
18
+ attr_accessor :key, :schema, :params
19
+ end
216
20
 
217
- class TimeParamChecker < BaseParamChecker
218
- prepend SimpleCommand
219
-
220
- def call
221
- return nil if fields[key][:allow_nil] && opts[key].nil?
222
-
223
- formatted_time
224
- end
225
-
226
- def formatted_time
227
- Time.parse opts[key]
228
- rescue => e
229
- add_error 'Invalid time.'
230
- end
231
- end
21
+ class NumParamChecker < BaseParamChecker
22
+ prepend SimpleCommand
232
23
 
233
- class DateTimeParamChecker < BaseParamChecker
234
- prepend SimpleCommand
235
-
236
- def call
237
- return nil if fields[key][:allow_nil] && opts[key].nil?
238
-
239
- formatted_datetime
240
- end
241
-
242
- def formatted_datetime
243
- DateTime.parse(opts[key])
244
- rescue => e
245
- add_error 'Invalid datetime.'
246
- end
247
- end
24
+ def call
25
+ return nil if schema[key][:allow_nil] && params[key].nil?
248
26
 
249
- class EmailParamChecker < BaseParamChecker
250
- prepend SimpleCommand
251
-
252
- def call
253
- return nil if fields[key][:allow_nil] && opts[key].nil?
254
-
255
- check_type && check_regrex && opts[key]
256
- end
257
-
258
- def check_type
259
- valid = opts[key].is_a? String
260
- add_error("Invalid email.") unless valid
261
- valid
262
- end
263
-
264
- def check_regrex
265
- valid = opts[key].match(URI::MailTo::EMAIL_REGEXP)
266
- add_error('Invalid email.') unless valid
267
- valid
268
- end
269
-
270
- end
27
+ check_type && check_param
28
+ params[key]
29
+ end
30
+
31
+ def check_type
32
+ valid = params[key].is_a? Numeric
33
+ add_field_error("This field's type must be numeric.") unless valid
34
+ valid
35
+ end
36
+
37
+ def check_param
38
+ min = schema[key][:min]
39
+ max = schema[key][:max]
40
+ valid = (min..max).include? params[key]
41
+ add_field_error("This numeric field's value must be in range from #{min} to #{max}.") unless valid
42
+ valid
43
+ end
44
+ end
45
+
46
+ class IntParamChecker < BaseParamChecker
47
+ prepend SimpleCommand
48
+
49
+ def call
50
+ return nil if schema[key][:allow_nil] && params[key].nil?
51
+
52
+ check_type && check_param
53
+ params[key]
54
+ end
55
+
56
+ def check_type
57
+ valid = params[key].is_a? Integer
58
+ add_field_error("This field's type must be integer.") unless valid
59
+ valid
60
+ end
61
+
62
+ def check_param
63
+ min = schema[key][:min]
64
+ max = schema[key][:max]
65
+ valid = (min..max).include? params[key]
66
+ add_field_error("This integer field's value must be in range from #{min} to #{max}.") unless valid
67
+ valid
68
+ end
69
+ end
70
+
71
+ class CharParamChecker < BaseParamChecker
72
+ prepend SimpleCommand
73
+
74
+ def call
75
+ return nil if schema[key][:allow_nil] && params[key].nil?
76
+
77
+ check_type && check_allow_blank && check_length && params[key]
78
+ end
79
+
80
+ def check_type
81
+ valid = params[key].is_a?(String)
82
+ add_field_error("This field's type must be string.") unless valid
83
+ valid
84
+ end
85
+
86
+ def check_allow_blank
87
+ valid = !(!schema[key][:allow_blank] && params[key].blank?)
88
+ add_field_error('This field cannot be blank.') unless valid
89
+ valid
90
+ end
91
+
92
+ def check_length
93
+ min_length = schema[key][:min_length]
94
+ max_length = schema[key][:max_length]
95
+ valid = (min_length..max_length).include? params[key].length
96
+ add_field_error("This string field's length must be in range from #{min_length} to #{max_length}.") unless valid
97
+ valid
98
+ end
99
+ end
100
+
101
+ class ArrParamChecker < BaseParamChecker
102
+ prepend SimpleCommand
103
+
104
+ def call
105
+ return nil if schema[key][:allow_nil] && params[key].nil?
106
+
107
+ check_type && check_allow_empty && params[key]
108
+ end
109
+
110
+ def check_type
111
+ valid = params[key].is_a? Array
112
+ add_field_error("This field's type must be array.") unless valid
113
+ valid
114
+ end
115
+
116
+ def check_allow_empty
117
+ valid = !(!schema[key][:allow_empty] && params[key].empty?)
118
+ add_field_error('This field cannot be empty.') unless valid
119
+ valid
120
+ end
121
+ end
122
+
123
+ class HashParamChecker < BaseParamChecker
124
+ prepend SimpleCommand
125
+
126
+ def call
127
+ return nil if schema[key][:allow_nil] && params[key].nil?
128
+
129
+ check_type && params[key]
130
+ end
131
+
132
+ def check_type
133
+ valid = params[key].is_a? Hash
134
+ add_field_error("This field's type must be hash.") unless valid
135
+ valid
136
+ end
137
+ end
138
+
139
+ class NestedHashChecker
140
+ prepend SimpleCommand
141
+ def initialize(key = '', schema = {}, params = {}, context = {})
142
+ @key = key
143
+ @schema = schema
144
+ @params = params
145
+ @context = context
146
+ end
147
+
148
+ def call
149
+ return nil if schema[key][:allow_nil] && params[key].nil?
150
+
151
+ check_type && formatted_nested_hash
152
+ end
153
+
154
+ def formatted_nested_hash
155
+ cmd = schema[key][:class].call(params: params[key], context: context, is_outest_hash: false)
156
+ return cmd.result if cmd.success?
271
157
 
272
- class BooleanChecker < BaseParamChecker
273
- prepend SimpleCommand
274
-
275
- def call
276
- return nil if fields[key][:allow_nil] && opts[key].nil?
277
-
278
- check_type && formatted_boolean
279
- end
280
-
281
- def formatted_boolean
282
- [false, "false", "1"].exclude?(opts[key])
283
- end
284
-
285
- def check_type
286
- valid = opts[key].in? [true, false, "true", "false", "1", "0"]
287
- add_error("This field's type must be boolean.") unless valid
288
- valid
289
- end
290
-
158
+ add_nested_hash_error(cmd.errors)
159
+ end
160
+
161
+ def check_type
162
+ valid = params[key].is_a?(ActionController::Parameters) || params[key].is_a?(Hash)
163
+ add_field_error("This field's type must be object or ActionController::Parameters.") unless valid
164
+
165
+ valid
166
+ end
167
+
168
+ def add_nested_hash_error(message = '')
169
+ errors.add(key, message[:errors][0][:field_errors])
170
+ end
171
+
172
+ def add_field_error(message = '')
173
+ errors.add(key, message)
174
+ end
175
+
176
+ attr_accessor :key, :schema, :params, :class, :context
177
+ end
178
+
179
+ class NestedHashsChecker
180
+ prepend SimpleCommand
181
+ def initialize(key = '', schema = {}, params = {}, context = {})
182
+ @key = key
183
+ @schema = schema
184
+ @params = params
185
+ @context = context
186
+ end
187
+
188
+ def call
189
+ return nil if schema[key][:allow_nil] && params[key].nil?
190
+
191
+ check_type && add_errors && formatted_nested_hashs
192
+ end
193
+
194
+ def add_errors
195
+ all_errors = params[key].map do |nested_hash|
196
+ get_error(nested_hash)
291
197
  end
198
+ return if all_errors.all?(&:nil?)
199
+
200
+ errors.add(key, all_errors)
201
+ end
292
202
 
293
- class FileChecker < BaseParamChecker
294
- prepend SimpleCommand
295
-
296
- def call
297
- return nil if fields[key][:allow_nil] && opts[key].nil?
298
-
299
- check_type && opts[key]
300
- end
301
-
302
- def check_type
303
- valid = opts[key].is_a?(ActionDispatch::Http::UploadedFile)
304
- add_error("This field's type must be file.") unless valid
305
- valid
306
- end
307
-
203
+ def get_error(nested_hash)
204
+ cmd = schema[key][:class].call(
205
+ params: nested_hash,
206
+ context: context,
207
+ is_outest_hash: false
208
+ )
209
+ return nil if cmd.success?
210
+
211
+ cmd.errors[:errors][0][:field_errors]
212
+ end
213
+
214
+ def formatted_nested_hashs
215
+ params[key].map do |nested_hash|
216
+ formatted_nested_hash(nested_hash)
308
217
  end
218
+ end
219
+
220
+ def formatted_nested_hash(nested_hash)
221
+ cmd = schema[key][:class].call(
222
+ params: nested_hash,
223
+ context: context,
224
+ is_outest_hash: false
225
+ )
226
+ cmd.result
227
+ end
309
228
 
229
+ def check_type
230
+ valid = params[key].is_a?(Array)
231
+ add_field_error("This field's type must be array.") unless valid
232
+ valid
233
+ end
234
+
235
+ def add_field_error(message = '')
236
+ errors.add(key, message)
237
+ end
238
+
239
+ attr_accessor :key, :schema, :params, :class, :context
240
+ end
241
+
242
+ class DateParamChecker < BaseParamChecker
243
+ prepend SimpleCommand
244
+
245
+ def call
246
+ return nil if schema[key][:allow_nil] && params[key].nil?
247
+
248
+ formatted_date
249
+ end
250
+
251
+ def formatted_date
252
+ Date.parse params[key]
253
+ rescue => e
254
+ add_field_error('Invalid date.')
255
+ end
310
256
  end
311
- end
312
257
 
258
+ class TimeParamChecker < BaseParamChecker
259
+ prepend SimpleCommand
260
+
261
+ def call
262
+ return nil if schema[key][:allow_nil] && params[key].nil?
263
+
264
+ formatted_time
265
+ end
266
+
267
+ def formatted_time
268
+ Time.parse params[key]
269
+ rescue => e
270
+ add_field_error('Invalid time.')
271
+ end
272
+ end
273
+
274
+ class DateTimeParamChecker < BaseParamChecker
275
+ prepend SimpleCommand
276
+
277
+ def call
278
+ return nil if schema[key][:allow_nil] && params[key].nil?
279
+
280
+ formatted_datetime
281
+ end
282
+
283
+ def formatted_datetime
284
+ DateTime.parse(params[key])
285
+ rescue => e
286
+ add_field_error('Invalid datetime.')
287
+ end
288
+ end
289
+
290
+ class BooleanChecker < BaseParamChecker
291
+ prepend SimpleCommand
292
+
293
+ def call
294
+ return nil if schema[key][:allow_nil] && params[key].nil?
295
+
296
+ check_type && formatted_boolean
297
+ end
298
+
299
+ def formatted_boolean
300
+ [false, "false", "1"].exclude?(params[key])
301
+ end
302
+
303
+ def check_type
304
+ valid = params[key].in? [true, false, "true", "false", "1", "0"]
305
+ add_field_error("This field's type must be boolean.") unless valid
306
+ valid
307
+ end
308
+ end
309
+
310
+ class FileChecker < BaseParamChecker
311
+ prepend SimpleCommand
312
+
313
+ def call
314
+ return nil if schema[key][:allow_nil] && params[key].nil?
315
+
316
+ check_type && params[key]
317
+ end
318
+
319
+ def check_type
320
+ valid = params[key].is_a?(ActionDispatch::Http::UploadedFile)
321
+ add_field_error("This field's type must be file.") unless valid
322
+ valid
323
+ end
324
+ end
325
+ end
326
+ end
@@ -1,3 +1,3 @@
1
1
  module ParamsChecker
2
- VERSION = '0.2.2'
2
+ VERSION = '0.2.3'
3
3
  end
@@ -1,9 +1,11 @@
1
- require "simple_command"
2
- require "params_checker/engine"
3
- require "params_checker/fields"
4
- require "params_checker/my_error"
5
- require "params_checker/param_checker"
6
- require "params_checker/base_params_checker"
1
+ require 'simple_command'
2
+ require 'params_checker/engine'
3
+ require 'params_checker/fields'
4
+ require 'params_checker/general_error'
5
+ require 'params_checker/field_error'
6
+ require 'params_checker/param_checker'
7
+ require 'params_checker/base_params_checker'
8
+ require 'params_checker/helper'
9
+
7
10
  module ParamsChecker
8
11
  end
9
-
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: params_checker
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - rexy tech
@@ -30,6 +30,20 @@ dependencies:
30
30
  - - ">="
31
31
  - !ruby/object:Gem::Version
32
32
  version: 6.0.3.1
33
+ - !ruby/object:Gem::Dependency
34
+ name: simple_command
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: 0.1.0
40
+ type: :runtime
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: 0.1.0
33
47
  - !ruby/object:Gem::Dependency
34
48
  name: sqlite3
35
49
  requirement: !ruby/object:Gem::Requirement
@@ -54,20 +68,15 @@ files:
54
68
  - MIT-LICENSE
55
69
  - README.md
56
70
  - Rakefile
57
- - app/assets/config/params_checker_manifest.js
58
- - app/assets/stylesheets/params_checker/application.css
59
- - app/controllers/params_checker/application_controller.rb
60
- - app/helpers/params_checker/application_helper.rb
61
- - app/jobs/params_checker/application_job.rb
62
- - app/mailers/params_checker/application_mailer.rb
63
- - app/models/params_checker/application_record.rb
64
- - app/views/layouts/params_checker/application.html.erb
65
71
  - config/routes.rb
66
72
  - lib/params_checker.rb
67
73
  - lib/params_checker/base_params_checker.rb
74
+ - lib/params_checker/constants.rb
68
75
  - lib/params_checker/engine.rb
76
+ - lib/params_checker/field_error.rb
69
77
  - lib/params_checker/fields.rb
70
- - lib/params_checker/my_error.rb
78
+ - lib/params_checker/general_error.rb
79
+ - lib/params_checker/helper.rb
71
80
  - lib/params_checker/param_checker.rb
72
81
  - lib/params_checker/version.rb
73
82
  homepage: https://rexy.tech/