add support for semver crate (#238)

---------

Co-authored-by: Omar Tawfik <15987992+OmarTawfik@users.noreply.github.com>
This commit is contained in:
Graham Esau 2023-08-27 20:00:36 +01:00 committed by GitHub
parent 1ac9d19a24
commit 0303f0334e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 65 additions and 0 deletions

View file

@ -2,6 +2,10 @@
## [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:
- Minimum supported rust version is now 1.56.0

View file

@ -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)
- `bigdecimal` - [bigdecimal](https://crates.io/crates/bigdecimal) (^0.3)
- `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:

View file

@ -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)
- `bigdecimal` - [bigdecimal](https://crates.io/crates/bigdecimal) (^0.3)
- `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:

View file

@ -33,6 +33,7 @@ rust_decimal = { version = "1", default-features = false, optional = true }
bigdecimal = { version = "0.3", default-features = false, optional = true }
enumset = { version = "1.0", optional = true }
smol_str = { version = "0.1.17", optional = true }
semver = { version = "1.0.9", features = ["serde"], optional = true }
[dev-dependencies]
pretty_assertions = "1.2.1"
@ -108,5 +109,9 @@ required-features = ["enumset"]
name = "smol_str"
required-features = ["smol_str"]
[[test]]
name = "semver"
required-features = ["semver"]
[package.metadata.docs.rs]
all-features = true

View file

@ -60,6 +60,8 @@ mod maps;
mod nonzero_signed;
mod nonzero_unsigned;
mod primitives;
#[cfg(feature = "semver")]
mod semver;
mod sequences;
mod serdejson;
#[cfg(feature = "smallvec")]

View 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()
}
}

View 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
View 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")
}