Class: Lutaml::Xsd::Validation::ValidationRule

Inherits:
Object
  • Object
show all
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.

Examples:

Create a custom validation rule

class MyRule < ValidationRule
  def category
    :structure
  end

  def validate(document, repository, collector)
    # Validation logic here
  end
end

Use a validation rule

rule = MyRule.new(strict: true)
rule.validate(document, repository, collector)

Constant Summary collapse

CATEGORIES =

Valid rule categories

%i[structure type constraint facet identity].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ ValidationRule

Initialize a new ValidationRule

Parameters:

  • options (Hash) (defaults to: {})

    Rule-specific options



37
38
39
40
# File 'lib/lutaml/xsd/validation/validation_rule.rb', line 37

def initialize(options = {})
  @options = options
  @enabled = options.fetch(:enabled, true)
end

Instance Attribute Details

#optionsHash (readonly)

Returns Rule options.

Returns:

  • (Hash)

    Rule options



32
33
34
# File 'lib/lutaml/xsd/validation/validation_rule.rb', line 32

def options
  @options
end

Instance Method Details

#applicable?(config) ⇒ Boolean

Check if rule should run for given configuration

Parameters:

Returns:

  • (Boolean)


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

#categorySymbol

This method is abstract.

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

Returns:

  • (Symbol)

    The rule category

Raises:

  • (NotImplementedError)

    if not implemented by subclass



72
73
74
75
# File 'lib/lutaml/xsd/validation/validation_rule.rb', line 72

def category
  raise NotImplementedError,
        "#{self.class} must implement #category"
end

#descriptionString

This method is abstract.

Get a human-readable description of the rule

Returns:

  • (String)

    Rule description

Raises:

  • (NotImplementedError)

    if not implemented by subclass



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

Returns:

  • (Boolean)


91
92
93
# File 'lib/lutaml/xsd/validation/validation_rule.rb', line 91

def enabled?
  @enabled
end

#inspectString

Detailed string representation

Returns:

  • (String)


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

Parameters:

  • key (Symbol)

    Option key

  • default (Object) (defaults to: nil)

    Default value if not found

Returns:

  • (Object)

    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

#priorityInteger

Get rule priority for execution order

Rules with lower priority numbers execute first. Default priority is 100.

Returns:

  • (Integer)


115
116
117
# File 'lib/lutaml/xsd/validation/validation_rule.rb', line 115

def priority
  @options.fetch(:priority, 100)
end

#to_hHash

Convert to hash representation

Returns:

  • (Hash)


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_sString

String representation

Returns:

  • (String)


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 is abstract.

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.

Parameters:

Raises:

  • (NotImplementedError)

    if not implemented by subclass



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