Class: Vapi::JsonSchema

Inherits:
Object
  • Object
show all
Defined in:
lib/vapi_server_sdk/types/json_schema.rb

Constant Summary collapse

OMIT =
Object.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type:, items: OMIT, properties: OMIT, description: OMIT, required: OMIT, additional_properties: nil) ⇒ Vapi::JsonSchema

Parameters:

  • type (Vapi::JsonSchemaType)

    This is the type of output you’d like. ‘string`, `number`, `integer`, `boolean` are the primitive types and should be obvious. `array` and `object` are more interesting and quite powerful. They allow you to define nested structures. For `array`, you can define the schema of the items in the array using the `items` property. For `object`, you can define the properties of the object using the `properties` property.

  • items (Hash{String => Object}) (defaults to: OMIT)

    This is required if the type is “array”. This is the schema of the items in the array. This is of type JsonSchema. However, Swagger doesn’t support circular references.

  • properties (Hash{String => Object}) (defaults to: OMIT)

    This is required if the type is “object”. This specifies the properties of the object. This is a map of string to JsonSchema. However, Swagger doesn’t support circular references.

  • description (String) (defaults to: OMIT)

    This is the description to help the model understand what it needs to output.

  • required (Array<String>) (defaults to: OMIT)

    This is a list of properties that are required. This only makes sense if the type is “object”.

  • additional_properties (OpenStruct) (defaults to: nil)

    Additional properties unmapped to the current class definition



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/vapi_server_sdk/types/json_schema.rb', line 64

def initialize(type:, items: OMIT, properties: OMIT, description: OMIT, required: OMIT, additional_properties: nil)
  @type = type
  @items = items if items != OMIT
  @properties = properties if properties != OMIT
  @description = description if description != OMIT
  @required = required if required != OMIT
  @additional_properties = additional_properties
  @_field_set = {
    "type": type,
    "items": items,
    "properties": properties,
    "description": description,
    "required": required
  }.reject do |_k, v|
    v == OMIT
  end
end

Instance Attribute Details

#additional_propertiesOpenStruct (readonly)

Returns Additional properties unmapped to the current class definition.

Returns:

  • (OpenStruct)

    Additional properties unmapped to the current class definition



35
36
37
# File 'lib/vapi_server_sdk/types/json_schema.rb', line 35

def additional_properties
  @additional_properties
end

#descriptionString (readonly)

Returns This is the description to help the model understand what it needs to output.

Returns:

  • (String)

    This is the description to help the model understand what it needs to output.



30
31
32
# File 'lib/vapi_server_sdk/types/json_schema.rb', line 30

def description
  @description
end

#itemsHash{String => Object} (readonly)

Returns This is required if the type is “array”. This is the schema of the items in the array. This is of type JsonSchema. However, Swagger doesn’t support circular references.

Returns:

  • (Hash{String => Object})

    This is required if the type is “array”. This is the schema of the items in the array. This is of type JsonSchema. However, Swagger doesn’t support circular references.



23
24
25
# File 'lib/vapi_server_sdk/types/json_schema.rb', line 23

def items
  @items
end

#propertiesHash{String => Object} (readonly)

Returns This is required if the type is “object”. This specifies the properties of the object. This is a map of string to JsonSchema. However, Swagger doesn’t support circular references.

Returns:

  • (Hash{String => Object})

    This is required if the type is “object”. This specifies the properties of the object. This is a map of string to JsonSchema. However, Swagger doesn’t support circular references.



28
29
30
# File 'lib/vapi_server_sdk/types/json_schema.rb', line 28

def properties
  @properties
end

#requiredArray<String> (readonly)

Returns This is a list of properties that are required. This only makes sense if the type is “object”.

Returns:

  • (Array<String>)

    This is a list of properties that are required. This only makes sense if the type is “object”.



33
34
35
# File 'lib/vapi_server_sdk/types/json_schema.rb', line 33

def required
  @required
end

#typeVapi::JsonSchemaType (readonly)

Returns This is the type of output you’d like. ‘string`, `number`, `integer`, `boolean` are the primitive types and should be obvious. `array` and `object` are more interesting and quite powerful. They allow you to define nested structures. For `array`, you can define the schema of the items in the array using the `items` property. For `object`, you can define the properties of the object using the `properties` property.

Returns:

  • (Vapi::JsonSchemaType)

    This is the type of output you’d like. ‘string`, `number`, `integer`, `boolean` are the primitive types and should be obvious. `array` and `object` are more interesting and quite powerful. They allow you to define nested structures. For `array`, you can define the schema of the items in the array using the `items` property. For `object`, you can define the properties of the object using the `properties` property.



18
19
20
# File 'lib/vapi_server_sdk/types/json_schema.rb', line 18

def type
  @type
end

Class Method Details

.from_json(json_object:) ⇒ Vapi::JsonSchema

Deserialize a JSON object to an instance of JsonSchema

Parameters:

  • json_object (String)

Returns:



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/vapi_server_sdk/types/json_schema.rb', line 86

def self.from_json(json_object:)
  struct = JSON.parse(json_object, object_class: OpenStruct)
  parsed_json = JSON.parse(json_object)
  type = parsed_json["type"]
  items = parsed_json["items"]
  properties = parsed_json["properties"]
  description = parsed_json["description"]
  required = parsed_json["required"]
  new(
    type: type,
    items: items,
    properties: properties,
    description: description,
    required: required,
    additional_properties: struct
  )
end

.validate_raw(obj:) ⇒ Void

Leveraged for Union-type generation, validate_raw attempts to parse the given

hash and check each fields type against the current object's property
definitions.

Parameters:

  • obj (Object)

Returns:

  • (Void)


117
118
119
120
121
122
123
# File 'lib/vapi_server_sdk/types/json_schema.rb', line 117

def self.validate_raw(obj:)
  obj.type.is_a?(Vapi::JsonSchemaType) != false || raise("Passed value for field obj.type is not the expected type, validation failed.")
  obj.items&.is_a?(Hash) != false || raise("Passed value for field obj.items is not the expected type, validation failed.")
  obj.properties&.is_a?(Hash) != false || raise("Passed value for field obj.properties is not the expected type, validation failed.")
  obj.description&.is_a?(String) != false || raise("Passed value for field obj.description is not the expected type, validation failed.")
  obj.required&.is_a?(Array) != false || raise("Passed value for field obj.required is not the expected type, validation failed.")
end

Instance Method Details

#to_json(*_args) ⇒ String

Serialize an instance of JsonSchema to a JSON object

Returns:

  • (String)


107
108
109
# File 'lib/vapi_server_sdk/types/json_schema.rb', line 107

def to_json(*_args)
  @_field_set&.to_json
end