Class: EasyJSONMatcher::Validator
- Inherits:
-
Object
- Object
- EasyJSONMatcher::Validator
- Defined in:
- lib/easy_json_matcher/validator.rb
Direct Known Subclasses
ArrayValidator, BooleanValidator, DateValidator, Node, NumberValidator, ObjectValidator, StringValidator, ValueValidator
Instance Attribute Summary collapse
-
#content ⇒ Object
readonly
Returns the value of attribute content.
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#key ⇒ Object
Returns the value of attribute key.
-
#required ⇒ Object
readonly
Returns the value of attribute required.
Instance Method Summary collapse
-
#_check_content_type(candidate) ⇒ Object
This method makees sure that the candidate behaves like a Hash, and not a value or an array.
- #_check_required? ⇒ Boolean
- #_create_validator(type:, opts: {}) ⇒ Object
- #_no_errors? ⇒ Boolean
-
#_post_initialise(options) ⇒ Object
Hook.
-
#_set_content(candidate) ⇒ Object
Hook Protected method that Validators use to set their content from the candidate.
-
#_validate ⇒ Object
Hook Protected method that Validators use to implement their validation logic.
-
#get_errors ⇒ Object
Hook.
-
#initialize(options: {}) ⇒ Validator
constructor
A new instance of Validator.
-
#reset! ⇒ Object
Hook.
- #valid?(candidate) ⇒ Boolean
Constructor Details
#initialize(options: {}) ⇒ Validator
Returns a new instance of Validator.
7 8 9 10 11 12 |
# File 'lib/easy_json_matcher/validator.rb', line 7 def initialize(options: {}) @key = [:key] @required = [:required] @errors = [] _post_initialise() end |
Instance Attribute Details
#content ⇒ Object (readonly)
Returns the value of attribute content.
4 5 6 |
# File 'lib/easy_json_matcher/validator.rb', line 4 def content @content end |
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
4 5 6 |
# File 'lib/easy_json_matcher/validator.rb', line 4 def errors @errors end |
#key ⇒ Object
Returns the value of attribute key.
5 6 7 |
# File 'lib/easy_json_matcher/validator.rb', line 5 def key @key end |
#required ⇒ Object (readonly)
Returns the value of attribute required.
4 5 6 |
# File 'lib/easy_json_matcher/validator.rb', line 4 def required @required end |
Instance Method Details
#_check_content_type(candidate) ⇒ Object
This method makees sure that the candidate behaves like a Hash, and not a value or an array.
61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/easy_json_matcher/validator.rb', line 61 def _check_content_type(candidate) # TODO perhaps this should raise an error instead of returning false? # if the value that has arrived at this point doesn't behave like a Hash then it # is in the wrong place. begin candidate[key] rescue TypeError return false end true end |
#_check_required? ⇒ Boolean
73 74 75 76 77 78 79 80 |
# File 'lib/easy_json_matcher/validator.rb', line 73 def _check_required? if required errors << "Value was not present" return true else return false end end |
#_create_validator(type:, opts: {}) ⇒ Object
82 83 84 |
# File 'lib/easy_json_matcher/validator.rb', line 82 def _create_validator(type:, opts: {}) ValidatorFactory.get_instance(type: type, opts: opts) end |
#_no_errors? ⇒ Boolean
86 87 88 |
# File 'lib/easy_json_matcher/validator.rb', line 86 def _no_errors? errors.empty? end |
#_post_initialise(options) ⇒ Object
Hook. Allows further setup to be carried out by subclasses
15 |
# File 'lib/easy_json_matcher/validator.rb', line 15 def _post_initialise(); end |
#_set_content(candidate) ⇒ Object
Hook Protected method that Validators use to set their content from the candidate.
44 45 46 |
# File 'lib/easy_json_matcher/validator.rb', line 44 def _set_content(candidate) @content = key ? candidate[key] : candidate end |
#_validate ⇒ Object
Hook Protected method that Validators use to implement their validation logic. Called by #valid?
38 39 40 |
# File 'lib/easy_json_matcher/validator.rb', line 38 def _validate raise NotImplementedError.new "Validators must override _validate" end |
#get_errors ⇒ Object
Hook. This method returns the errors that this validator has found in the candidate.
50 51 52 53 54 55 56 57 |
# File 'lib/easy_json_matcher/validator.rb', line 50 def get_errors = {} # Should the method just add errors even if there has been no error? Would # avoid undefined method [] for nil:NilClass if you look for a key where # there is no error but it would also make the output harder to read... [key.to_sym] = errors end |
#reset! ⇒ Object
Hook. Overriden in Node
31 32 33 |
# File 'lib/easy_json_matcher/validator.rb', line 31 def reset! errors.clear end |
#valid?(candidate) ⇒ Boolean
17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/easy_json_matcher/validator.rb', line 17 def valid?(candidate) if key return false unless _check_content_type(candidate) end _set_content(candidate) #Hook if content.nil? _check_required? else _validate #Hook end _no_errors? end |