typebox 0.1.0

JSON Schema type construction with validation, code generation, and binary layout
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
//! Schema types for JSON Schema construction.

use crate::value::Value;
use indexmap::IndexMap;
use serde::{Deserialize, Serialize};

/// JSON Schema type definition with metadata support.
///
/// Use [`SchemaBuilder`](crate::SchemaBuilder) for a fluent API to construct schemas.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Schema {
    /// The type definition.
    #[serde(flatten)]
    pub kind: SchemaKind,

    /// JSON Schema $id field for schema identification.
    #[serde(rename = "$id", skip_serializing_if = "Option::is_none")]
    pub id: Option<String>,

    /// JSON Schema $schema field to specify the schema version.
    #[serde(rename = "$schema", skip_serializing_if = "Option::is_none")]
    pub schema_version: Option<String>,

    /// Human-readable title for the schema.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub title: Option<String>,

    /// Description of the schema.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,

    /// Default value for the schema.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub default: Option<Value>,

    /// Example values matching this schema.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub examples: Option<Vec<Value>>,

    /// Mark as read-only.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub read_only: Option<bool>,

    /// Mark as write-only.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub write_only: Option<bool>,

    /// Mark as deprecated.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub deprecated: Option<bool>,
}

/// Schema type variants.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "camelCase")]
pub enum SchemaKind {
    /// Null type.
    Null,
    /// Boolean type.
    Bool,

    /// 8-bit signed integer.
    Int8 {
        /// Minimum value.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        minimum: Option<i8>,
        /// Maximum value.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        maximum: Option<i8>,
    },
    /// 16-bit signed integer.
    Int16 {
        /// Minimum value.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        minimum: Option<i16>,
        /// Maximum value.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        maximum: Option<i16>,
    },
    /// 32-bit signed integer.
    Int32 {
        /// Minimum value.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        minimum: Option<i32>,
        /// Maximum value.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        maximum: Option<i32>,
    },
    /// 64-bit signed integer.
    Int64 {
        /// Minimum value.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        minimum: Option<i64>,
        /// Maximum value.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        maximum: Option<i64>,
    },
    /// 8-bit unsigned integer.
    UInt8 {
        /// Minimum value.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        minimum: Option<u8>,
        /// Maximum value.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        maximum: Option<u8>,
    },
    /// 16-bit unsigned integer.
    UInt16 {
        /// Minimum value.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        minimum: Option<u16>,
        /// Maximum value.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        maximum: Option<u16>,
    },
    /// 32-bit unsigned integer.
    UInt32 {
        /// Minimum value.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        minimum: Option<u32>,
        /// Maximum value.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        maximum: Option<u32>,
    },
    /// 64-bit unsigned integer.
    UInt64 {
        /// Minimum value.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        minimum: Option<u64>,
        /// Maximum value.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        maximum: Option<u64>,
    },
    /// 32-bit floating point.
    Float32 {
        /// Minimum value.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        minimum: Option<f32>,
        /// Maximum value.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        maximum: Option<f32>,
    },
    /// 64-bit floating point.
    Float64 {
        /// Minimum value.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        minimum: Option<f64>,
        /// Maximum value.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        maximum: Option<f64>,
    },

    /// String type.
    String {
        /// Format constraint (e.g., email, uri).
        #[serde(default, skip_serializing_if = "Option::is_none")]
        format: Option<StringFormat>,
        /// Regex pattern (requires `pattern` feature).
        #[serde(default, skip_serializing_if = "Option::is_none")]
        pattern: Option<String>,
        /// Minimum length.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        min_length: Option<usize>,
        /// Maximum length.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        max_length: Option<usize>,
    },

    /// Byte array.
    Bytes {
        /// Minimum length.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        min_length: Option<usize>,
        /// Maximum length.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        max_length: Option<usize>,
    },

    /// Array with homogeneous items.
    Array {
        /// Item schema.
        items: Box<Schema>,
        /// Minimum items.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        min_items: Option<usize>,
        /// Maximum items.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        max_items: Option<usize>,
        /// Require unique items.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        unique_items: Option<bool>,
    },

    /// Object with named properties.
    Object {
        /// Property schemas.
        #[serde(default)]
        properties: IndexMap<String, Schema>,
        /// Required property names.
        #[serde(default)]
        required: Vec<String>,
        /// Schema for additional properties.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        additional_properties: Option<Box<Schema>>,
    },

    /// Tuple with fixed-position items.
    Tuple {
        /// Item schemas.
        items: Vec<Schema>,
    },

    /// Union matching any variant.
    Union {
        /// Variant schemas.
        any_of: Vec<Schema>,
    },

    /// Literal value.
    Literal {
        /// The literal value.
        value: LiteralValue,
    },

