Skip to content

Commit

Permalink
Improve semver validation error messsage (#845)
Browse files Browse the repository at this point in the history
More explicit error message with link to semver.org.
  • Loading branch information
ml-evs committed Jun 9, 2021
1 parent f80c842 commit c6f9977
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
5 changes: 4 additions & 1 deletion optimade/models/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,10 @@ def __modify_schema__(cls, field_schema):
@classmethod
def validate(cls, v: str):
if not cls.regex.match(v):
raise ValueError(f"Unable to validate version {v} as a semver.")
raise ValueError(
f"Unable to validate the version string {v!r} as a semantic version (expected <major>.<minor>.<patch>)."
"See https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string for more information."
)

return v

Expand Down
5 changes: 4 additions & 1 deletion tests/models/test_baseinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ def test_available_api_versions():
]
bad_versions = [
{"url": "https://example.com/v0", "version": "v0.1.9"},
{"url": "https://example.com/v0", "version": "0.1"},
{"url": "https://example.com/v1", "version": "1.0"},
{"url": "https://example.com/v1.0.2", "version": "v1.0.2"},
{"url": "https://example.com/optimade/v1.2", "version": "v1.2.3"},
{"url": "https://example.com/v1.0.0", "version": "1.asdfaf.0-rc55"},
Expand Down Expand Up @@ -48,7 +50,8 @@ def test_available_api_versions():
with pytest.raises(ValueError) as exc:
AvailableApiVersion(**data)
assert (
f"Unable to validate version {data['version']} as a semver" in exc.exconly()
f"Unable to validate the version string {data['version']!r} as a semantic version (expected <major>.<minor>.<patch>)"
in exc.exconly()
), f"SemVer validator not triggered as it should.\nException message: {exc.exconly()}.\nInputs: {data}"

for data in bad_combos:
Expand Down

0 comments on commit c6f9977

Please sign in to comment.