kharon 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/validate.rb CHANGED
@@ -1,4 +1,4 @@
1
- module Charon
1
+ module Kharon
2
2
  module Helpers
3
3
 
4
4
  # Validates the datas passed as parameter with a Phenix::Validator and the given instructions.
@@ -6,7 +6,7 @@ module Charon
6
6
  # @param [Proc] the instructions to apply on the validator.
7
7
  # @return [Hash] the validated and filtered datas.
8
8
  def validate(datas, &block)
9
- validator = Phenix::Validator.new(datas)
9
+ validator = Kharon::Validator.new(datas)
10
10
  validator.instance_eval(&block)
11
11
  return validator.filtered
12
12
  end
data/lib/validator.rb CHANGED
@@ -1,4 +1,4 @@
1
- module Charon
1
+ module Kharon
2
2
  # The validator uses aquarium as an AOP DSL to provide "before" and "after" joint point to its main methods.
3
3
  # @author Vincent Courtois <vincent.courtois@mycar-innovations.com>
4
4
  class Validator
@@ -32,7 +32,7 @@ module Charon
32
32
  # @param [Object] key the key about which verify the type.
33
33
  # @param [Hash] options a hash of options passed to this method (see documentation to know which options pass).
34
34
  def numeric(key, options = {})
35
- match?(key, /\A([+-]?\d+)([,.](\d+))?\Z/) ? store(key, ->(item){item.sub(/,/, ".").to_f}, options) : raise_type_error(key, "Numeric")
35
+ match?(key, /\A([+-]?\d+)([,.](\d+))?\Z/) ? store(key, ->(item){item.to_s.sub(/,/, ".").to_f}, options) : raise_type_error(key, "Numeric")
36
36
  end
37
37
 
38
38
  # Checks if the given key is a not-empty string or not.
@@ -108,7 +108,7 @@ module Charon
108
108
  end
109
109
 
110
110
  # After advice checking in numerics if limits are given, and if there are, if they are respected.
111
- after calls_to: [:integer, :numeric] do |joint_point, validator, *args|
111
+ before calls_to: [:integer, :numeric] do |joint_point, validator, *args|
112
112
  unless !defined?(args[1]) or args[1].nil? or args[1].empty?
113
113
  if(args[1].has_key?(:between))
114
114
  validator.check_min_value(args[0], args[1][:between][0])
@@ -120,6 +120,18 @@ module Charon
120
120
  end
121
121
  end
122
122
 
123
+ after calls_to: [:numeric] do |joint_point, validator, *args|
124
+ unless !defined?(args[1]) or args[1].nil? or args[1].empty?
125
+ if(args[1].has_key?(:round) and args[1][:round].kind_of?(Integer))
126
+ validator.filtered[args[0]] = validator.filtered[args[0]].round(args[1][:round]) if validator.filtered.has_key?(args[0])
127
+ elsif(args[1].has_key?(:floor) and args[1][:floor] == true)
128
+ validator.filtered[args[0]] = validator.filtered[args[0]].floor if validator.filtered.has_key?(args[0])
129
+ elsif(args[1].has_key?(:ceil) and args[1][:ceil] == true)
130
+ validator.filtered[args[0]] = validator.filtered[args[0]].ceil if validator.filtered.has_key?(args[0])
131
+ end
132
+ end
133
+ end
134
+
123
135
  # After advcie for all methods, checking the "in" and "equals" options.
124
136
  after calls_to: self.instance_methods(false), exclude_methods: [:initialize, :new, :required, :dependency, :dependencies] do |joint_point, validator, *args|
125
137
  unless !defined?(args[1]) or args[1].nil? or args[1].empty?
@@ -146,6 +158,12 @@ module Charon
146
158
  end
147
159
  end
148
160
 
161
+ after calls_to: [:text] do |joint_point, validator, *args|
162
+ unless !defined?(args[1]) or args[1].nil? or args[1].empty?
163
+ validator.match_regex?(args[0], validator.datas[args[0]], args[1][:regex]) if(args[1].has_key?(:regex))
164
+ end
165
+ end
166
+
149
167
  # @!endgroup Advices
150
168
 
151
169
  # Checks if a required key is present in provided datas.
