如何使用Rust中的现有模式文件验证JSON?

leo*_*ion 5 validation json rust

我正在尝试在给定JSON文件和架构的情况下验证JSON.

架构:

{
    "Address":{
        "properties":{
            "City":{
                "type":"string"
            },
            "Country":{
                "type":"string"
            },
            "Street":{
                "type":"string"
            }
        },
        "type":"object"
    }
}
Run Code Online (Sandbox Code Playgroud)

JSON:

{
    "Address":{
        "Street":"Downing Street 10",
        "City":"London",
        "Country":"Great Britain"
    }
}
Run Code Online (Sandbox Code Playgroud)

我的Rust文件:

#[macro_use]
extern crate serde_derive;
extern crate serde;
extern crate serde_json;
extern crate valico;

use std::fs::File;
use std::io::Read;
use serde_json::Value;

use valico::json_dsl;
use valico::json_schema;

fn main() {
    let mut schemaFile = File::open("src/schema.json").unwrap();
    let mut jsonSchemaString = String::new();
    schemaFile.read_to_string(&mut jsonSchemaString).unwrap();

    let json_v4_schema: Value = serde_json::from_str(&jsonSchemaString).unwrap();


    let state = jsonSchemaString.process(&mut json_v4_schema, &None); //this is wrong as jsonSchemaString is not a jsonDsl.

    println!("Is valid: {}", state.is_valid())

}
Run Code Online (Sandbox Code Playgroud)

我正在尝试使用valico进行JSON验证,但我无法弄清楚如何传递必须验证JSON的模式.我已经看到了JsonDsl使用valico构建器构建a的示例,但是如果我已经有一个JSON模式并且我想对此进行验证,我该怎么做呢?有没有其他方法可以实现这一目标?

ble*_*fly 6

迟到了,但如果其他人遇到这个问题,下面是一个 MVCE,我已经调整为使用您用作示例的模式和数据。为了“简单”,我将字符串直接包含在代码中,但您可以将其替换为您已有的fs::File/io::Read操作。

extern crate serde_json;
extern crate valico;

use serde_json::from_str;
use valico::json_schema;

fn main() {
    let s = from_str(r#"
    {
        "Address": {
            "Street":"Downing Street 10",
            "City":"London",
            "Country":"Great Britain"
        }
    }
    "#).unwrap();

    let j_schema = from_str(r#"
    {
        "type": "object",
        "properties": {
            "Address": {
                "type": "object",
                "properties": {
                    "Country": {
                        "type": "string"
                    },
                    "Street": {
                        "type": "string"
                    }
                },
                "required": ["Country", "Street"]
            }
        },
        "required": ["Address"]
    }
    "#).unwrap();

    let mut scope = json_schema::Scope::new();
    let r_schema = scope.compile_and_return(j_schema, true).ok().unwrap();

    println!("Is valid: {}", r_schema.validate(&s).is_valid())
}
Run Code Online (Sandbox Code Playgroud)

运行这个打印Is valid: true。将“地址”更改为“地址”并再次运行打印Is valid: false

请注意,我必须对您的架构进行一些小的调整。首先,为了验证 valico 是否正确验证了它,我设置了必填字段。其次,由于根对象没有名称(只有名称{}),因此“地址”将是该根对象的属性。

所以代替

{
    "Address": {
        "properties": {
            ...
Run Code Online (Sandbox Code Playgroud)

相反

{
    "type": "object",
    "properties": {
        "Address": {
            "type": "object",
                ....
Run Code Online (Sandbox Code Playgroud)

此外,似乎 valico 需要旧版本的 serde_json,因此我将其添加为 Cargo.toml 中的依赖项

serde_json = "0.8"
Run Code Online (Sandbox Code Playgroud)