JsonSchema
class JsonSchema<T : Any>
Definition of a data type.
These types can be objects, but also primitives and arrays. Represents a select subset of an JsonSchema object.
Note: While optional, including a description field in your JsonSchema is strongly encouraged. The more information the model has about what it's expected to generate, the better the results.
Summary
Public companion functions |
|
|---|---|
JsonSchema<String> |
anyOf(schemas: List<JsonSchema<*>>)Returns a |
JsonSchema<List<T>> |
<T : Any> array(Returns a |
JsonSchema<Boolean> |
Returns a |
JsonSchema<Double> |
double(Returns a |
JsonSchema<String> |
Returns a |
JsonSchema<T> |
<T : Any> enumeration(Returns a |
JsonSchema<Float> |
Returns a |
JsonSchema<Int> |
integer(Returns a |
JsonSchema<Long> |
Returns a |
JsonSchema<JsonObject> |
obj(Returns a |
JsonSchema<T> |
<T : Any> obj(Returns a |
JsonSchema<String> |
string(Returns a |
Public functions |
|
|---|---|
KSerializer<T> |
Public properties |
|
|---|---|
List<JsonSchema<*>>? |
|
KClass<T> |
|
String? |
|
List<String>? |
|
String? |
|
JsonSchema<*>? |
|
Int? |
|
Double? |
|
Int? |
|
Double? |
|
Boolean? |
|
Map<String, JsonSchema<*>>? |
|
List<String>? |
|
String? |
|
String |
Public companion functions
anyOf
fun anyOf(schemas: List<JsonSchema<*>>): JsonSchema<String>
Returns a JsonSchema representing a value that must conform to any (one of) the provided sub-schema.
Example: A field that can hold either a simple userID or a more detailed user object.
JsonSchema.anyOf( listOf( JsonSchema.integer(description = "User ID"), JsonSchema.obj( mapOf(
"userID" to JsonSchema.integer(description = "User ID"),
"username" to JsonSchema.string(description = "Username")
)))
| Parameters | |
|---|---|
schemas: List<JsonSchema<*>> |
The list of valid schemas which could be here |
array
fun <T : Any> array(
items: JsonSchema<T>,
description: String? = null,
nullable: Boolean = false,
title: String? = null,
minItems: Int? = null,
maxItems: Int? = null
): JsonSchema<List<T>>
Returns a JsonSchema for an array.
| Parameters | |
|---|---|
items: JsonSchema<T> |
The |
description: String? = null |
An optional description of what the array represents. |
nullable: Boolean = false |
Indicates whether the value can be |
boolean
fun boolean(description: String? = null, nullable: Boolean = false, title: String? = null): JsonSchema<Boolean>
Returns a JsonSchema representing a boolean value.
double
fun double(
description: String? = null,
nullable: Boolean = false,
title: String? = null,
minimum: Double? = null,
maximum: Double? = null
): JsonSchema<Double>
Returns a JsonSchema for a double-precision floating-point number.
enumeration
fun enumeration(
values: List<String>,
description: String? = null,
nullable: Boolean = false,
title: String? = null
): JsonSchema<String>
Returns a JsonSchema for an enumeration.
For example, the cardinal directions can be represented as:
JsonSchema.enumeration(listOf("north", "east", "south", "west"), "Cardinal directions")
enumeration
fun <T : Any> enumeration(
values: List<String>,
clazz: KClass<T>,
description: String? = null,
nullable: Boolean = false,
title: String? = null
): JsonSchema<T>
Returns a JsonSchema for an enumeration.
For example, the cardinal directions can be represented as:
JsonSchema.enumeration(
listOf("north", "east", "south", "west"),
Direction::class,
"Cardinal directions"
)
| Parameters | |
|---|---|
values: List<String> |
The list of valid values for this enumeration |
clazz: KClass<T> |
the real class that this schema represents |
description: String? = null |
The description of what the parameter should contain or represent |
nullable: Boolean = false |
Indicates whether the value can be |
float
fun float(
description: String? = null,
nullable: Boolean = false,
title: String? = null,
minimum: Double? = null,
maximum: Double? = null
): JsonSchema<Float>
Returns a JsonSchema for a single-precision floating-point number.
Important: This JsonSchema provides a hint to the model that it should generate a single-precision floating-point number, but only guarantees that the value will be a number. Therefore it's possible that decoding it as a Float variable (or float in Java) could overflow.
integer
fun integer(
description: String? = null,
nullable: Boolean = false,
title: String? = null,
minimum: Double? = null,
maximum: Double? = null
): JsonSchema<Int>
Returns a JsonSchema for a 32-bit signed integer number.
Important: This JsonSchema provides a hint to the model that it should generate a 32-bit integer, but only guarantees that the value will be an integer. Therefore it's possible that decoding it as an Int variable (or int in Java) could overflow.
long
fun long(
description: String? = null,
nullable: Boolean = false,
title: String? = null,
minimum: Double? = null,
maximum: Double? = null
): JsonSchema<Long>
Returns a JsonSchema for a 64-bit signed integer number.
obj
fun obj(
properties: Map<String, JsonSchema<*>>,
optionalProperties: List<String> = emptyList(),
description: String? = null,
nullable: Boolean = false,
title: String? = null
): JsonSchema<JsonObject>
Returns a JsonSchema for a complex data type.
This schema instructs the model to produce data of type object, which has keys of type String and values of type JsonSchema.
Example: A city could be represented with the following object JsonSchema.
JsonSchema.obj(mapOf(
"name" to JsonSchema.string(),
"population" to JsonSchema.integer()
))
| Parameters | |
|---|---|
properties: Map<String, JsonSchema<*>> |
The map of the object's property names to their |
optionalProperties: List<String> = emptyList() |
The list of optional properties. They must correspond to the keys provided in the |
description: String? = null |
An optional description of what the object represents. |
nullable: Boolean = false |
Indicates whether the value can be |
obj
fun <T : Any> obj(
properties: Map<String, JsonSchema<*>>,
clazz: KClass<T>,
optionalProperties: List<String> = emptyList(),
description: String? = null,
nullable: Boolean = false,
title: String? = null
): JsonSchema<T>
Returns a JsonSchema for a complex data type.
This schema instructs the model to produce data of type object, which has keys of type String and values of type JsonSchema.
Example: A city could be represented with the following object JsonSchema.
JsonSchema.obj(mapOf(
"name" to JsonSchema.string(),
"population" to JsonSchema.integer()
),
City::class
)
| Parameters | |
|---|---|
properties: Map<String, JsonSchema<*>> |
The map of the object's property names to their |
clazz: KClass<T> |
the real class that this schema represents |
optionalProperties: List<String> = emptyList() |
The list of optional properties. They must correspond to the keys provided in the |
description: String? = null |
An optional description of what the object represents. |
nullable: Boolean = false |
Indicates whether the value can be |
string
fun string(
description: String? = null,
nullable: Boolean = false,
format: StringFormat? = null,
title: String? = null
): JsonSchema<String>
Returns a JsonSchema for a string.
| Parameters | |
|---|---|
description: String? = null |
An optional description of what the string should contain or represent. |
nullable: Boolean = false |
Indicates whether the value can be |
format: StringFormat? = null |
An optional pattern that values need to adhere to. |