Class: Uniword::Validators::ElementValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/uniword/validators/element_validator.rb

Overview

Base validator for element validation Responsibility: Provide validation interface and registry for element validators

This class follows the Single Responsibility Principle by separating validation logic from the element classes themselves.

Examples:

Using the validator

validator = Uniword::Validators::ElementValidator.for(Paragraph)
validator.valid?(paragraph) # => true or false

Registering a custom validator

Uniword::Validators::ElementValidator.register(MyElement, MyValidator)

Direct Known Subclasses

ParagraphValidator, TableValidator

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.for(element_class) ⇒ ElementValidator

Get the appropriate validator for an element class

Parameters:

  • element_class (Class)

    The element class to get validator for

Returns:

Raises:

  • (ArgumentError)

    if element_class is not a valid Element class



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/uniword/validators/element_validator.rb', line 24

def for(element_class)
  unless element_class.is_a?(Class) &&
      element_class.ancestors.any? do |a|
        a.to_s.include?("Serializable")
      end
    raise ArgumentError,
          "element_class must be a lutaml-model serializable class"
  end

  validator_class = validator_registry[element_class] || self
  validator_class.new
end

.register(element_class, validator_class) ⇒ void

This method returns an undefined value.

Register a validator for a specific element class

Parameters:

  • element_class (Class)

    The element class

  • validator_class (Class)

    The validator class



42
43
44
45
46
47
48
49
# File 'lib/uniword/validators/element_validator.rb', line 42

def register(element_class, validator_class)
  unless validator_class.ancestors.include?(ElementValidator)
    raise ArgumentError,
          "validator_class must inherit from ElementValidator"
  end

  validator_registry[element_class] = validator_class
end

.reset_registryvoid

This method returns an undefined value.

Reset the validator registry (useful for testing)



61
62
63
# File 'lib/uniword/validators/element_validator.rb', line 61

def reset_registry
  @validator_registry = {}
end

.validator_registryHash

Get the validator registry

Returns:

  • (Hash)

    The registry mapping element classes to validators



54
55
56
# File 'lib/uniword/validators/element_validator.rb', line 54

def validator_registry
  @validator_registry ||= {}
end

Instance Method Details

#errors(element) ⇒ Array<String>

Get validation errors for an element

Parameters:

  • element (Element)

    The element to validate

Returns:

  • (Array<String>)

    Array of error messages



90
91
92
93
94
95
96
# File 'lib/uniword/validators/element_validator.rb', line 90

def errors(element)
  return ["Element is nil"] if element.nil?
  return ["Element must be a Uniword::Element"] unless element.class.ancestors.any? { |a| a.to_s.include?("Serializable") }
  return [] if valid?(element)

  ["Element validation failed"]
end

#valid?(element) ⇒ Boolean

Validate an element

Parameters:

  • element (Element)

    The element to validate

Returns:

  • (Boolean)

    true if valid, false otherwise



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/uniword/validators/element_validator.rb', line 70

def valid?(element)
  return false if element.nil?
  # v2.0: Check if element is a serializable object (has lutaml-model ancestry)
  return false unless element.class.ancestors.any? do |a|
    a.to_s.include?("Serializable")
  end

  # v2.0: All lutaml-model objects are valid by default
  # v1.x: Elements have a valid? method
  if element.respond_to?(:valid?)
    element.valid?
  else
    true
  end
end