kharon 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -26,6 +26,20 @@ gem install kharon
26
26
 
27
27
  And... That's it ! Now it's installed and you can learn how to properly use it !
28
28
 
29
+ ### With bundler
30
+
31
+ Add it to your Gemfile :
32
+
33
+ ```
34
+ gem "kharon", "~> 0.4.0"
35
+ ```
36
+
37
+ then run the following command :
38
+
39
+ ```
40
+ bundle install
41
+ ```
42
+
29
43
  ### From sources
30
44
 
31
45
  Clone this repository whenether you want, go inside, then type the following command :
Binary file
@@ -10,7 +10,7 @@ Gem::Specification.new do |specification|
10
10
  specification.summary = "Ruby Hash validator"
11
11
  specification.description = "Kharon is a ruby hash validator that helps you fix the structure of a hash (type of the keys, dependencies, ...)."
12
12
  specification.authors = ["Vincent Courtois"]
13
- specification.email = "vincent.courtois@mycar-innovations.com"
13
+ specification.email = "courtois.vincent@outlook.com"
14
14
  specification.files = `git ls-files`.split($/)
15
15
  specification.homepage = "https://rubygems.org/gems/kharon"
16
16
  specification.license = "Apache License 2"
@@ -1,7 +1,24 @@
1
1
  require "kharon/version"
2
2
  require "kharon/validator"
3
3
  require "kharon/helpers/validate"
4
+ require "kharon/configuration"
5
+
6
+ require "kharon/errors/validation"
7
+
8
+ require "kharon/handlers/exceptions"
9
+ require "kharon/handlers/messages"
4
10
 
5
11
  module Kharon
6
12
 
13
+ # Configuration method used to tell the module if it uses exceptions or stores error messages.
14
+ # @param [Boolean] use TRUE if you want to use exceptions, FALSE else.
15
+ def self.use_exceptions(use = true)
16
+ Kharon::Configuration.instance.use_exceptions(use)
17
+ end
18
+
19
+ # Returns the current error handler, defined by if you use exceptions or not.
20
+ # @return [Object] an instance of Kharon::Handlers::Exceptions if you use exceptions, an instance of Kharon::Handlers::Messages else.
21
+ def self.errors_handler
22
+ Kharon::Configuration.instance.uses_exceptions? ? Kharon::Handlers::Exceptions.instance : Kharon::Handlers::Messages.new
23
+ end
7
24
  end
@@ -0,0 +1,28 @@
1
+ require "singleton"
2
+ require "yaml"
3
+
4
+ module Kharon
5
+
6
+ # Simple wrapper for the configuration.
7
+ # @author Vincent Courtois <courtois.vincent@outlook.com>
8
+ class Configuration
9
+ include Singleton
10
+
11
+ attr_accessor :configuration
12
+
13
+ # Constructor of the configuration, initializes default values for each usable key.
14
+ def initialize
15
+ @configuration = {
16
+ exceptions: true
17
+ }
18
+ end
19
+
20
+ def use_exceptions(use = true)
21
+ @configuration[:exceptions] = use
22
+ end
23
+
24
+ def uses_exceptions?
25
+ @configuration[:exceptions]
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,25 @@
1
+ module Kharon
2
+ module Errors
3
+
4
+ # Standard exception raised in case the exceptions are used, and there is an error in the validation.
5
+ # @author Vincent Courtois <courtois.vincent@outlook.com>
6
+ class Validation < Exception
7
+
8
+ # @!attribute [rw] error_hash
9
+ # @return [Hash] the description of the encountered error as a Hash.
10
+ attr_accessor :error_hash
11
+
12
+ # Constructor of the class.
13
+ # @param [Hash] error_hash the description of the encountered error as a Hash.
14
+ def initialize(error_hash)
15
+ @error_hash = error_hash
16
+ end
17
+
18
+ # Generates a JSON version of the encountered error description hash.
19
+ # @return [String] the JSON representation of an error.
20
+ def message
21
+ JSON.generate(error_hash)
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,17 @@
1
+ module Kharon
2
+ module Handlers
3
+
4
+ # Errors handler that raises exception as soon as a problem is encountered during validation.
5
+ # @author Vincent Courtois <courtois.vincent@outlook.com>
6
+ class Exceptions
7
+ include Singleton
8
+
9
+ # Method used to report an error by raising the correct type of exception.
10
+ # @param [Hash] error_hash a Hash describing the error.
11
+ # @raises [Kharon::Errors::Validation] the exception raised when an error is encountered.
12
+ def report_error(error_hash)
13
+ raise Kharon::Errors::Validation.new(error_hash)
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,21 @@
1
+ module Kharon
2
+ module Handlers
3
+
4
+ # Errors handler that stores each problem encountered during validation.
5
+ # @author Vincent Courtois <courtois.vincent@outlook.com>
6
+ class Messages
7
+
8
+ # @!attribute [rw] errors the errors stored if encountered during validation process.
9
+ # @return [Array] an array of hashes, each Hash being the description of an error.
10
+ attr_accessor :errors
11
+
12
+ # Method used to report an error by storing it in an array.
13
+ # @param [Hash] error_hash a Hash describing the error.
14
+ # @return [Kharon::Handlers::Messages] the errors handler after insertion, so several calls can be chained.
15
+ def report_error(error_hash)
16
+ errors.push(error_hash)
17
+ self
18
+ end
19
+ end
20
+ end
21
+ end
@@ -6,7 +6,7 @@ module Kharon
6
6
  # @param [Proc] block the instructions to apply on the validator.
7
7
  # @return [Hash] the validated and filtered datas.
8
8
  def validate(datas, &block)
9
- validator = Kharon::Factory.validator(datas)
9
+ validator = Kharon::Validator.new(datas)
10
10
  validator.instance_eval(&block)
11
11
  return validator.filtered
12
12
  end
@@ -1,17 +1,21 @@
1
1
  module Kharon
2
2
 
3
3
  # The validator is the main class of Kharon, it validates a hash given a structure.
4
- # @author Vincent Courtois <vincent.courtois@mycar-innovations.com>
4
+ # @author Vincent Courtois <courtois.vincent@outlook.com>
5
5
  class Validator
6
6
 
7
7
  # @!attribute [r] datas
8
- # @return The datas to filter, they shouldn't be modified to guarantee their integrity.
8
+ # @return [Hash] The datas to filter, they shouldn't be modified to guarantee their integrity.
9
9
  attr_reader :datas
10
10
 
11
11
  # @!attribute [rw] filtered
12
- # @return The filtered datas are the datas after they have been filtered (renamed keys for example) by the validator.
12
+ # @return [Hash] The filtered datas are the datas after they have been filtered (renamed keys for example) by the validator.
13
13
  attr_accessor :filtered
14
14
 
15
+ # @!attribute [rw] handler
16
+ # @return [Object] the error handler given to this instance of the validator.
17
+ attr_accessor :handler
18
+
15
19
  # Constructor of the classe, receiving the datas to validate and filter.
16
20
  # @param [Hash] datas the datas to validate in the validator.
17
21
  # @example create a new instance of validator.
@@ -19,6 +23,7 @@ module Kharon
19
23
  def initialize(datas)
20
24
  @datas = datas
21
25
  @filtered = Hash.new
26
+ @handler = Kharon.errors_handler
22
27
  end
23
28
 
24
29
  # Checks if the given key is an integer or not.
@@ -215,19 +220,19 @@ module Kharon
215
220
 
216
221
  def store_box(key, options)
217
222
  if(options.has_key?(:at_least))
218
- box_contains?(datas[key], options[:at_least])
223
+ box_contains?(key, datas[key], options[:at_least])
219
224
  end
220
225
  if(options.has_key?(:at_most))
221
- box_contains?(options[:at_most], datas[key])
226
+ box_contains?(key, options[:at_most], datas[key])
222
227
  end
223
- store(key, ->(item){parse_box(datas[key])}, options)
228
+ store(key, ->(item){parse_box(key, datas[key])}, options)
224
229
  end
225
230
 
226
231
  # Checks if a required key is present in provided datas.
227
232
  # @param [Object] key the key of which check the presence.
228
233
  # @raise [ArgumentError] if the key is not present.
229
234
  def required(key)
230
- raise_error("The key #{key} is required and not provided.") unless @datas.has_key?(key)
235
+ raise_error(type: "required", key: key) unless @datas.has_key?(key)
231
236
  end
232
237
 
233
238
  # Syntaxic sugar used to chack several dependencies at once.
@@ -244,7 +249,7 @@ module Kharon
244
249
  # @param [Object] dependency the key needed by another key for it to properly work.
245
250
  # @raise [ArgumentError] if the required dependency is not present.
