2 个版本
0.8.1 | 2020年10月14日 |
---|---|
0.8.0 | 2020年9月30日 |
26 in #schema-validation
99KB
2K SLoC
不要使用。
lib.rs
:
从Rust代码生成JSON Schema文档
基本用法
如果你并不关心具体细节,为你的类型生成JSON Schema的最简单方法是使用 #[derive(JsonSchema)]
和 schema_for!
宏。类型的所有字段也必须实现 JsonSchema
- Schemars 为许多标准库类型实现了这个。
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> },
}
fn main() {
let schema = schema_for!(MyStruct);
println!("{}", serde_json::to_string_pretty(&schema).unwrap());
}
点击查看生成的JSON Schema...
{
"$schema": "https://json-schema.fullstack.org.cn/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 兼容性
本库的主要目标之一是与 Serde 兼容。任何生成的Schema都应与 serde_json 序列化/反序列化到/从JSON的方式相匹配。为了支持这一点,Schemars 将检查所有从 JsonSchema
继承的类型上的 #[serde(...)]
属性,并根据需要进行调整。
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> },
}
fn main() {
let schema = schema_for!(MyStruct);
println!("{}", serde_json::to_string_pretty(&schema).unwrap());
}
点击查看生成的JSON Schema...
{
"$schema": "https://json-schema.fullstack.org.cn/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"
}
}
}
}
]
}
}
}
可以使用 #[schemars(...)]
属性来覆盖 #[serde(...)]
属性,它们的行为相同(例如 #[schemars(rename_all = "camelCase")]
)。如果你想在不会影响Serde行为的情况下更改生成的Schema,或者你根本不使用Serde,这可能很有用。
功能标志
derive
(默认启用)- 提供了#[derive(JsonSchema)]
宏。impl_json_schema
- 为Schemars类型本身实现JsonSchema
。preserve_order
- 在Schema
和SchemaObject
中保留结构体字段的顺序
可选依赖项
Schemars可以在几种流行的crate类型上实现JsonSchema
,通过可选依赖项启用(依赖项版本显示在括号中)
依赖项
~0.7–2.2MB
~46K SLoC