Class: Uniword::Validators::ElementValidator
- Inherits:
-
Object
- Object
- Uniword::Validators::ElementValidator
- 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.
Direct Known Subclasses
Class Method Summary collapse
-
.for(element_class) ⇒ ElementValidator
Get the appropriate validator for an element class.
-
.register(element_class, validator_class) ⇒ void
Register a validator for a specific element class.
-
.reset_registry ⇒ void
Reset the validator registry (useful for testing).
-
.validator_registry ⇒ Hash
Get the validator registry.
Instance Method Summary collapse
-
#errors(element) ⇒ Array<String>
Get validation errors for an element.
-
#valid?(element) ⇒ Boolean
Validate an element.
Class Method Details
.for(element_class) ⇒ ElementValidator
Get the appropriate validator for an 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
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_registry ⇒ void
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_registry ⇒ Hash
Get the validator registry
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
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
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 |