@@ -220,6 +238,11 @@ module Charon
220
238
  raise ArgumentError.new("The key #{key} was supposed to contains values [#{required_values.join(", ")}]") if (values & required_values) != required_values
221
239
  end
222
240
 
241
+ def match_regex?(key, value, regex)
242
+ regex = Regexp.new(regex) if regex.kind_of?(String)
243
+ raise ArgumentError.new("The key #{key} was supposed to match the regex #{regex} but its value was #{value}") unless regex.match(value)
244
+ end
245
+
223
246
  private
224
247
 
225
248
  # Check if the value associated with the given key matches the given regular expression.
@@ -4,103 +4,103 @@ require './lib/validator.rb'
4
4
  shared_examples "options" do |process|
5
5
  context ":rename" do
6
6
  it "correctly renames a key when the value is valid" do
7
- validator = Charon::Validator.new(valid_datas)
7
+ validator = Kharon::Validator.new(valid_datas)
8
8
  validator.send(process, valid_datas.keys.first, rename: :another_name)
9
9
  expect(validator.filtered[:another_name]).to eq(valid_filtered[valid_datas.keys.first])
10
10
  end
11
11
 
12
12
  it "correctly doesn't rename a key when the value is invalid" do
13
- validator = Charon::Validator.new(invalid_datas)
13
+ validator = Kharon::Validator.new(invalid_datas)
14
14
  expect(->{validator.send(process, invalid_datas.keys.first, rename: :another_name)}).to raise_error(ArgumentError)
15
15
  end
16
16
  end
17
17
 
18
18
  context ":dependency" do
19
19
  it "succeeds when a dependency is given as a key and respected" do
20
- validator = Charon::Validator.new(valid_datas.merge({dep: "anything"}))
20
+ validator = Kharon::Validator.new(valid_datas.merge({dep: "anything"}))
21
21
  validator.send(process, valid_datas.keys.first, dependency: :dep)
22
22
  expect(validator.filtered).to eq(valid_filtered)
23
23
  end
24
24
 
25
25
  it "fails when a dependency is not respected" do
26
- validator = Charon::Validator.new(valid_datas)
26
+ validator = Kharon::Validator.new(valid_datas)
27
27
  expect(->{validator.send(process, valid_datas.keys.first, dependency: :another_key_not_existing)}).to raise_error(ArgumentError)
28
28
  end
29
29
  end
30
30
 
31
31
  context ":dependencies" do
32
32
  it "succeeds when dependencies are given as an array and respected" do
33
- validator = Charon::Validator.new(valid_datas.merge({dep1: "something", dep2: "something else"}))
33
+ validator = Kharon::Validator.new(valid_datas.merge({dep1: "something", dep2: "something else"}))
34
34
  validator.send(process, valid_datas.keys.first, dependencies: [:dep1, :dep2])
35
35
  expect(validator.filtered).to eq(valid_filtered)
36
36
  end
37
37
 
38
38
  it "fails when one of the dependencies is not respected" do
39
- validator = Charon::Validator.new(valid_datas.merge({dep1: "anything"}))
39
+ validator = Kharon::Validator.new(valid_datas.merge({dep1: "anything"}))
40
40
  expect(->{validator.send(process, valid_datas.keys.first, dependencies: [:dep1, :dep2])}).to raise_error(ArgumentError)
41
41
  end
42
42
  end
43
43
 
44
44
  context ":required" do
45
45
  it "succeeds when a not required key is not given, but filters nothing" do
46
- validator = Charon::Validator.new(valid_datas)
46
+ validator = Kharon::Validator.new(valid_datas)
47
47
  validator.send(process, :not_required_key)
48
48
  expect(validator.filtered).to be_empty
49
49
  end
50
50
 
51
51
  it "suceeds when a key has a required option to false, and is not given, but filters nothing" do
52
- validator = Charon::Validator.new(valid_datas)
52
+ validator = Kharon::Validator.new(valid_datas)
53
53
  validator.send(process, :not_in_hash, required: false)
54
54
  expect(validator.filtered).to be_empty
55
55
  end
56
56
 
57
57
  it "fails when a required key is not given" do
58
- validator = Charon::Validator.new(valid_datas)
58
+ validator = Kharon::Validator.new(valid_datas)
59
59
  expect(->{validator.send(process, :not_in_hash, required: true)}).to raise_error(ArgumentError)
60
60
  end
61
61
  end
62
62
 
63
63
  context ":in" do
64
64
  it "succeeds when the value is effectively in the possible values" do
65
- validator = Charon::Validator.new(valid_datas)
65
+ validator = Kharon::Validator.new(valid_datas)
66
66
  validator.send(process, valid_datas.keys.first, :in => [valid_datas[valid_datas.keys.first], "another random data"])
67
67
  expect(validator.filtered).to eq(valid_filtered)
68
68
  end
69
69
 
70
70
  it "succeeds if there are no values" do
71
- validator = Charon::Validator.new(valid_datas)
71
+ validator = Kharon::Validator.new(valid_datas)
72
72
  validator.send(process, valid_datas.keys.first, :in => [])
73
73
  expect(validator.filtered).to eq(valid_filtered)
74
74
  end
75
75
 
76
76
  it "fails if the value is not in the possible values" do
77
- validator = Charon::Validator.new(valid_datas)
77
+ validator = Kharon::Validator.new(valid_datas)
78
78
  expect(->{validator.send(process, valid_datas.keys.first, :in => ["anything but the value", "another impossible thing"])}).to raise_error(ArgumentError)
79
79
  end
80
80
  end
81
81
 
82
82
  context ":equals" do
83
83
  it "succeeds when the value is equal to the given value" do
84
- validator = Charon::Validator.new(valid_datas)
84
+ validator = Kharon::Validator.new(valid_datas)
85
85
  validator.send(process, valid_datas.keys.first, :equals => valid_datas[valid_datas.keys.first])
86
86
  expect(validator.filtered).to eq(valid_filtered)
87
87
  end
88
88
 
89
89
  it "fails if the value is not equal to the given value" do
90
- validator = Charon::Validator.new(valid_datas)
90
+ validator = Kharon::Validator.new(valid_datas)
91
91
  expect(->{validator.send(process, valid_datas.keys.first, :equals => "anything but the given value")}).to raise_error(ArgumentError)
92
92
  end
93
93
  end
94
94
 
95
95
  context ":extract" do
96
96
  it "etracts the data when given at true" do
97
- validator = Charon::Validator.new(valid_datas)
97
+ validator = Kharon::Validator.new(valid_datas)
98
98
  validator.send(process, valid_datas.keys.first, :extract => false)
99
99
  expect(validator.filtered).to eq({})
100
100
  end
101
101
 
102
102
  it "doesn't extract the data when given at false" do
103
- validator = Charon::Validator.new(valid_datas)
103
+ validator = Kharon::Validator.new(valid_datas)
104
104
  validator.send(process, valid_datas.keys.first, :extract => true)
105
105
  expect(validator.filtered).to eq(valid_filtered)
106
106
  end
@@ -108,13 +108,13 @@ shared_examples "options" do |process|
108
108
 
109
109
  context ":cast" do
110
110
  it "casts the data when given at true" do
111
- validator = Charon::Validator.new(valid_datas)
111
+ validator = Kharon::Validator.new(valid_datas)
112
112
  validator.send(process, valid_datas.keys.first, :cast => true)
113
113
  expect(validator.filtered).to eq(valid_filtered)
114
114
  end
115
115
 
116
116
  it "doesn't cast the data when given at false" do
117
- validator = Charon::Validator.new(valid_datas)
117
+ validator = Kharon::Validator.new(valid_datas)
118
118
  validator.send(process, valid_datas.keys.first, :cast => false)
119
119
  expect(validator.filtered).to eq(valid_datas)
120
120
  end
@@ -123,20 +123,20 @@ end
123
123
 
124
124
  shared_examples "type checker" do |type, process|
125
125
  it "succeeds when given an instance of #{type}" do
126
- validator = Charon::Validator.new(valid_datas)
126
+ validator = Kharon::Validator.new(valid_datas)
127
127
  validator.send(process, valid_datas.keys.first)
128
128
  expect(validator.filtered).to eq(valid_filtered)
129
129
  end
130
130
 
