Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add semantic versioning (semver.org) validator #246

Merged
merged 4 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,12 @@ Validates MAC addresses.
Examples:
- `mac()`: Allows any valid MAC address

### SemVer (Semantic Versioning) - `semver()`
Validates [Semantic Versioning](https://semver.org/) strings.

Examples:
- `semver()`: Allows any valid SemVer string

### Any - `any([validators])`
Validates against a union of types. Use when a node **must** contain **one and only one** of several types. It is valid
if at least one of the listed validators is valid. If no validators are given, accept any value.
Expand Down
80 changes: 80 additions & 0 deletions yamale/validators/tests/test_validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,83 @@ def test_mac():
assert not v.is_valid("ab:cd:ef:12:34:56:78")
assert not v.is_valid("abcdefghijkl")
assert not v.is_valid("1234567890ax")


def test_semver():
"""
- https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string
- https://regex101.com/r/Ly7O1x/3/
"""
v = val.SemVer()

assert v.is_valid("0.0.4")
assert v.is_valid("1.2.3")
assert v.is_valid("10.20.30")
assert v.is_valid("1.1.2-prerelease+meta")
assert v.is_valid("1.1.2+meta")
assert v.is_valid("1.1.2+meta-valid")
assert v.is_valid("1.0.0-alpha")
assert v.is_valid("1.0.0-beta")
assert v.is_valid("1.0.0-alpha.beta")
assert v.is_valid("1.0.0-alpha.beta.1")
assert v.is_valid("1.0.0-alpha.1")
assert v.is_valid("1.0.0-alpha0.valid")
assert v.is_valid("1.0.0-alpha.0valid")
assert v.is_valid("1.0.0-alpha-a.b-c-somethinglong+build.1-aef.1-its-okay")
assert v.is_valid("1.0.0-rc.1+build.1")
assert v.is_valid("2.0.0-rc.1+build.123")
assert v.is_valid("1.2.3-beta")
assert v.is_valid("10.2.3-DEV-SNAPSHOT")
assert v.is_valid("1.2.3-SNAPSHOT-123")
assert v.is_valid("1.0.0")
assert v.is_valid("2.0.0")
assert v.is_valid("1.1.7")
assert v.is_valid("2.0.0+build.1848")
assert v.is_valid("2.0.1-alpha.1227")
assert v.is_valid("1.0.0-alpha+beta")
assert v.is_valid("1.2.3----RC-SNAPSHOT.12.9.1--.12+788")
assert v.is_valid("1.2.3----R-S.12.9.1--.12+meta")
assert v.is_valid("1.2.3----RC-SNAPSHOT.12.9.1--.12")
assert v.is_valid("1.0.0+0.build.1-rc.10000aaa-kk-0.1")
assert v.is_valid("99999999999999999999999.999999999999999999.99999999999999999")
assert v.is_valid("1.0.0-0A.is.legal")

assert not v.is_valid("1")
assert not v.is_valid("1.2")
assert not v.is_valid("1.2.3-0123")
assert not v.is_valid("1.2.3-0123.0123")
assert not v.is_valid("1.1.2+.123")
assert not v.is_valid("+invalid")
assert not v.is_valid("-invalid")
assert not v.is_valid("-invalid+invalid")
assert not v.is_valid("-invalid.01")
assert not v.is_valid("alpha")
assert not v.is_valid("alpha.beta")
assert not v.is_valid("alpha.beta.1")
assert not v.is_valid("alpha.1")
assert not v.is_valid("alpha+beta")
assert not v.is_valid("alpha_beta")
assert not v.is_valid("alpha.")
assert not v.is_valid("alpha..")
assert not v.is_valid("beta")
assert not v.is_valid("1.0.0-alpha_beta")
assert not v.is_valid("-alpha.")
assert not v.is_valid("1.0.0-alpha..")
assert not v.is_valid("1.0.0-alpha..1")
assert not v.is_valid("1.0.0-alpha...1")
assert not v.is_valid("1.0.0-alpha....1")
assert not v.is_valid("1.0.0-alpha.....1")
assert not v.is_valid("1.0.0-alpha......1")
assert not v.is_valid("1.0.0-alpha.......1")
assert not v.is_valid("01.1.1")
assert not v.is_valid("1.01.1")
assert not v.is_valid("1.1.01")
assert not v.is_valid("1.2")
assert not v.is_valid("1.2.3.DEV")
assert not v.is_valid("1.2-SNAPSHOT")
assert not v.is_valid("1.2.31.2.3----RC-SNAPSHOT.12.09.1--..12+788")
assert not v.is_valid("1.2-RC-SNAPSHOT")
assert not v.is_valid("-1.0.3-gamma+b7718")
assert not v.is_valid("+justmeta")
assert not v.is_valid("9.8.7+meta+meta")
assert not v.is_valid("9.8.7-whatever+meta+meta")
13 changes: 13 additions & 0 deletions yamale/validators/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,19 @@ def __init__(self, *args, **kwargs):
]


class SemVer(Regex):
"""Semantic Versioning (semver.org) validator"""

tag = "semver"

def __init__(self, *args, **kwargs):
super(SemVer, self).__init__(*args, **kwargs)
self.regexes = [
# https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string
re.compile("^(?P<major>0|[1-9]\d*)\.(?P<minor>0|[1-9]\d*)\.(?P<patch>0|[1-9]\d*)(?:-(?P<prerelease>(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+(?P<buildmetadata>[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$"),
]
Dismissed Show dismissed Hide dismissed


DefaultValidators = {}

for v in util.get_subclasses(Validator):
Expand Down
Loading