Expand description

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(field(
        title = "Test flag",
        description = "The flag whether for test.",
    ))]
    test_flag: bool,
}
 
#[derive(Debug, Schematic)]
struct AppConfig {
    #[rschema(
        field(
            title = "Application name",
        ),
        required,
    )]
    name: String,
 
    #[rschema(
        field(
            title = "Application version",
            pattern = r"^(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)$",
        ),
        required,
    )]
    version: String,
 
    #[rschema(field(
        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
}

Attributes

Struct attributes

This attribute is available only for structs or struct variants.

AttributeTypeMeaning
additional_propertiesboolWhether to allow properties not included in properties.

Field attributes

Common

All types have the following attributes.

Top-level attributeSub attributeTypeMeaning
fieldtitleStringRequired. The short description about the field.
descriptionStringThe more lengthy description about the field.
requiredboolWhether the property is required or not.

Dedicated

Each types have dedicated attributes.

Using another type’s attributes does not raise errors, but no meaning.

String
AttributeTypeMeaning
min_lengthu64The minimum length.
max_lengthu64The maximum length.
patternStringThe regular expression to restrict value.
formatStringThe basic semantic identification of certain kinds of string values that are commonly used.
Number
AttributeTypeMeaning
minimumi64Specify the minimum of the range.
maximumi64Specify the maximum of the range.
multiple_ofi64Numbers can be restricted to a multiple of a given number.
exclusive_minimumi64Specify the exclusive minimum of the range.
exclusive_maximumi64Specify the exclusive maximum of the range.
Array
AttributeTypeMeaning
min_itemsusizeSpecify the minimum length of the array.
max_itemsusizeSpecify the maximum length of the array.

Structs

Attributes for array type properties.

Attributes for enum type (a kind of array type) properties.

Attributes for object type properties.

Properties map of an object type property.

One of the properties of an object type property.

This is a structure representing the JSON schema itself.

Attributes for string type properties.

Attributes for tuple type (a kind of array type) properties.

Enums

This type represents all possible errors that can occur when generate or write JSON Schema string.

Items of an array type or a tuple type property.

Represents some property type.

Traits

A data structure that can provide any schema informations.

Type Definitions

Alias for a Result with the error type rschema::Error.