Extensible framework for validating JSON structures.
```toml
[dependencies] json-model = "0.1" serde_json = "1" ```
```rust // main.rs
extern crate json_model;
extern crate serde_json;
use json_model::{Error, Validator};
fn document() -> serde_json::Value { json!({ "id": "model", "definitions": [ { "id": "user", "type": "object", "properties": { "required": ["username", "password"], "definitions": [ { "id": "username", "type": "string", "minLength": 3, "maxLength": 30, }, { "id": "password", "type": "string", "minLength": 5, "maxLength": 256, }, { "id": "age", "type": "integer", "exclusiveMin": 0, "max": 273, } ] } } ] }) }
fn validinput() -> serdejson::Value { json!({ "username": "johndoe", "password": "Tr0ub4dor&3", "age": 12, }) }
fn invalidinput() -> serdejson::Value {
json!({
"username": "johndoe",
"password": "123", // password
must be at least 5 characters long
})
}
fn build() -> Result
fn main() { let v = build().unwrap(); assert!(v.validatejson("model/user", &validinput()).isok()); assert!(v.validatejson("model/user", &invalidinput()).iserr()); } ```
MIT