246
251
  def dependency(key, dependency)
247
- raise_error("The key #{key} needs the key #{dependency} but it was not provided.") unless @datas.has_key?(dependency)
252
+ raise_error(type: "dependency", key: "key", needed: dependency) unless @datas.has_key?(dependency)
248
253
  end
249
254
 
250
255
  # Checks if the value associated with the given key is greater than the given minimum value.
@@ -252,7 +257,7 @@ module Kharon
252
257
  # @param [Numeric] min_value the required minimum value.
253
258
  # @raise [ArgumentError] if the initial value is strictly lesser than the minimum value.
254
259
  def check_min_value(key, min_value)
255
- raise_error("The key #{key} was supposed to be greater or equal than #{min_value}, the value was #{datas[key]}") unless datas[key].to_i >= min_value.to_i
260
+ raise_error(type: "min", supposed: min_value, key: key, value: datas[key]) unless datas[key].to_i >= min_value.to_i
256
261
  end
257
262
 
258
263
  # Checks if the value associated with the given key is lesser than the given maximum value.
@@ -260,7 +265,7 @@ module Kharon
260
265
  # @param [Numeric] max_value the required maximum value.
261
266
  # @raise [ArgumentError] if the initial value is strictly greater than the minimum value.
262
267
  def check_max_value(key, max_value)
263
- raise_error("The key #{key} was supposed to be lesser or equal than #{max_value}, the value was #{datas[key]}") unless datas[key].to_i <= max_value.to_i
268
+ raise_error(type: "max", supposed: max_value, key: key, value: datas[key]) unless datas[key].to_i <= max_value.to_i
264
269
  end
265
270
 
266
271
  # Checks if the value associated with the given key is included in the given array of values.
@@ -268,7 +273,7 @@ module Kharon
268
273
  # @param [Array] values the values in which the initial value should be contained.
269
274
  # @raise [ArgumentError] if the initial value is not included in the given possible values.
270
275
  def in_array?(key, values)
271
- raise_error("The key #{key} was supposed to be in [#{values.join(", ")}], the value was #{datas[key]}") unless (values.empty? or values.include?(datas[key]))
276
+ raise_error(type: "array.in", key: key, supposed: values, value: datas[key]) unless (values.empty? or values.include?(datas[key]))
272
277
  end
273
278
 
274
279
  # Checks if the value associated with the given key is equal to the given value.
@@ -276,7 +281,7 @@ module Kharon
276
281
  # @param [Object] value the values with which the initial value should be compared.
277
282
  # @raise [ArgumentError] if the initial value is not equal to the given value.
278
283
  def equals_to?(key, value)
279
- raise_error("The key #{key} was supposed to equal than #{value}, the value was #{datas[key]}") unless datas[key] == value
284
+ raise_error(type: "equals", key: key, supposed: value, found: datas[key]) unless datas[key] == value
280
285
  end
281
286
 
282
287
  # Checks if the value associated with the given key has the given required keys.
@@ -284,7 +289,7 @@ module Kharon
284
289
  # @param [Array] required_keys the keys that the initial Hash typed value should contain.
285
290
  # @raise [ArgumentError] if the initial value has not each and every one of the given keys.
286
291
  def has_keys?(key, required_keys)
287
- raise_error("The key #{key} was supposed to contains keys [#{required_keys.join(", ")}]") if (datas[key].keys & required_keys) != required_keys
292
+ raise_error(type: "contains.keys", required: required_keys, key: key) if (datas[key].keys & required_keys) != required_keys
288
293
  end
289
294
 
290
295
  # Checks if the value associated with the given key has the given required values.
@@ -292,12 +297,12 @@ module Kharon
292
297
  # @param [Array] required_keys the values that the initial Enumerable typed value should contain.
293
298
  # @raise [ArgumentError] if the initial value has not each and every one of the given values.
294
299
  def contains?(key, values, required_values)
295
- raise_error("The key #{key} was supposed to contains values [#{required_values.join(", ")}]") if (values & required_values) != required_values
300
+ raise_error(type: "contains.values", required: required_values, key: key) if (values & required_values) != required_values
296
301
  end
297
302
 
298
303
  def match_regex?(key, value, regex)
299
304
  regex = Regexp.new(regex) if regex.kind_of?(String)
