Class: Lutaml::Xsd::Validation::Facets::FacetValidator Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/lutaml/xsd/validation/facets/facet_validator.rb

Overview

This class is abstract.

Subclasses must implement #valid? and #error_message

Base class for all facet validators

Facet validators validate values against specific XSD facet constraints such as pattern, length, min/max values, etc.

Examples:

Creating a custom facet validator

class CustomFacetValidator < FacetValidator
  def valid?(value)
    # Custom validation logic
    value.start_with?('custom')
  end

  def error_message(value)
    "Value '#{value}' does not match custom constraint"
  end
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(facet) ⇒ FacetValidator

Initialize the facet validator

Parameters:

  • facet (Object)

    The facet object containing the constraint definition



33
34
35
# File 'lib/lutaml/xsd/validation/facets/facet_validator.rb', line 33

def initialize(facet)
  @facet = facet
end

Instance Attribute Details

#facetObject (readonly)

Returns the value of attribute facet.



27
28
29
# File 'lib/lutaml/xsd/validation/facets/facet_validator.rb', line 27

def facet
  @facet
end

Instance Method Details

#error_message(value) ⇒ String

This method is abstract.

Subclasses must implement this method

Generate an error message for an invalid value

Parameters:

  • value (String)

    The invalid value

Returns:

  • (String)

    A descriptive error message

Raises:

  • (NotImplementedError)

    if not implemented by subclass



54
55
56
57
# File 'lib/lutaml/xsd/validation/facets/facet_validator.rb', line 54

def error_message(value)
  raise NotImplementedError,
        "#{self.class.name} must implement #error_message"
end

#facet_valueObject

Get the facet value

Returns:

  • (Object)

    The facet constraint value



62
63
64
# File 'lib/lutaml/xsd/validation/facets/facet_validator.rb', line 62

def facet_value
  facet.value if facet.respond_to?(:value)
end

#valid?(value) ⇒ Boolean

This method is abstract.

Subclasses must implement this method

Validate a value against the facet constraint

Parameters:

  • value (String)

    The value to validate

Returns:

  • (Boolean)

    true if value is valid, false otherwise

Raises:

  • (NotImplementedError)

    if not implemented by subclass



43
44
45
46
# File 'lib/lutaml/xsd/validation/facets/facet_validator.rb', line 43

def valid?(value)
  raise NotImplementedError,
        "#{self.class.name} must implement #valid?"
end