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, pattern: OMIT, format: OMIT, required: OMIT, enum: OMIT, title: 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.

  • pattern (String) (defaults to: OMIT)

    This is the pattern of the string. This is a regex that will be used to validate the data in question. To use a common format, use the ‘format` property instead. OpenAI documentation: platform.openai.com/docs/guides/structured-outputs#supported-properties

  • format (Vapi::JsonSchemaFormat) (defaults to: OMIT)

    This is the format of the string. To pass a regex, use the ‘pattern` property instead. OpenAI documentation: cs/guides/structured-outputs?api-mode=chat&type-restrictions=string-restrictions

  • 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.

  • title (String) (defaults to: OMIT)

    This is the title of the schema.

  • additional_properties (OpenStruct) (defaults to: nil)

    Additional properties unmapped to the current class definition



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

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



51
52
53
# File 'lib/vapi_server_sdk/types/json_schema.rb', line 51

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.



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

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.



47
48
49
# File 'lib/vapi_server_sdk/types/json_schema.rb', line 47

def enum
  @enum
end

#formatVapi::JsonSchemaFormat (readonly)

Returns This is the format of the string. To pass a regex, use the ‘pattern` property instead. OpenAI documentation: cs/guides/structured-outputs?api-mode=chat&type-restrictions=string-restrictions.

Returns:

  • (Vapi::JsonSchemaFormat)

    This is the format of the string. To pass a regex, use the ‘pattern` property instead. OpenAI documentation: cs/guides/structured-outputs?api-mode=chat&type-restrictions=string-restrictions



41
42
43
# File 'lib/vapi_server_sdk/types/json_schema.rb', line 41

def format
  @format
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.



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

def items
  @items
end

#patternString (readonly)

Returns This is the pattern of the string. This is a regex that will be used to validate the data in question. To use a common format, use the ‘format` property instead. OpenAI documentation: platform.openai.com/docs/guides/structured-outputs#supported-properties.

Returns:



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

def pattern
  @pattern
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.



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

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”.



44
45
46
# File 'lib/vapi_server_sdk/types/json_schema.rb', line 44

def required
  @required
end

#titleString (readonly)

Returns This is the title of the schema.

Returns:

  • (String)

    This is the title of the schema.



49
50
51
# File 'lib/vapi_server_sdk/types/json_schema.rb', line 49

def title
  @title
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.



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

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:



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/vapi_server_sdk/types/json_schema.rb', line 122

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


161
162
163
164
165
166
167
168
169
170
171
# File 'lib/vapi_server_sdk/types/json_schema.rb', line 161

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.pattern&.is_a?(String) != false || raise("Passed value for field obj.pattern is not the expected type, validation failed.")
  obj.format&.is_a?(Vapi::JsonSchemaFormat) != false || raise("Passed value for field obj.format 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.")
  obj.title&.is_a?(String) != false || raise("Passed value for field obj.title 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)


151
152
153
# File 'lib/vapi_server_sdk/types/json_schema.rb', line 151

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