params_checker 0.2.1 → 0.2.5

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.
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Constants
4
+ ERROR_MESSAGES = {
5
+ required: 'This field is required.',
6
+ blank: 'This field cannot be blank.',
7
+ empty: 'This field cannot be empty.',
8
+
9
+ type: {
10
+ string: "This field's type must be string.",
11
+ file: "This field's type must be file.",
12
+ hash: "This field's type must be hash.",
13
+ nested_hash: "This field's type must be object or ActionController::Parameters.",
14
+ array: "This field's type must be array.",
15
+ integer: "This field's type must be integer.",
16
+ numeric: "This field's type must be numeric.",
17
+ boolean: "This field's type must be boolean.",
18
+ date: 'Invalid date.',
19
+ time: 'Invalid time.',
20
+ datetime: 'Invalid datetime.'
21
+ },
22
+
23
+ length: {
24
+ text: 'Invalid text length.',
25
+ char: 'Invalid char length.'
26
+ },
27
+
28
+ value: {
29
+ numeric: 'Invalid numeric value.',
30
+ integer: 'Invalid integer value.'
31
+ }
32
+ }.freeze
33
+
34
+ def get_integer_value_error_message(min: -2_000_000_000_000, max: 2_000_000_000_000)
35
+ "This integer field's value must be in range from #{min} to #{max}."
36
+ end
37
+
38
+ def get_numeric_value_error_message(min: -2_000_000_000_000, max: 2_000_000_000_000)
39
+ "This numeric field's value must be in range from #{min} to #{max}."
40
+ end
41
+
42
+ def get_string_length_error_message(min_length: 0, max_length: 30_000)
43
+ "This string field's length must be in range from #{min_length} to #{max_length}."
44
+ end
45
+ end
@@ -1,6 +1,10 @@
1
1
  module ParamsChecker
2
2
  class Engine < ::Rails::Engine
3
3
  isolate_namespace ParamsChecker
4
+
5
+ config.generators do |g|
6
+ g.test_framework :rspec
7
+ end
4
8
  end
5
9
  class NotEngine
6
10
  end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ParamsChecker
4
+ class FieldError < StandardError
5
+ attr_accessor :data
6
+
7
+ def initialize(data)
8
+ @data = data
9
+ end
10
+ end
11
+ end
@@ -1,228 +1,310 @@
1
-
1
+ # frozen_string_literal: true
2
2
 
3
3
  module ParamsChecker
4
4
  module Fields
5
- def text_field(required: true, default: nil, allow_blank: false, min_length: 0, max_length: 30_000, allow_nil: false)
6
- raise "This field's type must be integer." if [min_length, max_length].any? { |value| !value.is_a?(Integer) }
7
- raise "This field's type must be boolean." if [required, allow_blank, allow_nil].any? { |value| !value.in? [true, false] }
5
+ def text_field(
6
+ required: true,
7
+ default: nil,
8
+ allow_blank: false,
9
+ min_length: 0,
10
+ max_length: 30_000,
11
+ allow_nil: false
12
+ )
13
+ Helper.check_type(type: 'integer', values: [min_length, max_length])
14
+ Helper.check_type(type: 'boolean', values: [required, allow_blank, allow_nil])
8
15
  raise 'Invalid text length.' unless (min_length >= 0) && (max_length <= 30_000)
9
-
16
+
10
17
  {
11
- type: 'char',
12
- default: default,
13
- allow_blank: allow_blank,
14
- required: required,
15
- min_length: min_length,
16
- max_length: max_length,
17
- allow_nil: allow_nil
18
+ type: 'char',
19
+ default: default,
20
+ allow_blank: allow_blank,
21
+ required: required,
22
+ min_length: min_length,
23
+ max_length: max_length,
24
+ allow_nil: allow_nil
18
25
  }
19
26
  end
20
-
21
- def char_field(required: true, default: nil, allow_blank: false, min_length: 0, max_length: 255, allow_nil: false)
22
- raise "This field's type must be integer." if [min_length, max_length].any? { |value| !value.is_a?(Integer) }
23
- raise "This field's type must be boolean." if [required, allow_blank, allow_nil].any? { |value| !value.in? [true, false] }
27
+
28
+ def char_field(
29
+ required: true,
30
+ default: nil,
31
+ allow_blank: false,
32
+ min_length: 0,
33
+ max_length: 255,
34
+ allow_nil: false
35
+ )
36
+ Helper.check_type(type: 'integer', values: [min_length, max_length])
37
+ Helper.check_type(type: 'boolean', values: [required, allow_blank, allow_nil])
24
38
  raise 'Invalid char length.' unless (min_length >= 0) && (max_length <= 255)
