From fc18ee258fea1468b746296d73aecaa056a3f295 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Lemaitre?= Date: Sat, 30 Dec 2023 09:50:17 +0100 Subject: [PATCH] feat: Add semantic version String constraint (#199) --- .../iltotore/iron/constraint/string.scala | 5 ++++ .../iltotore/iron/testing/StringSuite.scala | 29 +++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/main/src/io/github/iltotore/iron/constraint/string.scala b/main/src/io/github/iltotore/iron/constraint/string.scala index c3cf4eff..024f7551 100644 --- a/main/src/io/github/iltotore/iron/constraint/string.scala +++ b/main/src/io/github/iltotore/iron/constraint/string.scala @@ -80,6 +80,11 @@ object string: type ValidUUID = Match["^([0-9a-fA-F]{8}\\-[0-9a-fA-F]{4}\\-[0-9a-fA-F]{4}\\-[0-9a-fA-F]{4}\\-[0-9a-fA-F]{12})"] DescribedAs "Should be an UUID" + /** + * Tests if the input is a valid semantic version as defined in [semver site](https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string). + */ + type SemanticVersion = + Match["^v?(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-]+)*))?$"] object Blank: given (Empty ==> Blank) = Implication() diff --git a/main/test/src/io/github/iltotore/iron/testing/StringSuite.scala b/main/test/src/io/github/iltotore/iron/testing/StringSuite.scala index e31c1fc6..a8276feb 100644 --- a/main/test/src/io/github/iltotore/iron/testing/StringSuite.scala +++ b/main/test/src/io/github/iltotore/iron/testing/StringSuite.scala @@ -77,4 +77,33 @@ object StringSuite extends TestSuite: test - "http:///".assertNotRefine[ValidURL] } + test("semantic version") { + test - "1.0.0".assertRefine[SemanticVersion] + test - "1.0.0-alpha".assertRefine[SemanticVersion] + test - "1.0.0-alpha.1".assertRefine[SemanticVersion] + test - "1.0.0-0.3.7".assertRefine[SemanticVersion] + test - "1.0.0-x.7.z.92".assertRefine[SemanticVersion] + test - "1.0.0-alpha+001".assertRefine[SemanticVersion] + test - "1.0.0+20130313144700".assertRefine[SemanticVersion] + test - "1.0.0-beta+exp.sha.5114f85".assertRefine[SemanticVersion] + test - "1.0.0-rc.1+exp.sha.5114f85".assertRefine[SemanticVersion] + test - "1".assertNotRefine[SemanticVersion] + test - "1.0".assertNotRefine[SemanticVersion] + test - "x.0.0".assertNotRefine[SemanticVersion] + test - "0.y.0".assertNotRefine[SemanticVersion] + test - "0.0.z".assertNotRefine[SemanticVersion] + test - "x.y.z".assertNotRefine[SemanticVersion] + test - "00000001.0.0".assertNotRefine[SemanticVersion] + test - "0.00000001.0".assertNotRefine[SemanticVersion] + test - "0.0.00000001".assertNotRefine[SemanticVersion] + test - "1.0.0-rc.1+exp.sha.51_14f85".assertNotRefine[SemanticVersion] + test - "1.0.0-rc.1+exp.sha.51 14f85".assertNotRefine[SemanticVersion] + test - "1.0.0-rc.1+exp.sha.51?14f85".assertNotRefine[SemanticVersion] + test - "1.0.0-rc.1+exp.sha.51#14f85".assertNotRefine[SemanticVersion] + test - "1.0.0-rc.1+exp.sha.51@14f85".assertNotRefine[SemanticVersion] + test - "1.0.0-rc.1+exp.sha.51:14f85".assertNotRefine[SemanticVersion] + test - "1.0.0-rc.1+exp.sha.51*14f85".assertNotRefine[SemanticVersion] + test - "1.0.0-rc.1+exp.sha.51|14f85".assertNotRefine[SemanticVersion] + } + }