fork of rust schemars
Find a file
2019-10-14 20:00:57 +01:00
schemars Set readOnly/writeOnly on properties 2019-10-14 20:00:57 +01:00
schemars_derive Set readOnly/writeOnly on properties 2019-10-14 20:00:57 +01:00
.gitignore Initial commit 2019-08-04 16:48:29 +01:00
Cargo.toml Add workspace cargo.toml 2019-09-03 19:26:37 +01:00
LICENSE Initial commit 2019-08-04 16:48:29 +01:00
README.md Add list of TODOs to readme 2019-09-15 21:27:38 +01:00

schemars

Generate JSON Schema documents from Rust code

Work in progress!

Basic Usage

use schemars::{JsonSchema, schema_for};
use serde::{Deserialize, Serialize};

#[derive(Deserialize, Serialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
struct MyStruct {
    my_int: i32,
    my_nullable: Option<bool>,
    my_nested_struct: Nested,
}

#[derive(Deserialize, Serialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
struct Nested {
    #[serde(default)]
    my_string: String,
    #[serde(rename = "myArray")]
    my_float_vec: Vec<f32>,
    my_recursive_struct: Option<Box<Nested>>,
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let schema = schema_for!(MyStruct)?;
    println!("{}", serde_json::to_string_pretty(&schema)?);
    Ok(())
}

This outputs the following:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "MyStruct",
  "type": "object",
  "definitions": {
    "Nested": {
      "type": "object",
      "required": [
        "myArray",
        "myRecursiveStruct"
      ],
      "properties": {
        "myArray": {
          "type": "array",
          "items": {
            "type": "number",
            "format": "float"
          }
        },
        "myRecursiveStruct": {
          "anyOf": [
            {
              "$ref": "#/definitions/Nested"
            },
            {
              "type": "null"
            }
          ]
        },
        "myString": {
          "type": "string"
        }
      }
    }
  },
  "required": [
    "myInt",
    "myNestedStruct",
    "myNullable"
  ],
  "properties": {
    "myInt": {
      "type": "integer",
      "format": "int32"
    },
    "myNestedStruct": {
      "$ref": "#/definitions/Nested"
    },
    "myNullable": {
      "type": [
        "boolean",
        "null"
      ]
    }
  }
}

Note that the #[serde(...)] attributes are respected.

TODO