jtd 0.3.1

A Rust implementation of JSON Type Definition
Documentation
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
use crate::SerdeSchema;
use serde_json::Value;
use std::collections::{BTreeMap, BTreeSet};
use thiserror::Error;

/// A convenience alias for the JSON Typedef `definitions` keyword value.
pub type Definitions = BTreeMap<String, Schema>;

/// A convenience alias for the JSON Typedef `metadata` keyword value.
pub type Metadata = BTreeMap<String, Value>;

/// A pattern-matching-friendly representation of a JSON Typedef schema.
///
/// Each variant of this schema corresponds to one of the eight "forms" a schema
/// may take on. All of the forms share the following fields:
///
/// * `definitions` corresponds to the JSON Typedef keyword of the same name.
///    This should only be non-empty on root schemas. Otherwise,
///    [`Schema::validate`] will return
///    [`SchemaValidateError::NonRootDefinitions`].
///
/// * `metadata` corresponds to the JSON Typedef keyword of the same name. Use
///   this to convey information not pertinent to validation, such as hints for
///   code generation. Do not expect other parties to understand the fields
///   inside metadata unless you've agreed upon them out-of-band.
///
/// Except for [`Schema::Empty`], all of the forms also share one additional
/// field:
///
/// * `nullable` corresponds to the JSON Typedef keyword of the same name. If
///   set to "true", then regardless of any other considerations the schema will
///   accept JSON `null` as valid.
///
/// [`Schema::Empty`] omits `nullable` because it's redundant; schemas of the
/// empty form already accept `null` anyway.
///
/// For convenience, these three common properties have associated borrowing
/// "getters": [`Schema::definitions`], [`Schema::metadata`], and
/// [`Schema::nullable`].
///
/// If you are trying to parse a JSON Typedef schema from JSON, see
/// [`SerdeSchema`] and [`Schema::from_serde_schema`].
///
/// ```
/// use jtd::{SerdeSchema, Schema};
/// use serde_json::json;
///
/// assert_eq!(
///     Schema::from_serde_schema(serde_json::from_value(json!({
///         "elements": {
///             "type": "uint32",
///             "nullable": true
///         }
///     })).unwrap()).unwrap(),
///     jtd::Schema::Elements {
///         definitions: Default::default(),
///         metadata: Default::default(),
///         nullable: false,
///         elements: Box::new(jtd::Schema::Type {
///             definitions: Default::default(),
///             metadata: Default::default(),
///             nullable: true,
///             type_: jtd::Type::Uint32,
///         })
///     }
/// );
/// ```
#[derive(Clone, Debug, PartialEq)]
pub enum Schema {
    /// The [empty](https://tools.ietf.org/html/rfc8927#section-2.2.1) form.
    ///
    /// The empty form will accept all inputs. It corresponds to the "top" type
    /// of many programming language, like Java's `Object` or TypeScript's
    /// `any`.
    Empty {
        definitions: Definitions,
        metadata: Metadata,
    },

    /// The [ref](https://tools.ietf.org/html/rfc8927#section-2.2.2) form.
    ///
    /// The ref form accepts whatever the definition it refers to accepts.
    Ref {
        definitions: Definitions,
        metadata: Metadata,
        nullable: bool,

        /// The name of the definition being referred to.
        ref_: String,
    },

    /// The [type](https://tools.ietf.org/html/rfc8927#section-2.2.3) form.
    ///
    /// The type form accepts JSON "primitives" (booleans, numbers, strings)
    /// whose value fall within a certain "type". These types are enumerated in
    /// [`Type`].
    Type {
        definitions: Definitions,
        metadata: Metadata,
        nullable: bool,

        /// The type of primitive value accepted.
        type_: Type,
    },

    /// The [enum](https://tools.ietf.org/html/rfc8927#section-2.2.4) form.
    ///
    /// The enum form accepts JSON strings whose values are within an enumerated
    /// set.
    Enum {
        definitions: Definitions,
        metadata: Metadata,
        nullable: bool,

        /// The values the schema accepts.
        enum_: BTreeSet<String>,
    },

    /// The [elements](https://tools.ietf.org/html/rfc8927#section-2.2.5) form.
    ///
    /// The elements form accepts JSON arrays, and each element of the array is
    /// validated against a sub-schema.
    Elements {
        definitions: Definitions,
        metadata: Metadata,
        nullable: bool,

        /// A schema for the elements of the array.
        elements: Box<Schema>,
    },