25
-
39
+
26
40
  {
27
- type: 'char',
28
- default: default,
29
- allow_blank: allow_blank,
30
- required: required,
31
- min_length: min_length,
32
- max_length: max_length,
33
- allow_nil: allow_nil
41
+ type: 'char',
42
+ default: default,
43
+ allow_blank: allow_blank,
44
+ required: required,
45
+ min_length: min_length,
46
+ max_length: max_length,
47
+ allow_nil: allow_nil
34
48
  }
35
49
  end
36
-
37
- def bignum_field(required: true, default: nil, min: -2_000_000_000_000, max: 2_000_000_000_000, allow_nil: false)
38
- raise "This field's type must be numeric." if [min, max].any? { |value| !value.is_a?(Numeric) }
39
- raise "This field's type must be boolean." if [required, allow_nil].any? { |value| !value.in? [true, false] }
50
+
51
+ def bignum_field(
52
+ required: true,
53
+ default: nil,
54
+ min: -2_000_000_000_000,
55
+ max: 2_000_000_000_000,
56
+ allow_nil: false
57
+ )
58
+ Helper.check_type(type: 'numeric', values: [min, max])
59
+ Helper.check_type(type: 'boolean', values: [required, allow_nil])
40
60
  raise 'Invalid numeric value.' unless (min >= -2_000_000_000_000) && (max <= 2_000_000_000_000)
41
-
61
+
42
62
  {
43
- type: 'num',
44
- default: default,
45
- required: required,
46
- min: min,
47
- max: max,
48
- allow_nil: allow_nil
63
+ type: 'num',
64
+ default: default,
65
+ required: required,
66
+ min: min,
67
+ max: max,
68
+ allow_nil: allow_nil
49
69
  }
50
70
  end
51
-
52
- def num_field(required: true, default: nil, min: -2_000_000_000, max: 2_000_000_000, allow_nil: false)
53
- raise "This field's type must be numeric." if [min, max].any? { |value| !value.is_a?(Numeric) }
54
- raise "This field's type must be boolean." if [required, allow_nil].any? { |value| !value.in? [true, false] }
71
+
72
+ def num_field(
73
+ required: true,
74
+ default: nil,
75
+ min: -2_000_000_000,
76
+ max: 2_000_000_000,
77
+ allow_nil: false
78
+ )
79
+ Helper.check_type(type: 'numeric', values: [min, max])
80
+ Helper.check_type(type: 'boolean', values: [required, allow_nil])
55
81
  raise 'Invalid numeric value.' unless (min >= -2_000_000_000) && (max <= 2_000_000_000)
56
-
82
+
57
83
  {
58
- type: 'num',
59
- default: default,
60
- required: required,
61
- min: min,
62
- max: max,
63
- allow_nil: allow_nil
84
+ type: 'num',
85
+ default: default,
86
+ required: required,
87
+ min: min,
88
+ max: max,
89
+ allow_nil: allow_nil
64
90
  }
65
91
  end
66
92
 
67
- def bigint_field(required: true, default: nil, min: -2_000_000_000_000, max: 2_000_000_000_000, allow_nil: false)
68
- raise "This field's type must be integer." if [min, max].any? { |value| !value.is_a?(Integer) }
69
- raise "This field's type must be boolean." if [required, allow_nil].any? { |value| !value.in? [true, false] }
93
+ def bigint_field(
94
+ required: true,
95
+ default: nil,
96
+ min: -2_000_000_000_000,
97
+ max: 2_000_000_000_000,
98
+ allow_nil: false
99
+ )
100
+ Helper.check_type(type: 'integer', values: [min, max])
101
+ Helper.check_type(type: 'boolean', values: [required, allow_nil])
70
102
  raise 'Invalid integer value.' unless (min >= -2_000_000_000_000) && (max <= 2_000_000_000_000)
71
-
103
+
72
104
  {
73
- type: 'int',
74
- default: default,
75
- required: required,
76
- min: min,
77
- max: max,
78
- allow_nil: allow_nil
105
+ type: 'int',
106
+ default: default,
107
+ required: required,
108
+ min: min,
109
+ max: max,
110
+ allow_nil: allow_nil
79
111
  }
80
112
  end
81
-
82
- def int_field(required: true, default: nil, min: -2_000_000_000, max: 2_000_000_000, allow_nil: false)
83
- raise "This field's type must be integer." if [min, max].any? { |value| !value.is_a?(Integer) }
84
- raise "This field's type must be boolean." if [required, allow_nil].any? { |value| !value.in? [true, false] }
113
+
114
+ def int_field(
115
+ required: true,
116
+ default: nil,
117
+ min: -2_000_000_000,
118
+ max: 2_000_000_000,
119
+ allow_nil: false
120
+ )
121
+ Helper.check_type(type: 'integer', values: [min, max])
122
+ Helper.check_type(type: 'boolean', values: [required, allow_nil])
85
123
  raise 'Invalid integer value.' unless (min >= -2_000_000_000) && (max <= 2_000_000_000)
