class SciolyFF::Validator

Checks if SciolyFF YAML files and/or representations (i.e. hashes that can be directly converted to YAML) comply with spec (i.e. safe for interpreting)

Public Class Methods

new(loglevel: Logger::WARN, canonical: true) click to toggle source
# File lib/sciolyff/validator.rb, line 20
def initialize(loglevel: Logger::WARN, canonical: true)
  @logger = Logger.new loglevel, canonical_checks: canonical
  @checkers = {}
end

Public Instance Methods

last_log() click to toggle source
# File lib/sciolyff/validator.rb, line 35
def last_log
  @logger.log
end
valid?(rep_or_yaml) click to toggle source
# File lib/sciolyff/validator.rb, line 25
def valid?(rep_or_yaml)
  @logger.flush

  if rep_or_yaml.instance_of? String
    valid_yaml?(rep_or_yaml, @logger)
  else
    valid_rep?(rep_or_yaml, @logger)
  end
end

Private Instance Methods

check(klass, top_level_rep, rep, logger) click to toggle source
# File lib/sciolyff/validator.rb, line 83
def check(klass, top_level_rep, rep, logger)
  @checkers[klass] ||= klass.new top_level_rep, logger
  checks = klass.instance_methods - Checker.instance_methods
  checks.map { |im| @checkers[klass].send im, rep, logger }.all?
end
check_all(rep, logger) click to toggle source
# File lib/sciolyff/validator.rb, line 65
def check_all(rep, logger)
  check(TopLevel, rep, rep, logger) &&
    check(Tournament, rep, rep[:Tournament], logger) &&
    [Subdivisions, Events, Teams, Placings, Penalties].all? do |klass|
      check_list(klass, rep, logger)
    end &&
    rep[:Placings].map { |p| p[:raw] }.compact.all? do |r|
      check(Raws, rep, r, logger)
    end
end
check_list(klass, rep, logger) click to toggle source
# File lib/sciolyff/validator.rb, line 76
def check_list(klass, rep, logger)
  key = klass.to_s.split('::').last.to_sym
  return true unless rep.key? key # ignore optional sections like Penalties

  rep[key].map { |e| check(klass, rep, e, logger) }.all?
end
valid_rep?(rep, logger) click to toggle source
# File lib/sciolyff/validator.rb, line 41
def valid_rep?(rep, logger)
  unless rep.instance_of? Hash
    logger.error 'improper file structure'
    return false
  end

  result = check_all(rep, logger)

  @checkers.clear # aka this method is not thread-safe
  result
end
valid_yaml?(yaml, logger) click to toggle source
# File lib/sciolyff/validator.rb, line 53
def valid_yaml?(yaml, logger)
  rep = Psych.safe_load(
    yaml,
    permitted_classes: [Date],
    symbolize_names: true
  )
rescue StandardError => e
  logger.error "could not read input as YAML:\n#{e.message}"
else
  valid_rep?(rep, logger)
end