Skip to content

Commit

Permalink
Various improvements for a new minor version (#14)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
ucarion authored Jan 22, 2021
1 parent c571e73 commit bac1e6b
Show file tree
Hide file tree
Showing 14 changed files with 1,848 additions and 1,900 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
on:
push:
branches:
- master
release:
types: [published]

jobs:
publish:
runs-on: ubuntu-latest
Expand Down
11 changes: 4 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
[package]
name = "jtd"
version = "0.2.1"
version = "0.3.0"
description = "A Rust implementation of JSON Type Definition"
authors = ["JSON Type Definition Contributors"]
edition = "2018"
license = "MIT"

[features]
fuzz = ["arbitrary"]

[dependencies]
arbitrary = { version = "0.4.0", features = ["derive"], optional = true }
chrono = "0.4"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
thiserror = "1"
132 changes: 132 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
# jtd: JSON Typedef for Rust ![Crates.io](https://img.shields.io/crates/v/jtd) ![Docs.rs](https://docs.rs/jtd/badge.svg)

[JSON Type Definition](https://jsontypedef.com), aka
[RFC8927](https://tools.ietf.org/html/rfc8927), is an easy-to-learn,
standardized way to define a schema for JSON data. You can use JSON Typedef to
portably validate data across programming languages, create dummy data, generate
code, and more.

`jtd` is a Rust implementation of JSON Typedef. You can use this crate to parse
JSON Typedef schemas, validate JSON data against those schemas, or build your
own tooling on top of JSON Typedef.

Here's an example of this crate in action:

```rust
use jtd::{Schema, ValidationErrorIndicator};
use serde_json::json;

let schema = Schema::from_serde_schema(
serde_json::from_value(json!({
"properties": {
"name": { "type": "string" },
"age": { "type": "uint32" },
"phones": {
"elements": {
"type": "string"
}
}
}
})).unwrap()).unwrap();

// Since this first example is valid, we'll get back an empty list of
// validation errors.
let input_ok = json!({
"name": "John Doe",
"age": 43,
"phones": ["+44 1234567", "+44 2345678"]
});

assert_eq!(
Vec::<ValidationErrorIndicator>::new(),
jtd::validate(&schema, &input_ok, Default::default()).unwrap(),
);

// This example is invalid, so we'll get back three validation errors:
//
// 1. "name" is required but not present,
// 2. "age" has the wrong type
// 3. "phones[1]" has the wrong type
let input_bad = json!({
"age": "43",
"phones": ["+44 1234567", 442345678]
});

// Each error indicator has two pieces of information: the path to the part
// of the input that was rejected (the "instance path"), and the part of the
// schema that rejected it (the "schema path").
//
// The exact values of the instance path and schema path is specified in the
// JSON Type Definition spec.
assert_eq!(
vec![
// "age" has the wrong type (required by "/properties/age/type")
ValidationErrorIndicator {
instance_path: vec!["age".into()],
schema_path: vec!["properties".into(), "age".into(), "type".into()],
},

// "name" is missing (required by "/properties/name")
ValidationErrorIndicator {
instance_path: vec![],
schema_path: vec!["properties".into(), "name".into()],
},

// "phones/1" has the wrong type (required by "/properties/phones/elements/type")
ValidationErrorIndicator {
instance_path: vec!["phones".into(), "1".into()],
schema_path: vec![
"properties".into(),
"phones".into(),
"elements".into(),
"type".into()
],
},
],
jtd::validate(&schema, &input_bad, Default::default()).unwrap(),
);
```

## What is JSON Type Definition?

[JSON Type Definition](https://jsontypedef.com) is a schema format for JSON
data. A JSON Type Definition schema describes what is and isn't a "valid" JSON
document. JSON Type Definition is easy to learn, portable (there are
functionally-identical implementations across many programming languages) and
standardized (the spec is set in stone as [IETF RFC
8927](https://tools.ietf.org/html/rfc8927)).

Here's an example of a JSON Type Definition schema:

```json
{
"properties": {
"name": {
"type": "string"
},
"isAdmin": {
"type": "boolean"
}
}
}
```

This schema considers any object with a `name` property (whose value must be a
string), an `isAdmin` property (whose value must a boolean), and no other
properties, to be valid.

To learn more about JSON Type Definition, [check out the online documentation at
jsontypedef.com](https://jsontypedef.com).

## Installation

Install this crate by adding the following to your `Cargo.toml`:

```toml
jtd = "0.3"
```

## Usage

For detailed documentation on how to use this crate, consult [the full API
documentation on docs.rs](https://docs.rs/jtd).
4 changes: 0 additions & 4 deletions fuzz/.gitignore

This file was deleted.

30 changes: 0 additions & 30 deletions fuzz/Cargo.toml

This file was deleted.

7 changes: 0 additions & 7 deletions fuzz/fuzz_targets/serde_schema_try_into.rs

This file was deleted.

20 changes: 0 additions & 20 deletions fuzz/fuzz_targets/validate.rs

This file was deleted.

134 changes: 0 additions & 134 deletions src/form.rs

This file was deleted.

Loading

0 comments on commit bac1e6b

Please sign in to comment.