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

semver v3 regex is more inclusive than the official semver.org regex #211

Closed
oshirohugo opened this issue Jul 12, 2023 · 5 comments
Closed
Labels

Comments

@oshirohugo
Copy link

The official Semantic Versioning website has a suggested regex.
Which is different from the one used in semver v3
https://github.com/Masterminds/semver/blob/master/version.go#L42
The results of applying them is different:
This is the official one: https://regex101.com/r/Ly7O1x/3/
This is the one from semver v3 applied to the same examples: https://regex101.com/r/LxcCgR/1

Shouldn't they be the same?

@d0x7
Copy link

d0x7 commented Jul 17, 2023

In my opinion, no, it should not be the same. As stated in the README, there is a StrictNewVersion and the more permissive NewVersion, which also allows things like v1 or v1.2 which are not strictly SemVer, but still ocurr often "in the wild".

In fact, I am very glad this library supports the more permissive one, as that's pretty much exactly what I need for a project. Now I only need to go from interval notations to this librarys format for comparing/validating versions and the more permissive NewVersion can handle the parsing and comparing :)

@oshirohugo
Copy link
Author

oshirohugo commented Jul 17, 2023

Tks for your answer @d0x7 . Indeed StrictNewVersion is more restrictive, but still the following invalid versions are parsed without any errors:

1.1.2+.123
1.0.0-alpha..
1.0.0-alpha..1
1.0.0-alpha...1
1.0.0-alpha....1
1.0.0-alpha.....1
1.0.0-alpha......1
1.0.0-alpha.......1

For more details: Simple tests
All of them are not valid according to the official regex test.
So, I believe at least StrictNewVersion should reject these cases

@oshirohugo oshirohugo changed the title semver v3 regex is more inclusive them the official semver.org regex semver v3 regex is more inclusive than the official semver.org regex Sep 11, 2023
@mattfarina mattfarina added the bug label Oct 21, 2024
@mattfarina
Copy link
Member

StrictNewVersion should definitely reject these cases.

NewVersion is loose but should still reject invalid versions. So, there are bugs in that, too.

@mattfarina
Copy link
Member

Note, the regex here picks up partials so it's not intended to be exactly the same. For example, when it sees 1.2 it extrapolates out 1.2.0. This is a bit of a loose handling and why there is also strict handling. The regex's will never be exactly the same.

But, things like 1.0.0-alpha.. should never validate.

mattfarina added a commit to mattfarina/semver that referenced this issue Nov 19, 2024
The NewVersion function, which uses the loose parser, had the regex for
detection updated based on the official one. A change was made to allow
for versions like 1.2 and other "loose" ones (to use the node semver
term).

StrictNewVersion had some internal validation updated to catch issues.

For NewVersion, the benchmarking is now faster than the previous regex.

Ref Masterminds#211

Signed-off-by: Matt Farina <matt.farina@suse.com>
@mattfarina
Copy link
Member

StrictNewVersion should now handle the errors described in #211 (comment). It catches them.

NewVersion now catches the edge cases it shouldn't let though (e.g., v1.2.3-.alpha) while still being loose.

New tests were added to detect more. No existing tests were changed or removed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants