20 releases (5 breaking)
0.6.1 | Apr 23, 2022 |
---|---|
0.6.0 | Apr 16, 2022 |
0.5.1 | Apr 11, 2022 |
0.4.3 | Apr 9, 2022 |
0.1.3 | Mar 30, 2022 |
#1100 in Encoding
57 downloads per month
50KB
1K
SLoC
Rschema
Rschema provides a macro for generating JSON schemas from Rust structures.
Example
use rschema::{
Schema,
Schematic,
};
#[derive(Debug, Schematic)]
#[rschema(additional_properties)]
struct Data {
#[rschema(
title = "Test flag",
description = "The flag whether for test.",
)]
test_flag: bool,
}
#[derive(Debug, Schematic)]
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::<AppConfig>("Application Config")
.write_pretty("../schemas/config.schema.json")?;
Ok(())
}
This code generates the following JSON schema file.
{
"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
}
Combination with Serde
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!
Dependencies
~2–2.9MB
~57K SLoC