Class: Restspec::Schema::Types::OneOfType

Inherits:
BasicType
  • Object
show all
Defined in:
lib/restspec/schema/types/one_of_type.rb

Overview

It will return an element of a set of elements while making a example and checks against that same set of element while validating. The following options are needed:

elements: An array of the element to use

@example: attribute :pet_type, one_of(elements: %wcat)

Instance Method Summary collapse

Methods inherited from BasicType

#initialize, #of, #totally_valid?, #|

Constructor Details

This class inherits a constructor from Restspec::Schema::Types::BasicType

Instance Method Details

#example_for(attribute) ⇒ Object

Returns A sample of the elements array.

Parameters:

Returns:

  • (Object)

    A sample of the elements array.

Raises:

  • KeyError if the elements option was not initialized.



16
17
18
# File 'lib/restspec/schema/types/one_of_type.rb', line 16

def example_for(attribute)
  elements.sample
end

#to_sObject



29
30
31
# File 'lib/restspec/schema/types/one_of_type.rb', line 29

def to_s
  "OneOfType(#{elements})"
end

#valid?(attribute, value) ⇒ true, false

Returns If the value is included in the elements.

Parameters:

Returns:

  • (true, false)

    If the value is included in the elements.

Raises:

  • KeyError if the elements option was not initialized.



25
26
27
# File 'lib/restspec/schema/types/one_of_type.rb', line 25

def valid?(attribute, value)
  elements.include?(value)
end