Skip to content

no valid error messages from Validate() when constraints are build with caret or tilde #145

@ghost

Description

Running the code below, gives the following error message:
"1.6.6 does not have same major version as 1.12.7. "

This error message does not make sense here, as I have the same Major for both.

I would expect instead an error message like the following:
"1.6.6. is not in the range >= 1.12.7, < 2.0.0"
OR
"1.6.6. is not >= 1.12.7"

package main

import (
"fmt"

"github.com/Masterminds/semver"

)

func main() {
versionConstraint, err := semver.NewConstraint("^1.12.7")
if err != nil {
return
}

currVersion, err := semver.NewVersion("1.6.6")
if err != nil {
	return
}

a, msgs := versionConstraint.Validate(currVersion)
if !a {

	for _, m := range msgs {
		fmt.Println(m)
	}
}

}

In the constraints.go is defined the following map, which pairs one error message per special character found in the constraint. This fails for cases (such as ~ and ^) in which the special character defines a range with lower and upper limits.

constraintMsg = map[string]string{
"": "%s is not equal to %s",
"=": "%s is not equal to %s",
"!=": "%s is equal to %s",
">": "%s is less than or equal to %s",
"<": "%s is greater than or equal to %s",
">=": "%s is less than %s",
"=>": "%s is less than %s",
"<=": "%s is greater than %s",
"=<": "%s is greater than %s",
"~": "%s does not have same major and minor version as %s",
"~>": "%s does not have same major and minor version as %s",
"^": "%s does not have same major version as %s",
}

It would be more meaningful if the constraints functions (e.g.func constraintCaret(v *Version, c *constraint) bool {) return the specific error message together with the false value.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions