Skip to content

Commit

Permalink
Implement JsonSchema on Uuid
Browse files Browse the repository at this point in the history
  • Loading branch information
GREsau committed Dec 27, 2019
1 parent 547f81f commit 008d70e
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## next
### Added:
- Implemented `JsonSchema` on types from `indexmap` and `either` (as optional dependencies)
- Implemented `JsonSchema` on types from `indexmap`, `either` and `uuid` (as optional dependencies)
### Changed
- Remove trait bounds from Map/Set JsonSchema impls. They are unnecessary as we never create/use any instances of these types.

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,5 +210,6 @@ fn main() {
- `chrono` - implements `JsonSchema` for all [Chrono](https://github.com/chronotope/chrono) types which are serializable by Serde.
- `indexmap` - implements `JsonSchema` on `IndexMap` and `IndexSet` from [indexmap](https://github.com/bluss/indexmap).
- `either` - implements `JsonSchema` on [`Either`](https://github.com/bluss/either).
- `uuid` - implements `JsonSchema` on [`Uuid`](https://github.com/uuid-rs/uuid).
- `impl_json_schema` - implements `JsonSchema` for Schemars types themselves

3 changes: 3 additions & 0 deletions docs/4-features.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,7 @@ Implements `JsonSchema` on `IndexMap` and `IndexSet` from [indexmap](https://git
### either
Implements `JsonSchema` on [`Either`](https://github.com/bluss/either).

### uuid
Implements `JsonSchema` on [`Uuid`](https://github.com/uuid-rs/uuid).

</div>
7 changes: 6 additions & 1 deletion schemars/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ serde_json = "1.0"

chrono = { version = "0.4", default-features = false, optional = true }
indexmap = { version = "1.2", optional = true }
either = { version = "1.3", optional = true }
either = { version = "1.3", default-features = false, optional = true }
uuid = { version = "0.8", default-features = false, optional = true }

[dev-dependencies]
pretty_assertions = "0.6.1"
Expand All @@ -41,6 +42,10 @@ required-features = ["indexmap"]
name = "either"
required-features = ["either"]

[[test]]
name = "uuid"
required-features = ["uuid"]

[[test]]
name = "schema_for_schema"
required-features = ["impl_json_schema"]
Expand Down
2 changes: 2 additions & 0 deletions schemars/src/json_schema_impls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ mod chrono;
mod indexmap;
#[cfg(feature = "either")]
mod either;
#[cfg(feature = "uuid")]
mod uuid;
mod core;
mod ffi;
mod maps;
Expand Down
21 changes: 21 additions & 0 deletions schemars/src/json_schema_impls/uuid.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use crate::gen::SchemaGenerator;
use crate::schema::*;
use crate::JsonSchema;
use uuid::Uuid;

impl JsonSchema for Uuid {
no_ref_schema!();

fn schema_name() -> String {
"Uuid".to_string()
}

fn json_schema(_: &mut SchemaGenerator) -> Schema {
SchemaObject {
instance_type: Some(InstanceType::String.into()),
format: Some("uuid".to_string()),
..Default::default()
}
.into()
}
}
6 changes: 6 additions & 0 deletions schemars/tests/expected/uuid.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Uuid",
"type": "string",
"format": "uuid"
}
8 changes: 8 additions & 0 deletions schemars/tests/uuid.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
mod util;
use uuid::Uuid;
use util::*;

#[test]
fn uuid() -> TestResult {
test_default_generated_schema::<Uuid>("uuid")
}

0 comments on commit 008d70e

Please sign in to comment.