Class: EasyJSONMatcher::Node
Instance Attribute Summary collapse
Attributes inherited from Validator
#content, #errors, #key, #required
Instance Method Summary
collapse
#[], #keys, #method_missing
Methods inherited from Validator
#_check_content_type, #_check_required?, #_create_validator, #valid?
Constructor Details
#initialize(opts: {}) ⇒ Node
Returns a new instance of Node.
10
11
12
13
14
|
# File 'lib/easy_json_matcher/node.rb', line 10
def initialize(opts: {})
super(options: opts)
@validators = []
@validity = true
end
|
Instance Attribute Details
#strict ⇒ Object
Returns the value of attribute strict.
8
9
10
|
# File 'lib/easy_json_matcher/node.rb', line 8
def strict
@strict
end
|
#validators ⇒ Object
Returns the value of attribute validators.
8
9
10
|
# File 'lib/easy_json_matcher/node.rb', line 8
def validators
@validators
end
|
#validity ⇒ Object
Returns the value of attribute validity.
8
9
10
|
# File 'lib/easy_json_matcher/node.rb', line 8
def validity
@validity
end
|
Instance Method Details
#_add_local_errors_to(child_errors) ⇒ Object
91
92
93
|
# File 'lib/easy_json_matcher/node.rb', line 91
def _add_local_errors_to(child_errors)
child_errors.merge!({node_errors_: errors}) unless errors.empty?
end
|
#_collect_child_errors ⇒ Object
76
77
78
79
80
|
# File 'lib/easy_json_matcher/node.rb', line 76
def _collect_child_errors
validators.each_with_object({}) do |val, container|
container.merge!(val.get_errors)
end
end
|
#_expected_keys ⇒ Object
45
46
47
48
49
|
# File 'lib/easy_json_matcher/node.rb', line 45
def _expected_keys
validators.each_with_object([]) do |validator, keyset|
keyset << validator.key
end
end
|
#_get_content_for(key) ⇒ Object
55
56
57
|
# File 'lib/easy_json_matcher/node.rb', line 55
def _get_content_for(key)
content[key]
end
|
#_get_validator_for(key) ⇒ Object
59
60
61
|
# File 'lib/easy_json_matcher/node.rb', line 59
def _get_validator_for(key)
validators[key]
end
|
#_is_root? ⇒ Boolean
67
68
69
|
# File 'lib/easy_json_matcher/node.rb', line 67
def _is_root?
key.nil?
end
|
#_no_errors? ⇒ Boolean
117
118
119
|
# File 'lib/easy_json_matcher/node.rb', line 117
def _no_errors?
validity && errors.empty?
end
|
#_parse_and_verify_json(json) ⇒ Object
109
110
111
112
113
114
115
|
# File 'lib/easy_json_matcher/node.rb', line 109
def _parse_and_verify_json(json)
begin
JSON.parse(json)
rescue JSON::ParserError
errors << '#{json} is not a valid JSON String'
end
end
|
#_post_initialise(options) ⇒ Object
20
21
22
|
# File 'lib/easy_json_matcher/node.rb', line 20
def _post_initialise(options)
@strict = options[:strict]
end
|
#_prep_root_content(candidate) ⇒ Object
105
106
107
|
# File 'lib/easy_json_matcher/node.rb', line 105
def _prep_root_content(candidate)
candidate.is_a?(String) ? _parse_and_verify_json(candidate) : candidate
end
|
#_root_errors(child_errors) ⇒ Object
101
102
103
|
# File 'lib/easy_json_matcher/node.rb', line 101
def _root_errors(child_errors)
errors.length > 0 ? {root: errors} : child_errors
end
|
#_set_content(candidate) ⇒ Object
63
64
65
|
# File 'lib/easy_json_matcher/node.rb', line 63
def _set_content(candidate)
@content = _is_root? ? _prep_root_content(candidate) : candidate[key]
end
|
#_use_validator(validator) ⇒ Object
51
52
53
|
# File 'lib/easy_json_matcher/node.rb', line 51
def _use_validator(validator)
validator.valid? self
end
|
#_validate ⇒ Object
24
25
26
27
28
29
|
# File 'lib/easy_json_matcher/node.rb', line 24
def _validate
_validate_strict_keyset
validators.each do |val|
@validity = false unless _use_validator val
end
end
|
#_validate_keyset ⇒ Object
40
41
42
43
|
# File 'lib/easy_json_matcher/node.rb', line 40
def _validate_keyset
unexpected_keys = keys - _expected_keys
errors << "#{unexpected_keys} found in addition to expected keys" unless unexpected_keys.empty?
end
|
#_validate_strict_keyset ⇒ Object
36
37
38
|
# File 'lib/easy_json_matcher/node.rb', line 36
def _validate_strict_keyset
_validate_keyset if strict
end
|
#_wrap_child_errors(child_errors) ⇒ Object
95
96
97
98
99
|
# File 'lib/easy_json_matcher/node.rb', line 95
def _wrap_child_errors(child_errors)
errors_wrapper = {}
errors_wrapper[key] = child_errors
errors_wrapper
end
|
#_wrap_errors(child_errors) ⇒ Object
82
83
84
85
86
87
88
89
|
# File 'lib/easy_json_matcher/node.rb', line 82
def _wrap_errors(child_errors)
_add_local_errors_to child_errors
unless _is_root?
_wrap_child_errors(child_errors)
else
_root_errors(child_errors)
end
end
|
#add_validator(validator) ⇒ Object
16
17
18
|
# File 'lib/easy_json_matcher/node.rb', line 16
def add_validator(validator)
validators << validator
end
|
#get_errors ⇒ Object
71
72
73
74
|
# File 'lib/easy_json_matcher/node.rb', line 71
def get_errors
child_errors = _collect_child_errors
_wrap_errors(child_errors)
end
|
#reset! ⇒ Object
31
32
33
34
|
# File 'lib/easy_json_matcher/node.rb', line 31
def reset!
errors.clear
validators.each(&:reset!)
end
|