Class: Lutaml::Xsd::Validation::ValidationRule
- Inherits:
-
Object
- Object
- Lutaml::Xsd::Validation::ValidationRule
- Defined in:
- lib/lutaml/xsd/validation/validation_rule.rb
Overview
ValidationRule is the base class for all validation rules
This abstract class defines the interface that all validation rules must implement. It follows the Template Method pattern, allowing subclasses to define specific validation logic while maintaining a consistent structure.
Direct Known Subclasses
Rules::AttributeValidationRule, Rules::ContentModelValidationRule, Rules::ElementStructureRule, Rules::OccurrenceValidationRule, Rules::TypeValidationRule
Constant Summary collapse
- CATEGORIES =
Valid rule categories
%i[structure type constraint facet identity].freeze
Instance Attribute Summary collapse
-
#options ⇒ Hash
readonly
Rule options.
Instance Method Summary collapse
-
#applicable?(config) ⇒ Boolean
Check if rule should run for given configuration.
-
#category ⇒ Symbol
abstract
Get the category of this validation rule.
-
#description ⇒ String
abstract
Get a human-readable description of the rule.
-
#disable! ⇒ void
Disable this rule.
-
#enable! ⇒ void
Enable this rule.
-
#enabled? ⇒ Boolean
Check if this rule is enabled.
-
#initialize(options = {}) ⇒ ValidationRule
constructor
Initialize a new ValidationRule.
-
#inspect ⇒ String
Detailed string representation.
-
#option(key, default = nil) ⇒ Object
Get option value.
-
#priority ⇒ Integer
Get rule priority for execution order.
-
#to_h ⇒ Hash
Convert to hash representation.
-
#to_s ⇒ String
String representation.
-
#validate(document, repository, collector) ⇒ void
abstract
Execute the validation rule.
Constructor Details
#initialize(options = {}) ⇒ ValidationRule
Initialize a new ValidationRule
37 38 39 40 |
# File 'lib/lutaml/xsd/validation/validation_rule.rb', line 37 def initialize( = {}) @options = @enabled = .fetch(:enabled, true) end |
Instance Attribute Details
#options ⇒ Hash (readonly)
Returns Rule options.
32 33 34 |
# File 'lib/lutaml/xsd/validation/validation_rule.rb', line 32 def @options end |
Instance Method Details
#applicable?(config) ⇒ Boolean
Check if rule should run for given configuration
123 124 125 126 127 128 129 130 |
# File 'lib/lutaml/xsd/validation/validation_rule.rb', line 123 def applicable?(config) return false unless enabled? # Check if category is enabled in config return true unless config.respond_to?(:category_enabled?) config.category_enabled?(category) end |
#category ⇒ Symbol
Get the category of this validation rule
Categories are used to group rules and execute them in phases:
-
:structure - Element existence, ordering, namespaces
-
:type - Type compliance (simple and complex types)
-
:constraint - Occurrences, patterns, ranges
-
:facet - Facet validation (length, pattern, etc.)
-
:identity - Key, keyref, unique constraints
72 73 74 75 |
# File 'lib/lutaml/xsd/validation/validation_rule.rb', line 72 def category raise NotImplementedError, "#{self.class} must implement #category" end |
#description ⇒ String
Get a human-readable description of the rule
83 84 85 86 |
# File 'lib/lutaml/xsd/validation/validation_rule.rb', line 83 def description raise NotImplementedError, "#{self.class} must implement #description" end |
#disable! ⇒ void
This method returns an undefined value.
Disable this rule
98 99 100 |
# File 'lib/lutaml/xsd/validation/validation_rule.rb', line 98 def disable! @enabled = false end |
#enable! ⇒ void
This method returns an undefined value.
Enable this rule
105 106 107 |
# File 'lib/lutaml/xsd/validation/validation_rule.rb', line 105 def enable! @enabled = true end |
#enabled? ⇒ Boolean
Check if this rule is enabled
91 92 93 |
# File 'lib/lutaml/xsd/validation/validation_rule.rb', line 91 def enabled? @enabled end |
#inspect ⇒ String
Detailed string representation
165 166 167 168 169 170 |
# File 'lib/lutaml/xsd/validation/validation_rule.rb', line 165 def inspect "#<#{self.class.name} " \ "category=#{category.inspect} " \ "enabled=#{enabled?} " \ "priority=#{priority}>" end |
#option(key, default = nil) ⇒ Object
Get option value
137 138 139 |
# File 'lib/lutaml/xsd/validation/validation_rule.rb', line 137 def option(key, default = nil) @options.fetch(key, default) end |
#priority ⇒ Integer
Get rule priority for execution order
Rules with lower priority numbers execute first. Default priority is 100.
115 116 117 |
# File 'lib/lutaml/xsd/validation/validation_rule.rb', line 115 def priority @options.fetch(:priority, 100) end |
#to_h ⇒ Hash
Convert to hash representation
144 145 146 147 148 149 150 151 152 153 |
# File 'lib/lutaml/xsd/validation/validation_rule.rb', line 144 def to_h { class: self.class.name, category: category, description: description, enabled: enabled?, priority: priority, options: @options, } end |
#to_s ⇒ String
String representation
158 159 160 |
# File 'lib/lutaml/xsd/validation/validation_rule.rb', line 158 def to_s "#{self.class.name}(category: #{category}, enabled: #{enabled?})" end |
#validate(document, repository, collector) ⇒ void
This method returns an undefined value.
Execute the validation rule
This is the main entry point for validation. Subclasses must implement this method to define their validation logic.
54 55 56 57 |
# File 'lib/lutaml/xsd/validation/validation_rule.rb', line 54 def validate(document, repository, collector) raise NotImplementedError, "#{self.class} must implement #validate" end |