@@ -42,6 +42,12 @@ internal constructor(
4242 public val properties: Map <String , Schema >? = null ,
4343 public val required: List <String >? = null ,
4444 public val items: Schema ? = null ,
45+ public val title: String? = null ,
46+ public val minItems: Int? = null ,
47+ public val maxItems: Int? = null ,
48+ public val minimum: Double? = null ,
49+ public val maximum: Double? = null ,
50+ public val anyOf: List <Schema >? = null ,
4551) {
4652
4753 public companion object {
@@ -53,12 +59,12 @@ internal constructor(
5359 */
5460 @JvmStatic
5561 @JvmOverloads
56- public fun boolean (description : String? = null, nullable : Boolean = false): Schema =
57- Schema (
58- description = description ,
59- nullable = nullable ,
60- type = " BOOLEAN " ,
61- )
62+ public fun boolean (
63+ description : String? = null,
64+ nullable : Boolean = false ,
65+ title : String? = null ,
66+ ): Schema =
67+ Schema (description = description, nullable = nullable, type = " BOOLEAN " , title = title )
6268
6369 /* *
6470 * Returns a [Schema] for a 32-bit signed integer number.
@@ -73,12 +79,21 @@ internal constructor(
7379 @JvmStatic
7480 @JvmName(" numInt" )
7581 @JvmOverloads
76- public fun integer (description : String? = null, nullable : Boolean = false): Schema =
82+ public fun integer (
83+ description : String? = null,
84+ nullable : Boolean = false,
85+ title : String? = null,
86+ minimum : Double? = null,
87+ maximum : Double? = null,
88+ ): Schema =
7789 Schema (
7890 description = description,
7991 format = " int32" ,
8092 nullable = nullable,
8193 type = " INTEGER" ,
94+ title = title,
95+ minimum = minimum,
96+ maximum = maximum,
8297 )
8398
8499 /* *
@@ -90,11 +105,20 @@ internal constructor(
90105 @JvmStatic
91106 @JvmName(" numLong" )
92107 @JvmOverloads
93- public fun long (description : String? = null, nullable : Boolean = false): Schema =
108+ public fun long (
109+ description : String? = null,
110+ nullable : Boolean = false,
111+ title : String? = null,
112+ minimum : Double? = null,
113+ maximum : Double? = null,
114+ ): Schema =
94115 Schema (
95116 description = description,
96117 nullable = nullable,
97118 type = " INTEGER" ,
119+ title = title,
120+ minimum = minimum,
121+ maximum = maximum,
98122 )
99123
100124 /* *
@@ -106,8 +130,21 @@ internal constructor(
106130 @JvmStatic
107131 @JvmName(" numDouble" )
108132 @JvmOverloads
109- public fun double (description : String? = null, nullable : Boolean = false): Schema =
110- Schema (description = description, nullable = nullable, type = " NUMBER" )
133+ public fun double (
134+ description : String? = null,
135+ nullable : Boolean = false,
136+ title : String? = null,
137+ minimum : Double? = null,
138+ maximum : Double? = null,
139+ ): Schema =
140+ Schema (
141+ description = description,
142+ nullable = nullable,
143+ type = " NUMBER" ,
144+ title = title,
145+ minimum = minimum,
146+ maximum = maximum,
147+ )
111148
112149 /* *
113150 * Returns a [Schema] for a single-precision floating-point number.
@@ -123,8 +160,22 @@ internal constructor(
123160 @JvmStatic
124161 @JvmName(" numFloat" )
125162 @JvmOverloads
126- public fun float (description : String? = null, nullable : Boolean = false): Schema =
127- Schema (description = description, nullable = nullable, type = " NUMBER" , format = " float" )
163+ public fun float (
164+ description : String? = null,
165+ nullable : Boolean = false,
166+ title : String? = null,
167+ minimum : Double? = null,
168+ maximum : Double? = null,
169+ ): Schema =
170+ Schema (
171+ description = description,
172+ nullable = nullable,
173+ type = " NUMBER" ,
174+ format = " float" ,
175+ title = title,
176+ minimum = minimum,
177+ maximum = maximum,
178+ )
128179
129180 /* *
130181 * Returns a [Schema] for a string.
@@ -139,13 +190,15 @@ internal constructor(
139190 public fun string (
140191 description : String? = null,
141192 nullable : Boolean = false,
142- format : StringFormat ? = null
193+ format : StringFormat ? = null,
194+ title : String? = null,
143195 ): Schema =
144196 Schema (
145197 description = description,
146198 format = format?.value,
147199 nullable = nullable,
148- type = " STRING"
200+ type = " STRING" ,
201+ title = title,
149202 )
150203
151204 /* *
@@ -155,6 +208,7 @@ internal constructor(
155208 * `String` and values of type [Schema].
156209 *
157210 * **Example:** A `city` could be represented with the following object `Schema`.
211+ *
158212 * ```
159213 * Schema.obj(mapOf(
160214 * "name" to Schema.string(),
@@ -176,6 +230,7 @@ internal constructor(
176230 optionalProperties : List <String > = emptyList(),
177231 description : String? = null,
178232 nullable : Boolean = false,
233+ title : String? = null,
179234 ): Schema {
180235 if (! properties.keys.containsAll(optionalProperties)) {
181236 throw IllegalArgumentException (
@@ -188,6 +243,7 @@ internal constructor(
188243 properties = properties,
189244 required = properties.keys.minus(optionalProperties.toSet()).toList(),
190245 type = " OBJECT" ,
246+ title = title,
191247 )
192248 }
193249
@@ -203,20 +259,25 @@ internal constructor(
203259 public fun array (
204260 items : Schema ,
205261 description : String? = null,
206- nullable : Boolean = false
262+ nullable : Boolean = false,
263+ title : String? = null,
264+ minItems : Int? = null,
265+ maxItems : Int? = null,
207266 ): Schema =
208267 Schema (
209268 description = description,
210269 nullable = nullable,
211270 items = items,
212271 type = " ARRAY" ,
272+ title = title,
273+ minItems = minItems,
274+ maxItems = maxItems,
213275 )
214276
215277 /* *
216278 * Returns a [Schema] for an enumeration.
217279 *
218280 * For example, the cardinal directions can be represented as:
219- *
220281 * ```
221282 * Schema.enumeration(listOf("north", "east", "south", "west"), "Cardinal directions")
222283 * ```
@@ -230,37 +291,79 @@ internal constructor(
230291 public fun enumeration (
231292 values : List <String >,
232293 description : String? = null,
233- nullable : Boolean = false
294+ nullable : Boolean = false,
295+ title : String? = null,
234296 ): Schema =
235297 Schema (
236298 description = description,
237299 format = " enum" ,
238300 nullable = nullable,
239301 enum = values,
240302 type = " STRING" ,
303+ title = title,
241304 )
305+
306+ /* *
307+ * Returns a [Schema] representing a value that must conform to *any* (one of) the provided
308+ * sub-schema.
309+ *
310+ * Example: A field that can hold either a simple userID or a more detailed user object.
311+ *
312+ * Schema.anyOf( listOf( Schema.integer(description = "User ID"), Schema.obj(mapOf(
313+ *
314+ * ```
315+ * "userID" to Schema.integer(description = "User ID"),
316+ * "username" to Schema.string(description = "Username")
317+ * ```
318+ *
319+ * )) )
320+ *
321+ * @param schemas The list of valid schemas which could be here
322+ */
323+ @JvmStatic
324+ public fun anyOf (schemas : List <Schema >): Schema = Schema (type = " ANYOF" , anyOf = schemas)
242325 }
243326
244- internal fun toInternal (): Internal =
245- Internal (
246- type,
327+ internal fun toInternal (): Internal {
328+ val cleanedType =
329+ if (type == " ANYOF" ) {
330+ null
331+ } else {
332+ type
333+ }
334+ return Internal (
335+ cleanedType,
247336 description,
248337 format,
249338 nullable,
250339 enum,
251340 properties?.mapValues { it.value.toInternal() },
252341 required,
253342 items?.toInternal(),
343+ title,
344+ minItems,
345+ maxItems,
346+ minimum,
347+ maximum,
348+ anyOf?.map { it.toInternal() },
254349 )
350+ }
351+
255352 @Serializable
256353 internal data class Internal (
257- val type : String ,
354+ val type : String? = null ,
258355 val description : String? = null ,
259356 val format : String? = null ,
260357 val nullable : Boolean? = false ,
261358 val enum : List <String >? = null ,
262359 val properties : Map <String , Internal >? = null ,
263360 val required : List <String >? = null ,
264361 val items : Internal ? = null ,
362+ val title : String? = null ,
363+ val minItems : Int? = null ,
364+ val maxItems : Int? = null ,
365+ val minimum : Double? = null ,
366+ val maximum : Double? = null ,
367+ val anyOf : List <Internal >? = null ,
265368 )
266369}
0 commit comments