Class: Lutaml::Xsd::Validation::Facets::WhiteSpaceFacetValidator

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

Overview

Validates and normalizes values against XSD whiteSpace facet

The whiteSpace facet specifies how whitespace should be handled:

  • preserve: All whitespace is preserved

  • replace: Each tab, newline, and carriage return is replaced with a space

  • collapse: Sequences of whitespace are collapsed to a single space, and leading/trailing whitespace is removed

Examples:

Validating whitespace handling

facet = Lutaml::Xsd::WhiteSpace.new(value: "collapse")
validator = WhiteSpaceFacetValidator.new(facet)
validator.valid?("  hello  world  ")  # => true
validator.normalize("  hello  world  ")  # => "hello world"

Constant Summary collapse

PRESERVE =
"preserve"
REPLACE =
"replace"
COLLAPSE =
"collapse"

Instance Attribute Summary

Attributes inherited from FacetValidator

#facet

Instance Method Summary collapse

Methods inherited from FacetValidator

#facet_value, #initialize

Constructor Details

This class inherits a constructor from Lutaml::Xsd::Validation::Facets::FacetValidator

Instance Method Details

#error_message(value) ⇒ String

Generate error message (not typically used for whiteSpace)

Parameters:

  • value (String)

    The value

Returns:

  • (String)

    Error message



42
43
44
# File 'lib/lutaml/xsd/validation/facets/white_space_facet_validator.rb', line 42

def error_message(value)
  "Invalid whitespace handling for value: #{value}"
end

#normalize(value) ⇒ String

Normalize value according to whiteSpace facet

Parameters:

  • value (String)

    The value to normalize

Returns:

  • (String)

    Normalized value



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/lutaml/xsd/validation/facets/white_space_facet_validator.rb', line 50

def normalize(value)
  return value if value.nil?

  case facet_value
  when PRESERVE
    value
  when REPLACE
    replace_whitespace(value)
  when COLLAPSE
    collapse_whitespace(value)
  else
    value
  end
end

#valid?(_value) ⇒ Boolean

Validate always returns true as whiteSpace is about normalization

Parameters:

  • value (String)

    The value to validate

Returns:

  • (Boolean)

    Always true



34
35
36
# File 'lib/lutaml/xsd/validation/facets/white_space_facet_validator.rb', line 34

def valid?(_value)
  true
end