json-schema-derive 0.0.2

JSON Schema derive macro with arbitrary fields support
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
//! Derive macro for generating JSON Schema from Rust types.
//!
//! This crate provides a `#[derive(JsonSchema)]` macro that generates a JSON Schema
//! for your types. It supports custom schema attributes via `#[json_schema(...)]`
//! and optionally integrates with common `serde` attributes when the `serde-compat` feature is enabled.
//!
//! # Example
//! ```rust
//! use json_schema_derive::JsonSchema;
//!
//! #[derive(JsonSchema)]
//! struct User {
//!     #[json_schema(comment = "User's name", minLength = 2)]
//!     name: String,
//!     /// User's age
//!     age: u32,
//!     tags: Vec<String>,
//! }
//!
//! let schema = User::json_schema();
//! ```
//!
//! # Features
//!
//! - `serde-compat`: Enables compatibility with serde attributes for schema generation
//! # Serde Compatibility
//!
//! When the `serde-compat` feature is enabled, the following `serde` attributes are supported:
//!
//! - `#[serde(skip)]` – Omits the field from the schema  
//! - `#[serde(rename = "new_name")]` – Renames the field in the schema  
//! - `#[serde(flatten)]` – Inlines nested struct fields  
//! - `#[serde(tag = "...")]` – Supports internally tagged enums
//!
//! ```rust
//! #[derive(JsonSchema)]
//! #[serde(tag = "type")]
//! enum Event {
//!     Login { user: String },
//!     Logout,
//! }
//! ```

use core::str;

pub use json_schema_derive_macro::JsonSchema;
// mod expanded;

/// Trait for generating JSON Schema from a type.
///
/// This trait is automatically implemented for types that derive `JsonSchema`.
/// It provides a method to generate a JSON Schema representation of the type.
pub trait JsonSchema {
    /// Generate a JSON Schema representation of the type.
    ///
    /// Returns a `serde_json::Value` containing the JSON Schema.
    fn json_schema() -> serde_json::Value;
}

macro_rules! impl_json_schema {
    ($name:expr, $($t:ty),*) => {
        $(
            impl JsonSchema for $t {
                fn json_schema() -> serde_json::Value {
                    serde_json::json!({ "type": $name })
                }
            }
        )*
    };
}

impl_json_schema!("number", u8, u16, u32, u64, i8, i16, i32, i64, f32, f64);
impl_json_schema!("boolean", bool);
impl_json_schema!("string", String, &str);

impl JsonSchema for () {
    fn json_schema() -> serde_json::Value {
        serde_json::json!({ "type": "null" })
    }
}

impl<T: JsonSchema> JsonSchema for Vec<T> {
    fn json_schema() -> serde_json::Value {
        serde_json::json!({ "type": "array", "items": T::json_schema() })
    }
}

impl<T: JsonSchema, const N: usize> JsonSchema for [T; N] {
    fn json_schema() -> serde_json::Value {
        serde_json::json!({ "type": "array", "items": T::json_schema(), "maxItems": N, "minItems": N })
    }
}

impl<T: JsonSchema> JsonSchema for Option<T> {
    fn json_schema() -> serde_json::Value {
        T::json_schema()
    }
}

impl<T: JsonSchema> JsonSchema for &Option<T> {
    fn json_schema() -> serde_json::Value {
        T::json_schema()
    }
}

impl<T: JsonSchema> JsonSchema for Box<T> {
    fn json_schema() -> serde_json::Value {
        T::json_schema()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde::Serialize;
    use serde_json::json;

    pub fn valid<T: JsonSchema + Serialize>(instance: &T) -> bool {
        let schema = T::json_schema();
        let json = serde_json::to_value(instance).unwrap();
        jsonschema::is_valid(&schema, &json)
    }

    #[test]
    fn test_impl_json_schema() {
        assert_eq!(u32::json_schema(), json!({ "type": "number" }));
        assert_eq!(bool::json_schema(), json!({ "type": "boolean" }));
        assert_eq!(String::json_schema(), json!({ "type": "string" }));
        assert_eq!(
            <Vec<u32>>::json_schema(),
            json!({ "type": "array", "items": { "type": "number" } })
        );
        assert_eq!(<Option<bool>>::json_schema(), json!({ "type": "boolean" }));
        assert_eq!(
            <[u32; 3]>::json_schema(),
            json!({ "type": "array", "items": { "type": "number" }, "maxItems": 3, "minItems": 3 })
        );

        assert!(valid::<u32>(&10));
        assert!(valid::<bool>(&true));
        assert!(valid::<String>(&"test".to_string()));
        assert!(valid::<Vec<u32>>(&vec![1, 2, 3]));
        assert!(valid::<Option<bool>>(&Some(true)));
        assert!(valid::<[u32; 3]>(&[1, 2, 3]));
    }

    #[derive(JsonSchema, Serialize)]
    #[json_schema(comment = "Test comment")]
    #[allow(dead_code)]
    struct TestStruct {
        #[json_schema(comment = "test field", minLength = 3)]
        name: String,
        age: u32,
        active: Option<bool>,
        scores: Vec<i32>,
    }

    #[test]
    fn test_struct_schema() {
        let schema = TestStruct::json_schema();
        let expected = json!({
            "type": "object",
            "properties": {
                "name": {
                    "type": "string",
                    "comment": "test field",
                    "minLength": 3
                },
                "age": {
                    "type": "number"
                },
                "active": {
                    "type": "boolean"
                },
                "scores": {
                    "type": "array",
                    "items": {"type": "number"}
                }
            },
            "required": ["name", "age", "scores"],
            "comment": "Test comment"
        });
        assert_eq!(schema, expected);
        assert!(valid(&TestStruct {
            name: "test".to_string(),
            age: 10,
            active: Some(true),
            scores: vec![1, 2, 3],
        }));
    }

    #[derive(JsonSchema)]
    #[allow(dead_code)]
    struct NestedStruct {
        inner: Option<TestStruct>,
        tags: Option<Vec<String>>,
    }

    #[test]
    fn test_nested_struct() {
        let schema = NestedStruct::json_schema();
        let expected = json!({
            "type": "object",
            "properties": {
                "inner": {
                    "type": "object",
                    "properties": {
                        "name": {
                            "type": "string",
                            "comment": "test field",
                            "minLength": 3
                        },
                        "age": {
                            "type": "number"
                        },
                        "active": {
                            "type": "boolean"
                        },
                        "scores": {
                            "type": "array",
                            "items": {"type": "number"}
                        }
                    },
                    "required": ["name", "age", "scores"],
                    "comment": "Test comment"
                },
                "tags": {
                    "type": "array",
                    "items": {"type": "string"}
                }
            },
            "required": []
        });
        assert_eq!(schema, expected);
    }

    #[derive(JsonSchema, Serialize)]
    #[json_schema(comment = "Test comment")]
    #[allow(dead_code)]
    struct TestStructUnnamed(String);

    #[test]
    fn test_struct_unnamed() {
        let schema = TestStructUnnamed::json_schema();
        let expected = json!({ "comment": "Test comment", "type": "string" });
        assert_eq!(schema, expected);
        assert!(valid(&TestStructUnnamed("test".to_string())));
    }

    #[derive(JsonSchema, Serialize)]
    #[json_schema(comment = "Test comment")]
    #[allow(dead_code)]
    struct TestStructUnnamedMultiple(String, u32);

    #[test]
    fn test_struct_unnamed_multiple() {
        let schema = TestStructUnnamedMultiple::json_schema();
        let expected = json!({
            "comment": "Test comment",
            "type": "array",
            "prefixItems": [{ "type": "string" }, { "type": "number" }],
            "minItems": 2,
            "maxItems": 2,
            "unevaluatedItems": false,
        });
        assert_eq!(schema, expected);
        assert!(valid(&TestStructUnnamedMultiple("test".to_string(), 10)));
    }

    #[derive(JsonSchema, Serialize)]
    #[json_schema(comment = "Test comment")]
    #[allow(dead_code)]
    enum EnumUnit {
        A,
        B,
        C,
    }

    #[test]
    fn test_enum_unit() {
        let schema = EnumUnit::json_schema();
        let expected = json!({
            "type": "string",
            "comment": "Test comment",
            "enum": ["A", "B", "C"],
        });
        println!("{:#?}", serde_json::to_value(&EnumUnit::A).unwrap());
        assert_eq!(schema, expected);
        assert!(valid(&EnumUnit::A));
        assert!(valid(&EnumUnit::B));
        assert!(valid(&EnumUnit::C));
    }

    #[derive(JsonSchema, Serialize)]
    #[json_schema(comment = "Test comment")]
    #[allow(dead_code)]
    enum EnumUnnamed {
        A(String),
        B(u32),
    }

    #[test]
    fn test_enum_unit_unnamed() {
        let schema = EnumUnnamed::json_schema();
        let expected = json!({
            "type": "object",
            "comment": "Test comment",
            "properties": {
                "A": { "type": "string" },
                "B": { "type": "number" },
            }
        });
        assert_eq!(schema, expected);
        assert!(valid(&EnumUnnamed::A("test".to_string())));
        assert!(valid(&EnumUnnamed::B(10)));
    }

    #[derive(JsonSchema, Serialize)]
    #[json_schema(comment = "Test comment")]
    #[allow(dead_code)]
    enum EnumNamed {
        A { name: String },
        B { age: u32 },
    }

    #[test]
    fn test_enum_named() {
        let schema = EnumNamed::json_schema();
        let expected = json!({
            "type": "object",
            "comment": "Test comment",
            "properties": {
                "A": { "type": "object", "properties": { "name": { "type": "string" } }, "required": ["name"] },
                "B": { "type": "object", "properties": { "age": { "type": "number" } }, "required": ["age"] },
            }
        });
        assert_eq!(schema, expected);
        assert!(valid(&EnumNamed::A {
            name: "test".to_string()
        }));
        assert!(valid(&EnumNamed::B { age: 10 }));
    }

    #[derive(JsonSchema, Serialize)]
    #[allow(dead_code)]
    /// Test description
    struct TestStructDoc {
        /// Test field description
        name: String,
    }

    #[test]
    fn test_struct_doc() {
        let schema = TestStructDoc::json_schema();
        let expected = json!({ "type": "object", "description": "Test description", "properties": { "name": { "type": "string", "description": "Test field description" } }, "required": ["name"] });
        assert_eq!(schema, expected);
        assert!(valid(&TestStructDoc {
            name: "test".to_string()
        }));
    }
}

#[cfg(feature = "serde-compat")]
#[cfg(test)]
mod tests_serde_compat {
    use super::*;
    use serde::Serialize;
    use serde_json::json;

    #[derive(JsonSchema, Serialize)]
    #[json_schema(comment = "Test comment")]
    #[allow(dead_code)]
    struct TestStructWithSerde {
        #[serde(skip)]
        skip: u32,
        #[serde(rename = "foo")]
        renamed: u32,
    }

    #[test]
    fn test_struct_with_serde() {
        let schema = TestStructWithSerde::json_schema();
        let expected = json!({
            "type": "object",
            "properties": { "foo": { "type": "number" } },
            "required": ["foo"],
            "comment": "Test comment"
        });
        assert_eq!(schema, expected);
        assert!(tests::valid(&TestStructWithSerde {
            skip: 0,
            renamed: 10,
        }));
    }

    #[derive(JsonSchema, Serialize)]
    #[json_schema(comment = "Test comment")]
    #[allow(dead_code)]
    struct TestStructWithFlatten {
        #[serde(flatten)]
        inner: TestStructWithSerde,
    }

    #[test]
    fn test_struct_with_flatten() {
        let schema = TestStructWithFlatten::json_schema();
        let expected = json!({
            "type": "object",
            "properties": { "foo": { "type": "number" } },
            "required": ["foo"],
            "comment": "Test comment"
        });
        println!("{:#?}", schema);
        assert_eq!(schema, expected);
        assert!(tests::valid(&TestStructWithFlatten {
            inner: TestStructWithSerde {
                skip: 0,
                renamed: 10,
            }
        }));
    }

    #[derive(JsonSchema, Serialize)]
    #[allow(dead_code)]
    #[serde(tag = "type")]
    enum EnumUnitSerdeTag {
        A,
        B,
    }

    #[test]
    fn test_enum_serde_tag() {
        let schema = EnumUnitSerdeTag::json_schema();
        let expected = json!({
            "oneOf": [
                { "type": "object", "properties": { "type": { "type": "string", "const": "A" } }, "required": ["type"] },
                { "type": "object", "properties": { "type": { "type": "string", "const": "B" } }, "required": ["type"] }
            ]
        });
        assert_eq!(schema, expected);
        assert!(tests::valid(&EnumUnitSerdeTag::A));
        assert!(tests::valid(&EnumUnitSerdeTag::B));
    }

    #[derive(JsonSchema, Serialize)]
    #[allow(dead_code)]
    #[serde(tag = "type")]
    enum EnumNamedSerdeTag {
        A { name: String },
        B { age: u32 },
        C,
    }

    #[test]
    fn test_enum_named_serde_tag() {
        let schema = EnumNamedSerdeTag::json_schema();
        let expected = json!({
            "oneOf": [
                { "type": "object", "properties": { "type": { "type": "string", "const": "A" }, "name": { "type": "string" } }, "required": ["name", "type"] },
                { "type": "object", "properties": { "type": { "type": "string", "const": "B" }, "age": { "type": "number" } }, "required": ["age", "type"] },
                { "type": "object", "properties": { "type": { "type": "string", "const": "C" } }, "required": ["type"] }
            ]
        });
        assert_eq!(schema, expected);
        assert!(tests::valid(&EnumNamedSerdeTag::A {
            name: "test".to_string()
        }));
        assert!(tests::valid(&EnumNamedSerdeTag::B { age: 10 }));
    }
}