add support for semver crate (#238)
--------- Co-authored-by: Omar Tawfik <15987992+OmarTawfik@users.noreply.github.com>
This commit is contained in:
parent
1ac9d19a24
commit
0303f0334e
8 changed files with 65 additions and 0 deletions
|
@ -2,6 +2,10 @@
|
||||||
|
|
||||||
## [0.8.13] - _in-dev_
|
## [0.8.13] - _in-dev_
|
||||||
|
|
||||||
|
### Added:
|
||||||
|
|
||||||
|
- Implement `JsonSchema` for `semver::Version` (https://github.com/GREsau/schemars/pull/195 / https://github.com/GREsau/schemars/pull/238)
|
||||||
|
|
||||||
### Changed:
|
### Changed:
|
||||||
|
|
||||||
- Minimum supported rust version is now 1.56.0
|
- Minimum supported rust version is now 1.56.0
|
||||||
|
|
|
@ -270,6 +270,7 @@ Schemars can implement `JsonSchema` on types from several popular crates, enable
|
||||||
- `rust_decimal` - [rust_decimal](https://crates.io/crates/rust_decimal) (^1.0)
|
- `rust_decimal` - [rust_decimal](https://crates.io/crates/rust_decimal) (^1.0)
|
||||||
- `bigdecimal` - [bigdecimal](https://crates.io/crates/bigdecimal) (^0.3)
|
- `bigdecimal` - [bigdecimal](https://crates.io/crates/bigdecimal) (^0.3)
|
||||||
- `smol_str` - [smol_str](https://crates.io/crates/smol_str) (^0.1.17)
|
- `smol_str` - [smol_str](https://crates.io/crates/smol_str) (^0.1.17)
|
||||||
|
- `semver` - [semver](https://crates.io/crates/semver) (^1.0.9)
|
||||||
|
|
||||||
For example, to implement `JsonSchema` on types from `chrono`, enable it as a feature in the `schemars` dependency in your `Cargo.toml` like so:
|
For example, to implement `JsonSchema` on types from `chrono`, enable it as a feature in the `schemars` dependency in your `Cargo.toml` like so:
|
||||||
|
|
||||||
|
|
|
@ -25,6 +25,7 @@ Schemars can implement `JsonSchema` on types from several popular crates, enable
|
||||||
- `rust_decimal` - [rust_decimal](https://crates.io/crates/rust_decimal) (^1.0)
|
- `rust_decimal` - [rust_decimal](https://crates.io/crates/rust_decimal) (^1.0)
|
||||||
- `bigdecimal` - [bigdecimal](https://crates.io/crates/bigdecimal) (^0.3)
|
- `bigdecimal` - [bigdecimal](https://crates.io/crates/bigdecimal) (^0.3)
|
||||||
- `smol_str` - [smol_str](https://crates.io/crates/smol_str) (^0.1.17)
|
- `smol_str` - [smol_str](https://crates.io/crates/smol_str) (^0.1.17)
|
||||||
|
- `semver` - [semver](https://crates.io/crates/semver) (^1.0.9)
|
||||||
|
|
||||||
For example, to implement `JsonSchema` on types from `chrono`, enable it as a feature in the `schemars` dependency in your `Cargo.toml` like so:
|
For example, to implement `JsonSchema` on types from `chrono`, enable it as a feature in the `schemars` dependency in your `Cargo.toml` like so:
|
||||||
|
|
||||||
|
|
|
@ -33,6 +33,7 @@ rust_decimal = { version = "1", default-features = false, optional = true }
|
||||||
bigdecimal = { version = "0.3", default-features = false, optional = true }
|
bigdecimal = { version = "0.3", default-features = false, optional = true }
|
||||||
enumset = { version = "1.0", optional = true }
|
enumset = { version = "1.0", optional = true }
|
||||||
smol_str = { version = "0.1.17", optional = true }
|
smol_str = { version = "0.1.17", optional = true }
|
||||||
|
semver = { version = "1.0.9", features = ["serde"], optional = true }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
pretty_assertions = "1.2.1"
|
pretty_assertions = "1.2.1"
|
||||||
|
@ -108,5 +109,9 @@ required-features = ["enumset"]
|
||||||
name = "smol_str"
|
name = "smol_str"
|
||||||
required-features = ["smol_str"]
|
required-features = ["smol_str"]
|
||||||
|
|
||||||
|
[[test]]
|
||||||
|
name = "semver"
|
||||||
|
required-features = ["semver"]
|
||||||
|
|
||||||
[package.metadata.docs.rs]
|
[package.metadata.docs.rs]
|
||||||
all-features = true
|
all-features = true
|
||||||
|
|
|
@ -60,6 +60,8 @@ mod maps;
|
||||||
mod nonzero_signed;
|
mod nonzero_signed;
|
||||||
mod nonzero_unsigned;
|
mod nonzero_unsigned;
|
||||||
mod primitives;
|
mod primitives;
|
||||||
|
#[cfg(feature = "semver")]
|
||||||
|
mod semver;
|
||||||
mod sequences;
|
mod sequences;
|
||||||
mod serdejson;
|
mod serdejson;
|
||||||
#[cfg(feature = "smallvec")]
|
#[cfg(feature = "smallvec")]
|
||||||
|
|
25
schemars/src/json_schema_impls/semver.rs
Normal file
25
schemars/src/json_schema_impls/semver.rs
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
use crate::gen::SchemaGenerator;
|
||||||
|
use crate::schema::*;
|
||||||
|
use crate::JsonSchema;
|
||||||
|
use semver::Version;
|
||||||
|
|
||||||
|
impl JsonSchema for Version {
|
||||||
|
no_ref_schema!();
|
||||||
|
|
||||||
|
fn schema_name() -> String {
|
||||||
|
"Version".to_owned()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn json_schema(_: &mut SchemaGenerator) -> Schema {
|
||||||
|
SchemaObject {
|
||||||
|
instance_type: Some(InstanceType::String.into()),
|
||||||
|
string: Some(Box::new(StringValidation {
|
||||||
|
// https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string
|
||||||
|
pattern: Some(r"^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$".to_owned()),
|
||||||
|
..Default::default()
|
||||||
|
})),
|
||||||
|
..Default::default()
|
||||||
|
}
|
||||||
|
.into()
|
||||||
|
}
|
||||||
|
}
|
12
schemars/tests/expected/semver.json
Normal file
12
schemars/tests/expected/semver.json
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
{
|
||||||
|
"$schema": "http://json-schema.org/draft-07/schema#",
|
||||||
|
"title": "SemverTypes",
|
||||||
|
"type": "object",
|
||||||
|
"required": ["version"],
|
||||||
|
"properties": {
|
||||||
|
"version": {
|
||||||
|
"type": "string",
|
||||||
|
"pattern": "^(0|[1-9]\\d*)\\.(0|[1-9]\\d*)\\.(0|[1-9]\\d*)(?:-((?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\\.(?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\\+([0-9a-zA-Z-]+(?:\\.[0-9a-zA-Z-]+)*))?$"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
15
schemars/tests/semver.rs
Normal file
15
schemars/tests/semver.rs
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
mod util;
|
||||||
|
use schemars::JsonSchema;
|
||||||
|
use semver::Version;
|
||||||
|
use util::*;
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
#[derive(JsonSchema)]
|
||||||
|
struct SemverTypes {
|
||||||
|
version: Version,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn semver_types() -> TestResult {
|
||||||
|
test_default_generated_schema::<SemverTypes>("semver")
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue