-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathJSONSchema.swift
More file actions
2247 lines (2087 loc) · 79.7 KB
/
JSONSchema.swift
File metadata and controls
2247 lines (2087 loc) · 79.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// JSONSchema.swift
// OpenAPI
//
// Created by Mathew Polzin on 6/22/19.
//
import OpenAPIKitCore
/// OpenAPI "Schema Object"
///
/// See [OpenAPI Schema Object](https://spec.openapis.org/oas/v3.2.0.html#schema-object).
public struct JSONSchema: JSONSchemaContext, HasWarnings, Sendable {
public let warnings: [OpenAPI.Warning]
public var value: Schema
internal init(warnings: [OpenAPI.Warning], schema: Schema) {
self.warnings = warnings
self.value = schema
}
public init(schema: Schema) {
warnings = []
value = schema
}
/// The null type, which replaces the functionality of the `nullable` property from
/// previous versions of the OpenAPI specification.
public static func null(_ core: CoreContext<JSONTypeFormat.AnyFormat> = .init(nullable: true)) -> Self {
.init(schema: .null(core.nullableContext()))
}
public static func boolean(_ core: CoreContext<JSONTypeFormat.BooleanFormat>) -> Self {
.init(schema: .boolean(core))
}
public static func number(_ core: CoreContext<JSONTypeFormat.NumberFormat>, _ numeric: NumericContext) -> Self {
.init(schema: .number(core, numeric))
}
public static func integer(_ core: CoreContext<JSONTypeFormat.IntegerFormat>, _ integral: IntegerContext) -> Self {
.init(schema: .integer(core, integral))
}
public static func string(_ core: CoreContext<JSONTypeFormat.StringFormat>, _ string: StringContext) -> Self {
.init(schema: .string(core, string))
}
public static func object(_ core: CoreContext<JSONTypeFormat.ObjectFormat>, _ object: ObjectContext) -> Self {
.init(schema: .object(core, object))
}
public static func array(_ core: CoreContext<JSONTypeFormat.ArrayFormat>, _ array: ArrayContext) -> Self {
.init(schema: .array(core, array))
}
public static func all(of schemas: [JSONSchema], core: CoreContext<JSONTypeFormat.AnyFormat>) -> Self {
.init(schema: .all(of: schemas, core: core))
}
public static func one(of schemas: [JSONSchema], core: CoreContext<JSONTypeFormat.AnyFormat>) -> Self {
.init(schema: .one(of: schemas, core: core))
}
public static func any(of schemas: [JSONSchema], core: CoreContext<JSONTypeFormat.AnyFormat>) -> Self {
.init(schema: .any(of: schemas, core: core))
}
public static func not(_ schema: JSONSchema, core: CoreContext<JSONTypeFormat.AnyFormat>) -> Self {
.init(schema: .not(schema, core: core))
}
public static func reference(_ reference: JSONReference<JSONSchema>, _ context: CoreContext<JSONTypeFormat.AnyFormat>) -> Self {
.init(schema: .reference(reference, context))
}
/// Schemas without a `type`.
public static func fragment(_ core: CoreContext<JSONTypeFormat.AnyFormat>) -> Self {
.init(schema: .fragment(core))
}
public enum Schema: Equatable, Sendable {
/// The null type, which replaces the functionality of the `nullable` property from
/// previous versions of the OpenAPI specification.
case null(CoreContext<JSONTypeFormat.AnyFormat>)
case boolean(CoreContext<JSONTypeFormat.BooleanFormat>)
case number(CoreContext<JSONTypeFormat.NumberFormat>, NumericContext)
case integer(CoreContext<JSONTypeFormat.IntegerFormat>, IntegerContext)
case string(CoreContext<JSONTypeFormat.StringFormat>, StringContext)
indirect case object(CoreContext<JSONTypeFormat.ObjectFormat>, ObjectContext)
indirect case array(CoreContext<JSONTypeFormat.ArrayFormat>, ArrayContext)
indirect case all(of: [JSONSchema], core: CoreContext<JSONTypeFormat.AnyFormat>)
indirect case one(of: [JSONSchema], core: CoreContext<JSONTypeFormat.AnyFormat>)
indirect case any(of: [JSONSchema], core: CoreContext<JSONTypeFormat.AnyFormat>)
indirect case not(JSONSchema, core: CoreContext<JSONTypeFormat.AnyFormat>)
case reference(JSONReference<JSONSchema>, CoreContext<JSONTypeFormat.AnyFormat>)
/// Schemas without a `type`.
case fragment(CoreContext<JSONTypeFormat.AnyFormat>) // This allows for the "{}" case and also fragments of schemas that will later be combined with `all(of:)`.
}
/// The type and format of the schema.
public var jsonTypeFormat: JSONTypeFormat? {
switch value {
case .null:
return .null
case .boolean(let context):
return .boolean(context.format)
case .object(let context, _):
return .object(context.format)
case .array(let context, _):
return .array(context.format)
case .number(let context, _):
return .number(context.format)
case .integer(let context, _):
return .integer(context.format)
case .string(let context, _):
return .string(context.format)
case .all, .one, .any, .not, .reference, .fragment:
return nil
}
}
/// The fundamental type of the schema.
///
/// - Important: "object," "array," "allOf,", "oneOf,"
/// "anyOf," "not," "reference," and "undefined" are
/// not considered types and such schemas will
/// return `nil` for this property.
public var jsonType: JSONType? {
return jsonTypeFormat?.jsonType
}
/// The format of the schema as a string value.
///
/// This can be set even when a schema type has
/// not be specified. If a type has been specified,
/// a type-safe format can be used and retrieved
/// via the `jsonTypeFormat` property.
public var formatString: String? {
switch value {
case .boolean(let context):
return context.format.rawValue
case .object(let context, _):
return context.format.rawValue
case .array(let context, _):
return context.format.rawValue
case .number(let context, _):
return context.format.rawValue
case .integer(let context, _):
return context.format.rawValue
case .string(let context, _):
return context.format.rawValue
case .fragment(let context),
.all(of: _, core: let context),
.one(of: _, core: let context),
.any(of: _, core: let context),
.not(_, core: let context):
return context.format.rawValue
case .reference, .null:
return nil
}
}
// See `JSONSchemaContext`
public var required: Bool {
return coreContext.required
}
// See `JSONSchemaContext`
public var description: String? {
return coreContext.description
}
// See `JSONSchemaContext`
public var discriminator: OpenAPI.Discriminator? {
switch value {
case .null(let context as JSONSchemaContext),
.boolean(let context as JSONSchemaContext),
.object(let context as JSONSchemaContext, _),
.array(let context as JSONSchemaContext, _),
.number(let context as JSONSchemaContext, _),
.integer(let context as JSONSchemaContext, _),
.string(let context as JSONSchemaContext, _),
.fragment(let context as JSONSchemaContext),
.all(of: _, core: let context as JSONSchemaContext),
.one(of: _, core: let context as JSONSchemaContext),
.any(of: _, core: let context as JSONSchemaContext),
.not(_, core: let context as JSONSchemaContext):
return context.discriminator
case .reference:
return nil
}
}
// See `JSONSchemaContext`
public var nullable: Bool {
return coreContext.nullable
}
// See `JSONSchemaContext`
public var readOnly: Bool {
return coreContext.readOnly
}
// See `JSONSchemaContext`
public var writeOnly: Bool {
return coreContext.writeOnly
}
// See `JSONSchemaContext`
public var deprecated: Bool {
return coreContext.deprecated
}
// See `JSONSchemaContext`
public var title: String? {
return coreContext.title
}
// See `JSONSchemaContext`
public var externalDocs: OpenAPI.ExternalDocumentation? {
return coreContext.externalDocs
}
// See `JSONSchemaContext`
public var allowedValues: [AnyCodable]? {
return coreContext.allowedValues
}
// See `JSONSchemaContext`
public var defaultValue: AnyCodable? {
return coreContext.defaultValue
}
// See `JSONSchemaContext`
public var examples: [AnyCodable] {
return coreContext.examples
}
// See `JSONSchemaContext`
public var anchor: String? {
return coreContext.anchor
}
// See `JSONSchemaContext`
public var dynamicAnchor: String? {
return coreContext.dynamicAnchor
}
// See `JSONSchemaContext`
public var defs: OrderedDictionary<String, JSONSchema> {
switch value {
case .null(let core):
return core.defs
case .boolean(let core):
return core.defs
case .number(let core, _):
return core.defs
case .integer(let core, _):
return core.defs
case .string(let core, _):
return core.defs
case .object(let core, _):
return core.defs
case .array(let core, _):
return core.defs
case .all(of: _, core: let core):
return core.defs
case .one(of: _, core: let core):
return core.defs
case .any(of: _, core: let core):
return core.defs
case .not(_, core: let core):
return core.defs
case .reference(_, let core):
return core.defs
case .fragment(let core):
return core.defs
}
}
public var xml: OpenAPI.XML? {
coreContext.xml
}
// See `JSONSchemaContext`
public var inferred: Bool {
coreContext.inferred
}
}
extension JSONSchema: Equatable {
public static func == (lhs: JSONSchema, rhs: JSONSchema) -> Bool {
lhs.value == rhs.value
}
}
// MARK: - Case Checks
extension JSONSchema {
/// Check if this schema is an _empty_ `.fragment`.
///
/// A special case of the `.fragment` schema is the "empty"
/// schema where no information about the schema component
/// is available.
///
/// This is equivalent to the following JSON Schema:
///
/// {
/// }
///
public var isEmpty: Bool {
guard case .fragment(let context) = value, context.isEmpty else {
return false
}
return true
}
/// Check if this schema is a `.fragment`.
public var isFragment: Bool {
guard case .fragment = value else { return false }
return true
}
/// Check if schema is `.null`
public var isNull : Bool {
guard case .null = value else { return false }
return true
}
/// Check if a schema is a `.boolean`.
public var isBoolean: Bool {
guard case .boolean = value else { return false }
return true
}
/// Check if a schema is a `.number`.
///
/// This returns `false` if the schema is an
/// `.integer` even though Integer schemas
/// can be easily transformed into Number schemas.
public var isNumber: Bool {
guard case .number = value else { return false }
return true
}
/// Check if a schema is an `.integer`.
public var isInteger: Bool {
guard case .integer = value else { return false }
return true
}
/// Check if a schema is a `.string`.
public var isString: Bool {
guard case .string = value else { return false }
return true
}
/// Check if a schema is an `.object`.
public var isObject: Bool {
guard case .object = value else { return false }
return true
}
/// Check if a schema is an `.array`.
public var isArray: Bool {
guard case .array = value else { return false }
return true
}
/// Check if a schema is a `.reference`.
public var isReference: Bool {
guard case .reference = value else { return false }
return true
}
public var reference: JSONReference<JSONSchema>? {
guard case let .reference(reference, _) = value else { return nil }
return reference
}
}
// MARK: - Context Accessors
extension JSONSchema {
/// Get the core context most JSONSchemas have.
///
/// This is the information shared by all schemas.
public var coreContext: JSONSchemaContext {
switch value {
case .null(let context as JSONSchemaContext),
.boolean(let context as JSONSchemaContext),
.object(let context as JSONSchemaContext, _),
.array(let context as JSONSchemaContext, _),
.number(let context as JSONSchemaContext, _),
.integer(let context as JSONSchemaContext, _),
.string(let context as JSONSchemaContext, _),
.fragment(let context as JSONSchemaContext),
.all(of: _, core: let context as JSONSchemaContext),
.one(of: _, core: let context as JSONSchemaContext),
.any(of: _, core: let context as JSONSchemaContext),
.not(_, core: let context as JSONSchemaContext),
.reference(_, let context as JSONSchemaContext):
return context
}
}
/// Get the context specific to an `object` schema. If not an
/// object schema, returns `nil`.
public var objectContext: ObjectContext? {
guard case .object(_, let context) = value else {
return nil
}
return context
}
/// Get the context specific to an `array` schema. If not an
/// array schema, returns `nil`.
public var arrayContext: ArrayContext? {
guard case .array(_, let context) = value else {
return nil
}
return context
}
/// Get the context specific to a `number` schema. If not a
/// number schema, returns `nil`.
///
/// Although integers are numbers, an `integer` schema will
/// still return `nil` when asked for a `numberContext`.
///
/// If you wish to get a `NumericContext` from an `integer`
/// schema, take an `IntegerContext` and explicitly request
/// a `NumericContext` from it via its `numericContext`
/// accessor.
///
public var numberContext: NumericContext? {
guard case .number(_, let context) = value else {
return nil
}
return context
}
/// Get the context specific to an `integer` schema. If not an
/// integer schema, returns `nil`.
public var integerContext: IntegerContext? {
guard case .integer(_, let context) = value else {
return nil
}
return context
}
/// Get the context specific to a `string` schema. If not a
/// string schema, returns `nil`.
public var stringContext: StringContext? {
guard case .string(_, let context) = value else {
return nil
}
return context
}
/// Get subschemas if this schema is an anyOf, allOf, etc.
/// Returns an empty array for any schema that does not have
/// subschemas.
///
/// - IMPORTANT: An object's properties are NOT considered
/// subschemas.
public var subschemas: [JSONSchema] {
switch self.value {
case .not(let schema, core: _):
return [schema]
case .array(_, let arrayContext):
return arrayContext.items.map { [$0] } ?? []
case .all(of: let schemas, core: _):
return schemas
case .any(of: let schemas, core: _):
return schemas
case .one(of: let schemas, core: _):
return schemas
default:
return []
}
}
}
// MARK: - Vendor Extensions
extension JSONSchema: VendorExtendable {
/// Dictionary of vendor extensions.
///
/// These should be of the form:
/// `[ "x-extensionKey": <anything>]`
/// where the values are anything codable.
public var vendorExtensions: VendorExtensions {
get {
coreContext.vendorExtensions
}
set(extensions) {
self.value = value.with(vendorExtensions: extensions)
}
}
public func with(vendorExtensions: [String: AnyCodable]) -> JSONSchema {
.init(
warnings: warnings,
schema: value.with(vendorExtensions: vendorExtensions)
)
}
}
extension JSONSchema.Schema {
public func with(vendorExtensions: [String: AnyCodable]) -> JSONSchema.Schema {
switch self {
case .null(let context):
return .null(context.with(vendorExtensions: vendorExtensions))
case .boolean(let context):
return .boolean(context.with(vendorExtensions: vendorExtensions))
case .number(let contextA, let contextB):
return .number(contextA.with(vendorExtensions: vendorExtensions), contextB)
case .integer(let contextA, let contextB):
return .integer(contextA.with(vendorExtensions: vendorExtensions), contextB)
case .string(let contextA, let contextB):
return .string(contextA.with(vendorExtensions: vendorExtensions), contextB)
case .object(let contextA, let contextB):
return .object(contextA.with(vendorExtensions: vendorExtensions), contextB)
case .array(let contextA, let contextB):
return .array(contextA.with(vendorExtensions: vendorExtensions), contextB)
case .all(of: let of, core: let core):
return .all(of: of, core: core.with(vendorExtensions: vendorExtensions))
case .one(of: let of, core: let core):
return .one(of: of, core: core.with(vendorExtensions: vendorExtensions))
case .any(of: let of, core: let core):
return .any(of: of, core: core.with(vendorExtensions: vendorExtensions))
case .not(let of, core: let core):
return .not(of, core: core.with(vendorExtensions: vendorExtensions))
case .reference(let context, let coreContext):
return .reference(context, coreContext.with(vendorExtensions: vendorExtensions))
case .fragment(let context):
return .fragment(context.with(vendorExtensions: vendorExtensions))
}
}
}
// MARK: - Transformations
extension JSONSchema {
/// Return the optional version of this `JSONSchema`
public func optionalSchemaObject() -> JSONSchema {
switch value {
case .boolean(let context):
return .init(
warnings: warnings,
schema: .boolean(context.optionalContext())
)
case .object(let contextA, let contextB):
return .init(
warnings: warnings,
schema: .object(contextA.optionalContext(), contextB)
)
case .array(let contextA, let contextB):
return .init(
warnings: warnings,
schema: .array(contextA.optionalContext(), contextB)
)
case .number(let context, let contextB):
return .init(
warnings: warnings,
schema: .number(context.optionalContext(), contextB)
)
case .integer(let context, let contextB):
return .init(
warnings: warnings,
schema: .integer(context.optionalContext(), contextB)
)
case .string(let context, let contextB):
return .init(
warnings: warnings,
schema: .string(context.optionalContext(), contextB)
)
case .fragment(let context):
return .init(
warnings: warnings,
schema: .fragment(context.optionalContext())
)
case .all(of: let fragments, core: let core):
return .init(
warnings: warnings,
schema: .all(of: fragments.map { $0.optionalSchemaObject() }, core: core.optionalContext())
)
case .one(of: let schemas, core: let core):
return .init(
warnings: warnings,
schema: .one(of: schemas, core: core.optionalContext())
)
case .any(of: let schemas, core: let core):
return .init(
warnings: warnings,
schema: .any(of: schemas, core: core.optionalContext())
)
case .not(let schema, core: let core):
return .init(
warnings: warnings,
schema: .not(schema, core: core.optionalContext())
)
case .reference(let reference, let context):
return .init(
warnings: warnings,
schema: .reference(reference, context.optionalContext())
)
case .null(let context):
return .init(
warnings: warnings,
schema: .null(context.optionalContext())
)
}
}
/// Return the required version of this `JSONSchema`
public func requiredSchemaObject() -> JSONSchema {
switch value {
case .boolean(let context):
return .init(
warnings: warnings,
schema: .boolean(context.requiredContext())
)
case .object(let contextA, let contextB):
return .init(
warnings: warnings,
schema: .object(contextA.requiredContext(), contextB)
)
case .array(let contextA, let contextB):
return .init(
warnings: warnings,
schema: .array(contextA.requiredContext(), contextB)
)
case .number(let context, let contextB):
return .init(
warnings: warnings,
schema: .number(context.requiredContext(), contextB)
)
case .integer(let context, let contextB):
return .init(
warnings: warnings,
schema: .integer(context.requiredContext(), contextB)
)
case .string(let context, let contextB):
return .init(
warnings: warnings,
schema: .string(context.requiredContext(), contextB)
)
case .fragment(let context):
return .init(
warnings: warnings,
schema: .fragment(context.requiredContext())
)
case .all(of: let fragments, core: let core):
return .init(
warnings: warnings,
schema: .all(of: fragments.map { $0.requiredSchemaObject() }, core: core.requiredContext())
)
case .one(of: let schemas, core: let core):
return .init(
warnings: warnings,
schema: .one(of: schemas, core: core.requiredContext())
)
case .any(of: let schemas, core: let core):
return .init(
warnings: warnings,
schema: .any(of: schemas, core: core.requiredContext())
)
case .not(let schema, core: let core):
return .init(
warnings: warnings,
schema: .not(schema, core: core.requiredContext())
)
case .reference(let reference, let context):
return .init(
warnings: warnings,
schema: .reference(reference, context.requiredContext())
)
case .null(let context):
return .init(
warnings: warnings,
schema: .null(context.requiredContext())
)
}
}
/// Return the nullable version of this `JSONSchema`
public func nullableSchemaObject() -> JSONSchema {
switch value {
case .boolean(let context):
return .init(
warnings: warnings,
schema: .boolean(context.nullableContext())
)
case .object(let contextA, let contextB):
return .init(
warnings: warnings,
schema: .object(contextA.nullableContext(), contextB)
)
case .array(let contextA, let contextB):
return .init(
warnings: warnings,
schema: .array(contextA.nullableContext(), contextB)
)
case .number(let context, let contextB):
return .init(
warnings: warnings,
schema: .number(context.nullableContext(), contextB)
)
case .integer(let context, let contextB):
return .init(
warnings: warnings,
schema: .integer(context.nullableContext(), contextB)
)
case .string(let context, let contextB):
return .init(
warnings: warnings,
schema: .string(context.nullableContext(), contextB)
)
case .fragment(let context):
return .init(
warnings: warnings,
schema: .fragment(context.nullableContext())
)
case .all(of: let fragments, core: let core):
return .init(
warnings: warnings,
schema: .all(of: fragments, core: core.nullableContext())
)
case .one(of: let schemas, core: let core):
return .init(
warnings: warnings,
schema: .one(of: schemas, core: core.nullableContext())
)
case .any(of: let schemas, core: let core):
return .init(
warnings: warnings,
schema: .any(of: schemas, core: core.nullableContext())
)
case .not(let schema, core: let core):
return .init(
warnings: warnings,
schema: .not(schema, core: core.nullableContext())
)
case .reference, .null:
return self
}
}
/// Return a version of this `JSONSchema` that only allows the given
/// values.
public func with(allowedValues: [AnyCodable]) -> JSONSchema {
switch value {
case .boolean(let context):
return .init(
warnings: warnings,
schema: .boolean(context.with(allowedValues: allowedValues))
)
case .object(let contextA, let contextB):
return .init(
warnings: warnings,
schema: .object(contextA.with(allowedValues: allowedValues), contextB)
)
case .array(let contextA, let contextB):
return .init(
warnings: warnings,
schema: .array(contextA.with(allowedValues: allowedValues), contextB)
)
case .number(let context, let contextB):
return .init(
warnings: warnings,
schema: .number(context.with(allowedValues: allowedValues), contextB)
)
case .integer(let context, let contextB):
return .init(
warnings: warnings,
schema: .integer(context.with(allowedValues: allowedValues), contextB)
)
case .string(let context, let contextB):
return .init(
warnings: warnings,
schema: .string(context.with(allowedValues: allowedValues), contextB)
)
case .fragment(let context):
return .init(
warnings: warnings,
schema: .fragment(context.with(allowedValues: allowedValues))
)
case .all(of: let fragments, core: let core):
return .init(
warnings: warnings,
schema: .all(of: fragments, core: core.with(allowedValues: allowedValues))
)
case .one(of: let schemas, core: let core):
return .init(
warnings: warnings,
schema: .one(of: schemas, core: core.with(allowedValues: allowedValues))
)
case .any(of: let schemas, core: let core):
return .init(
warnings: warnings,
schema: .any(of: schemas, core: core.with(allowedValues: allowedValues))
)
case .not(let schema, core: let core):
return .init(
warnings: warnings,
schema: .not(schema, core: core.with(allowedValues: allowedValues))
)
case .reference(let schema, let core):
return .init(
warnings: warnings,
schema: .reference(schema, core.with(allowedValues: allowedValues))
)
case .null(let core):
return .init(
warnings: warnings,
schema: .null(core.with(allowedValues: allowedValues))
)
}
}
/// Return a version of this `JSONSchema` that has the given default value.
public func with(defaultValue: AnyCodable) -> JSONSchema {
switch value {
case .boolean(let context):
return .init(
warnings: warnings,
schema: .boolean(context.with(defaultValue: defaultValue))
)
case .object(let contextA, let contextB):
return .init(
warnings: warnings,
schema: .object(contextA.with(defaultValue: defaultValue), contextB)
)
case .array(let contextA, let contextB):
return .init(
warnings: warnings,
schema: .array(contextA.with(defaultValue: defaultValue), contextB)
)
case .number(let context, let contextB):
return .init(
warnings: warnings,
schema: .number(context.with(defaultValue: defaultValue), contextB)
)
case .integer(let context, let contextB):
return .init(
warnings: warnings,
schema: .integer(context.with(defaultValue: defaultValue), contextB)
)
case .string(let context, let contextB):
return .init(
warnings: warnings,
schema: .string(context.with(defaultValue: defaultValue), contextB)
)
case .fragment(let context):
return .init(
warnings: warnings,
schema: .fragment(context.with(defaultValue: defaultValue))
)
case .all(of: let fragments, core: let core):
return .init(
warnings: warnings,
schema: .all(of: fragments, core: core.with(defaultValue: defaultValue))
)
case .one(of: let schemas, core: let core):
return .init(
warnings: warnings,
schema: .one(of: schemas, core: core.with(defaultValue: defaultValue))
)
case .any(of: let schemas, core: let core):
return .init(
warnings: warnings,
schema: .any(of: schemas, core: core.with(defaultValue: defaultValue))
)
case .not(let schema, core: let core):
return .init(
warnings: warnings,
schema: .not(schema, core: core.with(defaultValue: defaultValue))
)
case .reference(let schema, let core):
return .init(
warnings: warnings,
schema: .reference(schema, core.with(defaultValue: defaultValue))
)
case .null(let core):
return .init(
warnings: warnings,
schema: .null(core.with(defaultValue: defaultValue))
)
}
}
/// Returns a version of this `JSONSchema` that has the given example
/// attached.
public func with(example: AnyCodable) throws -> JSONSchema {
return try with(examples: [example])
}
/// Returns a version of this `JSONSchema` that has the given examples
/// attached.
public func with(examples: [AnyCodable]) throws -> JSONSchema {
switch value {
case .boolean(let context):
return .init(
warnings: warnings,
schema: .boolean(context.with(examples: examples))
)
case .object(let contextA, let contextB):
return .init(
warnings: warnings,
schema: .object(contextA.with(examples: examples), contextB)
)
case .array(let contextA, let contextB):
return .init(
warnings: warnings,
schema: .array(contextA.with(examples: examples), contextB)
)
case .number(let context, let contextB):
return .init(
warnings: warnings,
schema: .number(context.with(examples: examples), contextB)
)
case .integer(let context, let contextB):
return .init(
warnings: warnings,
schema: .integer(context.with(examples: examples), contextB)
)
case .string(let context, let contextB):
return .init(
warnings: warnings,
schema: .string(context.with(examples: examples), contextB)
)
case .fragment(let context):
return .init(
warnings: warnings,
schema: .fragment(context.with(examples: examples))
)
case .all(of: let fragments, core: let core):
return .init(
warnings: warnings,
schema: .all(of: fragments, core: core.with(examples: examples))
)
case .one(of: let schemas, core: let core):
return .init(
warnings: warnings,
schema: .one(of: schemas, core: core.with(examples: examples))
)
case .any(of: let schemas, core: let core):
return .init(
warnings: warnings,
schema: .any(of: schemas, core: core.with(examples: examples))
)
case .not(let schema, core: let core):
return .init(
warnings: warnings,
schema: .not(schema, core: core.with(examples: examples))
)
case .reference(let schema, let core):
return .init(
warnings: warnings,
schema: .reference(schema, core.with(examples: examples))
)
case .null(let core):
return .init(
warnings: warnings,
schema: .null(core.with(examples: examples))
)
}
}
/// Returns a version of this `JSONSchema` that has the given discriminator.
public func with(discriminator: OpenAPI.Discriminator) -> JSONSchema {
switch value {
case .boolean(let context):
return .init(
warnings: warnings,
schema: .boolean(context.with(discriminator: discriminator))
)
case .object(let contextA, let contextB):
return .init(
warnings: warnings,
schema: .object(contextA.with(discriminator: discriminator), contextB)
)
case .array(let contextA, let contextB):
return .init(
warnings: warnings,
schema: .array(contextA.with(discriminator: discriminator), contextB)
)
case .number(let context, let contextB):
return .init(
warnings: warnings,
schema: .number(context.with(discriminator: discriminator), contextB)
)
case .integer(let context, let contextB):
return .init(
warnings: warnings,
schema: .integer(context.with(discriminator: discriminator), contextB)
)
case .string(let context, let contextB):
return .init(
warnings: warnings,
schema: .string(context.with(discriminator: discriminator), contextB)
)
case .fragment(let context):
return .init(
warnings: warnings,
schema: .fragment(context.with(discriminator: discriminator))
)
case .all(of: let fragments, core: let core):
return .init(
warnings: warnings,
schema: .all(of: fragments, core: core.with(discriminator: discriminator))