Rschema provides a macro for generating JSON schemas from Rust structures.
```rust use rschema::{ Schema, Schematic, };
struct Data { #[rschema( title = "Test flag", description = "The flag whether for test.", )] test_flag: bool, }
struct AppConfig { #[rschema( title = "Application name", required, )] name: String,
#[rschema(
title = "Application version",
pattern = r"^(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)$",
required,
)]
version: String,
#[rschema(
title = "Application data",
description = "This property is optional.",
)]
other_data: Data,
}
fn main() -> rschema::Result<()> {
Schema::new::
Ok(())
} ```
This code generates the following JSON schema file.
json
{
"title": "Application Config",
"type": "object",
"properties": {
"name": {
"title": "Application name",
"type": "string"
},
"version": {
"title": "Application version",
"type": "string",
"pattern": "^(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)$"
},
"other_data": {
"title": "Application data",
"type": "object",
"properties": {
"test_flag": {
"title": "Test flag",
"description": "The flag whether for test.",
"type": "boolean"
}
},
"additionalProperties": true
}
},
"required": [
"name",
"version"
],
"additionalProperties": false
}
Rschema is strongly intended to be used in combination with Serde.
For example, generate a JSON schema from structs and enums you define. Data files validated by the JSON schema are always deserializable to the original structures!