Class: Lutaml::Hal::EndpointParameter::Schema

Inherits:
Object
  • Object
show all
Defined in:
lib/lutaml/hal/endpoint_parameter.rb

Overview

Schema validation and type checking

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(definition = {}) ⇒ Schema

Returns a new instance of Schema.



93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/lutaml/hal/endpoint_parameter.rb', line 93

def initialize(definition = {})
  @type = definition[:type] || :string
  @format = definition[:format]
  @enum = definition[:enum]
  @minimum = definition[:minimum]
  @maximum = definition[:maximum]
  @min_length = definition[:min_length]
  @max_length = definition[:max_length]
  @pattern = definition[:pattern]
  @items = definition[:items]
  @default = definition[:default]
  @nullable = definition[:nullable] || false
end

Instance Attribute Details

#defaultObject (readonly)

Returns the value of attribute default.



90
91
92
# File 'lib/lutaml/hal/endpoint_parameter.rb', line 90

def default
  @default
end

#enumObject (readonly)

Returns the value of attribute enum.



90
91
92
# File 'lib/lutaml/hal/endpoint_parameter.rb', line 90

def enum
  @enum
end

#formatObject (readonly)

Returns the value of attribute format.



90
91
92
# File 'lib/lutaml/hal/endpoint_parameter.rb', line 90

def format
  @format
end

#itemsObject (readonly)

Returns the value of attribute items.



90
91
92
# File 'lib/lutaml/hal/endpoint_parameter.rb', line 90

def items
  @items
end

#max_lengthObject (readonly)

Returns the value of attribute max_length.



90
91
92
# File 'lib/lutaml/hal/endpoint_parameter.rb', line 90

def max_length
  @max_length
end

#maximumObject (readonly)

Returns the value of attribute maximum.



90
91
92
# File 'lib/lutaml/hal/endpoint_parameter.rb', line 90

def maximum
  @maximum
end

#min_lengthObject (readonly)

Returns the value of attribute min_length.



90
91
92
# File 'lib/lutaml/hal/endpoint_parameter.rb', line 90

def min_length
  @min_length
end

#minimumObject (readonly)

Returns the value of attribute minimum.



90
91
92
# File 'lib/lutaml/hal/endpoint_parameter.rb', line 90

def minimum
  @minimum
end

#nullableObject (readonly)

Returns the value of attribute nullable.



90
91
92
# File 'lib/lutaml/hal/endpoint_parameter.rb', line 90

def nullable
  @nullable
end

#patternObject (readonly)

Returns the value of attribute pattern.



90
91
92
# File 'lib/lutaml/hal/endpoint_parameter.rb', line 90

def pattern
  @pattern
end

#typeObject (readonly)

Returns the value of attribute type.



90
91
92
# File 'lib/lutaml/hal/endpoint_parameter.rb', line 90

def type
  @type
end

Instance Method Details

#validate(value) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/lutaml/hal/endpoint_parameter.rb', line 107

def validate(value)
  return true if value.nil? && @nullable
  return false if value.nil? && !@nullable

  case @type
  when :string
    validate_string(value)
  when :integer
    validate_integer(value)
  when :number
    validate_number(value)
  when :boolean
    validate_boolean(value)
  when :array
    validate_array(value)
  else
    true
  end
end

#validate_definition!Object

Raises:

  • (ArgumentError)


127
128
129
130
131
132
133
134
# File 'lib/lutaml/hal/endpoint_parameter.rb', line 127

def validate_definition!
  valid_types = %i[string integer number boolean array object]
  raise ArgumentError, "Invalid schema type: #{@type}" unless valid_types.include?(@type)

  return unless @type == :array && @items.nil?

  raise ArgumentError, "Array type requires 'items' definition"
end