Class: Lutaml::Xsd::Validation::BaseTypes::BooleanValidator

Inherits:
BaseTypeValidator show all
Defined in:
lib/lutaml/xsd/validation/base_types/boolean_validator.rb

Overview

Validates XSD boolean type

The boolean type accepts the following values:

  • true, false (literal boolean values)
  • "true", "false" (string representations)
  • "1", "0" (numeric representations)

Examples:

Validating boolean values

validator = BooleanValidator.new
validator.valid?("true")   # => true
validator.valid?("false")  # => true
validator.valid?("1")      # => true
validator.valid?("0")      # => true
validator.valid?(true)     # => true
validator.valid?(false)    # => true
validator.valid?("yes")    # => false

Constant Summary collapse

VALID_VALUES =
%w[true false 1 0].freeze

Instance Method Summary collapse

Methods inherited from BaseTypeValidator

for, registered?, #type_name

Instance Method Details

#error_message(value) ⇒ String

Generate error message for invalid boolean

Parameters:

  • value (Object)

    The invalid value

Returns:

  • (String)

    Error message



44
45
46
47
# File 'lib/lutaml/xsd/validation/base_types/boolean_validator.rb', line 44

def error_message(value)
  "Value '#{value}' is not a valid boolean. " \
    "Expected: true, false, 1, or 0"
end

#valid?(value) ⇒ Boolean

Validate value is a valid boolean

Parameters:

  • value (Object)

    The value to validate

Returns:

  • (Boolean)

    true if value is a valid boolean representation



33
34
35
36
37
38
# File 'lib/lutaml/xsd/validation/base_types/boolean_validator.rb', line 33

def valid?(value)
  return false if value.nil?
  return true if value.is_a?(TrueClass) || value.is_a?(FalseClass)

  VALID_VALUES.include?(to_string(value).downcase)
end