All Projects → GREsau → Schemars

GREsau / Schemars

Licence: mit
Generate JSON Schema documents from Rust code

Programming Languages

rust
11053 projects

Projects that are alternatives of or similar to Schemars

Dms
基于Json Schema的动态Json数据配置平台
Stars: ✭ 142 (-21.55%)
Mutual labels:  json-schema
Ngx Formly
JSON powered / Dynamic forms for Angular
Stars: ✭ 2,109 (+1065.19%)
Mutual labels:  json-schema
Json Schema To Openapi Schema
A little NodeJS package to convert JSON Schema to OpenAPI Schema Objects
Stars: ✭ 168 (-7.18%)
Mutual labels:  json-schema
Weld
Full fake REST API generator written with Rust
Stars: ✭ 146 (-19.34%)
Mutual labels:  serde
Skillset
✨ Intuitive job-candidate skill visualization, taking advantage of D3.js and JSONResume.
Stars: ✭ 152 (-16.02%)
Mutual labels:  json-schema
Xdm
Experience Data Model
Stars: ✭ 160 (-11.6%)
Mutual labels:  json-schema
Kubernetes Json Schema
Schemas for every version of every object in every version of Kubernetes
Stars: ✭ 140 (-22.65%)
Mutual labels:  json-schema
Json
Strongly typed JSON library for Rust
Stars: ✭ 2,544 (+1305.52%)
Mutual labels:  serde
Cfworker
A collection of packages optimized for Cloudflare Workers and service workers.
Stars: ✭ 152 (-16.02%)
Mutual labels:  json-schema
Newtonsoft.json.schema
Json.NET Schema is a powerful, complete and easy to use JSON Schema framework for .NET
Stars: ✭ 167 (-7.73%)
Mutual labels:  json-schema
Json Schema Viewer
JavaScript tool for visualizing json-schemas
Stars: ✭ 147 (-18.78%)
Mutual labels:  json-schema
Jsonschema2db
Generate tables dynamically from a JSON Schema and insert data
Stars: ✭ 152 (-16.02%)
Mutual labels:  json-schema
Go Jsonschema
A tool to generate Go data types from JSON Schema definitions.
Stars: ✭ 164 (-9.39%)
Mutual labels:  json-schema
Aptos
☀️ A tool for validating data using JSON Schema and converting JSON Schema documents into different data-interchange formats
Stars: ✭ 144 (-20.44%)
Mutual labels:  json-schema
Marshmallow Jsonschema
JSON Schema Draft v7 (http://json-schema.org/) formatting with marshmallow
Stars: ✭ 172 (-4.97%)
Mutual labels:  json-schema
Serde Xml Rs
xml-rs based deserializer for Serde (compatible with 1.0+)
Stars: ✭ 141 (-22.1%)
Mutual labels:  serde
Raml Server
run a mocked server JUST based on a RAML API's definition .. zero coding
Stars: ✭ 158 (-12.71%)
Mutual labels:  json-schema
Staticjson
Fast, direct and static typed parsing of JSON with C++
Stars: ✭ 177 (-2.21%)
Mutual labels:  json-schema
Serde Wasm Bindgen
Native integration of Serde with wasm-bindgen
Stars: ✭ 176 (-2.76%)
Mutual labels:  serde
Liform React
Generate forms from JSON Schema to use with React (& redux-form)
Stars: ✭ 167 (-7.73%)
Mutual labels:  json-schema

Schemars

CI Build Crates.io Docs rustc 1.36+

Generate JSON Schema documents from Rust code

Basic Usage

If you don't really care about the specifics, the easiest way to generate a JSON schema for your types is to #[derive(JsonSchema)] and use the schema_for! macro. All fields of the type must also implement JsonSchema - Schemars implements this for many standard library types.

use schemars::{schema_for, JsonSchema};

#[derive(JsonSchema)]
pub struct MyStruct {
    pub my_int: i32,
    pub my_bool: bool,
    pub my_nullable_enum: Option<MyEnum>,
}

#[derive(JsonSchema)]
pub enum MyEnum {
    StringNewType(String),
    StructVariant { floats: Vec<f32> },
}

let schema = schema_for!(MyStruct);
println!("{}", serde_json::to_string_pretty(&schema).unwrap());
Click to see the output JSON schema...
{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "MyStruct",
  "type": "object",
  "required": [
    "my_bool",
    "my_int"
  ],
  "properties": {
    "my_bool": {
      "type": "boolean"
    },
    "my_int": {
      "type": "integer",
      "format": "int32"
    },
    "my_nullable_enum": {
      "anyOf": [
        {
          "$ref": "#/definitions/MyEnum"
        },
        {
          "type": "null"
        }
      ]
    }
  },
  "definitions": {
    "MyEnum": {
      "anyOf": [
        {
          "type": "object",
          "required": [
            "StringNewType"
          ],
          "properties": {
            "StringNewType": {
              "type": "string"
            }
          }
        },
        {
          "type": "object",
          "required": [
            "StructVariant"
          ],
          "properties": {
            "StructVariant": {
              "type": "object",
              "required": [
                "floats"
              ],
              "properties": {
                "floats": {
                  "type": "array",
                  "items": {
                    "type": "number",
                    "format": "float"
                  }
                }
              }
            }
          }
        }
      ]
    }
  }
}

Serde Compatibility

One of the main aims of this library is compatibility with Serde. Any generated schema should match how serde_json would serialize/deserialize to/from JSON. To support this, Schemars will check for any #[serde(...)] attributes on types that derive JsonSchema, and adjust the generated schema accordingly.

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

#[derive(Deserialize, Serialize, JsonSchema)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct MyStruct {
    #[serde(rename = "myNumber")]
    pub my_int: i32,
    pub my_bool: bool,
    #[serde(default)]
    pub my_nullable_enum: Option<MyEnum>,
}

#[derive(Deserialize, Serialize, JsonSchema)]
#[serde(untagged)]
pub enum MyEnum {
    StringNewType(String),
    StructVariant { floats: Vec<f32> },
}

let schema = schema_for!(MyStruct);
println!("{}", serde_json::to_string_pretty(&schema).unwrap());
Click to see the output JSON schema...
{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "MyStruct",
  "type": "object",
  "required": [
    "myBool",
    "myNumber"
  ],
  "properties": {
    "myBool": {
      "type": "boolean"
    },
    "myNullableEnum": {
      "default": null,
      "anyOf": [
        {
          "$ref": "#/definitions/MyEnum"
        },
        {
          "type": "null"
        }
      ]
    },
    "myNumber": {
      "type": "integer",
      "format": "int32"
    }
  },
  "additionalProperties": false,
  "definitions": {
    "MyEnum": {
      "anyOf": [
        {
          "type": "string"
        },
        {
          "type": "object",
          "required": [
            "floats"
          ],
          "properties": {
            "floats": {
              "type": "array",
              "items": {
                "type": "number",
                "format": "float"
              }
            }
          }
        }
      ]
    }
  }
}

#[serde(...)] attributes can be overriden using #[schemars(...)] attributes, which behave identically (e.g. #[schemars(rename_all = "camelCase")]). You may find this useful if you want to change the generated schema without affecting Serde's behaviour, or if you're just not using Serde.

Feature Flags

  • derive (enabled by default) - provides #[derive(JsonSchema)] macro
  • impl_json_schema - implements JsonSchema for Schemars types themselves
  • preserve_order - keep the order of struct fields in Schema and SchemaObject

Optional Dependencies

Schemars can implement JsonSchema on types from several popular crates, enabled via optional dependencies (dependency versions are shown in brackets):

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].