Class: NtiReceiptBuilder::LayoutValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/nti_receipt_builder/layout_validator.rb

Overview

Structural + whitelist validation of a template's jsonb layout. This is the single place that decides whether a submitted layout is safe to persist and render — it never trusts client-side snap/clamp/whitelist checks, since those are UX conveniences only.

Allowed variable keys and order-line column keys come from the host's variables class, so this validator holds no domain vocabulary of its own.

Constant Summary collapse

GEOMETRY_KEYS =
%w[x_mm y_mm width_mm height_mm].freeze

Instance Method Summary collapse

Constructor Details

#initialize(layout:, paper_width_mm:, paper_height_mm:, margin_top_mm:, margin_right_mm:, margin_bottom_mm:, margin_left_mm:, height_mode:, variables_class: NtiReceiptBuilder.variables_class) ⇒ LayoutValidator

Returns a new instance of LayoutValidator.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/nti_receipt_builder/layout_validator.rb', line 13

def initialize(layout:, paper_width_mm:, paper_height_mm:, margin_top_mm:, margin_right_mm:,
               margin_bottom_mm:, margin_left_mm:, height_mode:,
               variables_class: NtiReceiptBuilder.variables_class)
  @layout = layout
  @paper_width_mm = paper_width_mm
  @paper_height_mm = paper_height_mm
  @margin_top_mm = margin_top_mm
  @margin_right_mm = margin_right_mm
  @margin_bottom_mm = margin_bottom_mm
  @margin_left_mm = margin_left_mm
  @height_mode = height_mode
  @variables_class = variables_class
  @errors = []
end

Instance Method Details

#callObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/nti_receipt_builder/layout_validator.rb', line 28

def call
  unless @layout.is_a?(Array)
    @errors << 'must be an array of elements'
    return @errors
  end

  if @layout.size > Elements::MAX_ELEMENTS
    @errors << "may contain at most #{Elements::MAX_ELEMENTS} elements (has #{@layout.size})"
  end

  seen_ids = Set.new
  @layout.each_with_index { |element, index| validate_element(element, index, seen_ids) }

  @errors
end