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

时间:2017-06-24 06:39:49

标签: json validation 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模式并且我想对此进行验证,我该怎么做呢?有没有其他方法可以实现这一目标?

2 个答案:

答案 0 :(得分:2)

Late to the party here, but in case anyone else is having trouble with this, below is a MVCE I've adjusted to using the schema and data you used as an example. I included the strings directly in the code for "simplicity", but you can just replace that with fs::File/io::Read operations you already had.

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())
}

Running this prints Is valid: true. Changing the "Address" to "Addresses" and running it again prints Is valid: false

Please note that I had to make some small adjustments to your schema. First, to verify that valico is validating it correctly I set required fields. Secondly, since the root object doesn't have a name (it's only {}) the "Address" would be a property of that root object.

So instead of

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

it is instead

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

Also, it seems like valico requires an older version of serde_json, so I added this as dependency in my Cargo.toml

serde_json = "0.8"

答案 1 :(得分:0)

我想你可以试试这个:

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);

而不是使用类型值。这应该有用。