300
- raise_error("The key #{key} was supposed to match the regex #{regex} but its value was #{value}") unless regex.match(value)
305
+ raise_error(type: "regex", regex: regex, value: value, key: key) unless regex.match(value)
301
306
  end
302
307
 
303
308
  # Check if the value associated with the given key matches the given regular expression.
@@ -344,13 +349,13 @@ module Kharon
344
349
  # Parses a box given as a string of four numbers separated by commas.
345
350
  # @param [String] box the string representing the box.
346
351
  # @return [Array] an array of size 2, containing two arrays of size 2 (the first being the coordinates of the top-left corner, the second the ones of the bottom-right corner)
347
- def parse_box(box)
352
+ def parse_box(key, box)
348
353
  if box.kind_of?(String)
349
354
  begin
350
355
  raw_box = box.split(",").map(&:to_f)
351
356
  box = [[raw_box[0], raw_box[1]], [raw_box[2], raw_box[3]]]
352
357
  rescue
353
- raise_error("The box was not correctly formatted")
358
+ raise_error(type: "box.format", key: "key", value: box)
354
359
  end
355
360
  end
356
361
  return box
@@ -360,11 +365,11 @@ module Kharon
360
365
  # @param [Object] container any object that can be treated as a box, container of the other box
361
366
  # @param [Object] contained any object that can be treated as a box, contained in the other box
362
367
  # @return [Boolean] TRUE if the box is contained in the other one, FALSE if not.
363
- def box_contains?(container, contained)
364
- container = parse_box(container)
365
- contained = parse_box(contained)
368
+ def box_contains?(key, container, contained)
369
+ container = parse_box(key, container)
370
+ contained = parse_box(key, contained)
366
371
  result = ((container[0][0] <= contained[0][0]) and (container[0][1] <= container[0][1]) and (container[1][0] >= container[1][0]) and (container[1][1] >= container[1][1]))
367
- raise_error("The box #{contained} was supposed to be contained in #{container}") unless result
372
+ raise_error(type: "box.containment", contained: contained, container: container, key: key) unless result
368
373
  end
369
374
 
370
375
  # Raises a type error with a generic message.
@@ -372,7 +377,7 @@ module Kharon
372
377
  # @param [Class] type the expected type, not respected by the initial value.
373
378
  # @raise [ArgumentError] the chosen type error.
374
379
  def raise_type_error(key, type)
375
- raise_error("The key {key} was supposed to be an instance of #{type}, #{key.class} found.")
380
+ raise_error(type: "type", key: key, supposed: type, found: key.class)
376
381
  end
377
382
 
378
383
  protected
@@ -381,7 +386,13 @@ module Kharon
381
386
  # @param [String] message the the message to display with the exception.
382
387
  # @raises ArgumentError an error to stop the execution when this method is invoked.
383
388
  def raise_error(message)
384
- raise ArgumentError.new(message)
389
+ handler.report_error(message)
390
+ end
391
+
392
+ # Accessor for the errors, use only if the handler is a Kharon::Handlers::Messages.
393
+ # @return [Array] the errors encountered during validation or an empty array if the handler was a Kharon::Handlers::Exceptions.
394
+ def errors
395
+ handler.respond_to?(:errors) ? handler.errors : []
385
396
  end
386
397
 
387
398
  end
@@ -1,3 +1,3 @@
1
1
  module Kharon
2
- VERSION = "0.4.0"
2
+ VERSION = "0.5.0"
3
3
  end
@@ -0,0 +1,13 @@
1
+ require "spec_helper"
2
+
3
+ describe "Errors::Validation" do
4
+ let!(:error_hash) { {type: "type", key: "custom key"} }
5
+
6
+ it "correctly stores the error hash" do
7
+ expect(Kharon::Errors::Validation.new(error_hash).error_hash).to eq(error_hash)
8
+ end
9
+
10
+ it "correctly return the JSON equivalent of the error hash as message" do
11
+ expect(Kharon::Errors::Validation.new(error_hash).message).to eq(JSON.generate(error_hash))
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ require "spec_helper"
2
+
3
+ describe "Handlers::Exceptions" do
4
+ it "always returns the same instance" do
5
+ expect(Kharon::Handlers::Exceptions.instance).to be(Kharon::Handlers::Exceptions.instance)
6
+ end
7
+
8
+ context "#report_error" do
9
+ it "raises an error of the right type" do
10
+ expect{Kharon::Handlers::Exceptions.instance.report_error({})}.to raise_error(Kharon::Errors::Validation)
11
+ end
12
+ end
13
+ end
@@ -10,7 +10,7 @@ shared_examples "options" do |process|
10
10
 
