Skip to content

Commit bac1e6b

Browse files
authored
Various improvements for a new minor version (#14)
* Remove fuzzing * Begin refactor to enum-based schema representation * Use Cow, document public members * Change publish to happen on GitHub release publication * Add README * Refactor README title * Prepare for 0.3
1 parent c571e73 commit bac1e6b

14 files changed

+1848
-1900
lines changed

.github/workflows/publish.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
on:
2-
push:
3-
branches:
4-
- master
2+
release:
3+
types: [published]
4+
55
jobs:
66
publish:
77
runs-on: ubuntu-latest

Cargo.toml

+4-7
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
[package]
22
name = "jtd"
3-
version = "0.2.1"
3+
version = "0.3.0"
44
description = "A Rust implementation of JSON Type Definition"
55
authors = ["JSON Type Definition Contributors"]
66
edition = "2018"
77
license = "MIT"
88

9-
[features]
10-
fuzz = ["arbitrary"]
11-
129
[dependencies]
13-
arbitrary = { version = "0.4.0", features = ["derive"], optional = true }
1410
chrono = "0.4"
15-
serde = { version = "1.0", features = ["derive"] }
16-
serde_json = "1.0"
11+
serde = { version = "1", features = ["derive"] }
12+
serde_json = "1"
13+
thiserror = "1"

README.md

+132
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
# jtd: JSON Typedef for Rust ![Crates.io](https://img.shields.io/crates/v/jtd) ![Docs.rs](https://docs.rs/jtd/badge.svg)
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).

fuzz/.gitignore

-4
This file was deleted.

fuzz/Cargo.toml

-30
This file was deleted.

fuzz/fuzz_targets/serde_schema_try_into.rs

-7
This file was deleted.

fuzz/fuzz_targets/validate.rs

-20
This file was deleted.

src/form.rs

-134
This file was deleted.

0 commit comments

Comments
 (0)