Class: Darlingtonia::Validator Abstract
- Inherits:
-
Object
- Object
- Darlingtonia::Validator
- Defined in:
- lib/darlingtonia/validator.rb
Overview
A null validator; always returns an empty error collection
Validators are used to ensure the correctness of input to a parser. Each validator must respond to ‘#validate` and return a collection of errors found during validation. If the input is valid, this collection must be empty. Otherwise, it contains any number of `Validator::Error` structs which should be sent to the `#error_stream` by the validator.
The validation process accepts an entire ‘Parser` and is free to inspect the input `#file` content, or view its individual `#records`.
The base class provides infrastructure for the key behavior, relying on a private ‘#run_validation` method to provide the core behavior. In most cases implementers will want to simply override this method.
Direct Known Subclasses
Defined Under Namespace
Classes: Error
Instance Attribute Summary collapse
Instance Method Summary collapse
-
#initialize(error_stream: Darlingtonia.config.default_error_stream) ⇒ Validator
constructor
A new instance of Validator.
-
#validate(parser:) ⇒ Enumerator<Error>
A collection of errors found in validation.
Constructor Details
#initialize(error_stream: Darlingtonia.config.default_error_stream) ⇒ Validator
Returns a new instance of Validator.
90 91 92 |
# File 'lib/darlingtonia/validator.rb', line 90 def initialize(error_stream: Darlingtonia.config.default_error_stream) self.error_stream = error_stream end |
Instance Attribute Details
#error_stream ⇒ #<<
86 87 88 |
# File 'lib/darlingtonia/validator.rb', line 86 def error_stream @error_stream end |
Instance Method Details
#validate(parser:) ⇒ Enumerator<Error>
Returns a collection of errors found in validation.
98 99 100 101 102 |
# File 'lib/darlingtonia/validator.rb', line 98 def validate(parser:) run_validation(parser: parser).tap do |errors| errors.map { |error| error_stream << error } end end |