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:, **args) ⇒ Validator

Returns a new instance of Validator.



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

def initialize(validate_with:, **args)
  super(**args)
  @validation_chain = validate_with
end

Instance Attribute Details

#coercerObject (readonly)

Returns the value of attribute coercer.



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

def coercer
  @coercer
end

#keyObject

Returns the value of attribute key.



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

def key
  @key
end

#validation_chainObject (readonly)

Returns the value of attribute validation_chain.



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

def validation_chain
  @validation_chain
end

Instance Method Details

#_post_initialise(options) ⇒ Object

Hook. Allows further setup to be carried out by subclasses



14
# File 'lib/easy_json_matcher/validator.rb', line 14

def _post_initialise(options); end

#valid?(candidate:) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#validate(candidate:) ⇒ Object



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

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