    /// Enum of string values.
    Enum {
        /// Allowed values.
        values: Vec<String>,
    },

    /// Reference to a named schema.
    Ref {
        /// Reference path (e.g., "#/definitions/MyType").
        #[serde(rename = "$ref")]
        reference: String,
    },

    /// Named schema for code generation.
    Named {
        /// Type name.
        name: String,
        /// Inner schema.
        schema: Box<Schema>,
    },

    /// Function type with parameters and return type.
    Function {
        /// Parameter schemas.
        parameters: Vec<Schema>,
        /// Return type schema.
        returns: Box<Schema>,
    },

    /// Void type - represents no value (function return).
    Void,

    /// Never type - uninhabitable type for exhaustive matching.
    Never,

    /// Any type - escape hatch, validates any value.
    Any,

    /// Unknown type - like Any but semantically requires checking.
    Unknown,

    /// Undefined type - for TypeScript optional unions.
    Undefined,

    /// Recursive type wrapper for self-referencing schemas.
    ///
    /// The `schema` field contains a schema that can reference itself
    /// via `Ref { reference: <id> }` where `<id>` matches this schema's `$id`.
    Recursive {
        /// Inner schema that may reference itself.
        schema: Box<Schema>,
    },

    /// Intersection type - value must match all schemas.
    ///
    /// Equivalent to JSON Schema's `allOf` constraint.
    Intersect {
        /// Schemas that must all match.
        all_of: Vec<Schema>,
    },
}

/// String format constraints.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum StringFormat {
    /// Email address format.
    Email,
    /// UUID format.
    Uuid,
    /// URI format.
    Uri,
    /// DateTime format (ISO 8601).
    DateTime,
    /// Date format (ISO 8601).
    Date,
    /// Time format (ISO 8601).
    Time,
    /// Hostname format.
    Hostname,
    /// IPv4 address format.
    Ipv4,
    /// IPv6 address format.
    Ipv6,
    /// Custom format with arbitrary name.
    Custom(String),
}

/// Literal value types.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum LiteralValue {
    /// String literal.
    String(String),
    /// Integer literal.
    Number(i64),
    /// Float literal.
    Float(f64),
    /// Boolean literal.
    Boolean(bool),
    /// Null literal.
    Null,
}

impl Schema {
    /// Creates a new schema with the given kind and no metadata.
    pub fn new(kind: SchemaKind) -> Self {
        Self {
            kind,
            id: None,
            schema_version: None,
            title: None,
            description: None,
            default: None,
            examples: None,
            read_only: None,
            write_only: None,
            deprecated: None,
        }
    }

    /// Returns the kind name of this schema.
    pub fn kind(&self) -> &'static str {
        self.kind.kind_name()
    }

    /// Sets the $id field.
    pub fn with_id(mut self, id: impl Into<String>) -> Self {
        self.id = Some(id.into());
        self
    }

    /// Sets the title field.
    pub fn with_title(mut self, title: impl Into<String>) -> Self {
        self.title = Some(title.into());
        self
    }

    /// Sets the description field.
    pub fn with_description(mut self, description: impl Into<String>) -> Self {
        self.description = Some(description.into());
        self
    }

    /// Sets the default value.
    pub fn with_default(mut self, default: Value) -> Self {
        self.default = Some(default);
        self
    }

    /// Sets the examples.
    pub fn with_examples(mut self, examples: Vec<Value>) -> Self {
        self.examples = Some(examples);
        self
    }

    /// Sets the read-only flag.
    pub fn with_read_only(mut self, read_only: bool) -> Self {
        self.read_only = Some(read_only);
        self
    }

    /// Sets the write-only flag.
    pub fn with_write_only(mut self, write_only: bool) -> Self {
        self.write_only = Some(write_only);
        self
    }

    /// Sets the deprecated flag.
    pub fn with_deprecated(mut self, deprecated: bool) -> Self {
        self.deprecated = Some(deprecated);
        self
    }

    /// Checks if this schema is optional within the given parent object.
    pub fn is_optional_in(&self, parent: &Schema) -> bool {
        if let SchemaKind::Object { required, .. } = &parent.kind {
            match &self.kind {
                SchemaKind::Named { name, .. } => !required.contains(name),
                _ => false,
            }
        } else {
            false
        }
    }
}

impl SchemaKind {
    /// Returns the kind name for this schema variant.
    pub fn kind_name(&self) -> &'static str {
        match self {
            SchemaKind::Null => "Null",
            SchemaKind::Bool => "Bool",
            SchemaKind::Int8 { .. } => "Int8",
            SchemaKind::Int16 { .. } => "Int16",
            SchemaKind::Int32 { .. } => "Int32",
            SchemaKind::Int64 { .. } => "Int64",
            SchemaKind::UInt8 { .. } => "UInt8",
            SchemaKind::UInt16 { .. } => "UInt16",
            SchemaKind::UInt32 { .. } => "UInt32",
            SchemaKind::UInt64 { .. } => "UInt64",
            SchemaKind::Float32 { .. } => "Float32",
            SchemaKind::Float64 { .. } => "Float64",
            SchemaKind::String { .. } => "String",
            SchemaKind::Bytes { .. } => "Bytes",
            SchemaKind::Array { .. } => "Array",
            SchemaKind::Object { .. } => "Object",
            SchemaKind::Tuple { .. } => "Tuple",
            SchemaKind::Union { .. } => "Union",
            SchemaKind::Literal { .. } => "Literal",
            SchemaKind::Enum { .. } => "Enum",
            SchemaKind::Ref { .. } => "Ref",
            SchemaKind::Named { .. } => "Named",
            SchemaKind::Function { .. } => "Function",
            SchemaKind::Void => "Void",
            SchemaKind::Never => "Never",
            SchemaKind::Any => "Any",
            SchemaKind::Unknown => "Unknown",
            SchemaKind::Undefined => "Undefined",
            SchemaKind::Recursive { .. } => "Recursive",
            SchemaKind::Intersect { .. } => "Intersect",
        }
    }
}

impl std::fmt::Display for Schema {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.kind)
    }
}

