Class: EasyJSONMatcher::Validator
- Inherits:
-
Object
- Object
- EasyJSONMatcher::Validator
- Defined in:
- lib/easy_json_matcher/validator.rb
Instance Attribute Summary collapse
-
#coercer ⇒ Object
readonly
Returns the value of attribute coercer.
-
#key ⇒ Object
Returns the value of attribute key.
-
#validation_chain ⇒ Object
readonly
Returns the value of attribute validation_chain.
Instance Method Summary collapse
-
#_post_initialise(options) ⇒ Object
Hook.
-
#initialize(validate_with:, coerce_with: JsonCoercer.new) ⇒ Validator
constructor
A new instance of Validator.
- #valid?(candidate:) ⇒ Boolean
- #validate(candidate:) ⇒ Object
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
#coercer ⇒ Object (readonly)
Returns the value of attribute coercer.
6 7 8 |
# File 'lib/easy_json_matcher/validator.rb', line 6 def coercer @coercer end |
#key ⇒ Object
Returns the value of attribute key.
7 8 9 |
# File 'lib/easy_json_matcher/validator.rb', line 7 def key @key end |
#validation_chain ⇒ Object (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(); end |
#valid?(candidate:) ⇒ 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 |