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, enum: 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".

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

    This array specifies the allowed values that can be used to restrict the output of the model.

  • additional_properties (OpenStruct) (defaults to: nil)

    Additional properties unmapped to the current class definition



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/vapi_server_sdk/types/json_schema.rb', line 69

def initialize(type:, items: OMIT, properties: OMIT, description: OMIT, required: OMIT, enum: 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
  @enum = enum if enum != OMIT
  @additional_properties = additional_properties
  @_field_set = {
    "type": type,
    "items": items,
    "properties": properties,
    "description": description,
    "required": required,
    "enum": enum
  }.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



38
39
40
# File 'lib/vapi_server_sdk/types/json_schema.rb', line 38

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

#enumArray<String> (readonly)

Returns This array specifies the allowed values that can be used to restrict the output of the model.

Returns:

  • (Array<String>)

    This array specifies the allowed values that can be used to restrict the output of the model.



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

def enum
  @enum
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:



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/vapi_server_sdk/types/json_schema.rb', line 94

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"]
  enum = parsed_json["enum"]
  new(
    type: type,
    items: items,
    properties: properties,
    description: description,
    required: required,
    enum: enum,
    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)


127
128
129
130
131
132
133
134
# File 'lib/vapi_server_sdk/types/json_schema.rb', line 127

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.")
  obj.enum&.is_a?(Array) != false || raise("Passed value for field obj.enum 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)


117
118
119
# File 'lib/vapi_server_sdk/types/json_schema.rb', line 117

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