11
11
  it "correctly doesn't rename a key when the value is invalid" do
12
12
  validator = Kharon::Validator.new(invalid_datas)
13
- expect(->{validator.send(process, invalid_datas.keys.first, rename: :another_name)}).to raise_error(ArgumentError)
13
+ expect(->{validator.send(process, invalid_datas.keys.first, rename: :another_name)}).to raise_error(Kharon::Errors::Validation)
14
14
  end
15
15
  end
16
16
 
@@ -23,7 +23,7 @@ shared_examples "options" do |process|
23
23
 
24
24
  it "fails when a dependency is not respected" do
25
25
  validator = Kharon::Validator.new(valid_datas)
26
- expect(->{validator.send(process, valid_datas.keys.first, dependency: :another_key_not_existing)}).to raise_error(ArgumentError)
26
+ expect(->{validator.send(process, valid_datas.keys.first, dependency: :another_key_not_existing)}).to raise_error(Kharon::Errors::Validation)
27
27
  end
28
28
  end
29
29
 
@@ -36,7 +36,7 @@ shared_examples "options" do |process|
36
36
 
37
37
  it "fails when one of the dependencies is not respected" do
38
38
  validator = Kharon::Validator.new(valid_datas.merge({dep1: "anything"}))
39
- expect(->{validator.send(process, valid_datas.keys.first, dependencies: [:dep1, :dep2])}).to raise_error(ArgumentError)
39
+ expect(->{validator.send(process, valid_datas.keys.first, dependencies: [:dep1, :dep2])}).to raise_error(Kharon::Errors::Validation)
40
40
  end
41
41
  end
42
42
 
@@ -55,7 +55,7 @@ shared_examples "options" do |process|
55
55
 
56
56
  it "fails when a required key is not given" do
57
57
  validator = Kharon::Validator.new(valid_datas)
58
- expect(->{validator.send(process, :not_in_hash, required: true)}).to raise_error(ArgumentError)
58
+ expect(->{validator.send(process, :not_in_hash, required: true)}).to raise_error(Kharon::Errors::Validation)
59
59
  end
60
60
  end
61
61
 
@@ -74,7 +74,7 @@ shared_examples "options" do |process|
74
74
 
75
75
  it "fails if the value is not in the possible values" do
76
76
  validator = Kharon::Validator.new(valid_datas)
77
- expect(->{validator.send(process, valid_datas.keys.first, :in => ["anything but the value", "another impossible thing"])}).to raise_error(ArgumentError)
77
+ expect(->{validator.send(process, valid_datas.keys.first, :in => ["anything but the value", "another impossible thing"])}).to raise_error(Kharon::Errors::Validation)
78
78
  end
79
79
  end
80
80
 
@@ -87,7 +87,7 @@ shared_examples "options" do |process|
87
87
 
88
88
  it "fails if the value is not equal to the given value" do
89
89
  validator = Kharon::Validator.new(valid_datas)
90
- expect(->{validator.send(process, valid_datas.keys.first, :equals => "anything but the given value")}).to raise_error(ArgumentError)
90
+ expect(->{validator.send(process, valid_datas.keys.first, :equals => "anything but the given value")}).to raise_error(Kharon::Errors::Validation)
91
91
  end
92
92
  end
93
93
 
@@ -129,7 +129,7 @@ shared_examples "type checker" do |type, process|
129
129
 
130
130
  it "fails when given something else than an instance of #{type}" do
131
131
  validator = Kharon::Validator.new(invalid_datas)
132
- expect(->{validator.send(process, invalid_datas.keys.first)}).to raise_error(ArgumentError)
132
+ expect(->{validator.send(process, invalid_datas.keys.first)}).to raise_error(Kharon::Errors::Validation)
133
133
  end
134
134
  end
135
135
 
@@ -149,7 +149,7 @@ shared_examples "min/max checker" do |process, key, transformation|
149
149
  end
150
150
 
151
151
  it "fails when a min option is given, but not respected" do
152
- expect(->{validator.send(process, key, {min: value+1})}).to raise_error(ArgumentError)
152
+ expect(->{validator.send(process, key, {min: value+1})}).to raise_error(Kharon::Errors::Validation)
153
153
  end
154
154
  end
155
155
 
@@ -165,7 +165,7 @@ shared_examples "min/max checker" do |process, key, transformation|
165
165
  end
166
166
 
167
167
  it "fails when a max option is given, but not respected" do
168
- expect(->{validator.send(process, key, {max: value-1})}).to raise_error(ArgumentError)
168
+ expect(->{validator.send(process, key, {max: value-1})}).to raise_error(Kharon::Errors::Validation)
169
169
  end
170
170
  end
171
171
 
@@ -177,11 +177,11 @@ shared_examples "min/max checker" do |process, key, transformation|
177
177
  end
178
178
 
179
179
  it "fails when a max between option is given, but the value is strictly lesser" do
180
- expect(->{validator.send(process, key, {between: [value+1, value+2]})}).to raise_error(ArgumentError)
180
+ expect(->{validator.send(process, key, {between: [value+1, value+2]})}).to raise_error(Kharon::Errors::Validation)
181
181
  end
182
182
 
183
183
  it "fails when a max between option is given, but the value is strictly greater" do
184
- expect(->{validator.send(process, key, {between: [value-2, value-1]})}).to raise_error(ArgumentError)
184
+ expect(->{validator.send(process, key, {between: [value-2, value-1]})}).to raise_error(Kharon::Errors::Validation)
185
185
  end
186
186
 
187
187
  it "fails when a max between option is given, but the value is equal to the inferior limit" do
@@ -206,12 +206,12 @@ shared_examples "contains option" do |process, key|
206
206
 
207
207
  it "fails if only some values are contained" do
208
208
  validator = Kharon::Validator.new(valid_datas)
209
- expect(->{validator.send(process, key, contains: ["val1", "val3"])}).to raise_error(ArgumentError)
209
+ expect(->{validator.send(process, key, contains: ["val1", "val3"])}).to raise_error(Kharon::Errors::Validation)
210
210
  end
211
211
 
212
212
  it "fails if none of the values are contained" do
213
213
  validator = Kharon::Validator.new(valid_datas)
214
- expect(->{validator.send(process, key, contains: ["val3", "val4"])}).to raise_error(ArgumentError)
214
+ expect(->{validator.send(process, key, contains: ["val3", "val4"])}).to raise_error(Kharon::Errors::Validation)
215
215
  end
216
216
  end
217
217
  end
@@ -230,12 +230,12 @@ describe "Validator" do
230
230
 
231
231
  it "fails when given a float" do
232
232
  validator = Kharon::Validator.new({is_not_an_integer: 1000.5})
233
- expect(->{validator.integer(:is_not_an_integer)}).to raise_error(ArgumentError)
233
+ expect(->{validator.integer(:is_not_an_integer)}).to raise_error(Kharon::Errors::Validation)
234
234
  end
235
235
 
236
236
  it "fails when not given a numeric" do
237
237
  validator = Kharon::Validator.new(invalid_datas)
238
- expect(->{validator.integer(:is_not_an_integer)}).to raise_error(ArgumentError)
238
+ expect(->{validator.integer(:is_not_an_integer)}).to raise_error(Kharon::Errors::Validation)
239
239
  end
240
240
 
241
241
  context "options" do
@@ -269,7 +269,7 @@ describe "Validator" do
269
269
 
270
270
  it "fails when not given a numeric" do
271
271
  validator = Kharon::Validator.new(invalid_datas)
272
- expect(->{validator.integer(:is_not_a_numeric)}).to raise_error(ArgumentError)
272
+ expect(->{validator.integer(:is_not_a_numeric)}).to raise_error(Kharon::Errors::Validation)
273
273
  end
274
274
 
275
275
  context "options" do
@@ -339,7 +339,7 @@ describe "Validator" do
339
339
 
340
340
  it "fails when the regular expression is not respected" do
341
341
  validator = Kharon::Validator.new(valid_datas)
342
- expect(->{validator.text(:is_a_string, regex: "anything else")}).to raise_error(ArgumentError)
342
+ expect(->{validator.text(:is_a_string, regex: "anything else")}).to raise_error(Kharon::Errors::Validation)
343
343
  end
344
344
  end
345
345
  end
@@ -365,7 +365,7 @@ describe "Validator" do
365
365
 
366
366
  it "fails when given something else than a valid datetime" do
367
367
  validator = Kharon::Validator.new(invalid_datas)
368
- expect(->{validator.datetime(:is_not_a_datetime)}).to raise_error(ArgumentError)
368
+ expect(->{validator.datetime(:is_not_a_datetime)}).to raise_error(Kharon::Errors::Validation)
369
369
  end
370
370
 
371
371
  context "options" do
@@ -393,7 +393,7 @@ describe "Validator" do
393
393
 
394
394
  it "fails when given something else than a valid date" do
395
395
  validator = Kharon::Validator.new(invalid_datas)
396
- expect(->{validator.date(:is_not_a_date)}).to raise_error(ArgumentError)
396
+ expect(->{validator.date(:is_not_a_date)}).to raise_error(Kharon::Errors::Validation)
397
397
  end
398
398
 
399
399
  context "options" do
@@ -434,12 +434,12 @@ describe "Validator" do
434
434
 
435
435
  it "fails if not all keys are given in the hash" do
436
436
  validator = Kharon::Validator.new(valid_datas)
437
- expect(->{validator.hash(:is_a_hash, has_keys: [:key1, :key3])}).to raise_error(ArgumentError)
437
+ expect(->{validator.hash(:is_a_hash, has_keys: [:key1, :key3])}).to raise_error(Kharon::Errors::Validation)
438
438
  end
439
439
 
440
440
  it "fails if no keys are contained in the hash" do
441
441
  validator = Kharon::Validator.new(valid_datas)
442
- expect(->{validator.hash(:is_a_hash, has_keys: [:key3, :key4])}).to raise_error(ArgumentError)
442
+ expect(->{validator.hash(:is_a_hash, has_keys: [:key3, :key4])}).to raise_error(Kharon::Errors::Validation)
443
443
  end
444
444
  end
445
445
  end
@@ -458,7 +458,7 @@ describe "Validator" do
458
458
 
459
459
  it "fails when not given a boolean" do
460
460
  validator = Kharon::Validator.new(invalid_datas)
461
- expect(->{validator.boolean(:is_not_a_boolean)}).to raise_error(ArgumentError)
461
+ expect(->{validator.boolean(:is_not_a_boolean)}).to raise_error(Kharon::Errors::Validation)
462
462
  end
463
463
 
464
464
  context "options" do
@@ -480,7 +480,7 @@ describe "Validator" do
480
480
 
481
481
  it "fails when not given a SSID" do
482
482
  validator = Kharon::Validator.new(invalid_datas)
483
- expect(->{validator.ssid(:is_not_a_ssid)}).to raise_error(ArgumentError)
483
+ expect(->{validator.ssid(:is_not_a_ssid)}).to raise_error(Kharon::Errors::Validation)
484
484
  end
485
485
 
486
486
  context "options" do
@@ -504,27 +504,27 @@ describe "Validator" do
504
504
 
505
505
  it "fails when not given a box" do
506
506
  validator = Kharon::Validator.new(invalid_datas)
507
- expect(->{validator.box(:is_not_a_box)}).to raise_error(ArgumentError)
507
+ expect(->{validator.box(:is_not_a_box)}).to raise_error(Kharon::Errors::Validation)
508
508
  end
509
509
 
510
510
  it "fails with an invalid top longitude" do
511
511
  validator = Kharon::Validator.new(is_an_invalid_box: "test,12,12.0,-12.0")
512
- expect(->{validator.box(:is_an_invalid_box)}).to raise_error(ArgumentError)
512
+ expect(->{validator.box(:is_an_invalid_box)}).to raise_error(Kharon::Errors::Validation)
513
513
  end
514
514
 
515
515
  it "fails with an invalid top latitude" do
516
516
  validator = Kharon::Validator.new(is_an_invalid_box: "0,test,12.0,-12.0")
517
- expect(->{validator.box(:is_an_invalid_box)}).to raise_error(ArgumentError)
517
+ expect(->{validator.box(:is_an_invalid_box)}).to raise_error(Kharon::Errors::Validation)
518
518
  end
519
519
 
520
520
  it "fails with an invalid down longitude" do
521
521
  validator = Kharon::Validator.new(is_an_invalid_box: "0,12,test,-12.0")
522
- expect(->{validator.box(:is_an_invalid_box)}).to raise_error(ArgumentError)
522
+ expect(->{validator.box(:is_an_invalid_box)}).to raise_error(Kharon::Errors::Validation)
523
523
  end
524
524
 
