@@ -170,6 +170,7 @@ class Schema<T>(
170
170
val name : String ,
171
171
val description : String ,
172
172
val format : String? = null ,
173
+ val nullable : Boolean? = null ,
173
174
val enum : List <String >? = null ,
174
175
val properties : Map <String , Schema <out Any >>? = null ,
175
176
val required : List <String >? = null ,
@@ -186,29 +187,64 @@ class Schema<T>(
186
187
companion object {
187
188
/* * Registers a schema for an integer number */
188
189
fun int (name : String , description : String ) =
189
- Schema <Long >(name = name, description = description, type = FunctionType .INTEGER )
190
+ Schema <Long >(
191
+ name = name,
192
+ description = description,
193
+ type = FunctionType .INTEGER ,
194
+ nullable = false ,
195
+ )
190
196
191
197
/* * Registers a schema for a string */
192
198
fun str (name : String , description : String ) =
193
- Schema <String >(name = name, description = description, type = FunctionType .STRING )
199
+ Schema <String >(
200
+ name = name,
201
+ description = description,
202
+ type = FunctionType .STRING ,
203
+ nullable = false ,
204
+ )
194
205
195
206
/* * Registers a schema for a boolean */
196
207
fun bool (name : String , description : String ) =
197
- Schema <Boolean >(name = name, description = description, type = FunctionType .BOOLEAN )
208
+ Schema <Boolean >(
209
+ name = name,
210
+ description = description,
211
+ type = FunctionType .BOOLEAN ,
212
+ nullable = false ,
213
+ )
198
214
199
215
/* * Registers a schema for a floating point number */
200
216
fun num (name : String , description : String ) =
201
- Schema <Double >(name = name, description = description, type = FunctionType .NUMBER )
217
+ Schema <Double >(
218
+ name = name,
219
+ description = description,
220
+ type = FunctionType .NUMBER ,
221
+ nullable = false ,
222
+ )
202
223
203
224
/* *
204
225
* Registers a schema for a complex object. In a function it will be returned as a [JSONObject]
205
226
*/
206
- fun obj (name : String , description : String ) =
207
- Schema <JSONObject >(name = name, description = description, type = FunctionType .OBJECT )
227
+ fun obj (name : String , description : String , vararg contents : Schema <out Any >) =
228
+ Schema <JSONObject >(
229
+ name = name,
230
+ description = description,
231
+ type = FunctionType .OBJECT ,
232
+ required = contents.map { it.name },
233
+ properties = contents.associateBy { it.name }.toMap(),
234
+ )
208
235
209
- /* * Registers a schema for an array */
210
- fun arr (name : String , description : String ) =
211
- Schema <List <String >>(name = name, description = description, type = FunctionType .ARRAY )
236
+ /* *
237
+ * Registers a schema for an array.
238
+ * @param items can be used to specify the type of the array
239
+ */
240
+ fun arr (name : String , description : String , items : Schema <out Any >? = null) =
241
+ Schema <List <String >>(
242
+ name = name,
243
+ description = description,
244
+ type = FunctionType .ARRAY ,
245
+ items = items,
246
+ nullable = false ,
247
+ )
212
248
213
249
/* * Registers a schema for an enum */
214
250
fun enum (name : String , description : String , values : List <String >) =
@@ -218,6 +254,7 @@ class Schema<T>(
218
254
format = " enum" ,
219
255
enum = values,
220
256
type = FunctionType .STRING ,
257
+ nullable = false ,
221
258
)
222
259
}
223
260
}
0 commit comments