easy_json_matcher 0.2.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (72) hide show
  1. checksums.yaml +4 -4
  2. data/lib/easy_json_matcher/array_content_validator.rb +22 -0
  3. data/lib/easy_json_matcher/array_generator.rb +41 -0
  4. data/lib/easy_json_matcher/array_validator.rb +11 -76
  5. data/lib/easy_json_matcher/attribute_generator.rb +37 -0
  6. data/lib/easy_json_matcher/attribute_type_methods.rb +29 -0
  7. data/lib/easy_json_matcher/coercion_error.rb +10 -0
  8. data/lib/easy_json_matcher/content_wrapper.rb +2 -4
  9. data/lib/easy_json_matcher/{validation_error.rb → easy_json_matcher_error.rb} +2 -1
  10. data/lib/easy_json_matcher/json_coercer.rb +34 -0
  11. data/lib/easy_json_matcher/node.rb +10 -114
  12. data/lib/easy_json_matcher/node_generator.rb +58 -0
  13. data/lib/easy_json_matcher/schema_generator.rb +47 -71
  14. data/lib/easy_json_matcher/schema_library.rb +8 -11
  15. data/lib/easy_json_matcher/unknown_validation_step_error.rb +10 -0
  16. data/lib/easy_json_matcher/validation_chain_factory.rb +48 -0
  17. data/lib/easy_json_matcher/validation_rules.rb +59 -0
  18. data/lib/easy_json_matcher/validation_step.rb +36 -0
  19. data/lib/easy_json_matcher/validator.rb +12 -87
  20. data/lib/easy_json_matcher/validator_set.rb +31 -0
  21. data/lib/easy_json_matcher/version.rb +1 -1
  22. data/lib/easy_json_matcher.rb +6 -2
  23. data/lib/easy_json_matcher.rb~ +5 -0
  24. data/test/array_content_validator_test.rb +17 -0
  25. data/test/custom_validations_test.rb +17 -17
  26. data/test/global_validation_options_test.rb +39 -44
  27. data/test/json_coercer_test.rb +25 -0
  28. data/test/managing_schemas_test.rb +53 -52
  29. data/test/node_test.rb +28 -0
  30. data/test/primitives_boolean_test.rb +27 -0
  31. data/test/primitives_date_test.rb +28 -0
  32. data/test/primitives_number_test.rb +27 -0
  33. data/test/primitives_object_test.rb +27 -0
  34. data/test/primitives_string_test.rb +27 -0
  35. data/test/primtives_value_test.rb +23 -0
  36. data/test/required_validation_test.rb +7 -7
  37. data/test/schema_generator_test.rb +23 -0
  38. data/test/strict_mode_test.rb +25 -54
  39. data/test/test_helper.rb +8 -6
  40. data/test/validating_arrays_test.rb +40 -132
  41. data/test/validation_chain_factory_test.rb +45 -0
  42. data/test/validation_chain_test_helper.rb +13 -0
  43. data/test/validation_step_array_test.rb +27 -0
  44. data/test/validation_step_boolean_test.rb +19 -0
  45. data/test/validation_step_date_test.rb +20 -0
  46. data/test/validation_step_not_required_test.rb +35 -0
  47. data/test/validation_step_number_test.rb +23 -0
  48. data/test/validation_step_object_test.rb +20 -0
  49. data/test/validation_step_required_test.rb +19 -0
  50. data/test/validation_step_string_test.rb +20 -0
  51. data/test/validation_step_test.rb +55 -0
  52. data/test/validation_step_value_test.rb +15 -0
  53. data/test/validator_set_test.rb +49 -0
  54. data/test/validator_test.rb +44 -0
  55. metadata +64 -26
  56. data/lib/easy_json_matcher/boolean_validator.rb +0 -14
  57. data/lib/easy_json_matcher/date_validator.rb +0 -44
  58. data/lib/easy_json_matcher/node.rb~ +0 -121
  59. data/lib/easy_json_matcher/number_validator.rb +0 -22
  60. data/lib/easy_json_matcher/object_validator.rb +0 -12
  61. data/lib/easy_json_matcher/schema_generator.rb~ +0 -104
  62. data/lib/easy_json_matcher/string_validator.rb +0 -16
  63. data/lib/easy_json_matcher/validator.rb~ +0 -99
  64. data/lib/easy_json_matcher/validator_factory.rb +0 -76
  65. data/lib/easy_json_matcher/value_validator.rb +0 -10
  66. data/lib/easy_json_matcher/version.rb~ +0 -3
  67. data/test/custom_validations_test.rb~ +0 -25
  68. data/test/easy_json_matcher_test.rb +0 -262
  69. data/test/easy_json_matcher_test.rb~ +0 -262
  70. data/test/error_messages_test.rb +0 -148
  71. data/test/reset_test.rb +0 -32
  72. data/test/reset_test.rb~ +0 -32
@@ -0,0 +1,35 @@
1
+ require "test_helper"
2
+ require "validation_chain_test_helper"
3
+
4
+ include ValidationChainTestHelper
5
+
6
+ module EasyJSONMatcher
7
+
8
+ describe "Not Required Validation Step" do
9
+
10
+ describe "should halt the chain if the value is missing" do
11
+
12
+ it "should halt the chain if the value is missing" do
13
+ head, tail = get_validation_chain
14
+ test_value = nil
15
+ head.check(value: test_value).must_be :empty?
16
+ end
17
+
18
+ it "should allow the chain to continue if the value is present" do
19
+ head, tail = get_validation_chain
20
+ test_value = 1
21
+ tail.expect(:check, [], [{ value: 1 }])
22
+ tail.expect(:nil?, false)
23
+ head.check(value: test_value)
24
+ tail.verify
25
+ end
26
+ end
27
+
28
+ def get_validation_chain
29
+ head = ValidationChainFactory.get_step_for validating: :not_required
30
+ tail = Minitest::Mock.new
31
+ head >> tail
32
+ return head, tail
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,23 @@
1
+ require "test_helper"
2
+ require "validation_chain_test_helper"
3
+
4
+ include ValidationChainTestHelper
5
+
6
+ describe "Number Validation Step" do
7
+
8
+ describe "should be able to return a instance that validates numbers" do
9
+
10
+ it "the instance should not return any errors for a valid number" do
11
+ require 'bigdecimal'
12
+ (-10..10).step(0.01) do |n|
13
+ n = BigDecimal.new(n,3).to_f
14
+ assert_chain_verifies(type: :number, test_value: n, outcome: true )
15
+ end
16
+ end
17
+
18
+ it "the instance should return errors for invalid numbers" do
19
+ no_number = "hello"
20
+ assert_chain_verifies(type: :number, test_value: no_number, outcome: false)
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,20 @@
1
+ require "test_helper"
2
+ require "validation_chain_test_helper"
3
+
4
+ include ValidationChainTestHelper
5
+
6
+ describe "Object validation step" do
7
+
8
+ describe "should be able to return an instance that validates objects" do
9
+
10
+ it "the instance should not return any errors for a valid object" do
11
+ test_value = JSON.parse({}.to_json) # Yes, I know this is odd
12
+ assert_chain_verifies(type: :object, test_value: test_value, outcome: true)
13
+ end
14
+
15
+ it "the instance should return errors for an invalid object" do
16
+ no_object = String.new
17
+ assert_chain_verifies(type: :object, test_value: no_object, outcome: false)
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,19 @@
1
+ require "test_helper"
2
+ require "validation_chain_test_helper"
3
+
4
+ include ValidationChainTestHelper
5
+
6
+ describe "Required Validation Step" do
7
+
8
+ describe ":required option" do
9
+
10
+ it "should not return any errors if the value is present" do
11
+ present = 1
12
+ assert_chain_verifies type: :required, test_value: present, outcome: true
13
+ end
14
+
15
+ it "should return an error if a value is not present" do
16
+ [nil].each { |v| assert_chain_verifies type: :required, test_value: v, outcome: false }
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,20 @@
1
+ require "test_helper"
2
+ require "validation_chain_test_helper"
3
+
4
+ include ValidationChainTestHelper
5
+
6
+ describe "String Validation Chain" do
7
+
8
+ describe "should be able to retun an instance that validates strings" do
9
+
10
+ it "the instance should not return any errors for a valid string" do
11
+ string = String.new
12
+ assert_chain_verifies(type: :string, test_value: string, outcome: true)
13
+ end
14
+
15
+ it "the instance should return errors if the value is not a string" do
16
+ no_string = 1
17
+ assert_chain_verifies(type: :string, test_value: no_string, outcome: false)
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,55 @@
1
+ require "test_helper"
2
+ require "easy_json_matcher/validation_step"
3
+
4
+ module EasyJSONMatcher
5
+
6
+ class ValidationStepTest < ActiveSupport::TestCase
7
+
8
+ setup do
9
+ @subject = ValidationStep.new(verify_with: ->(value, errors) {} )
10
+ end
11
+
12
+ test "it_should_respond_to_check" do
13
+ assert_respond_to(@subject, :check)
14
+ end
15
+
16
+ test "It should allow the user to chain validation steps together" do
17
+ head = @subject
18
+ tail = MiniTest::Mock.new
19
+ test_value = "hello!"
20
+ head >> tail
21
+ tail.expect(:check, [], [Hash])
22
+ tail.expect(:nil?, false)
23
+ head.check(value: test_value)
24
+ tail.verify
25
+ end
26
+
27
+ test "It should send call to the verifier assigned to it" do
28
+ verifier = MiniTest::Mock.new
29
+ subject = ValidationStep.new(verify_with: verifier)
30
+ test_value = "verify me"
31
+ verifier.expect(:call, nil, [test_value, []])
32
+ subject.check(value: test_value)
33
+ end
34
+
35
+ test "If the verifier returns :stop the chain will be halted" do
36
+ verifier = ->(value, errors) { false }
37
+ middle = ValidationStep.new(verify_with: verifier)
38
+ tail = MiniTest::Mock.new
39
+ test_value = "verify me"
40
+ @subject >> middle >> tail
41
+ @subject.check(value: test_value)
42
+ # There is no official way to check that something wasn't called in
43
+ # MiniTest, so we'll just have to assume that since verify passes, then
44
+ # check wasn't called in tail
45
+ tail.verify
46
+ end
47
+
48
+ test "It should return an array of errors found" do
49
+ head = ValidationStep.new(verify_with: ->(value, errors) { errors << 1 })
50
+ tail = ValidationStep.new(verify_with: ->(value, errors) { errors << 2 })
51
+ head >> tail
52
+ assert(head.check(value: "Oh! Mrs. Mogs") == [1, 2])
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,15 @@
1
+ require "test_helper"
2
+ require "validation_chain_test_helper"
3
+
4
+ include ValidationChainTestHelper
5
+
6
+ describe "Value Validation Step" do
7
+
8
+ describe "should be able to return an instance that verifies values" do
9
+
10
+ it "should not return any errors for a valid value, which includes nil" do
11
+ value = nil
12
+ assert_chain_verifies type: :value, test_value: value, outcome: true
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,49 @@
1
+ require "test_helper"
2
+ require "easy_json_matcher/validator_set.rb"
3
+
4
+ module EasyJSONMatcher
5
+
6
+ describe ValidatorSet do
7
+
8
+ it "should hold other validators" do
9
+ test_val = Object.new
10
+ subject = ValidatorSet.new validators: { test: test_val }
11
+ subject.validators.values.include?(test_val).must_be :==, true
12
+ end
13
+
14
+ it "should return true if all its validators validate their candidates" do
15
+ mock_validators = { key1: mock_validator, key2: mock_validator }
16
+ subject = ValidatorSet.new validators: mock_validators
17
+ subject.check(value: {}).must_be :empty?
18
+ end
19
+
20
+ it "should return false if any of its validators find an invalid value" do
21
+ mock_validators = {
22
+ key1: mock_validator(validity: true),
23
+ key2: mock_validator(validity: false)
24
+ }
25
+ subject = ValidatorSet.new validators: mock_validators
26
+ subject.check(value: { key1: "test", key2: "test" }).wont_be :empty?
27
+ end
28
+
29
+ it "should return error messages in a Array" do
30
+ subject = ValidatorSet.new validators: { invalid: mock_validator }
31
+ assert(subject.check(value: {}).is_a?(Array))
32
+ end
33
+
34
+ it "should return the error messages for all its validators" do
35
+ error_validators = {
36
+ a: mock_validator(validity: false, error_message: "a"),
37
+ b: mock_validator(validity: false, error_message: "b")
38
+ }
39
+ subject = ValidatorSet.new(validators: error_validators)
40
+ expected_error_message = [{ a: ["a"], b: ["b"] }]
41
+ assert_equal(expected_error_message, subject.check(value: {}))
42
+ end
43
+
44
+ def mock_validator(validity: true, error_message: nil)
45
+ mock = MiniTest::Mock.new
46
+ mock.expect(:check, validity ? [] : [error_message], [Object])
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,44 @@
1
+ require "test_helper"
2
+
3
+ Validator = EasyJSONMatcher::Validator
4
+
5
+ describe Validator do
6
+
7
+ describe "#valid?" do
8
+
9
+ before do
10
+ @v_step = Minitest::Mock.new
11
+ @subject = Validator.new(validate_with: @v_step)
12
+ end
13
+
14
+ it "should use a ValidationStep chain to verify candidates" do
15
+ @v_step.expect(:check, {}, [Hash])
16
+ @subject.valid? candidate: Hash.new.to_json
17
+ @v_step.verify
18
+ end
19
+
20
+ it "should return false if any errors are generated" do
21
+ @v_step.expect(:check, { a: 1, b: 2,c: 3 }, [Hash])
22
+ @subject.valid?(candidate: Hash.new.to_json).must_be :==, false
23
+ end
24
+
25
+ it "should return true if no errors are generated" do
26
+ @v_step.expect(:check, {}, [Hash])
27
+ @subject.valid?(candidate: Hash.new.to_json).must_be :==, true
28
+ end
29
+ end
30
+
31
+ describe "#validate" do
32
+
33
+ before do
34
+ @v_step = Minitest::Mock.new
35
+ @subject = Validator.new(validate_with: @v_step)
36
+ end
37
+
38
+ it "should call check on its verifier" do
39
+ @v_step.expect(:check, Hash.new, [Hash])
40
+ test_value = { a: 1, b: 2, c: 3 }.to_json
41
+ @subject.validate(candidate: test_value)
42
+ end
43
+ end
44
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: easy_json_matcher
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - WJD Hamilton
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-04-01 00:00:00.000000000 Z
11
+ date: 2016-05-19 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Test your JSON output in Ruby, with a DSL that makes reasoning about
14
14
  your JSON very straightforward. See the Homepage for docs.
@@ -21,41 +21,61 @@ files:
21
21
  - MIT-LICENSE
22
22
  - Rakefile
23
23
  - lib/easy_json_matcher.rb
24
+ - lib/easy_json_matcher.rb~
25
+ - lib/easy_json_matcher/array_content_validator.rb
26
+ - lib/easy_json_matcher/array_generator.rb
24
27
  - lib/easy_json_matcher/array_validator.rb
25
- - lib/easy_json_matcher/boolean_validator.rb
28
+ - lib/easy_json_matcher/attribute_generator.rb
29
+ - lib/easy_json_matcher/attribute_type_methods.rb
30
+ - lib/easy_json_matcher/coercion_error.rb
26
31
  - lib/easy_json_matcher/content_wrapper.rb
27
- - lib/easy_json_matcher/date_validator.rb
32
+ - lib/easy_json_matcher/easy_json_matcher_error.rb
28
33
  - lib/easy_json_matcher/exceptions.rb
34
+ - lib/easy_json_matcher/json_coercer.rb
29
35
  - lib/easy_json_matcher/node.rb
30
- - lib/easy_json_matcher/node.rb~
31
- - lib/easy_json_matcher/number_validator.rb
32
- - lib/easy_json_matcher/object_validator.rb
36
+ - lib/easy_json_matcher/node_generator.rb
33
37
  - lib/easy_json_matcher/schema_generator.rb