impl std::fmt::Display for SchemaKind {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            SchemaKind::Null => write!(f, "null"),
            SchemaKind::Bool => write!(f, "boolean"),
            SchemaKind::Int8 { .. } => write!(f, "int8"),
            SchemaKind::Int16 { .. } => write!(f, "int16"),
            SchemaKind::Int32 { .. } => write!(f, "int32"),
            SchemaKind::Int64 { .. } => write!(f, "int64"),
            SchemaKind::UInt8 { .. } => write!(f, "uint8"),
            SchemaKind::UInt16 { .. } => write!(f, "uint16"),
            SchemaKind::UInt32 { .. } => write!(f, "uint32"),
            SchemaKind::UInt64 { .. } => write!(f, "uint64"),
            SchemaKind::Float32 { .. } => write!(f, "float32"),
            SchemaKind::Float64 { .. } => write!(f, "float64"),
            SchemaKind::String { .. } => write!(f, "string"),
            SchemaKind::Bytes { .. } => write!(f, "bytes"),
            SchemaKind::Array { items, .. } => write!(f, "Array<{}>", items),
            SchemaKind::Object {
                properties,
                required,
                ..
            } => {
                write!(f, "{{")?;
                for (i, (name, schema)) in properties.iter().enumerate() {
                    if i > 0 {
                        write!(f, ", ")?;
                    }
                    if required.contains(name) {
                        write!(f, "{}: {}", name, schema)?;
                    } else {
                        write!(f, "{}?: {}", name, schema)?;
                    }
                }
                write!(f, "}}")
            }
            SchemaKind::Tuple { items } => {
                write!(f, "[")?;
                for (i, item) in items.iter().enumerate() {
                    if i > 0 {
                        write!(f, ", ")?;
                    }
                    write!(f, "{}", item)?;
                }
                write!(f, "]")
            }
            SchemaKind::Union { any_of } => {
                for (i, variant) in any_of.iter().enumerate() {
                    if i > 0 {
                        write!(f, " | ")?;
                    }
                    write!(f, "{}", variant)?;
                }
                Ok(())
            }
            SchemaKind::Literal { value } => match value {
                LiteralValue::String(s) => write!(f, "\"{}\"", s),
                LiteralValue::Number(n) => write!(f, "{}", n),
                LiteralValue::Float(fl) => write!(f, "{}", fl),
                LiteralValue::Boolean(b) => write!(f, "{}", b),
                LiteralValue::Null => write!(f, "null"),
            },
            SchemaKind::Enum { values } => {
                for (i, v) in values.iter().enumerate() {
                    if i > 0 {
                        write!(f, " | ")?;
                    }
                    write!(f, "\"{}\"", v)?;
                }
                Ok(())
            }
            SchemaKind::Ref { reference } => write!(f, "{}", reference),
            SchemaKind::Named { name, schema } => write!(f, "type {} = {}", name, schema),
            SchemaKind::Function {
                parameters,
                returns,
            } => {
                write!(f, "(")?;
                for (i, param) in parameters.iter().enumerate() {
                    if i > 0 {
                        write!(f, ", ")?;
                    }
                    write!(f, "{}", param)?;
                }
                write!(f, ") => {}", returns)
            }
            SchemaKind::Void => write!(f, "void"),
            SchemaKind::Never => write!(f, "never"),
            SchemaKind::Any => write!(f, "any"),
            SchemaKind::Unknown => write!(f, "unknown"),
            SchemaKind::Undefined => write!(f, "undefined"),
            SchemaKind::Recursive { schema } => write!(f, "Recursive<{}>", schema),
            SchemaKind::Intersect { all_of } => {
                for (i, schema) in all_of.iter().enumerate() {
                    if i > 0 {
                        write!(f, " & ")?;
                    }
                    write!(f, "{}", schema)?;
                }
                Ok(())
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_schema_serialize() {
        let schema = Schema::new(SchemaKind::Object {
            properties: {
                let mut props = IndexMap::new();
                props.insert(
                    "id".to_string(),
                    Schema::new(SchemaKind::Int64 {
                        minimum: None,
                        maximum: None,
                    }),
                );
                props.insert(
                    "name".to_string(),
                    Schema::new(SchemaKind::String {
                        format: None,
                        pattern: None,
                        min_length: Some(1),
                        max_length: None,
                    }),
                );
                props
            },
            required: vec!["id".to_string(), "name".to_string()],
            additional_properties: None,
        });

        let json = serde_json::to_string_pretty(&schema).unwrap();
        assert!(json.contains("\"kind\": \"object\""));
        assert!(json.contains("\"id\""));
    }

    #[test]
    fn test_schema_deserialize() {
        let json = r#"{"kind": "object", "properties": {"x": {"kind": "int64", "minimum": null, "maximum": null}}, "required": ["x"]}"#;
        let schema: Schema = serde_json::from_str(json).unwrap();
        assert!(matches!(schema.kind, SchemaKind::Object { .. }));
    }

    #[test]
    fn test_string_format_serialize() {
        let schema = Schema::new(SchemaKind::String {
            format: Some(StringFormat::Email),
            pattern: None,
            min_length: None,
            max_length: None,
        });
        let json = serde_json::to_string(&schema).unwrap();
        assert!(json.contains("\"format\":\"email\""));
    }

    #[test]
    fn test_schema_with_metadata() {
        let schema = Schema::new(SchemaKind::String {
            format: Some(StringFormat::Email),
            pattern: None,
            min_length: None,
            max_length: None,
        })
        .with_id("https://example.com/schemas/email")
        .with_title("Email")
        .with_description("An email address");

        let json = serde_json::to_string_pretty(&schema).unwrap();
        assert!(json.contains("\"$id\": \"https://example.com/schemas/email\""));
        assert!(json.contains("\"title\": \"Email\""));
        assert!(json.contains("\"description\": \"An email address\""));
    }

    #[test]
    fn test_function_type() {
        let schema = Schema::new(SchemaKind::Function {
            parameters: vec![Schema::new(SchemaKind::Int64 {
                minimum: None,
                maximum: None,
            })],
            returns: Box::new(Schema::new(SchemaKind::String {
                format: None,
                pattern: None,
                min_length: None,
                max_length: None,
            })),
        });

        let json = serde_json::to_string(&schema).unwrap();
        assert!(json.contains("\"kind\":\"function\""));
        assert!(json.contains("\"parameters\""));
        assert!(json.contains("\"returns\""));
    }

    #[test]
    fn test_void_never_any_unknown_undefined() {
        assert_eq!(Schema::new(SchemaKind::Void).kind(), "Void");
        assert_eq!(Schema::new(SchemaKind::Never).kind(), "Never");
        assert_eq!(Schema::new(SchemaKind::Any).kind(), "Any");
        assert_eq!(Schema::new(SchemaKind::Unknown).kind(), "Unknown");
        assert_eq!(Schema::new(SchemaKind::Undefined).kind(), "Undefined");
    }

    #[test]
    fn test_schema_display() {
        assert_eq!(format!("{}", Schema::new(SchemaKind::Void)), "void");
        assert_eq!(format!("{}", Schema::new(SchemaKind::Never)), "never");
        assert_eq!(format!("{}", Schema::new(SchemaKind::Any)), "any");
        assert_eq!(format!("{}", Schema::new(SchemaKind::Unknown)), "unknown");
        assert_eq!(
            format!("{}", Schema::new(SchemaKind::Undefined)),
            "undefined"
        );
    }

    #[test]
    fn test_recursive_type() {
        let schema = Schema::new(SchemaKind::Recursive {
            schema: Box::new(Schema::new(SchemaKind::Union {
                any_of: vec![
                    Schema::new(SchemaKind::Null),
                    Schema::new(SchemaKind::Int64 {
                        minimum: None,
                        maximum: None,
                    }),
                ],
            })),
        })
        .with_id("RecursiveValue");

        let json = serde_json::to_string(&schema).unwrap();
        assert!(json.contains("\"kind\":\"recursive\""));
        assert!(json.contains("\"$id\":\"RecursiveValue\""));
    }
}