    /// The [properties](https://tools.ietf.org/html/rfc8927#section-2.2.6)
    /// form.
    ///
    /// The properties form accepts JSON objects being used as "structs".
    Properties {
        definitions: Definitions,
        metadata: Metadata,
        nullable: bool,

        /// The required properties of the "struct", and the schema that each
        /// must satisfy.
        properties: BTreeMap<String, Schema>,

        /// The optional properties of the "struct", and the schema that each
        /// must satisfy if present.
        optional_properties: BTreeMap<String, Schema>,

        /// Whether the `properties` keyword is present on the schema.
        ///
        /// It is invalid to set this to `false` while having `properties` be
        /// non-empty.
        ///
        /// This is used only to handle the corner case of a properties-form
        /// schema being used to validate a non-object; in order to ensure the
        /// returned `schema_path` points to a part of the schema that really
        /// exists, validators need to be able to tell the difference between
        /// `properties` being an empty object versus being omitted from the
        /// schema.
        ///
        /// This field does not affect whether an input is valid. It only
        /// affects the `schema_path` that will be returned if that input is not
        /// an object. For more details, see the first sub-bullet after
        /// "Otherwise" in [RFC 8927, Section
        /// 3.3.6](https://tools.ietf.org/html/rfc8927#section-3.3.6).
        ///
        /// [`Schema::from_serde_schema`] correctly handles populating this
        /// field. If you are constructing schemas by hand and want to play it
        /// safe, it is always safe to set this to `true`.
        properties_is_present: bool,

        /// Whether additional properties not specified in `properties` or
        /// `optional_properties` are permitted.
        additional_properties: bool,
    },

    /// The [values](https://tools.ietf.org/html/rfc8927#section-2.2.7) form.
    ///
    /// The values form accepts JSON objects being used as "dictionaries"; each
    /// value of the dictionary is validated against a sub-schema.
    Values {
        definitions: Definitions,
        metadata: Metadata,
        nullable: bool,

        /// A schema for the values of the "dictionary" object.
        values: Box<Schema>,
    },

    /// The [discriminator](https://tools.ietf.org/html/rfc8927#section-2.2.8)
    /// form.
    ///
    /// The discriminator form accepts JSON objects being used as "discriminated
    /// unions", or "tagged unions".
    Discriminator {
        definitions: Definitions,
        metadata: Metadata,
        nullable: bool,

        /// The "discriminator" property of the schema.
        ///
        /// For an input to be valid, this property must exist and its value
        /// must be a key in `mapping`.
        discriminator: String,

        /// A mapping from the value of the `discriminator` property in the
        /// input to a schema that the rest of the input (without the
        /// `discriminator` property) must satisfy.
        mapping: BTreeMap<String, Schema>,
    },
}

/// The values [`Schema::Type::type_`] may take on.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Type {
    /// Either JSON `true` or `false`.
    Boolean,

    /// A JSON number with zero fractional part within the range of [`i8`].
    Int8,

    /// A JSON number with zero fractional part within the range of [`u8`].
    Uint8,

    /// A JSON number with zero fractional part within the range of [`i16`].
    Int16,

    /// A JSON number with zero fractional part within the range of [`u16`].
    Uint16,

    /// A JSON number with zero fractional part within the range of [`i32`].
    Int32,

    /// A JSON number with zero fractional part within the range of [`u32`].
    Uint32,

    /// A JSON number. Code generators will treat this like a Rust [`f32`].
    Float32,

    /// A JSON number. Code generators will treat this like a Rust [`f64`].
    Float64,

    /// A JSON string.
    String,

    /// A JSON string encoding a [RFC3339](https://tools.ietf.org/html/rfc3339)
    /// timestamp.
    Timestamp,
}

/// Errors that may arise from [`Schema::from_serde_schema`].
#[derive(Clone, Debug, PartialEq, Eq, Error)]
pub enum FromSerdeSchemaError {
    /// Indicates the schema uses an invalid combination of keywords.
    ///
    /// ```
    /// use jtd::{FromSerdeSchemaError, Schema, SerdeSchema};
    ///
    /// assert_eq!(
    ///     Err(FromSerdeSchemaError::InvalidForm),
    ///
    ///     // it's invalid to have both "type" and "enum" on a schema
    ///     Schema::from_serde_schema(SerdeSchema {
    ///         type_: Some("uint8".to_owned()),
    ///         enum_: Some(Default::default()),
    ///         ..Default::default()
    ///     })
    /// )
    /// ```
    #[error("invalid combination of keywords in schema")]
    InvalidForm,

    /// Indicates the schema uses a value for `type` that isn't in [`Type`].
    ///
    /// ```
    /// use jtd::{FromSerdeSchemaError, Schema, SerdeSchema};
    ///
    /// assert_eq!(
    ///     Err(FromSerdeSchemaError::InvalidType("uint64".to_owned())),
    ///
    ///     // there is no uint64 in JSON Typedef
    ///     Schema::from_serde_schema(SerdeSchema {
    ///         type_: Some("uint64".to_owned()),
    ///         ..Default::default()
    ///     })
    /// )
    /// ```
    #[error("invalid type: {0:?}")]
    InvalidType(String),

