|
| 1 | +# jtd: JSON Typedef for Rust   |
| 2 | + |
| 3 | +[JSON Type Definition](https://jsontypedef.com), aka |
| 4 | +[RFC8927](https://tools.ietf.org/html/rfc8927), is an easy-to-learn, |
| 5 | +standardized way to define a schema for JSON data. You can use JSON Typedef to |
| 6 | +portably validate data across programming languages, create dummy data, generate |
| 7 | +code, and more. |
| 8 | + |
| 9 | +`jtd` is a Rust implementation of JSON Typedef. You can use this crate to parse |
| 10 | +JSON Typedef schemas, validate JSON data against those schemas, or build your |
| 11 | +own tooling on top of JSON Typedef. |
| 12 | + |
| 13 | +Here's an example of this crate in action: |
| 14 | + |
| 15 | +```rust |
| 16 | +use jtd::{Schema, ValidationErrorIndicator}; |
| 17 | +use serde_json::json; |
| 18 | + |
| 19 | +let schema = Schema::from_serde_schema( |
| 20 | + serde_json::from_value(json!({ |
| 21 | + "properties": { |
| 22 | + "name": { "type": "string" }, |
| 23 | + "age": { "type": "uint32" }, |
| 24 | + "phones": { |
| 25 | + "elements": { |
| 26 | + "type": "string" |
| 27 | + } |
| 28 | + } |
| 29 | + } |
| 30 | + })).unwrap()).unwrap(); |
| 31 | + |
| 32 | +// Since this first example is valid, we'll get back an empty list of |
| 33 | +// validation errors. |
| 34 | +let input_ok = json!({ |
| 35 | + "name": "John Doe", |
| 36 | + "age": 43, |
| 37 | + "phones": ["+44 1234567", "+44 2345678"] |
| 38 | +}); |
| 39 | + |
| 40 | +assert_eq!( |
| 41 | + Vec::<ValidationErrorIndicator>::new(), |
| 42 | + jtd::validate(&schema, &input_ok, Default::default()).unwrap(), |
| 43 | +); |
| 44 | + |
| 45 | +// This example is invalid, so we'll get back three validation errors: |
| 46 | +// |
| 47 | +// 1. "name" is required but not present, |
| 48 | +// 2. "age" has the wrong type |
| 49 | +// 3. "phones[1]" has the wrong type |
| 50 | +let input_bad = json!({ |
| 51 | + "age": "43", |
| 52 | + "phones": ["+44 1234567", 442345678] |
| 53 | +}); |
| 54 | + |
| 55 | +// Each error indicator has two pieces of information: the path to the part |
| 56 | +// of the input that was rejected (the "instance path"), and the part of the |
| 57 | +// schema that rejected it (the "schema path"). |
| 58 | +// |
| 59 | +// The exact values of the instance path and schema path is specified in the |
| 60 | +// JSON Type Definition spec. |
| 61 | +assert_eq!( |
| 62 | + vec![ |
| 63 | + // "age" has the wrong type (required by "/properties/age/type") |
| 64 | + ValidationErrorIndicator { |
| 65 | + instance_path: vec!["age".into()], |
| 66 | + schema_path: vec!["properties".into(), "age".into(), "type".into()], |
| 67 | + }, |
| 68 | + |
| 69 | + // "name" is missing (required by "/properties/name") |
| 70 | + ValidationErrorIndicator { |
| 71 | + instance_path: vec![], |
| 72 | + schema_path: vec!["properties".into(), "name".into()], |
| 73 | + }, |
| 74 | + |
| 75 | + // "phones/1" has the wrong type (required by "/properties/phones/elements/type") |
| 76 | + ValidationErrorIndicator { |
| 77 | + instance_path: vec!["phones".into(), "1".into()], |
| 78 | + schema_path: vec![ |
| 79 | + "properties".into(), |
| 80 | + "phones".into(), |
| 81 | + "elements".into(), |
| 82 | + "type".into() |
| 83 | + ], |
| 84 | + }, |
| 85 | + ], |
| 86 | + jtd::validate(&schema, &input_bad, Default::default()).unwrap(), |
| 87 | +); |
| 88 | +``` |
| 89 | + |
| 90 | +## What is JSON Type Definition? |
| 91 | + |
| 92 | +[JSON Type Definition](https://jsontypedef.com) is a schema format for JSON |
| 93 | +data. A JSON Type Definition schema describes what is and isn't a "valid" JSON |
| 94 | +document. JSON Type Definition is easy to learn, portable (there are |
| 95 | +functionally-identical implementations across many programming languages) and |
| 96 | +standardized (the spec is set in stone as [IETF RFC |
| 97 | +8927](https://tools.ietf.org/html/rfc8927)). |
| 98 | + |
| 99 | +Here's an example of a JSON Type Definition schema: |
| 100 | + |
| 101 | +```json |
| 102 | +{ |
| 103 | + "properties": { |
| 104 | + "name": { |
| 105 | + "type": "string" |
| 106 | + }, |
| 107 | + "isAdmin": { |
| 108 | + "type": "boolean" |
| 109 | + } |
| 110 | + } |
| 111 | +} |
| 112 | +``` |
| 113 | + |
| 114 | +This schema considers any object with a `name` property (whose value must be a |
| 115 | +string), an `isAdmin` property (whose value must a boolean), and no other |
| 116 | +properties, to be valid. |
| 117 | + |
| 118 | +To learn more about JSON Type Definition, [check out the online documentation at |
| 119 | +jsontypedef.com](https://jsontypedef.com). |
| 120 | + |
| 121 | +## Installation |
| 122 | + |
| 123 | +Install this crate by adding the following to your `Cargo.toml`: |
| 124 | + |
| 125 | +```toml |
| 126 | +jtd = "0.3" |
| 127 | +``` |
| 128 | + |
| 129 | +## Usage |
| 130 | + |
| 131 | +For detailed documentation on how to use this crate, consult [the full API |
| 132 | +documentation on docs.rs](https://docs.rs/jtd). |
0 commit comments