34
- - lib/easy_json_matcher/schema_generator.rb~
35
38
  - lib/easy_json_matcher/schema_library.rb
36
- - lib/easy_json_matcher/string_validator.rb
37
- - lib/easy_json_matcher/validation_error.rb
39
+ - lib/easy_json_matcher/unknown_validation_step_error.rb
40
+ - lib/easy_json_matcher/validation_chain_factory.rb
41
+ - lib/easy_json_matcher/validation_rules.rb
42
+ - lib/easy_json_matcher/validation_step.rb
38
43
  - lib/easy_json_matcher/validator.rb
39
- - lib/easy_json_matcher/validator.rb~
40
- - lib/easy_json_matcher/validator_factory.rb
41
- - lib/easy_json_matcher/value_validator.rb
44
+ - lib/easy_json_matcher/validator_set.rb
42
45
  - lib/easy_json_matcher/version.rb
43
- - lib/easy_json_matcher/version.rb~
44
46
  - lib/tasks/jsonapi_matcher_tasks.rake
47
+ - test/array_content_validator_test.rb
45
48
  - test/custom_validations_test.rb
46
- - test/custom_validations_test.rb~
47
- - test/easy_json_matcher_test.rb
48
- - test/easy_json_matcher_test.rb~
49
- - test/error_messages_test.rb
50
49
  - test/global_validation_options_test.rb