131
131
  it "fails when given something else than an instance of #{type}" do
132
- validator = Charon::Validator.new(invalid_datas)
132
+ validator = Kharon::Validator.new(invalid_datas)
133
133
  expect(->{validator.send(process, invalid_datas.keys.first)}).to raise_error(ArgumentError)
134
134
  end
135
135
  end
136
136
 
137
137
  shared_examples "min/max checker" do |process, key, transformation|
138
138
  let(:value) { valid_datas[key].send(transformation) }
139
- let(:validator) { Charon::Validator.new(valid_datas) }
139
+ let(:validator) { Kharon::Validator.new(valid_datas) }
140
140
 
141
141
  context ":min" do
142
142
  it "succeeds when a min option is given, and the value is strictly greater than it" do
@@ -200,18 +200,18 @@ end
200
200
  shared_examples "contains option" do |process, key|
201
201
  context ":contains" do
202
202
  it "succeeds if all values are contained" do
203
- validator = Charon::Validator.new(valid_datas)
203
+ validator = Kharon::Validator.new(valid_datas)
204
204
  validator.send(process, key, contains: ["val1", "val2"])
205
205
  expect(validator.filtered).to eq(valid_filtered)
206
206
  end
207
207
 
208
208
  it "fails if only some values are contained" do
209
- validator = Charon::Validator.new(valid_datas)
209
+ validator = Kharon::Validator.new(valid_datas)
210
210
  expect(->{validator.send(process, key, contains: ["val1", "val3"])}).to raise_error(ArgumentError)
211
211
  end
212
212
 
213
213
  it "fails if none of the values are contained" do
214
- validator = Charon::Validator.new(valid_datas)
214
+ validator = Kharon::Validator.new(valid_datas)
215
215
  expect(->{validator.send(process, key, contains: ["val3", "val4"])}).to raise_error(ArgumentError)
216
216
  end
217
217
  end
@@ -224,18 +224,18 @@ describe "Validator" do
224
224
  let(:invalid_datas) { {is_not_an_integer: "something else"} }
225
225
 
226
226
  it "succeeds when given an integer" do
227
- validator = Charon::Validator.new(valid_datas)
227
+ validator = Kharon::Validator.new(valid_datas)
228
228
  validator.integer(:is_an_integer)
229
229
  expect(validator.filtered).to eq(valid_filtered)
230
230
  end
231
231
 
232
232
  it "fails when given a float" do
233
- validator = Charon::Validator.new({is_not_an_integer: 1000.5})
233
+ validator = Kharon::Validator.new({is_not_an_integer: 1000.5})
234
234
  expect(->{validator.integer(:is_not_an_integer)}).to raise_error(ArgumentError)
235
235
  end
236
236
 
237
237
  it "fails when not given a numeric" do
238
- validator = Charon::Validator.new(invalid_datas)
238
+ validator = Kharon::Validator.new(invalid_datas)
239
239
  expect(->{validator.integer(:is_not_an_integer)}).to raise_error(ArgumentError)
240
240
  end
241
241
 
@@ -251,31 +251,73 @@ describe "Validator" do
251
251
  let(:invalid_datas) { {is_not_a_numeric: "something else"} }
252
252
 
253
253
  it "succeeds when given an integer" do
254
- validator = Charon::Validator.new({is_an_integer: "1000"})
254
+ validator = Kharon::Validator.new({is_an_integer: "1000"})
255
255
  validator.numeric(:is_an_integer)
256
256
  expect(validator.filtered).to eq({is_an_integer: 1000})
257
257
  end
258
258
 
259
259
  it "succeeds when given an decimal number with a dot" do
260
- validator = Charon::Validator.new(valid_datas)
260
+ validator = Kharon::Validator.new(valid_datas)
261
261
  validator.numeric(:is_a_double)
262
262
  expect(validator.filtered).to eq(valid_filtered)
263
263
  end
264
264
 
265
265
  it "succeeds when given an decimal number with a comma" do
266
- validator = Charon::Validator.new({is_a_double: "1000,5"})
266
+ validator = Kharon::Validator.new({is_a_double: "1000,5"})
267
267
  validator.numeric(:is_a_double)