525
525
  it "fails with an invalid down latitude" do
526
526
  validator = Kharon::Validator.new(is_an_invalid_box: "0,12,12.0,test")
527
- expect(->{validator.box(:is_an_invalid_box)}).to raise_error(ArgumentError)
527
+ expect(->{validator.box(:is_an_invalid_box)}).to raise_error(Kharon::Errors::Validation)
528
528
  end
529
529
 
530
530
  context "options" do
@@ -541,7 +541,7 @@ describe "Validator" do
541
541
  end
542
542
 
543
543
  it "fails if the box is smaller than the one given with the at_least option" do
544
- expect(->{@validator.box(:is_a_box, at_least: [[-20, -20], [1000, 1000]])}).to raise_error(ArgumentError)
544
+ expect(->{@validator.box(:is_a_box, at_least: [[-20, -20], [1000, 1000]])}).to raise_error(Kharon::Errors::Validation)
545
545
  end
546
546
  end
547
547
 
@@ -556,7 +556,7 @@ describe "Validator" do
556
556
  end
557
557
 
558
558
  it "fails if the box is bigger than the one given with the at_most option" do
559
- expect(->{@validator.box(:is_a_box, at_most: [[10, 10], [20, 20]])}).to raise_error(ArgumentError)
559
+ expect(->{@validator.box(:is_a_box, at_most: [[10, 10], [20, 20]])}).to raise_error(Kharon::Errors::Validation)
560
560
  end
561
561
  end
562
562
  end
@@ -575,7 +575,7 @@ describe "Validator" do
575
575
 
576
576
  it "fails when not given a email" do
577
577
  validator = Kharon::Validator.new(invalid_datas)
578
- expect(->{validator.email(:is_not_an_email)}).to raise_error(ArgumentError)
578
+ expect(->{validator.email(:is_not_an_email)}).to raise_error(Kharon::Errors::Validation)
579
579
  end
580
580
 
581
581
  context "options" do
@@ -0,0 +1,13 @@
1
+ require "spec_helper"
2
+
3
+ describe "Kharon" do
4
+ it "Can not use exceptions" do
5
+ Kharon.use_exceptions(false)
6
+ expect(Kharon.errors_handler).to be_a(Kharon::Handlers::Messages)
7
+ end
8
+
9
+ it "Can use exceptions" do
10
+ Kharon.use_exceptions(true)
11
+ expect(Kharon.errors_handler).to be_a(Kharon::Handlers::Exceptions)
12
+ end
13
+ end
@@ -4,4 +4,6 @@ Bundler.setup
4
4
  # Sets the environment variable to test before loading all the files.
5
5
  ENV['RACK_ENV'] = 'test'
6
6
 
7
+ require "bson"
8
+ require "json"
7
9
  require "kharon"
File without changes
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.4.0
4
+ version: 0.5.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -29,7 +29,7 @@ dependencies:
29
29
  version: 2.2.2
30
30
  description: Kharon is a ruby hash validator that helps you fix the structure of a
31
31
  hash (type of the keys, dependencies, ...).
32
- email: vincent.courtois@mycar-innovations.com
32
+ email: courtois.vincent@outlook.com
33
33
  executables: []
34
34
  extensions: []
35
35
  extra_rdoc_files: []
@@ -46,13 +46,22 @@ files:
46
46
  - kharon-0.2.0.gem
47
47
  - kharon-0.3.0.gem
48
48
  - kharon-0.3.1.gem
49
+ - kharon-0.4.0.gem
49
50
  - kharon.gemspec
50
51
  - lib/kharon.rb
52
+ - lib/kharon/configuration.rb
53
+ - lib/kharon/errors/validation.rb
54
+ - lib/kharon/handlers/exceptions.rb
55
+ - lib/kharon/handlers/messages.rb
51
56
  - lib/kharon/helpers/validate.rb
52
57
  - lib/kharon/validator.rb
53
58
  - lib/kharon/version.rb
59
+ - spec/lib/kharon/errors/validation_spec.rb
60
+ - spec/lib/kharon/handlers/exceptions_spec.rb
54
61
  - spec/lib/kharon/validator_spec.rb
62
+ - spec/lib/kharon_spec.rb
55
63
  - spec/spec_helper.rb
64
+ - spec/utils/fake_config.yml
56
65
  homepage: https://rubygems.org/gems/kharon
57
66
  licenses:
58
67
  - Apache License 2