#json-schema #schema-validation #validate-json #parse-json #data #format-json #serde-json

jtd

JSON 类型定义的 Rust 实现

9 个版本

0.3.1 2021 年 1 月 31 日
0.3.0 2021 年 1 月 22 日
0.2.1 2020 年 10 月 16 日
0.1.4 2020 年 4 月 12 日
0.1.1 2020 年 3 月 22 日

#596数据库接口

Download history 80/week @ 2024-03-11 22/week @ 2024-03-18 50/week @ 2024-03-25 98/week @ 2024-04-01 89/week @ 2024-04-08 88/week @ 2024-04-15 78/week @ 2024-04-22 67/week @ 2024-04-29 50/week @ 2024-05-06 71/week @ 2024-05-13 94/week @ 2024-05-20 74/week @ 2024-05-27 238/week @ 2024-06-03 168/week @ 2024-06-10 78/week @ 2024-06-17 96/week @ 2024-06-24

585 每月下载量
3 crates 中使用

MIT 许可证

78KB
1K SLoC

jtd: Rust 的 JSON 类型定义 Crates.io Docs.rs

JSON 类型定义(又称 RFC8927),是一种易于学习、标准化的方式,用于定义 JSON 数据的架构。您可以使用 JSON 类型定义在不同的编程语言中便携地验证数据、创建占位符数据、生成代码等。

jtd 是 JSON 类型定义的 Rust 实现。您可以使用这个 crate 解析 JSON 类型定义架构、验证 JSON 数据与这些架构,或在此之上构建自己的工具。

以下是一个使用此 crate 的示例

use jtd::{Schema, ValidationErrorIndicator};
use serde_json::json;

let schema = Schema::from_serde_schema(
    serde_json::from_value(json!({
        "properties": {
            "name": { "type": "string" },
            "age": { "type": "uint32" },
            "phones": {
                "elements": {
                    "type": "string"
                }
            }
        }
    })).unwrap()).unwrap();

// Since this first example is valid, we'll get back an empty list of
// validation errors.
let input_ok = json!({
    "name": "John Doe",
    "age": 43,
    "phones": ["+44 1234567", "+44 2345678"]
});

assert_eq!(
    Vec::<ValidationErrorIndicator>::new(),
    jtd::validate(&schema, &input_ok, Default::default()).unwrap(),
);

// This example is invalid, so we'll get back three validation errors:
//
// 1. "name" is required but not present,
// 2. "age" has the wrong type
// 3. "phones[1]" has the wrong type
let input_bad = json!({
    "age": "43",
    "phones": ["+44 1234567", 442345678]
});

// Each error indicator has two pieces of information: the path to the part
// of the input that was rejected (the "instance path"), and the part of the
// schema that rejected it (the "schema path").
//
// The exact values of the instance path and schema path is specified in the
// JSON Type Definition spec.
assert_eq!(
    vec![
        // "age" has the wrong type (required by "/properties/age/type")
        ValidationErrorIndicator {
            instance_path: vec!["age".into()],
            schema_path: vec!["properties".into(), "age".into(), "type".into()],
        },

        // "name" is missing (required by "/properties/name")
        ValidationErrorIndicator {
            instance_path: vec![],
            schema_path: vec!["properties".into(), "name".into()],
        },

        // "phones/1" has the wrong type (required by "/properties/phones/elements/type")
        ValidationErrorIndicator {
            instance_path: vec!["phones".into(), "1".into()],
            schema_path: vec![
                "properties".into(),
                "phones".into(),
                "elements".into(),
                "type".into()
            ],
        },
    ],
    jtd::validate(&schema, &input_bad, Default::default()).unwrap(),
);

什么是 JSON 类型定义?

JSON 类型定义是 JSON 数据的架构格式。一个 JSON 类型定义架构描述了什么是“有效”的 JSON 文档。JSON 类型定义易于学习、可移植(在许多编程语言中都有功能上相同的实现)且标准化(规范在 IETF RFC 8927 中固定)。

以下是一个 JSON 类型定义架构的示例

{
    "properties": {
        "name": {
            "type": "string"
        },
        "isAdmin": {
            "type": "boolean"
        }
    }
}

此架构将任何具有 name 属性(其值必须为字符串)的对象、具有 isAdmin 属性(其值必须为布尔值)的对象,以及没有其他属性的对象视为有效的。

要了解更多关于 JSON 类型定义的信息,请访问 jsontypedef.com 上的在线文档

安装

通过将以下内容添加到您的 Cargo.toml 中来安装此 crate

jtd = "0.3"

用法

有关如何使用此 crate 的详细文档,请参阅 docs.rs 上的完整 API 文档

依赖项

~1.6–2.6MB
~50K SLoC