Skip to content

Instantly share code, notes, and snippets.

@hugoledoux
Last active June 26, 2019 08:58
Show Gist options
  • Save hugoledoux/70f39ee7d602d58f746ce2fe8bdc4871 to your computer and use it in GitHub Desktop.
Save hugoledoux/70f39ee7d602d58f746ce2fe8bdc4871 to your computer and use it in GitHub Desktop.
json schema validation Rust valico
[package]
name = "testvalico"
version = "0.1.0"
authors = ["Hugo Ledoux <[email protected]>"]
edition = "2018"
[dependencies]
valico = "3.0"
serde = "1.0"
serde_json = "1.0"
extern crate serde_json;
extern crate valico;
// use serde_json::from_str;
use valico::json_schema;
use std::fs;
fn main() {
let jsstr =
fs::read_to_string("/Users/hugo/projects/cityjson/schemas/cityjson.min.schema.json")
.expect("Unable to read file");
let js: serde_json::Value = serde_json::from_str(&jsstr).expect("JSON was not well-formatted");
let jstr = fs::read_to_string("/Users/hugo/temp/example2.json").expect("Unable to read file");
let j: serde_json::Value = serde_json::from_str(&jstr).expect("JSON was not well-formatted");
let mut scope = json_schema::Scope::new();
let r_schema = scope.compile_and_return(js, true).ok().unwrap();
let re = r_schema.validate(&j);
println!("Is valid: {}", re.is_valid());
// println!("{:?}", r_schema.validate(&j));
println!("{}", re.errors.len());
for each in re.errors.iter() {
println!("{:?}", each);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment