SemanticVersion icon indicating copy to clipboard operation
SemanticVersion copied to clipboard

SemanticVersion.IsVersion accepts versions that SemanticVersion.TryParse does not

Open ssiltanen opened this issue 4 years ago • 3 comments

The following code is done with F# interactive, but I trust you get the point. Here's how to repro the problem:

> SemanticVersion.IsVersion "2.0";;                                                                                            
val it : bool = true

> SemanticVersion.TryParse "2.0";;
val it : bool * SemanticVersion = (false, null)

When given a version string "2.0", IsVersion function accepts it as valid version, but TryParse cannot parse it.

ssiltanen avatar May 27 '20 07:05 ssiltanen

I agree. Probably IsVersion could use a regex pattern suggested in document (https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string) which is strict about having a patch number. However, the regex that is used here also accepts an * symbol, which is supposed to act like a wildcard, but as far as I know, this wasn't implemented fully. Therefore I think a IsVersionStrict method could use the document regex and a IsVersionLoose - a slightly modified regex, which would parse * and, probably, allow patch to be missing (if that is needed, for example for parsing 1.2 as 1.2.0, but I guess, that's another issue that needs to be discussed)

STeeL835 avatar Feb 09 '21 21:02 STeeL835

The code already uses regex to match the version string, albeit a slightly different one. The regex in question is:

"^(?<major>[0]|[1-9]+[0-9]*|[*])((\.(?<minor>[0]|[1-9]+[0-9]*|[*]))(\.(?<patch>[0]|[1-9]+[0-9]*|[*]))?)?(\-(?<pre>[0-9A-Za-z\-\.]+|[*]))?(\+(?<build>[0-9A-Za-z\-\.]+|[*]))?$"

The SemanticVersion.IsVersion(string) method then matches this regex. Strangely enough, the SemanticVersion.TryParse(string, out SemanticVersion) also does this after an initial String.IsNullOrEmpty(string) check.

That's why I'm a bit confused why this doesn't work. But I'll check in the next few days.

Ruhrpottpatriot avatar Feb 11 '21 17:02 Ruhrpottpatriot

I think that the regex still needs to be replaced/improved, because the current one does not fully go by the standard: https://regex101.com/r/jH9dS0/1

STeeL835 avatar Feb 17 '21 21:02 STeeL835