Skip to content

Commit

Permalink
feat: Add semantic version String constraint (#199)
Browse files Browse the repository at this point in the history
  • Loading branch information
rlemaitre authored Dec 30, 2023
1 parent ac1b221 commit fc18ee2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
5 changes: 5 additions & 0 deletions main/src/io/github/iltotore/iron/constraint/string.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
29 changes: 29 additions & 0 deletions main/test/src/io/github/iltotore/iron/testing/StringSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}

}

0 comments on commit fc18ee2

Please sign in to comment.