# frozen_string_literal: true module Sports class RulesSchema INTEGER_RULES = { "volley" => %w[sets_to_win points_per_set points_deciding_set min_point_lead], "racket" => %w[sets_to_win points_per_set points_deciding_set min_point_lead], "basket" => %w[periods period_duration_secs overtime_duration_secs], "timed" => %w[periods period_duration_secs overtime_duration_secs], "timer" => [], "generic" => [] }.freeze def self.validate!(board_type, rules) board = board_type.to_s return if board == "generic" || board == "timer" hash = rules.is_a?(Hash) ? rules : {} INTEGER_RULES.fetch(board, []).each do |key| val = hash[key] || hash[key.to_sym] next if val.blank? unless val.is_a?(Integer) || val.to_s.match?(/\A\d+\z/) raise ArgumentError, "#{key} non valido" end int_val = val.to_i raise ArgumentError, "#{key} deve essere positivo" if int_val < 1 end end def self.validate(board_type, rules) validate!(board_type, rules) true rescue ArgumentError false end end end