268
268
  expect(validator.filtered).to eq(valid_filtered)
269
269
  end
270
270
 
271
271
  it "fails when not given a numeric" do
272
- validator = Charon::Validator.new(invalid_datas)
272
+ validator = Kharon::Validator.new(invalid_datas)
273
273
  expect(->{validator.integer(:is_not_a_numeric)}).to raise_error(ArgumentError)
274
274
  end
275
275
 
276
276
  context "options" do
277
277
  include_examples "options", :numeric
278
278
  include_examples "min/max checker", :numeric, :is_a_double, :to_f
279
+
280
+ context ":round" do
281
+ it "rounds the number when the option is passed as an integer" do
282
+ validator = Kharon::Validator.new({is_a_double: 1.02363265})
283
+ validator.numeric(:is_a_double, round: 4)
284
+ expect(validator.filtered).to eq({is_a_double: 1.0236})
285
+ end
286
+
287
+ it "doesn't round the number if passed with another type" do
288
+ validator = Kharon::Validator.new({is_a_double: "1.02363265"})
289
+ validator.numeric(:is_a_double, round: "anything here")
290
+ expect(validator.filtered).to eq({is_a_double: 1.02363265})
291
+ end
292
+ end
293
+
294
+ context ":floor" do
295
+ it "floors the number if passed to true" do
296
+ validator = Kharon::Validator.new(valid_datas)
297
+ validator.numeric(:is_a_double, floor: true)
298
+ expect(validator.filtered).to eq({is_a_double: 1000})
299
+ end
300
+
301
+ it "doesn't floor the number if passed to false" do
302
+ validator = Kharon::Validator.new(valid_datas)
303
+ validator.numeric(:is_a_double, floor: false)
304
+ expect(validator.filtered).to eq({is_a_double: 1000.5})
305
+ end
306
+ end
307
+
308
+ context ":ceil" do
309
+ it "ceils the number if passed to true" do
310
+ validator = Kharon::Validator.new(valid_datas)
311
+ validator.numeric(:is_a_double, ceil: true)
312
+ expect(validator.filtered).to eq({is_a_double: 1001})
313
+ end
314
+
315
+ it "doesn't ceil the number if passed to false" do
316
+ validator = Kharon::Validator.new(valid_datas)
317
+ validator.numeric(:is_a_double, ceil: false)
318
+ expect(validator.filtered).to eq({is_a_double: 1000.5})
319
+ end
320
+ end
279
321
  end
280
322
  end
281
323
 
@@ -288,6 +330,19 @@ describe "Validator" do
288
330
 
289
331
  context "options" do
290
332
  include_examples "options", :text
333
+
334
+ context ":regex" do
335
+ it "succeeds when the regular expression is respected" do
336
+ validator = Kharon::Validator.new(valid_datas)
337
+ validator.text(:is_a_string, regex: "some")
338
+ expect(validator.filtered).to eq(valid_filtered)
339
+ end
340
+
341
+ it "fails when the regular expression is not respected" do
342
+ validator = Kharon::Validator.new(valid_datas)
343
+ expect(->{validator.text(:is_a_string, regex: "anything else")}).to raise_error(ArgumentError)
344
+ end
345
+ end
291
346
  end
292
347
  end
293
348
 
@@ -298,19 +353,19 @@ describe "Validator" do
298
353
  let(:invalid_datas) { {is_not_a_datetime: "something else"} }
299
354
 
300
355
  it "succeeds when given a valid datetime as a string" do
301
- validator = Charon::Validator.new(valid_datas)
356
+ validator = Kharon::Validator.new(valid_datas)
302
357
  validator.datetime(:is_a_datetime)
303
358
  expect(validator.filtered).to eq(valid_filtered)
304
359
  end
305
360
 
306
361
  it "succeeds when given a valid datetime as a DateTime Object" do
307
- validator = Charon::Validator.new(valid_filtered)
362
+ validator = Kharon::Validator.new(valid_filtered)
308
363
  validator.datetime(:is_a_datetime)
309
364
  expect(validator.filtered).to eq(valid_filtered)
310
365
  end
311
366
 
312
367
  it "fails when given something else than a valid datetime" do
