JsonSchema.Companion

public static class JsonSchema.Companion


Summary

Public methods

static final @NonNull JsonSchema<@NonNull String>

Returns a JsonSchema representing a value that must conform to any (one of) the provided sub-schema.

static final @NonNull JsonSchema<@NonNull List<@NonNull T>>
<T extends Object> array(
    @NonNull JsonSchema<@NonNull T> items,
    String description,
    boolean nullable,
    String title,
    Integer minItems,
    Integer maxItems
)

Returns a JsonSchema for an array.

static final @NonNull JsonSchema<@NonNull Boolean>
bool(String description, boolean nullable, String title)

Returns a JsonSchema representing a boolean value.

static final @NonNull JsonSchema<@NonNull String>
enumeration(
    @NonNull List<@NonNull String> values,
    String description,
    boolean nullable,
    String title
)

Returns a JsonSchema for an enumeration.

static final @NonNull JsonSchema<@NonNull T>
<T extends Object> enumeration(
    @NonNull List<@NonNull String> values,
    @NonNull KClass<@NonNull T> clazz,
    String description,
    boolean nullable,
    String title
)

Returns a JsonSchema for an enumeration.

static final @NonNull JsonSchema<@NonNull Double>
numDouble(
    String description,
    boolean nullable,
    String title,
    Double minimum,
    Double maximum
)

Returns a JsonSchema for a double-precision floating-point number.

static final @NonNull JsonSchema<@NonNull Float>
numFloat(
    String description,
    boolean nullable,
    String title,
    Double minimum,
    Double maximum
)

Returns a JsonSchema for a single-precision floating-point number.

static final @NonNull JsonSchema<@NonNull Integer>
numInt(
    String description,
    boolean nullable,
    String title,
    Double minimum,
    Double maximum
)

Returns a JsonSchema for a 32-bit signed integer number.

static final @NonNull JsonSchema<@NonNull Long>
numLong(
    String description,
    boolean nullable,
    String title,
    Double minimum,
    Double maximum
)

Returns a JsonSchema for a 64-bit signed integer number.

static final @NonNull JsonSchema<@NonNull JsonObject>
obj(
    @NonNull Map<@NonNull String, @NonNull JsonSchema<@NonNull ?>> properties,
    @NonNull List<@NonNull String> optionalProperties,
    String description,
    boolean nullable,
    String title
)

Returns a JsonSchema for a complex data type.

static final @NonNull JsonSchema<@NonNull T>
<T extends Object> obj(
    @NonNull Map<@NonNull String, @NonNull JsonSchema<@NonNull ?>> properties,
    @NonNull KClass<@NonNull T> clazz,
    @NonNull List<@NonNull String> optionalProperties,
    String description,
    boolean nullable,
    String title
)

Returns a JsonSchema for a complex data type.

static final @NonNull JsonSchema<@NonNull String>
string(
    String description,
    boolean nullable,
    StringFormat format,
    String title
)

Returns a JsonSchema for a string.

Public methods

anyOf

public static final @NonNull JsonSchema<@NonNull StringanyOf(@NonNull List<@NonNull JsonSchema<@NonNull ?>> schemas)

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
@NonNull List<@NonNull JsonSchema<@NonNull ?>> schemas

The list of valid schemas which could be here

array

public static final @NonNull JsonSchema<@NonNull List<@NonNull T>> <T extends Object> array(
    @NonNull JsonSchema<@NonNull T> items,
    String description,
    boolean nullable,
    String title,
    Integer minItems,
    Integer maxItems
)

Returns a JsonSchema for an array.

Parameters
@NonNull JsonSchema<@NonNull T> items

The JsonSchema of the elements stored in the array.

String description

An optional description of what the array represents.

boolean nullable

Indicates whether the value can be null. Defaults to false.

bool

public static final @NonNull JsonSchema<@NonNull Booleanbool(String description, boolean nullable, String title)

Returns a JsonSchema representing a boolean value.

Parameters
String description

An optional description of what the boolean should contain or represent.

boolean nullable

Indicates whether the value can be null. Defaults to false.

enumeration

public static final @NonNull JsonSchema<@NonNull Stringenumeration(
    @NonNull List<@NonNull String> values,
    String description,
    boolean nullable,
    String title
)