    /// Indicates the schema has the same value appearing twice in an `enum`.
    ///
    /// ```
    /// use jtd::{FromSerdeSchemaError, Schema, SerdeSchema};
    ///
    /// assert_eq!(
    ///     Err(FromSerdeSchemaError::DuplicatedEnumValue("foo".to_owned())),
    ///
    ///     // it's invalid to have the same value appear twice in an enum array
    ///     Schema::from_serde_schema(SerdeSchema {
    ///         enum_: Some(vec!["foo".into(), "bar".into(), "foo".into()]),
    ///         ..Default::default()
    ///     })
    /// )
    /// ```
    #[error("duplicated enum value: {0:?}")]
    DuplicatedEnumValue(String),
}

/// Errors that may arise from [`Schema::validate`].
#[derive(Clone, Debug, PartialEq, Eq, Error)]
pub enum SchemaValidateError {
    /// Indicates the schema has a `ref` to a definition that doesn't exist.
    ///
    /// ```
    /// use jtd::{Schema, SchemaValidateError};
    ///
    /// assert_eq!(
    ///     Err(SchemaValidateError::NoSuchDefinition("foo".into())),
    ///
    ///     // a "ref" without definitions is always invalid
    ///     Schema::Ref {
    ///         definitions: Default::default(),
    ///         metadata: Default::default(),
    ///         nullable: Default::default(),
    ///         ref_: "foo".into(),
    ///     }.validate(),
    /// )
    /// ```
    #[error("no such definition: {0:?}")]
    NoSuchDefinition(String),

    /// Indicates the schema has non-empty `definitions` below the root level.
    ///
    /// ```
    /// use jtd::{Schema, SchemaValidateError};
    ///
    /// assert_eq!(
    ///     Err(SchemaValidateError::NonRootDefinitions),
    ///
    ///     // definitions can only be present at the root level
    ///     Schema::Elements {
    ///         definitions: Default::default(),
    ///         metadata: Default::default(),
    ///         nullable: Default::default(),
    ///         elements: Box::new(Schema::Empty {
    ///             definitions: vec![(
    ///                 "foo".to_owned(),
    ///                 Schema::Empty {
    ///                     definitions: Default::default(),
    ///                     metadata: Default::default(),
    ///                 }
    ///             )].into_iter().collect(),
    ///             metadata: Default::default(),
    ///         }),
    ///     }.validate(),
    /// )
    /// ```
    #[error("non-root definitions")]
    NonRootDefinitions,

    /// Indicates the schema has an `enum` with no values in it.
    ///
    /// ```
    /// use jtd::{Schema, SchemaValidateError};
    ///
    /// assert_eq!(
    ///     Err(SchemaValidateError::EmptyEnum),
    ///
    ///     // empty enums are illegal
    ///     Schema::Enum {
    ///         definitions: Default::default(),
    ///         metadata: Default::default(),
    ///         nullable: Default::default(),
    ///         enum_: Default::default(),
    ///     }.validate(),
    /// )
    /// ```
    #[error("empty enum")]
    EmptyEnum,

    /// Indicates the schema has the same property appear in `properties` and
    /// `optional_properties`.
    ///
    /// ```
    /// use jtd::{Schema, SchemaValidateError};
    ///
    /// assert_eq!(
    ///     Err(SchemaValidateError::RepeatedProperty("foo".into())),
    ///
    ///     // properties and optional_properties must not overlap
    ///     Schema::Properties {
    ///         definitions: Default::default(),
    ///         metadata: Default::default(),
    ///         nullable: Default::default(),
    ///         properties: vec![(
    ///             "foo".to_owned(),
    ///             Schema::Empty {
    ///                 definitions: Default::default(),
    ///                 metadata: Default::default(),
    ///             },
    ///         )].into_iter().collect(),
    ///         optional_properties: vec![(
    ///             "foo".to_owned(),
    ///             Schema::Empty {
    ///                 definitions: Default::default(),
    ///                 metadata: Default::default(),
    ///             },
    ///         )].into_iter().collect(),
    ///         properties_is_present: true,
    ///         additional_properties: false,
    ///     }.validate(),
    /// )
    /// ```
    #[error("property repeated in optionalProperties: {0:?}")]
    RepeatedProperty(String),

