Class: Zizia::Validator Abstract

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

Overview

This class is abstract.

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 logged to Rails.logger 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.

Examples:

validating a parser

validator = MyValidator.new
validator.validate(parser: myParser)

validating an invalid parser

validator = MyValidator.new
validator.validate(parser: invalidParser)
# => Error<#... validator: MyValidator,
                name: 'An Error Name',
                description: '...'
                lineno: 37>

Implementing a custom Validator and using it in a Parser

# Validator checks that the title, when downcased is equal to `moomin`
class TitleIsMoominValidator
  def run_validation(parser:)
    parser.records.each_with_object([]) do |record, errors|
      errors << Error.new(self, :title_is_not_moomin) unless
        title_is_moomin?(record)
    end
  end

  def title_is_moomin?(record)
    return false unless record.respond_to?(:title)
    return true if record.title.downcase == 'moomin
    true
  end
end

parser = MyParser.new(some_content)
parser.validations << TitleIsMoominvalidator.new
parser.validate
parser.valid? # => false (unless all the records match the title)

See Also:

Defined Under Namespace

Classes: Error

Instance Method Summary collapse

Instance Method Details

#validate(parser:) ⇒ Enumerator<Error>

Returns a collection of errors found in validation.

Parameters:

Returns:

  • (Enumerator<Error>)

    a collection of errors found in validation



87
88
89
90
91
# File 'lib/zizia/validator.rb', line 87

def validate(parser:)
  run_validation(parser: parser).tap do |errors|
    errors.map { |error| Rails.logger.error "[zizia] #{error}" }
  end
end