Returns a JsonSchema for an enumeration.

For example, the cardinal directions can be represented as:

JsonSchema.enumeration(listOf("north", "east", "south", "west"), "Cardinal directions")
Parameters
@NonNull List<@NonNull String> values

The list of valid values for this enumeration

String description

The description of what the parameter should contain or represent

boolean nullable

Indicates whether the value can be null. Defaults to false.

enumeration

public static final @NonNull JsonSchema<@NonNull T> <T extends Object> enumeration(
    @NonNull List<@NonNull String> values,
    @NonNull KClass<@NonNull T> clazz,
    String description,
    boolean nullable,
    String title
)

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
@NonNull List<@NonNull String> values

The list of valid values for this enumeration

@NonNull KClass<@NonNull T> clazz

the real class that this schema represents

String description

The description of what the parameter should contain or represent

boolean nullable

Indicates whether the value can be null. Defaults to false.

numDouble

public static final @NonNull JsonSchema<@NonNull DoublenumDouble(
    String description,
    boolean nullable,
    String title,
    Double minimum,
    Double maximum
)

Returns a JsonSchema for a double-precision floating-point number.

Parameters
String description

An optional description of what the number should contain or represent.

boolean nullable

Indicates whether the value can be null. Defaults to false.

numFloat

public static final @NonNull JsonSchema<@NonNull FloatnumFloat(
    String description,
    boolean nullable,
    String title,
    Double minimum,
    Double maximum
)

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.

Parameters
String description

An optional description of what the number should contain or represent.

boolean nullable

Indicates whether the value can be null. Defaults to false.

numInt

public static final @NonNull JsonSchema<@NonNull IntegernumInt(
    String description,
    boolean nullable,
    String title,
    Double minimum,
    Double maximum
)

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.

Parameters
String description

An optional description of what the integer should contain or represent.

boolean nullable

Indicates whether the value can be null. Defaults to false.

numLong

public static final @NonNull JsonSchema<@NonNull LongnumLong(
    String description,
    boolean nullable,
    String title,
    Double minimum,
    Double maximum
)

Returns a JsonSchema for a 64-bit signed integer number.

Parameters
String description

An optional description of what the number should contain or represent.

boolean nullable

Indicates whether the value can be null. Defaults to false.

obj

public static final @NonNull JsonSchema<@NonNull JsonObjectobj(
    @NonNull Map<@NonNull String, @NonNull JsonSchema<@NonNull ?>> properties,
    @NonNull List<@NonNull String> optionalProperties,
    String description,
    boolean nullable,
    String title
)

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
@NonNull Map<@NonNull String, @NonNull JsonSchema<@NonNull ?>> properties

The map of the object's property names to their JsonSchemas.

@NonNull List<@NonNull String> optionalProperties

The list of optional properties. They must correspond to the keys provided in the properties map. By default it's empty, signaling the model that all properties are to be included.

String description

An optional description of what the object represents.

boolean nullable

Indicates whether the value can be null. Defaults to false.

obj

public static final @NonNull JsonSchema<@NonNull T> <T extends Object> obj(
    @NonNull Map<@NonNull String, @NonNull JsonSchema<@NonNull ?>> properties,
    @NonNull KClass<@NonNull T> clazz,
    @NonNull List<@NonNull String> optionalProperties,
    String description,
    boolean nullable,
    String title
)

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
@NonNull Map<@NonNull String, @NonNull JsonSchema<@NonNull ?>> properties

The map of the object's property names to their JsonSchemas.

@NonNull KClass<@NonNull T> clazz

the real class that this schema represents

@NonNull List<@NonNull String> optionalProperties

The list of optional properties. They must correspond to the keys provided in the properties map. By default it's empty, signaling the model that all properties are to be included.

String description

An optional description of what the object represents.

boolean nullable

Indicates whether the value can be null. Defaults to false.

string

public static final @NonNull JsonSchema<@NonNull Stringstring(
    String description,
    boolean nullable,
    StringFormat format,
    String title
)

Returns a JsonSchema for a string.

Parameters
String description

An optional description of what the string should contain or represent.

boolean nullable

Indicates whether the value can be null. Defaults to false.

StringFormat format

An optional pattern that values need to adhere to.