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

Incorrectly says some inputs are semver #78

Open
toddself opened this issue Sep 14, 2020 · 0 comments
Open

Incorrectly says some inputs are semver #78

toddself opened this issue Sep 14, 2020 · 0 comments

Comments

@toddself
Copy link

When provided an input like a formatted date, this library incorrectly parses it as semver.

package main

import (
	"fmt"
	"regexp"
)

const (
	VersionRegexpRaw string = `v?([0-9]+(\.[0-9]+)*?)` +
		`(-([0-9]+[0-9A-Za-z\-~]*(\.[0-9A-Za-z\-~]+)*)|(-?([A-Za-z\-~]+[0-9A-Za-z\-~]*(\.[0-9A-Za-z\-~]+)*)))?` +
		`(\+([0-9A-Za-z\-~]+(\.[0-9A-Za-z\-~]+)*))?` +
		`?`

	// SemverRegexpRaw requires a separator between version and prerelease
	SemverRegexpRaw string = `v?([0-9]+(\.[0-9]+)*?)` +
		`(-([0-9]+[0-9A-Za-z\-~]*(\.[0-9A-Za-z\-~]+)*)|(-([A-Za-z\-~]+[0-9A-Za-z\-~]*(\.[0-9A-Za-z\-~]+)*)))?` +
		`(\+([0-9A-Za-z\-~]+(\.[0-9A-Za-z\-~]+)*))?` +
		`?`
)

// mostly from https://semver.org, but v added in front for npm compatibility
const validatingRE = `^[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-]+)*))?$`

var semverOrgMatcher = regexp.MustCompile(validatingRE)

func main() {
	version := "2020-02-26T7-36-46"
	versionRegexp := regexp.MustCompile("^" + VersionRegexpRaw + "$")
	semverRegexp := regexp.MustCompile("^" + SemverRegexpRaw + "$")
	fmt.Println(versionRegexp.FindStringSubmatch(version))
	fmt.Println(semverRegexp.FindStringSubmatch(version))
	fmt.Println("is semver according to semver.org:", semverOrgMatcher.MatchString(version))
}

Go playground

I would have expected this to return an error about a malformed version rather than [2020-02-26T7-36-46 2020 -02-26T7-36-46 02-26T7-36-46 ]

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

No branches or pull requests

1 participant