    /// Indicates the schema has a value in `mapping` with `nullable` set to
    /// `true`.
    ///
    /// ```
    /// use jtd::{Schema, SchemaValidateError};
    ///
    /// assert_eq!(
    ///     Err(SchemaValidateError::NullableMapping),
    ///
    ///     // mappings must not be nullable
    ///     Schema::Discriminator {
    ///         definitions: Default::default(),
    ///         metadata: Default::default(),
    ///         nullable: Default::default(),
    ///         discriminator: "foo".into(),
    ///         mapping: vec![(
    ///             "bar".to_owned(),
    ///             Schema::Properties {
    ///                 definitions: Default::default(),
    ///                 metadata: Default::default(),
    ///                 nullable: true,
    ///                 properties: Default::default(),
    ///                 optional_properties: Default::default(),
    ///                 properties_is_present: true,
    ///                 additional_properties: false,
    ///             }
    ///         )].into_iter().collect(),
    ///     }.validate(),
    /// );
    /// ```
    #[error("nullable schema in mapping")]
    NullableMapping,

    /// Indicates the schema has a value in `mapping` that isn't a
    /// [`Schema::Properties`].
    ///
    /// ```
    /// use jtd::{Schema, SchemaValidateError};
    ///
    /// assert_eq!(
    ///     Err(SchemaValidateError::NonPropertiesMapping),
    ///
    ///     // mappings must be of the properties form
    ///     Schema::Discriminator {
    ///         definitions: Default::default(),
    ///         metadata: Default::default(),
    ///         nullable: Default::default(),
    ///         discriminator: "foo".into(),
    ///         mapping: vec![(
    ///             "bar".to_owned(),
    ///             Schema::Empty {
    ///                 definitions: Default::default(),
    ///                 metadata: Default::default(),
    ///             }
    ///         )].into_iter().collect(),
    ///     }.validate(),
    /// );
    /// ```
    #[error("non-properties schema in mapping")]
    NonPropertiesMapping,

    /// Indicates the schema has a value in `mapping` whose `properties` or
    /// `optional_properties` contains `discriminator`.
    ///
    /// ```
    /// use jtd::{Schema, SchemaValidateError};
    ///
    /// assert_eq!(
    ///     Err(SchemaValidateError::RepeatedDiscriminator("foo".into())),
    ///
    ///     // mappings must not re-define the discriminator property
    ///     Schema::Discriminator {
    ///         definitions: Default::default(),
    ///         metadata: Default::default(),
    ///         nullable: Default::default(),
    ///         discriminator: "foo".into(),
    ///         mapping: vec![(
    ///             "bar".to_owned(),
    ///             Schema::Properties {
    ///                 definitions: Default::default(),
    ///                 metadata: Default::default(),
    ///                 nullable: Default::default(),
    ///                 properties: vec![(
    ///                     "foo".into(),
    ///                     Schema::Empty {
    ///                         definitions: Default::default(),
    ///                         metadata: Default::default(),
    ///                     }
    ///                 )].into_iter().collect(),
    ///                 optional_properties: Default::default(),
    ///                 properties_is_present: true,
    ///                 additional_properties: false,
    ///             }
    ///         )].into_iter().collect(),
    ///     }.validate(),
    /// );
    /// ```
    #[error("discriminator redefined in mapping: {0:?}")]
    RepeatedDiscriminator(String),
}

// Index of valid form "signatures" -- i.e., combinations of the presence of the
// keywords (in order):
//
// ref type enum elements properties optionalProperties additionalProperties
// values discriminator mapping
//
// The keywords "definitions", "nullable", and "metadata" are not included here,
// because they would restrict nothing.
const VALID_FORM_SIGNATURES: [[bool; 10]; 13] = [
    // Empty form
    [
        false, false, false, false, false, false, false, false, false, false,
    ],
    // Ref form
    [
        true, false, false, false, false, false, false, false, false, false,
    ],
    // Type form
    [
        false, true, false, false, false, false, false, false, false, false,
    ],
    // Enum form
    [
        false, false, true, false, false, false, false, false, false, false,
    ],
    // Elements form
    [
        false, false, false, true, false, false, false, false, false, false,
    ],
    // Properties form -- properties or optional properties or both, and never
    // additional properties on its own
    [
        false, false, false, false, true, false, false, false, false, false,
    ],
    [
        false, false, false, false, false, true, false, false, false, false,
    ],
    [
        false, false, false, false, true, true, false, false, false, false,
    ],
    [
        false, false, false, false, true, false, true, false, false, false,
    ],
    [
        false, false, false, false, false, true, true, false, false, false,
    ],
    [
        false, false, false, false, true, true, true, false, false, false,
    ],
    // Values form
    [
        false, false, false, false, false, false, false, true, false, false,
    ],
    // Discriminator form
    [
        false, false, false, false, false, false, false, false, true, true,
    ],
];