313
- validator = Charon::Validator.new(invalid_datas)
368
+ validator = Kharon::Validator.new(invalid_datas)
314
369
  expect(->{validator.datetime(:is_not_a_datetime)}).to raise_error(ArgumentError)
315
370
  end
316
371
 
@@ -326,19 +381,19 @@ describe "Validator" do
326
381
  let(:invalid_datas) { {is_not_a_date: "something else"} }
327
382
 
328
383
  it "succeeds when given a valid date as a string" do
329
- validator = Charon::Validator.new(valid_datas)
384
+ validator = Kharon::Validator.new(valid_datas)
330
385
  validator.date(:is_a_date)
331
386
  expect(validator.filtered).to eq(valid_filtered)
332
387
  end
333
388
 
334
389
  it "succeeds when given a valid date as a Date Object" do
335
- validator = Charon::Validator.new(valid_filtered)
390
+ validator = Kharon::Validator.new(valid_filtered)
336
391
  validator.date(:is_a_date)
337
392
  expect(validator.filtered).to eq(valid_filtered)
338
393
  end
339
394
 
340
395
  it "fails when given something else than a valid date" do
341
- validator = Charon::Validator.new(invalid_datas)
396
+ validator = Kharon::Validator.new(invalid_datas)
342
397
  expect(->{validator.date(:is_not_a_date)}).to raise_error(ArgumentError)
343
398
  end
344
399
 
@@ -373,18 +428,18 @@ describe "Validator" do
373
428
 
374
429
  context ":has_keys" do
375
430
  it "succeeds if all keys are contained in the hash" do
376
- validator = Charon::Validator.new(valid_datas)
431
+ validator = Kharon::Validator.new(valid_datas)
377
432
  validator.hash(:is_a_hash, has_keys: [:key1, :key2])
378
433
  expect(validator.filtered).to eq(valid_filtered)
379
434
  end
380
435
 
381
436
  it "fails if not all keys are given in the hash" do
382
- validator = Charon::Validator.new(valid_datas)
437
+ validator = Kharon::Validator.new(valid_datas)
383
438
  expect(->{validator.hash(:is_a_hash, has_keys: [:key1, :key3])}).to raise_error(ArgumentError)
384
439
  end
385
440
 
386
441
  it "fails if no keys are contained in the hash" do
387
- validator = Charon::Validator.new(valid_datas)
442
+ validator = Kharon::Validator.new(valid_datas)
388
443
  expect(->{validator.hash(:is_a_hash, has_keys: [:key3, :key4])}).to raise_error(ArgumentError)
389
444
  end
390
445
  end
@@ -397,13 +452,13 @@ describe "Validator" do
397
452
  let(:invalid_datas) { {is_not_a_boolean: "anything else"} }
398
453
 
399
454
  it "succeeds when given a boolean" do
400
- validator = Charon::Validator.new(valid_datas)
455
+ validator = Kharon::Validator.new(valid_datas)
401
456
  validator.boolean(:is_a_boolean)
402
457
  expect(validator.filtered).to eq(valid_filtered)
403
458
  end
404
459
 
405
460
  it "fails when not given a boolean" do
406
- validator = Charon::Validator.new(invalid_datas)
461
+ validator = Kharon::Validator.new(invalid_datas)
407
462
  expect(->{validator.boolean(:is_not_a_boolean)}).to raise_error(ArgumentError)
408
463
  end
409
464
 
@@ -419,13 +474,13 @@ describe "Validator" do
419
474
  let(:invalid_datas) { {is_not_a_ssid: "anything else"} }
420
475
 
421
476
  it "succeeds when given a valid SSID" do
422
- validator = Charon::Validator.new(valid_datas)
477
+ validator = Kharon::Validator.new(valid_datas)
423
478
  validator.ssid(:is_a_ssid)
424
479
  expect(validator.filtered).to eq(valid_filtered)
425
480
  end
426
481
 
427
482
  it "fails when not given a SSID" do
428
- validator = Charon::Validator.new(invalid_datas)
483
+ validator = Kharon::Validator.new(invalid_datas)
429
484
  expect(->{validator.ssid(:is_not_a_ssid)}).to raise_error(ArgumentError)
430
485
  end
431
486
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kharon
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: