Crates.io | rschema |
lib.rs | rschema |
version | 0.6.1 |
source | src |
created_at | 2022-03-27 20:30:00.000184 |
updated_at | 2022-04-23 15:48:13.998284 |
description | A json-schema generator |
homepage | https://github.com/glaceef |
repository | https://github.com/glaceef/rschema |
max_upload_size | |
id | 557406 |
size | 53,841 |
Rschema provides a macro for generating JSON schemas from Rust structures.
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
}
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!