51
50
  - test/json_api_home.txt
51
+ - test/json_coercer_test.rb
52
52
  - test/managing_schemas_test.rb
53
+ - test/node_test.rb
54
+ - test/primitives_boolean_test.rb
55
+ - test/primitives_date_test.rb
56
+ - test/primitives_number_test.rb
57
+ - test/primitives_object_test.rb
58
+ - test/primitives_string_test.rb
59
+ - test/primtives_value_test.rb
53
60
  - test/required_validation_test.rb
54
- - test/reset_test.rb
55
- - test/reset_test.rb~
61
+ - test/schema_generator_test.rb
56
62
  - test/strict_mode_test.rb
57
63
  - test/test_helper.rb
58
64
  - test/validating_arrays_test.rb
65
+ - test/validation_chain_factory_test.rb
66
+ - test/validation_chain_test_helper.rb
67
+ - test/validation_step_array_test.rb
68
+ - test/validation_step_boolean_test.rb
69
+ - test/validation_step_date_test.rb
70
+ - test/validation_step_not_required_test.rb
71
+ - test/validation_step_number_test.rb
72
+ - test/validation_step_object_test.rb
73
+ - test/validation_step_required_test.rb
74
+ - test/validation_step_string_test.rb
75
+ - test/validation_step_test.rb
76
+ - test/validation_step_value_test.rb
77
+ - test/validator_set_test.rb
78
+ - test/validator_test.rb
59
79
  homepage: https://github.com/wjdhamilton/easy-json-matcher
60
80
  licenses:
61
81
  - MIT
@@ -81,17 +101,35 @@ signing_key:
81
101
  specification_version: 4
82
102
  summary: Easily test your JSON output with templates and Schemas
83
103
  test_files:
104
+ - test/array_content_validator_test.rb
84
105
  - test/custom_validations_test.rb
85
- - test/custom_validations_test.rb~
86
- - test/easy_json_matcher_test.rb
87
- - test/easy_json_matcher_test.rb~
88
- - test/error_messages_test.rb
89
106
  - test/global_validation_options_test.rb
90
107
  - test/json_api_home.txt
108
+ - test/json_coercer_test.rb
91
109
  - test/managing_schemas_test.rb
110
+ - test/node_test.rb
111
+ - test/primitives_boolean_test.rb
112
+ - test/primitives_date_test.rb
113
+ - test/primitives_number_test.rb
114
+ - test/primitives_object_test.rb
115
+ - test/primitives_string_test.rb
116
+ - test/primtives_value_test.rb
92
117
  - test/required_validation_test.rb
93
- - test/reset_test.rb
94
- - test/reset_test.rb~
118
+ - test/schema_generator_test.rb
95
119
  - test/strict_mode_test.rb
96
120
  - test/test_helper.rb
97
121
  - test/validating_arrays_test.rb
122
+ - test/validation_chain_factory_test.rb
123
+ - test/validation_chain_test_helper.rb
124
+ - test/validation_step_array_test.rb
125
+ - test/validation_step_boolean_test.rb
126
+ - test/validation_step_date_test.rb
127
+ - test/validation_step_not_required_test.rb
128
+ - test/validation_step_number_test.rb
129
+ - test/validation_step_object_test.rb
130
+ - test/validation_step_required_test.rb
131
+ - test/validation_step_string_test.rb
132
+ - test/validation_step_test.rb
133
+ - test/validation_step_value_test.rb
134
+ - test/validator_set_test.rb
135
+ - test/validator_test.rb
@@ -1,14 +0,0 @@
1
- require 'easy_json_matcher/validator'
2
- module EasyJSONMatcher
3
- class BooleanValidator < Validator
4
-
5
- def _validate
6
- errors << "#{content} is not a Boolean" unless _content_is_boolean?
7
- end
8
-
9
- def _content_is_boolean?
10
- clazz = content.class
11
- (clazz == TrueClass) || (clazz == FalseClass)
12
- end
13
- end
14
- end
@@ -1,44 +0,0 @@
1
- require 'easy_json_matcher/validator'
2
- module EasyJSONMatcher
3
- class DateValidator < Validator
4
-
5
- attr_reader :string_validator, :date_format
6
- DEFAULT_DATE_FORMAT = "%Y-%m-%d"
7
-
8
- def initialize(opts = {})
9
- super(opts)
10
- @date_format = opts[:format]
11
- @string_validator = _create_validator(type: :string)
12
- end
13
-
14
- def _validate
15
- _validate_string
16
- _validate_date
17
- end
18
-
19
- def _content_is_a_string?
20
- string_validator.valid? content
21
- end
22
-
23
- def _content_is_a_date?
24
- require 'date'
25
- begin
26
- Date.strptime(content, date_format)
27
- rescue ArgumentError
28
- false
29
- end
30
- end
31
-
32
- def date_format
33
- @date_format || DEFAULT_DATE_FORMAT
34
- end
35
-
36
- def _validate_string
37
- errors << "#{content} must be provided as a String for Date validation" unless _content_is_a_string?
38
- end
39
-
40
- def _validate_date
41
- errors << "#{content} is not a Date" unless _content_is_a_date?
42
- end
43
- end
44
- end
@@ -1,121 +0,0 @@
1
- require 'easy_json_matcher/validator_factory'
2
- require 'json'
3
- require 'easy_json_matcher/content_wrapper'
4
- module EasyJSONMatcher
5
- class Node < Validator
6
- include ContentWrapper
7
-
8
- attr_reader :validators, :validity, :strict
9
-
10
- def initialize(opts: {})
11
- super(options: opts)
12
- @validators = []
13
- @validity = true
14
- end
15
-
16
- def add_validator(validator)
17
- validators << validator
18
- end
19
-
20
- def _post_initialise(options)
21
- @strict = options[:strict]
22
- end
23
-
24
- def _validate
25
- _validate_strict_keyset
26
- validators.each do |val|
27
- @validity = false unless _use_validator val
28
- end
29
- end
30
-
31
- def reset!
32
- errors.clear
33
- validators.each(&:reset!)
34
- end
35
-
36
- def _validate_strict_keyset
37
- _validate_keyset if strict
38
- end
39
-
40
- def _validate_keyset
41
- unexpected_keys = keys - _expected_keys
42
- errors << "#{unexpected_keys} found in addition to expected keys" unless unexpected_keys.empty?
43
- end
44
-
45
- def _expected_keys
46
- validators.each_with_object([]) do |validator, keyset|
47
- keyset << validator.key
48
- end
49
- end
50
-
51
- def _use_validator(validator)
52
- validator.valid? self
53
- end
54
-
55
- def _get_content_for(key)
56
- content[key]
57
- end
58
-
59
- def _get_validator_for(key)
60
- validators[key]
61
- end
62
-
63
- def _set_content(candidate)
64
- @content = _is_root? ? _prep_root_content(candidate) : candidate[key]
65
- end
66
-
67
- def _is_root?
68
- key.nil?
69
- end
70
-
71
- def get_errors
72
- child_errors = _collect_child_errors
73
- _wrap_errors(child_errors)
74
- end
75
-
76
- def _collect_child_errors
77
- validators.each_with_object({}) do |val, container|
78
- container.merge!(val.get_errors)
79
- end
80
- end
81
-
82
- def _wrap_errors(child_errors)
83
- _add_local_errors_to child_errors
84
- unless _is_root?
85
- _wrap_child_errors(child_errors)
86
- else
87
- _root_errors(child_errors)
88
- end
89
- end
90
-
91
- def _add_local_errors_to(child_errors)
92
- child_errors.merge!({node_errors_: errors}) unless errors.empty?
93
- end
94
-
95
- def _wrap_child_errors(child_errors)
96
- errors_wrapper = {}
97
- errors_wrapper[key] = child_errors
98
- errors_wrapper
99
- end
100
-
101
- def _root_errors(child_errors)
102
- errors.length > 0 ? {root: errors} : child_errors
103
- end
104
-
105
- def _prep_root_content(candidate)
106
- candidate.is_a?(String) ? _parse_and_verify_json(candidate) : candidate
107
- end
108
-
109
- def _parse_and_verify_json(json)
110
- begin
111
- JSON.parse(json)
112
- rescue JSON::ParserError
113
- errors << '#{json} is not a valid JSON String'
114
- end
115
- end
116
-
117
- def _no_errors?
118
- validity && errors.empty?
119
- end
120
- end
121
- end
@@ -1,22 +0,0 @@
1
- # Asserts that a value is a double-precision floating point number in javascript format
2
- require 'easy_json_matcher/validator'
3
- module EasyJSONMatcher
4
-
5
- class NumberValidator < Validator
6
-
7
- def _validate
8
- errors << "#{content} is not a Number" unless _content_is_a_number?
9
- end
10
-
11
- def _content_is_a_number?
12
- begin
13
- Kernel::Float(content)
14
- true
15
- rescue ArgumentError
16
- false
17
- rescue TypeError
18
- false
19
- end
20
- end
21
- end
22
- end
@@ -1,12 +0,0 @@
1
- require 'easy_json_matcher/validator'
2
- module EasyJSONMatcher
3
- class ObjectValidator < Validator
4
- def _validate
5
- _content_is_object?
6
- end
7
-
8
- def _content_is_object?
9
- errors << "#{content} is not an Object" unless content.is_a? Hash
10
- end
11
- end
12
- end