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.
-
#enum ⇒ Array<String>
readonly
This array specifies the allowed values that can be used to restrict the output of the model.
-
#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, enum: 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, enum: OMIT, additional_properties: nil) ⇒ Vapi::JsonSchema
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_properties ⇒ OpenStruct (readonly)
Returns 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 |
#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 |
#enum ⇒ Array<String> (readonly)
Returns 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 |
#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
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.
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
117 118 119 |
# File 'lib/vapi_server_sdk/types/json_schema.rb', line 117 def to_json(*_args) @_field_set&.to_json end |