Class: Vapi::JsonSchema
- Inherits:
-
Object
- Object
- Vapi::JsonSchema
- Defined in:
- lib/vapi_server_sdk/types/json_schema.rb
Constant Summary collapse
- OMIT =
Object.new
Instance Attribute Summary collapse
-
#additional_properties ⇒ OpenStruct
readonly
Additional properties unmapped to the current class definition.
-
#description ⇒ String
readonly
This is the description to help the model understand what it needs to output.
-
#items ⇒ Hash{String => Object}
readonly
This is required if the type is “array”.
-
#properties ⇒ Hash{String => Object}
readonly
This is required if the type is “object”.
-
#required ⇒ Array<String>
readonly
This is a list of properties that are required.
-
#type ⇒ Vapi::JsonSchemaType
readonly
This is the type of output you’d like.
Class Method Summary collapse
-
.from_json(json_object:) ⇒ Vapi::JsonSchema
Deserialize a JSON object to an instance of JsonSchema.
-
.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.
Instance Method Summary collapse
- #initialize(type:, items: OMIT, properties: OMIT, description: OMIT, required: OMIT, additional_properties: nil) ⇒ Vapi::JsonSchema constructor
-
#to_json(*_args) ⇒ String
Serialize an instance of JsonSchema to a JSON object.
Constructor Details
#initialize(type:, items: OMIT, properties: OMIT, description: OMIT, required: OMIT, additional_properties: nil) ⇒ Vapi::JsonSchema
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_properties ⇒ OpenStruct (readonly)
Returns 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 |
#description ⇒ String (readonly)
Returns 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 |
#items ⇒ Hash{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.
23 24 25 |
# File 'lib/vapi_server_sdk/types/json_schema.rb', line 23 def items @items end |
#properties ⇒ Hash{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.
28 29 30 |
# File 'lib/vapi_server_sdk/types/json_schema.rb', line 28 def properties @properties end |
#required ⇒ Array<String> (readonly)
Returns 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 |
#type ⇒ Vapi::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.
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
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.
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
107 108 109 |
# File 'lib/vapi_server_sdk/types/json_schema.rb', line 107 def to_json(*_args) @_field_set&.to_json end |