簡體   English   中英

如何使用Rust中的現有模式文件驗證JSON?

[英]How do I validate JSON using an existing schema file in Rust?

我正在嘗試在給定JSON文件和架構的情況下驗證JSON。

架構:

{
    "Address":{
        "properties":{
            "City":{
                "type":"string"
            },
            "Country":{
                "type":"string"
            },
            "Street":{
                "type":"string"
            }
        },
        "type":"object"
    }
}

JSON:

{
    "Address":{
        "Street":"Downing Street 10",
        "City":"London",
        "Country":"Great Britain"
    }
}

我的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())

}

我正在嘗試使用valico進行JSON驗證,但我無法弄清楚如何傳遞必須驗證JSON的模式。 我已經看到了使用valico構建器構建JsonDsl的示例,但是如果我已經有一個JSON模式並且我想對此進行驗證,我該怎么做呢? 有沒有其他方法可以實現這一目標?

遲到這里的派對,但是如果其他人遇到這個問題,下面是一個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())
}

運行此打印Is valid: true 將“地址”更改為“地址”並再次運行它將打印Is valid: false

請注意,我必須對您的架構進行一些小的調整。 首先,為了驗證valico正確驗證它我設置了必填字段。 其次,由於根對象沒有名稱(它只是{} ),因此“地址”將是該根對象的屬性。

而不是

{
    "Address": {
        "properties": {
            ...

相反

{
    "type": "object",
    "properties": {
        "Address": {
            "type": "object",
                ....

此外,似乎valico需要舊版本的serde_json,所以我在Cargo.toml中將其添加為依賴項

serde_json = "0.8"

我想你可以試試這個:

use valico::json_dsl;
use serde_json::{from_str, to_string_pretty}

...

let params = json_dsl::Builder::build(|params| {
    // code here
});

let mut obj = from_str(&jsonSchemaString).unwrap();

let state = params.process(&mut obj, &None);

而不是使用類型值。 這應該工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM