diff --git a/schemars/src/json_schema_impls/semver.rs b/schemars/src/json_schema_impls/semver.rs index 99c5db8e..37c1d249 100644 --- a/schemars/src/json_schema_impls/semver.rs +++ b/schemars/src/json_schema_impls/semver.rs @@ -14,7 +14,8 @@ impl JsonSchema for Version { SchemaObject { instance_type: Some(InstanceType::String.into()), string: Some(Box::new(StringValidation { - pattern: Some(r"^\d+\.\d+\.\d+([\-\+].+)?$".to_owned()), + // 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() diff --git a/schemars/tests/expected/semver.json b/schemars/tests/expected/semver.json index ceee11c8..d87ad047 100644 --- a/schemars/tests/expected/semver.json +++ b/schemars/tests/expected/semver.json @@ -6,7 +6,7 @@ "properties": { "version": { "type": "string", - "pattern": "^\\d+\\.\\d+\\.\\d+([\\-\\+].+)?$" + "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-]+)*))?$" } } }