// Package semver provides utilities for working with semantic // versioning based on https://semver.org package semver import ( "log" "regexp" "errors" ) const ( // SemverRegexpString is the officially recommended regular expression for // validating SemVer: https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string SemverRegexpString = `^(?P0|[1-9]\d*)\.(?P0|[1-9]\d*)\.(?P0|[1-9]\d*)(?:-(?P(?: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[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$` ) func init() { _, err := regexp.Compile(SemverRegexpString) if err != nil { log.Fatal("semver.init: failed to compile the SemVer regular expression") } } // IsValidSemver checks the input string against the recommended SemVer // regular expression. func IsValidSemver(ver string) error { // We can drop the error because we have a test that confirms the Regexp string // compiles, which is the only type of error regexp.MatchString can return ok, _ := regexp.MatchString(SemverRegexpString, ver) // if its not valid, return our custom error if !ok { return ErrInvalidSemVer } return nil } // ErrInvalidSemVer when the SemVer string simply don't follow that SemVer.org // specification. var ErrInvalidSemVer = errors.New("invalid semver provided")