86
-
124
+
87
125
  {
88
- type: 'int',
89
- default: default,
90
- required: required,
91
- min: min,
92
- max: max,
93
- allow_nil: allow_nil
126
+ type: 'int',
127
+ default: default,
128
+ required: required,
129
+ min: min,
130
+ max: max,
131
+ allow_nil: allow_nil
94
132
  }
95
133
  end
96
134
 
97
- # asdasda
98
-
99
- def positive_bignum_field(required: true, default: nil, min: 0, max: 2_000_000_000_000, allow_nil: false)
100
- raise "This field's type must be numeric." if [min, max].any? { |value| !value.is_a?(Numeric) }
101
- raise "This field's type must be boolean." if [required, allow_nil].any? { |value| !value.in? [true, false] }
135
+ def positive_bignum_field(
136
+ required: true,
137
+ default: nil,
138
+ min: 0, max: 2_000_000_000_000,
139
+ allow_nil: false
140
+ )
141
+ Helper.check_type(type: 'numeric', values: [min, max])
142
+ Helper.check_type(type: 'boolean', values: [required, allow_nil])
102
143
  raise 'Invalid numeric value.' unless (min >= 0) && (max <= 2_000_000_000_000)
103
-
144
+
104
145
  {
105
- type: 'num',
106
- default: default,
107
- required: required,
108
- min: min,
109
- max: max,
110
- allow_nil: allow_nil
146
+ type: 'num',
147
+ default: default,
148
+ required: required,
149
+ min: min,
150
+ max: max,
151
+ allow_nil: allow_nil
111
152
  }
112
153
  end
113
-
114
- def positive_num_field(required: true, default: nil, min: 0, max: 2_000_000_000, allow_nil: false)
115
- raise "This field's type must be numeric." if [min, max].any? { |value| !value.is_a?(Numeric) }
116
- raise "This field's type must be boolean." if [required, allow_nil].any? { |value| !value.in? [true, false] }
154
+
155
+ def positive_num_field(
156
+ required: true,
157
+ default: nil,
158
+ min: 0,
159
+ max: 2_000_000_000,
160
+ allow_nil: false
161
+ )
162
+ Helper.check_type(type: 'numeric', values: [min, max])
163
+ Helper.check_type(type: 'boolean', values: [required, allow_nil])
117
164
  raise 'Invalid numeric value.' unless (min >= 0) && (max <= 2_000_000_000)
118
-
165
+
119
166
  {
120
- type: 'num',
121
- default: default,
122
- required: required,
123
- min: min,
124
- max: max,
125
- allow_nil: allow_nil
167
+ type: 'num',
168
+ default: default,
169
+ required: required,
170
+ min: min,
171
+ max: max,
172
+ allow_nil: allow_nil
126
173
  }
127
174
  end
128
175
 
129
- def positive_bigint_field(required: true, default: nil, min: 0, max: 2_000_000_000_000, allow_nil: false)
130
- raise "This field's type must be integer." if [min, max].any? { |value| !value.is_a?(Integer) }
131
- raise "This field's type must be boolean." if [required, allow_nil].any? { |value| !value.in? [true, false] }
176
+ def positive_bigint_field(
177
+ required: true,
178
+ default: nil,
179
+ min: 0,
180
+ max: 2_000_000_000_000,
181
+ allow_nil: false
182
+ )
183
+ Helper.check_type(type: 'integer', values: [min, max])
184
+ Helper.check_type(type: 'boolean', values: [required, allow_nil])
132
185
  raise 'Invalid integer value.' unless (min >= 0) && (max <= 2_000_000_000_000)
133
-
186
+
134
187
  {
135
- type: 'int',
136
- default: default,
137
- required: required,
138
- min: min,
139
- max: max,
140
- allow_nil: allow_nil
188
+ type: 'int',
189
+ default: default,
190
+ required: required,
191
+ min: min,
192
+ max: max,
193
+ allow_nil: allow_nil
141
194
  }
142
195
  end
143
-
144
- def positive_int_field(required: true, default: nil, min: 0, max: 2_000_000_000, allow_nil: false)
145
- raise "This field's type must be integer." if [min, max].any? { |value| !value.is_a?(Integer) }
146
- raise "This field's type must be boolean." if [required, allow_nil].any? { |value| !value.in? [true, false] }
196
+
197
+ def positive_int_field(
198
+ required: true,
199
+ default: nil,
200
+ min: 0,
201
+ max: 2_000_000_000,
202
+ allow_nil: false
203
+ )
204
+ Helper.check_type(type: 'integer', values: [min, max])
205
+ Helper.check_type(type: 'boolean', values: [required, allow_nil])
147
206
  raise 'Invalid integer value.' unless (min >= 0) && (max <= 2_000_000_000)
148
-
207
+
149
208
  {
150
- type: 'int',
151
- default: default,
152
- required: required,
153
- min: min,
154
- max: max,
155
- allow_nil: allow_nil
209
+ type: 'int',
210
+ default: default,
211
+ required: required,
212
+ min: min,
213
+ max: max,
214
+ allow_nil: allow_nil
156
215
  }
157
216
  end
158
-
159
- def arr_field(required: true, default: nil, allow_empty: false, allow_nil: false)
160
- raise "This field's type must be boolean." if [required, allow_empty, allow_nil].any? { |value| !value.in? [true, false] }
161
-
217
+
218
+ def arr_field(
219
+ required: true,
220
+ default: nil,
221
+ allow_empty: false,
222
+ allow_nil: false
223
+ )
224
+ Helper.check_type(type: 'boolean', values: [required, allow_empty, allow_nil])
225
+
162
226
  {
163
- type: 'arr',
164
- default: default,
165
- required: required,
166
- allow_empty: allow_empty,
167
- allow_nil: allow_nil
227
+ type: 'arr',
228
+ default: default,
229
+ required: required,
230
+ allow_empty: allow_empty,
231
+ allow_nil: allow_nil
168
232
  }
169
233
  end
170
-
171
- def date_field(required: true, default: nil, allow_nil: false)
172
- raise "This field's type must be boolean." if [required, allow_nil].any? { |value| !value.in? [true, false] }
173
-
234
+
235
+ def hash_field(
236
+ required: true,
237
+ default: nil,
238
+ allow_nil: false
239
+ )
240
+ Helper.check_type(type: 'boolean', values: [required, allow_nil])
241
+
174
242
  {
175
- type: 'date',
176
- default: default,
177
- required: required,
178
- allow_nil: allow_nil
243
+ type: 'hash',
244
+ default: default,
245
+ required: required,
246
+ allow_nil: allow_nil
179
247
  }
180
248
  end
181
-
182
- def time_field(required: true, default: nil, allow_nil: false)
183
- raise "This field's type must be boolean." if [required, allow_nil].any? { |value| !value.in? [true, false] }
184
-
249
+
250
+ def date_field(
251
+ required: true,
252
+ default: nil,
253
+ allow_nil: false
254
+ )
255
+ Helper.check_type(type: 'boolean', values: [required, allow_nil])
256
+
185
257
  {
186
- type: 'time',
187
- default: default,
188
- required: required,
189
- allow_nil: allow_nil
258
+ type: 'date',
259
+ default: default,
260
+ required: required,
261
+ allow_nil: allow_nil
190
262
  }
191
263
  end
192
-
193
- def datetime_field(required: true, default: nil, allow_nil: false)
194
- raise "This field's type must be boolean." if [required, allow_nil].any? { |value| !value.in? [true, false] }
195
-
264
+
265
+ def time_field(
266
+ required: true,
267
+ default: nil,
268
+ allow_nil: false
269
+ )
270
+ Helper.check_type(type: 'boolean', values: [required, allow_nil])
271
+
196
272
  {
197
- type: 'datetime',
198
- default: default,
199
- required: required,
200
- allow_nil: allow_nil
273
+ type: 'time',
274
+ default: default,
275
+ required: required,
276
+ allow_nil: allow_nil
201
277
  }
202
278
  end
203
279
 
204
- def email_field(required: true, default: nil, allow_nil: false)
205
- raise "This field's type must be boolean." if [required, allow_nil].any? { |value| !value.in? [true, false] }
206
-
280
+ def datetime_field(
281
+ required: true,
282
+ default: nil,
283
+ allow_nil: false
284
+ )
285
+ Helper.check_type(type: 'boolean', values: [required, allow_nil])
286
+
207
287
  {
208
- type: 'email',
209
- default: default,
210
- required: required,
211
- allow_nil: allow_nil
288
+ type: 'datetime',
289
+ default: default,
290
+ required: required,
291
+ allow_nil: allow_nil
212
292
  }
213
293
  end
214
294
 
215
- def boolean_field(required: true, default: nil, allow_nil: false)
216
- raise "This field's type must be boolean." if [required, allow_nil].any? { |value| !value.in? [true, false] }
217
-
295
+ def boolean_field(
296
+ required: true,
297
+ default: nil,
298
+ allow_nil: false
299
+ )
300
+ Helper.check_type(type: 'boolean', values: [required, allow_nil])
301
+
218
302
  {
219
- type: 'boolean',
220
- default: default,
221
- required: required,
222
- allow_nil: allow_nil
303
+ type: 'boolean',
304
+ default: default,
305
+ required: required,
306
+ allow_nil: allow_nil
223
307
  }
224
308
  end
225
-
226
309
  end
227
-
228
310
  end