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(validate_with:, coerce_with: JsonCoercer.new) ⇒ Validator

Returns a new instance of Validator.



9
10
11
12
# File 'lib/easy_json_matcher/validator.rb', line 9

def initialize(validate_with:, coerce_with: JsonCoercer.new)
  @validation_chain = validate_with
  @coercer = coerce_with
end

Instance Attribute Details

#coercerObject (readonly)

Returns the value of attribute coercer.



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

def coercer
  @coercer
end

#keyObject

Returns the value of attribute key.



7
8
9
# File 'lib/easy_json_matcher/validator.rb', line 7

def key
  @key
end

#validation_chainObject (readonly)

Returns the value of attribute validation_chain.



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

def validation_chain
  @validation_chain
end

Instance Method Details

#_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

#valid?(candidate:) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
20
# File 'lib/easy_json_matcher/validator.rb', line 17

def valid?(candidate:)
  errors = validate(candidate: candidate)
  errors.empty?
end

#validate(candidate:) ⇒ Object



22
23
24
25
# File 'lib/easy_json_matcher/validator.rb', line 22

def validate(candidate:)
  candidate = coercer.coerce(json: candidate)
  validation_chain.check(value: candidate)
end