Class: EasyJSONMatcher::Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/easy_json_matcher/validator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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 = options[:key]
  @required = options[:required]
  @errors = []
  _post_initialise(options)
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



4
5
6
# File 'lib/easy_json_matcher/validator.rb', line 4

def content
  @content
end

#errorsObject (readonly)

Returns the value of attribute errors.



4
5
6
# File 'lib/easy_json_matcher/validator.rb', line 4

def errors
  @errors
end

#keyObject

Returns the value of attribute key.



5
6
7
# File 'lib/easy_json_matcher/validator.rb', line 5

def key
  @key
end

#requiredObject (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

Returns:

  • (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

Returns:

  • (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(options); 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

#_validateObject

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_errorsObject

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
  error_message = {}
  # 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...
  error_message[key.to_sym] = errors
  error_message
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

Returns:

  • (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