Class: LiquidXlsx::Validator

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

Overview

Checks a template without rendering it and without any data.

Why this is not "render and see what happens": the renderer parses only the Liquid it actually executes (Renderer#cached_parse), so a typo inside an if % branch that this document does not take, or inside a loop over an empty collection, stays invisible until the day some other document takes that branch. It also stops at the first failure, and rendering executes the template against live records — printing a document is a side effect nobody asked for when all they did was upload a file.

So: parse everything, execute nothing, and report every problem found.

Constant Summary collapse

LIQUID_MARKER =
/\{\{|\{%/

Instance Method Summary collapse

Constructor Details

#initialize(template_path, dynamic_sheets: false, known_filters: nil) ⇒ Validator

Returns a new instance of Validator.

Parameters:

  • template_path (String)

    path to the .xlsx template

  • dynamic_sheets (Boolean) (defaults to: false)

    whether the caller renders with dynamic_sheets: true — sheet % is an error when it does not

  • known_filters (Array<String>, nil) (defaults to: nil)

    filter names available at render time; nil disables the unknown-filter check



23
24
25
26
27
28
# File 'lib/liquid_xlsx/validator.rb', line 23

def initialize(template_path, dynamic_sheets: false, known_filters: nil)
  @template_path  = template_path
  @dynamic_sheets = dynamic_sheets
  @known_filters  = known_filters&.map(&:to_s)
  @result         = Validation::Result.new
end

Instance Method Details

#callValidation::Result

Returns:



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/liquid_xlsx/validator.rb', line 31

def call
  package = open_package
  return @result if package.nil?

  sheet_names = safe_sheet_names(package)
  @result.sheets.concat(sheet_names)

  package.sheets.each { |sheet_info| validate_sheet(package, sheet_info) }

  check_sheet_tags(sheet_names)
  check_filters

  @result
rescue Nokogiri::XML::SyntaxError => e
  invalid_xlsx("Corrupt XML inside the workbook: #{e.message}")
end