{"id":15012947,"url":"https://github.com/gresau/schemars","last_synced_at":"2025-05-12T20:52:33.841Z","repository":{"id":38984032,"uuid":"200511807","full_name":"GREsau/schemars","owner":"GREsau","description":"Generate JSON Schema documents from Rust code","archived":false,"fork":false,"pushed_at":"2025-02-25T12:20:10.000Z","size":858,"stargazers_count":996,"open_issues_count":139,"forks_count":252,"subscribers_count":9,"default_branch":"master","last_synced_at":"2025-05-08T19:01:57.134Z","etag":null,"topics":["json-schema","rust","serde"],"latest_commit_sha":null,"homepage":"https://graham.cool/schemars/","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/GREsau.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null},"funding":{"github":"GREsau"}},"created_at":"2019-08-04T15:48:29.000Z","updated_at":"2025-05-08T17:16:21.000Z","dependencies_parsed_at":"2024-01-18T04:07:19.012Z","dependency_job_id":"f128ecc8-9926-4b4a-9b15-f505abfb8b3c","html_url":"https://github.com/GREsau/schemars","commit_stats":{"total_commits":555,"total_committers":28,"mean_commits":"19.821428571428573","dds":"0.11171171171171168","last_synced_commit":"a479e6cc0ee158aedf97ad53c637c31da2b5a6a3"},"previous_names":[],"tags_count":58,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GREsau%2Fschemars","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GREsau%2Fschemars/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GREsau%2Fschemars/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GREsau%2Fschemars/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/GREsau","download_url":"https://codeload.github.com/GREsau/schemars/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253821609,"owners_count":21969738,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["json-schema","rust","serde"],"created_at":"2024-09-24T19:43:28.424Z","updated_at":"2025-05-12T20:52:33.819Z","avatar_url":"https://github.com/GREsau.png","language":"Rust","funding_links":["https://github.com/sponsors/GREsau"],"categories":[],"sub_categories":[],"readme":"# Schemars\n\n\u003e [!NOTE]\n\u003e This branch is for the current v1 alpha version of Schemars which is still under development.\n\u003e For the current stable release of Schemars (v0.8.x), see the [v0 branch](https://github.com/GREsau/schemars/tree/v0).\n\u003e\n\u003e For information on migrating from 0.8 to 1.0, see [the migration guide](https://graham.cool/schemars/migrating/).\n\n[![CI Build](https://img.shields.io/github/actions/workflow/status/GREsau/schemars/ci.yml?branch=master\u0026logo=GitHub)](https://github.com/GREsau/schemars/actions)\n[![Crates.io](https://img.shields.io/crates/v/schemars)](https://crates.io/crates/schemars)\n[![Docs](https://img.shields.io/docsrs/schemars/1.0.0--latest?label=docs)](https://docs.rs/schemars/1.0.0--latest)\n[![MSRV 1.70+](https://img.shields.io/badge/msrv-1.70-blue)](https://blog.rust-lang.org/2023/06/01/Rust-1.70.0.html)\n\nGenerate JSON Schema documents from Rust code\n\n## Basic Usage\n\nIf 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.\n\n```rust\nuse schemars::{schema_for, JsonSchema};\n\n#[derive(JsonSchema)]\npub struct MyStruct {\n    pub my_int: i32,\n    pub my_bool: bool,\n    pub my_nullable_enum: Option\u003cMyEnum\u003e,\n}\n\n#[derive(JsonSchema)]\npub enum MyEnum {\n    StringNewType(String),\n    StructVariant { floats: Vec\u003cf32\u003e },\n}\n\nlet schema = schema_for!(MyStruct);\nprintln!(\"{}\", serde_json::to_string_pretty(\u0026schema).unwrap());\n```\n\n\u003cdetails\u003e\n\u003csummary\u003eClick to see the output JSON schema...\u003c/summary\u003e\n\n```json\n{\n  \"$schema\": \"https://json-schema.org/draft/2020-12/schema\",\n  \"title\": \"MyStruct\",\n  \"type\": \"object\",\n  \"properties\": {\n    \"my_bool\": {\n      \"type\": \"boolean\"\n    },\n    \"my_int\": {\n      \"type\": \"integer\",\n      \"format\": \"int32\"\n    },\n    \"my_nullable_enum\": {\n      \"anyOf\": [\n        {\n          \"$ref\": \"#/$defs/MyEnum\"\n        },\n        {\n          \"type\": \"null\"\n        }\n      ]\n    }\n  },\n  \"required\": [\"my_int\", \"my_bool\"],\n  \"$defs\": {\n    \"MyEnum\": {\n      \"oneOf\": [\n        {\n          \"type\": \"object\",\n          \"properties\": {\n            \"StringNewType\": {\n              \"type\": \"string\"\n            }\n          },\n          \"additionalProperties\": false,\n          \"required\": [\"StringNewType\"]\n        },\n        {\n          \"type\": \"object\",\n          \"properties\": {\n            \"StructVariant\": {\n              \"type\": \"object\",\n              \"properties\": {\n                \"floats\": {\n                  \"type\": \"array\",\n                  \"items\": {\n                    \"type\": \"number\",\n                    \"format\": \"float\"\n                  }\n                }\n              },\n              \"required\": [\"floats\"]\n            }\n          },\n          \"additionalProperties\": false,\n          \"required\": [\"StructVariant\"]\n        }\n      ]\n    }\n  }\n}\n```\n\n\u003c/details\u003e\n\n### Serde Compatibility\n\nOne of the main aims of this library is compatibility with [Serde](https://github.com/serde-rs/serde). Any generated schema _should_ match how [serde_json](https://github.com/serde-rs/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.\n\n```rust\nuse schemars::{schema_for, JsonSchema};\nuse serde::{Deserialize, Serialize};\n\n#[derive(Deserialize, Serialize, JsonSchema)]\n#[serde(rename_all = \"camelCase\", deny_unknown_fields)]\npub struct MyStruct {\n    #[serde(rename = \"myNumber\")]\n    pub my_int: i32,\n    pub my_bool: bool,\n    #[serde(default)]\n    pub my_nullable_enum: Option\u003cMyEnum\u003e,\n}\n\n#[derive(Deserialize, Serialize, JsonSchema)]\n#[serde(untagged)]\npub enum MyEnum {\n    StringNewType(String),\n    StructVariant { floats: Vec\u003cf32\u003e },\n}\n\nlet schema = schema_for!(MyStruct);\nprintln!(\"{}\", serde_json::to_string_pretty(\u0026schema).unwrap());\n```\n\n\u003cdetails\u003e\n\u003csummary\u003eClick to see the output JSON schema...\u003c/summary\u003e\n\n```json\n{\n  \"$schema\": \"https://json-schema.org/draft/2020-12/schema\",\n  \"title\": \"MyStruct\",\n  \"type\": \"object\",\n  \"properties\": {\n    \"myBool\": {\n      \"type\": \"boolean\"\n    },\n    \"myNullableEnum\": {\n      \"anyOf\": [\n        {\n          \"$ref\": \"#/$defs/MyEnum\"\n        },\n        {\n          \"type\": \"null\"\n        }\n      ],\n      \"default\": null\n    },\n    \"myNumber\": {\n      \"type\": \"integer\",\n      \"format\": \"int32\"\n    }\n  },\n  \"additionalProperties\": false,\n  \"required\": [\"myNumber\", \"myBool\"],\n  \"$defs\": {\n    \"MyEnum\": {\n      \"anyOf\": [\n        {\n          \"type\": \"string\"\n        },\n        {\n          \"type\": \"object\",\n          \"properties\": {\n            \"floats\": {\n              \"type\": \"array\",\n              \"items\": {\n                \"type\": \"number\",\n                \"format\": \"float\"\n              }\n            }\n          },\n          \"required\": [\"floats\"]\n        }\n      ]\n    }\n  }\n}\n```\n\n\u003c/details\u003e\n\n`#[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.\n\n### Schema from Example Value\n\nIf you want a schema for a type that can't/doesn't implement `JsonSchema`, but does implement `serde::Serialize`, then you can generate a JSON schema from a value of that type. However, this schema will generally be less precise than if the type implemented `JsonSchema` - particularly when it involves enums, since schemars will not make any assumptions about the structure of an enum based on a single variant.\n\n```rust\nuse schemars::schema_for_value;\nuse serde::Serialize;\n\n#[derive(Serialize)]\npub struct MyStruct {\n    pub my_int: i32,\n    pub my_bool: bool,\n    pub my_nullable_enum: Option\u003cMyEnum\u003e,\n}\n\n#[derive(Serialize)]\npub enum MyEnum {\n    StringNewType(String),\n    StructVariant { floats: Vec\u003cf32\u003e },\n}\n\nlet schema = schema_for_value!(MyStruct {\n    my_int: 123,\n    my_bool: true,\n    my_nullable_enum: Some(MyEnum::StringNewType(\"foo\".to_string()))\n});\nprintln!(\"{}\", serde_json::to_string_pretty(\u0026schema).unwrap());\n```\n\n\u003cdetails\u003e\n\u003csummary\u003eClick to see the output JSON schema...\u003c/summary\u003e\n\n```json\n{\n  \"$schema\": \"http://json-schema.org/draft-07/schema#\",\n  \"title\": \"MyStruct\",\n  \"examples\": [\n    {\n      \"my_bool\": true,\n      \"my_int\": 123,\n      \"my_nullable_enum\": {\n        \"StringNewType\": \"foo\"\n      }\n    }\n  ],\n  \"type\": \"object\",\n  \"properties\": {\n    \"my_bool\": {\n      \"type\": \"boolean\"\n    },\n    \"my_int\": {\n      \"type\": \"integer\"\n    },\n    \"my_nullable_enum\": true\n  }\n}\n```\n\n\u003c/details\u003e\n\n## Feature Flags\n\n- `std` (enabled by default) - implements `JsonSchema` for types in the rust standard library (`JsonSchema` is still implemented on types in `core` and `alloc`, even when this feature is disabled). Disable this feature to use schemars in `no_std` environments.\n- `derive` (enabled by default) - provides `#[derive(JsonSchema)]` macro\n- `preserve_order` - keep the order of struct fields in `Schema` properties\n- `raw_value` - implements `JsonSchema` for `serde_json::value::RawValue` (enables the serde_json `raw_value` feature)\n\nSchemars can implement `JsonSchema` on types from several popular crates, enabled via feature flags (dependency versions are shown in brackets):\n\n- `arrayvec07` - [arrayvec](https://crates.io/crates/arrayvec) (^0.7)\n- `bigdecimal04` - [bigdecimal](https://crates.io/crates/bigdecimal) (^0.4)\n- `bytes1` - [bytes](https://crates.io/crates/bytes) (^1.0)\n- `chrono04` - [chrono](https://crates.io/crates/chrono) (^0.4)\n- `either1` - [either](https://crates.io/crates/either) (^1.3)\n- `indexmap2` - [indexmap](https://crates.io/crates/indexmap) (^2.0)\n- `rust_decimal1` - [rust_decimal](https://crates.io/crates/rust_decimal) (^1.0)\n- `semver1` - [semver](https://crates.io/crates/semver) (^1.0.9)\n- `smallvec1` - [smallvec](https://crates.io/crates/smallvec) (^1.0)\n- `smol_str02` - [smol_str](https://crates.io/crates/smol_str) (^0.2.1)\n- `url2` - [url](https://crates.io/crates/url) (^2.0)\n- `uuid1` - [uuid](https://crates.io/crates/uuid) (^1.0)\n\nFor example, to implement `JsonSchema` on types from `chrono`, enable it as a feature in the `schemars` dependency in your `Cargo.toml` like so:\n\n```toml\n[dependencies]\nschemars = { version = \"1.0.0-alpha.17\", features = [\"chrono04\"] }\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgresau%2Fschemars","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgresau%2Fschemars","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgresau%2Fschemars/lists"}