impl Schema {
    /// Converts a [`Schema`] into a [`SerdeSchema`].
    ///
    /// ```
    /// use jtd::{Schema, SerdeSchema, Type};
    ///
    /// assert_eq!(
    ///     SerdeSchema {
    ///         type_: Some("uint8".to_owned()),
    ///         ..Default::default()
    ///     },
    ///     Schema::Type {
    ///         definitions: Default::default(),
    ///         metadata: Default::default(),
    ///         nullable: false,
    ///         type_: Type::Uint8,
    ///     }.into_serde_schema(),
    /// );
    /// ```
    pub fn into_serde_schema(self) -> SerdeSchema {
        let mut serde_schema: SerdeSchema = Default::default();

        match self {
            Schema::Empty {
                definitions,
                metadata,
            } => {
                serde_schema.definitions = Self::definitions_into_serde_schema(definitions);
                serde_schema.metadata = Self::metadata_into_serde_schema(metadata);
            }

            Schema::Ref {
                definitions,
                metadata,
                nullable,
                ref_,
            } => {
                serde_schema.definitions = Self::definitions_into_serde_schema(definitions);
                serde_schema.metadata = Self::metadata_into_serde_schema(metadata);
                serde_schema.nullable = Self::nullable_into_serde_schema(nullable);
                serde_schema.ref_ = Some(ref_);
            }

            Schema::Type {
                definitions,
                metadata,
                nullable,
                type_,
            } => {
                serde_schema.definitions = Self::definitions_into_serde_schema(definitions);
                serde_schema.metadata = Self::metadata_into_serde_schema(metadata);
                serde_schema.nullable = Self::nullable_into_serde_schema(nullable);
                serde_schema.type_ = Some(
                    match type_ {
                        Type::Boolean => "boolean",
                        Type::Int8 => "int8",
                        Type::Uint8 => "uint8",
                        Type::Int16 => "int16",
                        Type::Uint16 => "uint16",
                        Type::Int32 => "int32",
                        Type::Uint32 => "uint32",
                        Type::Float32 => "float32",
                        Type::Float64 => "float64",
                        Type::String => "string",
                        Type::Timestamp => "timestamp",
                    }
                    .to_owned(),
                );
            }

            Schema::Enum {
                definitions,
                metadata,
                nullable,
                enum_,
            } => {
                serde_schema.definitions = Self::definitions_into_serde_schema(definitions);
                serde_schema.metadata = Self::metadata_into_serde_schema(metadata);
                serde_schema.nullable = Self::nullable_into_serde_schema(nullable);
                serde_schema.enum_ = Some(enum_.into_iter().collect());
            }

            Schema::Elements {
                definitions,
                metadata,
                nullable,
                elements,
            } => {
                serde_schema.definitions = Self::definitions_into_serde_schema(definitions);
                serde_schema.metadata = Self::metadata_into_serde_schema(metadata);
                serde_schema.nullable = Self::nullable_into_serde_schema(nullable);
                serde_schema.elements = Some(Box::new(elements.into_serde_schema()));
            }

            Schema::Properties {
                definitions,
                metadata,
                nullable,
                properties,
                optional_properties,
                properties_is_present,
                additional_properties,
            } => {
                serde_schema.definitions = Self::definitions_into_serde_schema(definitions);
                serde_schema.metadata = Self::metadata_into_serde_schema(metadata);
                serde_schema.nullable = Self::nullable_into_serde_schema(nullable);

                if properties_is_present {
                    serde_schema.properties = Some(
                        properties
                            .into_iter()
                            .map(|(k, v)| (k, v.into_serde_schema()))
                            .collect(),
                    );
                }

                if !optional_properties.is_empty() {
                    serde_schema.optional_properties = Some(
                        optional_properties
                            .into_iter()
                            .map(|(k, v)| (k, v.into_serde_schema()))
                            .collect(),
                    );
                }

                if additional_properties {
                    serde_schema.additional_properties = Some(additional_properties);
                }
            }

            Schema::Values {
                definitions,
                metadata,
                nullable,
                values,
            } => {
                serde_schema.definitions = Self::definitions_into_serde_schema(definitions);
                serde_schema.metadata = Self::metadata_into_serde_schema(metadata);
                serde_schema.nullable = Self::nullable_into_serde_schema(nullable);
                serde_schema.values = Some(Box::new(values.into_serde_schema()));
            }

            Schema::Discriminator {
                definitions,
                metadata,
                nullable,
                discriminator,
                mapping,
            } => {
                serde_schema.definitions = Self::definitions_into_serde_schema(definitions);
                serde_schema.metadata = Self::metadata_into_serde_schema(metadata);
                serde_schema.nullable = Self::nullable_into_serde_schema(nullable);
                serde_schema.discriminator = Some(discriminator);
                serde_schema.mapping = Some(
                    mapping
                        .into_iter()
                        .map(|(k, v)| (k, v.into_serde_schema()))
                        .collect(),
                );
            }
        }

        serde_schema
    }

    fn definitions_into_serde_schema(
        definitions: Definitions,
    ) -> Option<BTreeMap<String, SerdeSchema>> {
        if definitions.is_empty() {
            None
        } else {
            Some(
                definitions
                    .into_iter()
                    .map(|(k, v)| (k, v.into_serde_schema()))
                    .collect(),
            )
        }
    }

    fn metadata_into_serde_schema(metadata: Metadata) -> Option<BTreeMap<String, Value>> {
        if metadata.is_empty() {
            None
        } else {
            Some(metadata)
        }
    }

    fn nullable_into_serde_schema(nullable: bool) -> Option<bool> {
        if nullable {
            Some(true)
        } else {
            None
        }
    }

    /// Constructs a [`Schema`] from a [`SerdeSchema`].
    ///
    /// ```
    /// use jtd::{Schema, SerdeSchema, Type};
    ///
    /// assert_eq!(
    ///     Schema::Type {
    ///         definitions: Default::default(),
    ///         metadata: Default::default(),
    ///         nullable: false,
    ///         type_: Type::Uint8,
    ///     },
    ///     Schema::from_serde_schema(SerdeSchema {
    ///         type_: Some("uint8".to_owned()),
    ///         ..Default::default()
    ///     }).unwrap(),
    /// );
    /// ```
    ///
    /// See the documentation for [`FromSerdeSchemaError`] for examples of how
    /// this function may return an error.
    pub fn from_serde_schema(serde_schema: SerdeSchema) -> Result<Self, FromSerdeSchemaError> {
        let mut definitions = BTreeMap::new();
        for (name, sub_schema) in serde_schema.definitions.unwrap_or_default() {
            definitions.insert(name, Self::from_serde_schema(sub_schema)?);
        }

        let metadata = serde_schema.metadata.unwrap_or_default();
        let nullable = serde_schema.nullable.unwrap_or(false);

        // Ensure the schema is using a valid combination of keywords.
        let form_signature = [
            serde_schema.ref_.is_some(),
            serde_schema.type_.is_some(),
            serde_schema.enum_.is_some(),
            serde_schema.elements.is_some(),
            serde_schema.properties.is_some(),
            serde_schema.optional_properties.is_some(),
            serde_schema.additional_properties.is_some(),
            serde_schema.values.is_some(),
            serde_schema.discriminator.is_some(),
            serde_schema.mapping.is_some(),
        ];

        if !VALID_FORM_SIGNATURES.contains(&form_signature) {
            return Err(FromSerdeSchemaError::InvalidForm);
        }

        // From here on out, we can use the presence of certain keywords to
        // determine the form the schema takes on.
        //
        // We'll handle the empty form as a fallback, and handle the other forms
        // in standard order.
        if let Some(ref_) = serde_schema.ref_ {
            return Ok(Schema::Ref {
                definitions,
                metadata,
                nullable,
                ref_,
            });
        }

        if let Some(type_) = serde_schema.type_ {
            let type_ = match &type_[..] {
                "boolean" => Type::Boolean,
                "int8" => Type::Int8,
                "uint8" => Type::Uint8,
                "int16" => Type::Int16,
                "uint16" => Type::Uint16,
                "int32" => Type::Int32,
                "uint32" => Type::Uint32,
                "float32" => Type::Float32,
                "float64" => Type::Float64,
                "string" => Type::String,
                "timestamp" => Type::Timestamp,
                _ => return Err(FromSerdeSchemaError::InvalidType(type_)),
            };

            return Ok(Schema::Type {
                definitions,
                metadata,
                nullable,
                type_,
            });
        }

        if let Some(enum_) = serde_schema.enum_ {
            // We do this construction by hand, rather than using collect, to
            // detect the case of an enum value being repeated. This can't be
            // detected once the values are put in the set.
            let mut values = BTreeSet::new();
            for value in enum_ {
                if values.contains(&value) {
                    return Err(FromSerdeSchemaError::DuplicatedEnumValue(value));
                }

                values.insert(value);
            }

            return Ok(Schema::Enum {
                definitions,
                metadata,
                nullable,
                enum_: values,
            });
        }

        if let Some(elements) = serde_schema.elements {
            return Ok(Schema::Elements {
                definitions,
                metadata,
                nullable,
                elements: Box::new(Self::from_serde_schema(*elements)?),
            });
        }

        if serde_schema.properties.is_some() || serde_schema.optional_properties.is_some() {
            let properties_is_present = serde_schema.properties.is_some();
            let additional_properties = serde_schema.additional_properties.unwrap_or(false);

            let mut properties = BTreeMap::new();
            for (name, sub_schema) in serde_schema.properties.unwrap_or_default() {
                properties.insert(name, Self::from_serde_schema(sub_schema)?);
            }

            let mut optional_properties = BTreeMap::new();
            for (name, sub_schema) in serde_schema.optional_properties.unwrap_or_default() {
                optional_properties.insert(name, Self::from_serde_schema(sub_schema)?);
            }

            return Ok(Schema::Properties {
                definitions,
                metadata,
                nullable,
                properties,
                optional_properties,
                properties_is_present,
                additional_properties,
            });
        }

        if let Some(values) = serde_schema.values {
            return Ok(Schema::Values {
                definitions,
                metadata,
                nullable,
                values: Box::new(Self::from_serde_schema(*values)?),
            });
        }

        if let Some(discriminator) = serde_schema.discriminator {
            // This is safe because the form signature check ensures mapping is
            // present if discriminator is present.
            let mut mapping = BTreeMap::new();
            for (name, sub_schema) in serde_schema.mapping.unwrap() {
                mapping.insert(name, Self::from_serde_schema(sub_schema)?);
            }

            return Ok(Schema::Discriminator {
                definitions,
                metadata,
                nullable,
                discriminator,
                mapping,
            });
        }

        Ok(Schema::Empty {
            definitions,
            metadata,
        })
    }

    /// Ensures a [`Schema`] is well-formed.
    ///
    /// ```
    /// use jtd::{Schema, Type};
    ///
    /// let schema = Schema::Type {
    ///     definitions: Default::default(),
    ///     metadata: Default::default(),
    ///     nullable: false,
    ///     type_: Type::Uint8,
    /// };
    ///
    /// schema.validate().expect("Invalid schema");
    /// ```
    ///
    /// See the documentation for [`SchemaValidateError`] for examples of how
    /// this function may return an error.
    pub fn validate(&self) -> Result<(), SchemaValidateError> {
        self._validate(None)
    }

    fn _validate(&self, root: Option<&Self>) -> Result<(), SchemaValidateError> {
        let sub_root = root.or(Some(self));

        if root.is_some() && !self.definitions().is_empty() {
            return Err(SchemaValidateError::NonRootDefinitions);
        }

        for sub_schema in self.definitions().values() {
            sub_schema._validate(sub_root)?;
        }

        match self {
            Self::Empty { .. } => {}
            Self::Ref { ref_, .. } => {
                if !sub_root
                    .map(|r| r.definitions())
                    .unwrap()
                    .contains_key(ref_)
                {
                    return Err(SchemaValidateError::NoSuchDefinition(ref_.clone()));
                }
            }
            Self::Type { .. } => {}
            Self::Enum { enum_, .. } => {
                if enum_.is_empty() {
                    return Err(SchemaValidateError::EmptyEnum);
                }
            }
            Self::Elements { elements, .. } => {
                elements._validate(sub_root)?;
            }
            Self::Properties {
                properties,
                optional_properties,
                ..
            } => {
                for key in properties.keys() {
                    if optional_properties.contains_key(key) {
                        return Err(SchemaValidateError::RepeatedProperty(key.clone()));
                    }
                }

                for sub_schema in properties.values() {
                    sub_schema._validate(sub_root)?;
                }

                for sub_schema in optional_properties.values() {
                    sub_schema._validate(sub_root)?;
                }
            }
            Self::Values { values, .. } => {
                values._validate(sub_root)?;
            }
            Self::Discriminator {
                discriminator,
                mapping,
                ..
            } => {
                for sub_schema in mapping.values() {
                    if let Self::Properties {
                        nullable,
                        properties,
                        optional_properties,
                        ..
                    } = sub_schema
                    {
                        if *nullable {
                            return Err(SchemaValidateError::NullableMapping);
                        }

                        if properties.contains_key(discriminator)
                            || optional_properties.contains_key(discriminator)
                        {
                            return Err(SchemaValidateError::RepeatedDiscriminator(
                                discriminator.clone(),
                            ));
                        }
                    } else {
                        return Err(SchemaValidateError::NonPropertiesMapping);
                    }

                    sub_schema._validate(sub_root)?;
                }
            }
        }

        Ok(())
    }

