Class: Restspec::Schema::Types::EmbeddedSchemaType

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

Overview

An embedded schema is an attribute with all the options of a given schema.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from BasicType

#of, #to_s, #totally_valid?, #|

Constructor Details

#initialize(schema_name, options = {}) ⇒ EmbeddedSchemaType

Returns a new instance of EmbeddedSchemaType


7
8
9
10
# File 'lib/restspec/schema/types/embedded_schema_type.rb', line 7

def initialize(schema_name, options = {})
  self.schema_name = schema_name
  super(options)
end

Instance Attribute Details

#schema_nameObject

Returns the value of attribute schema_name


5
6
7
# File 'lib/restspec/schema/types/embedded_schema_type.rb', line 5

def schema_name
  @schema_name
end

Instance Method Details

#example_for(attribute) ⇒ Object

Generates examples by creating a SchemaExample using the passed schema.

Parameters:


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

def example_for(attribute)
  Restspec::Schema::SchemaExample.new(schema).value
end

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

Checks if the value is an embedded valid example of the schema used to initialize.

Examples:

# Let's assume this schema
schema :category do
  attribute :name, string
end

# We can do this anywhere
schema :product do
  attribute :name, string
  attribute :category, embedded_schema(:category)
end

{ name: 'Something', category: { name: 'Products' }  } # a valid product!
{ name: 'Something' } # an invalid product!

Parameters:

Returns:

  • (true, false)

    If the value is an embedded valid example of the schema.


42
43
44
45
46
47
48
49
50
51
# File 'lib/restspec/schema/types/embedded_schema_type.rb', line 42

def valid?(attribute, value)
  begin
    Restspec::Schema::Checker.new(schema).check!(value)
    true
  rescue  Restspec::Schema::Checker::NoAttributeError,
          Restspec::Schema::Checker::InvalidationError,
          Restspec::Schema::Checker::NoObjectError
    false
  end
end