diff --git a/CHANGELOG.md b/CHANGELOG.md index aec5c3a..6ba6989 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/README.md b/README.md index c09440a..b35304e 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/docs/4-features.md b/docs/4-features.md index d5a415c..8c2d601 100644 --- a/docs/4-features.md +++ b/docs/4-features.md @@ -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). + diff --git a/schemars/Cargo.toml b/schemars/Cargo.toml index 4704737..91b4312 100644 --- a/schemars/Cargo.toml +++ b/schemars/Cargo.toml @@ -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" @@ -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"] diff --git a/schemars/src/json_schema_impls/mod.rs b/schemars/src/json_schema_impls/mod.rs index 9b5b07b..59dd121 100644 --- a/schemars/src/json_schema_impls/mod.rs +++ b/schemars/src/json_schema_impls/mod.rs @@ -40,6 +40,8 @@ mod chrono; mod indexmap; #[cfg(feature = "either")] mod either; +#[cfg(feature = "uuid")] +mod uuid; mod core; mod ffi; mod maps; diff --git a/schemars/src/json_schema_impls/uuid.rs b/schemars/src/json_schema_impls/uuid.rs new file mode 100644 index 0000000..c90a78b --- /dev/null +++ b/schemars/src/json_schema_impls/uuid.rs @@ -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() + } +} diff --git a/schemars/tests/expected/uuid.json b/schemars/tests/expected/uuid.json new file mode 100644 index 0000000..8ba1a01 --- /dev/null +++ b/schemars/tests/expected/uuid.json @@ -0,0 +1,6 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "Uuid", + "type": "string", + "format": "uuid" +} \ No newline at end of file diff --git a/schemars/tests/uuid.rs b/schemars/tests/uuid.rs new file mode 100644 index 0000000..df4d15e --- /dev/null +++ b/schemars/tests/uuid.rs @@ -0,0 +1,8 @@ +mod util; +use uuid::Uuid; +use util::*; + +#[test] +fn uuid() -> TestResult { + test_default_generated_schema::("uuid") +}