@@ -93,8 +93,8 @@ s.object({
93
93
s .property (" email" , s .string ({ format: " email" })),
94
94
s .property (" phoneNumber" , s .ref (" phoneNumber" )),
95
95
s .property (" billingAddress" , s .oneOf (s .ref (" ukAddress" ), s .ref (" usAddress" ))),
96
+ s .patternProperty (" ^[A-Za-z]$" , s .string ()),
96
97
],
97
- patternProperties: [s .patternProperty (" ^[A-Za-z]$" , s .string ())],
98
98
additionalProperties: s .array ({
99
99
items: s .number ({ minimum: 0 , maximum: 5000 }),
100
100
}),
@@ -208,17 +208,296 @@ Or if you want to import a specific dialect:
208
208
import { s } from " json-schema-fns/2020" ;
209
209
```
210
210
211
- ### ` object `
211
+ All builder methods return a ` SchemaBuilder ` , and you can generate the JSON schema created by the builder using ` toSchemaDocument ` like so
212
212
213
- ### ` array `
213
+ ``` typescript
214
+ s .object ().toSchemaDocument ();
215
+ ```
216
+
217
+ Which will result in the following document
218
+
219
+ ``` json
220
+ {
221
+ "$schema" : " https://json-schema.org/draft/2020-12/schema" ,
222
+ "type" : " object"
223
+ }
224
+ ```
225
+
226
+ If you don't want the ` $schema ` property, use ` toSchema ` instead:
227
+
228
+ ``` typescript
229
+ s .object ().toSchema (); // { "type": "object" }
230
+ ```
231
+
232
+ All builder methods also support the options in ` AnnotationSchema ` :
233
+
234
+ ``` typescript
235
+ s .object ({
236
+ $id: " #/foobar" ,
237
+ $comment: " This is a comment" ,
238
+ default: {},
239
+ title: " FooBar Object Schema" ,
240
+ description: " This is the FooBar schema description" ,
241
+ examples: [{ foo: " bar" }],
242
+ deprecated: true ,
243
+ readOnly: true ,
244
+ writeOnly: false ,
245
+ }).toSchema ();
246
+ ```
247
+
248
+ Produces the schema
249
+
250
+ ``` json
251
+ {
252
+ "type" : " object" ,
253
+ "$id" : " #/foobar" ,
254
+ "$comment" : " This is a comment" ,
255
+ "default" : {},
256
+ "title" : " FooBar Object Schema" ,
257
+ "description" : " This is the FooBar schema description" ,
258
+ "examples" : [{ "foo" : " bar" }],
259
+ "deprecated" : true ,
260
+ "readOnly" : true ,
261
+ "writeOnly" : false
262
+ }
263
+ ```
264
+
265
+ ### ` s.object(options: ObjectOptions) `
266
+
267
+ Builds a schema of type ` object ` , accepting a single argument of type ` ObjectOptions `
268
+
269
+ #### ` ObjectOptions.properties `
270
+
271
+ An array of optional and required properties
272
+
273
+ ``` typescript
274
+ s .object ({
275
+ properties: [
276
+ s .property (" name" , s .string ()),
277
+ s .requiredProperty (" email" , s .string ({ format: " email" })),
278
+ ],
279
+ }).toSchema ();
280
+ ```
281
+
282
+ Produces the schema
283
+
284
+ ``` json
285
+ {
286
+ "type" : " object" ,
287
+ "properties" : {
288
+ "name" : { "type" : " string" },
289
+ "email" : { "type" : " string" , "format" : " email" }
290
+ },
291
+ "required" : [" email" ]
292
+ }
293
+ ```
294
+
295
+ You can also add [ patternProperties] ( https://json-schema.org/understanding-json-schema/reference/object.html#pattern-properties )
296
+
297
+ ``` typescript
298
+ s .object ({ properties: [s .patternProperty (" ^S_" , s .string ())] }).toSchema ();
299
+ ```
300
+
301
+ which produces the schema
302
+
303
+ ``` json
304
+ {
305
+ "type" : " object" ,
306
+ "patternProperties" : {
307
+ "^S_" : { "type" : " string" }
308
+ }
309
+ }
310
+ ```
311
+
312
+ #### ` ObjectOptions.additonalProperties `
313
+
314
+ Add an [ additonalProperties] ( https://json-schema.org/understanding-json-schema/reference/object.html#additional-properties ) schema:
315
+
316
+ ``` typescript
317
+ s .object ({ additionalProperties: s .number () }).toSchema ();
318
+ ```
319
+
320
+ Produces the schema
321
+
322
+ ``` json
323
+ {
324
+ "type" : " object" ,
325
+ "additionalProperties" : {
326
+ "type" : " number"
327
+ }
328
+ }
329
+ ```
330
+
331
+ #### ` ObjectOptions.propertyNames `
332
+
333
+ Add a [ propertyNames] ( https://json-schema.org/understanding-json-schema/reference/object.html#property-names ) pattern:
334
+
335
+ ``` typescript
336
+ s .object ({ propertyNames: " ^[A-Za-z_][A-Za-z0-9_]*$" }).toSchema ();
337
+ ```
338
+
339
+ Produces the schema
340
+
341
+ ``` json
342
+ {
343
+ "type" : " object" ,
344
+ "propertyNames" : {
345
+ "pattern" : " ^[A-Za-z_][A-Za-z0-9_]*$"
346
+ }
347
+ }
348
+ ```
349
+
350
+ #### ` ObjectOptions.minProperties ` and ` ObjectOptions.maxProperties `
351
+
352
+ Validate the number of properties in an object using [ min/maxProperties] ( https://json-schema.org/understanding-json-schema/reference/object.html#size )
353
+
354
+ ``` typescript
355
+ s .object ({ minProperties: 4 , maxProperties: 10 }).toSchema ();
356
+ ```
357
+
358
+ Produces the schema
359
+
360
+ ``` json
361
+ {
362
+ "type" : " object" ,
363
+ "minProperties" : 4 ,
364
+ "maxProperties" : 10
365
+ }
366
+ ```
367
+
368
+ #### ` ObjectOptions.unevaluatedProperties `
369
+
370
+ Specify the handling of [ unevaluatedProperties] ( https://json-schema.org/understanding-json-schema/reference/object.html#unevaluated-properties )
371
+
372
+ ``` typescript
373
+ s .object ({ unevaluatedProperties: false }).toSchema ();
374
+ ```
375
+
376
+ Produces the schema
377
+
378
+ ``` json
379
+ {
380
+ "type" : " object" ,
381
+ "unevaluatedProperties" : false
382
+ }
383
+ ```
384
+
385
+ ### ` s.array(options: ArrayOptions) `
386
+
387
+ Builds a schema of type ` array ` , accepting a single argument of type ` ArrayOptions `
388
+
389
+ #### ` ArrayOptions.items `
390
+
391
+ Define the [ items] ( https://json-schema.org/understanding-json-schema/reference/array.html#items ) schema for an array:
392
+
393
+ ``` typescript
394
+ s .array ({ items: s .string () }).toSchema ();
395
+ ```
396
+
397
+ Produces the schema
398
+
399
+ ``` json
400
+ {
401
+ "type" : " array" ,
402
+ "items" : { "type" : " string" }
403
+ }
404
+ ```
405
+
406
+ #### ` ArrayOptions.minItems ` and ` ArrayOptions.maxItems `
407
+
408
+ Define the array [ length] ( https://json-schema.org/understanding-json-schema/reference/array.html#length )
409
+
410
+ ``` typescript
411
+ s .array ({ contains: { schema: s .number (), min: 1 , max: 3 }).toSchema ();
412
+ ` ` `
413
+
414
+ Produces the schema
415
+
416
+ ` ` ` json
417
+ {
418
+ " type" : " array" ,
419
+ " contains" : { " type" : " number" },
420
+ " minContains" : 1 ,
421
+ " maxContains" : 3
422
+ }
423
+ ` ` `
424
+
425
+ #### ` ArrayOptions .prefixItems `
426
+
427
+ Allows you to perform [tuple validation](https://json-schema.org/understanding-json-schema/reference/array.html#tuple-validation):
428
+
429
+ ` ` ` typescript
430
+ s .array ({ prefixItems: [s .string (), s .number ()] }).toSchema ();
431
+ ` ` `
432
+
433
+ Produces the schema
434
+
435
+ ` ` ` json
436
+ {
437
+ " type" : " array" ,
438
+ " prefixItems" : [{ " type" : " string" }, { " type" : " number" }]
439
+ }
440
+ ` ` `
441
+
442
+ #### ` ArrayOptions .unevaluatedItems `
443
+
444
+ Define the schema for [unevaluatedItems](https://json-schema.org/understanding-json-schema/reference/array.html#unevaluated-items)
445
+
446
+ ` ` ` typescript
447
+ s .array ({ unevaluatedItems: s .object () }).toSchema ();
448
+ ` ` `
449
+
450
+ Produces the schema
451
+
452
+ ` ` ` json
453
+ {
454
+ " type" : " array" ,
455
+ " unevaluatedItems" : { " type" : " object" }
456
+ }
457
+ ` ` `
458
+
459
+ #### ` ArrayOptions .contains `
460
+
461
+ Define the schema [contains](https://json-schema.org/understanding-json-schema/reference/array.html#contains)
462
+
463
+ ` ` ` typescript
464
+ s .array ({ contains: { schema: s .number (), min: 1 , max: 3 }).toSchema ();
465
+ ` ` `
466
+
467
+ Produces the schema
468
+
469
+ ` ` ` json
470
+ {
471
+ " type" : " array" ,
472
+ " contains" : { " type" : " number" },
473
+ " minContains" : 1 ,
474
+ " maxContains" : 3
475
+ }
476
+ ` ` `
214
477
215
478
### ` string `
216
479
217
480
### ` integer ` and ` number `
218
481
482
+ ### ` boolean `
483
+
219
484
### ` nil `
220
485
221
- ### ` boolean `
486
+ ### ` nullable `
487
+
488
+ ### ` anyOf ` | ` allOf ` | ` oneOf `
489
+
490
+ ### ` ifThenElse ` and ` ifThen `
491
+
492
+ ### ` not `
493
+
494
+ ### ` def ` and ` ref `
495
+
496
+ ### ` $const `
497
+
498
+ ### ` $enumerator `
499
+
500
+ ### ` $true ` and ` $false `
222
501
223
502
## Roadmap
224
503
0 commit comments