20 releases (5 breaking)

Uses new Rust 2021

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

#355 in Encoding

Download history 41/week @ 2022-04-18 135/week @ 2022-04-25 182/week @ 2022-05-02 21/week @ 2022-05-09 346/week @ 2022-05-16 82/week @ 2022-05-23 144/week @ 2022-05-30 63/week @ 2022-06-06 2/week @ 2022-06-20 40/week @ 2022-06-27 21/week @ 2022-07-11 40/week @ 2022-07-18 60/week @ 2022-07-25 21/week @ 2022-08-01

142 downloads per month

MIT/Apache

50KB
1K SLoC

Rschema   Crates Badge Docs Badge License: Apache License: MIT

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

~1.4–2.3MB
~48K SLoC