Class: Uniword::Validators::TableValidator

Inherits:
ElementValidator show all
Defined in:
lib/uniword/validators/table_validator.rb

Overview

Validator for Table elements Responsibility: Validate table-specific constraints

A valid table:

  • Must be a Table instance

  • Can have zero or more rows (empty tables are valid)

  • All rows must be valid TableRow instances

  • All rows should have consistent column counts (warning, not error)

  • Properties, if present, must be valid TableProperties

Examples:

Using the table validator

validator = Uniword::Validators::TableValidator.new
validator.valid?(table) # => true or false
validator.errors(table) # => ["error message", ...]

Instance Method Summary collapse

Methods inherited from ElementValidator

for, register, reset_registry, validator_registry

Instance Method Details

#errors(element) ⇒ Array<String>

Get validation errors for a table

Parameters:

  • element (Table)

    The table to validate

Returns:

  • (Array<String>)

    Array of error messages



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/uniword/validators/table_validator.rb', line 39

def errors(element)
  errors = []

  # Check if element is nil
  return ["Element is nil"] if element.nil?

  # Check table type first (more specific than base check)
  return ["Element must be a Table"] unless element.is_a?(Uniword::Wordprocessingml::Table)

  # Validate rows - collect all specific errors
  errors.concat(row_errors(element))

  # Validate properties - collect all specific errors
  errors.concat(property_errors(element))

  errors
end

#valid?(element) ⇒ Boolean

Validate a table element

Parameters:

  • element (Table)

    The table to validate

Returns:

  • (Boolean)

    true if valid, false otherwise



27
28
29
30
31
32
33
# File 'lib/uniword/validators/table_validator.rb', line 27

def valid?(element)
  return false unless super
  return false unless element.is_a?(Uniword::Wordprocessingml::Table)

  validate_rows(element) &&
    validate_properties(element)
end

#warnings(element) ⇒ Array<String>

Get validation warnings (non-critical issues)

Parameters:

  • element (Table)

    The table to validate

Returns:

  • (Array<String>)

    Array of warning messages



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/uniword/validators/table_validator.rb', line 61

def warnings(element)
  warnings = []

  return warnings unless element.is_a?(Uniword::Wordprocessingml::Table)
  return warnings if element.rows.empty?

  # Check for inconsistent column counts
  column_counts = element.rows.map { |row| row.cells.count }.uniq
  warnings << "Table has inconsistent column counts: #{column_counts.join(', ')}" if column_counts.size > 1

  warnings
end