    /// Gets the schema's definitions.
    ///
    /// ```
    /// use jtd::{Definitions, Schema};
    ///
    /// assert_eq!(
    ///     &vec![(
    ///         "foo".to_owned(),
    ///         Schema::Empty {
    ///             definitions: Default::default(),
    ///             metadata: Default::default(),
    ///         },
    ///     )].into_iter().collect::<Definitions>(),
    ///
    ///      Schema::Empty {
    ///          definitions: vec![(
    ///             "foo".to_owned(),
    ///             Schema::Empty {
    ///                 definitions: Default::default(),
    ///                 metadata: Default::default(),
    ///             },
    ///         )].into_iter().collect(),
    ///          metadata: Default::default(),
    ///      }.definitions(),
    /// );
    /// ```
    pub fn definitions(&self) -> &BTreeMap<String, Schema> {
        match self {
            Self::Empty { definitions, .. } => definitions,
            Self::Ref { definitions, .. } => definitions,
            Self::Enum { definitions, .. } => definitions,
            Self::Type { definitions, .. } => definitions,
            Self::Elements { definitions, .. } => definitions,
            Self::Properties { definitions, .. } => definitions,
            Self::Values { definitions, .. } => definitions,
            Self::Discriminator { definitions, .. } => definitions,
        }
    }

    /// Gets the schema's metadata.
    ///
    /// ```
    /// use jtd::{Metadata, Schema};
    /// use serde_json::json;
    ///
    /// assert_eq!(
    ///     &vec![(
    ///         "foo".to_owned(),
    ///         json!("bar"),
    ///     )].into_iter().collect::<Metadata>(),
    ///
    ///     Schema::Empty {
    ///         definitions: Default::default(),
    ///         metadata: vec![(
    ///            "foo".to_owned(),
    ///            json!("bar"),
    ///        )].into_iter().collect(),
    ///     }.metadata(),
    /// );
    /// ```
    pub fn metadata(&self) -> &BTreeMap<String, Value> {
        match self {
            Self::Empty { metadata, .. } => metadata,
            Self::Ref { metadata, .. } => metadata,
            Self::Enum { metadata, .. } => metadata,
            Self::Type { metadata, .. } => metadata,
            Self::Elements { metadata, .. } => metadata,
            Self::Properties { metadata, .. } => metadata,
            Self::Values { metadata, .. } => metadata,
            Self::Discriminator { metadata, .. } => metadata,
        }
    }

    /// Gets whether the schema is nullable.
    ///
    /// For [`Schema::Empty`], this always returns true. For all other forms,
    /// this fetches the `nullable` property.
    ///
    /// ```
    /// use jtd::{Schema, Type};
    ///
    /// assert!(
    ///     Schema::Empty {
    ///         definitions: Default::default(),
    ///         metadata: Default::default(),
    ///     }.nullable(),
    /// );
    ///
    /// assert!(
    ///     !Schema::Type {
    ///         definitions: Default::default(),
    ///         metadata: Default::default(),
    ///         nullable: false,
    ///         type_: Type::Uint8,
    ///     }.nullable(),
    /// );
    /// ```
    pub fn nullable(&self) -> bool {
        match self {
            Self::Empty { .. } => true,
            Self::Ref { nullable, .. } => *nullable,
            Self::Enum { nullable, .. } => *nullable,
            Self::Type { nullable, .. } => *nullable,
            Self::Elements { nullable, .. } => *nullable,
            Self::Properties { nullable, .. } => *nullable,
            Self::Values { nullable, .. } => *nullable,
            Self::Discriminator { nullable, .. } => *nullable,
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::{Schema, SerdeSchema};

    #[test]
    fn invalid_schemas() {
        use std::collections::BTreeMap;

        let test_cases: BTreeMap<String, serde_json::Value> = serde_json::from_str(include_str!(
            "../json-typedef-spec/tests/invalid_schemas.json"
        ))
        .expect("parse invalid_schemas.json");

        for (test_case_name, test_case) in test_cases {
            if let Ok(serde_schema) = serde_json::from_value::<SerdeSchema>(test_case) {
                if let Ok(schema) = Schema::from_serde_schema(serde_schema) {
                    if schema.validate().is_ok() {
                        panic!(
                            "failed to detect invalid schema: {}, got: {:?}",
                            test_case_name, schema
                        );
                    }
                }
            }
        }
    }

    #[test]
    fn valid_schemas() {
        use std::collections::BTreeMap;

        #[derive(serde::Deserialize)]
        struct TestCase {
            schema: serde_json::Value,
        }

        let test_cases: BTreeMap<String, TestCase> =
            serde_json::from_str(include_str!("../json-typedef-spec/tests/validation.json"))
                .expect("parse validation.json");

        for (test_case_name, test_case) in test_cases {
            let serde_schema =
                serde_json::from_value::<SerdeSchema>(test_case.schema).expect(&test_case_name);
            let schema = Schema::from_serde_schema(serde_schema).expect(&test_case_name);
            schema.validate().expect(&test_case_name);
        }
    }
}