SemVer 1.1.0 Released With Validation Support

The first release of the github.com/Masterminds/semver package for Go was fairly complete. Not only did it include the ability to parse Semantic Versions it including sorting and range support that would be familiar for those coming from JavaScript, PHP, Python, and numerous other languages. It was everything we needed for Glide and more.

There was one interesting feature request that came in fairly quickly. That is the ability to know why a version failed when tested against a constraint.

For example, say you want to test a version, possibly passed in by user or outside input, of 1.2.3 against the constraint >= 1.1.1, < 2, != 1.2.3, != 1.4.5. Why did it fail? It could even be the case the constraint is passed in programmatically as well.

The 1.1.0 release of the semver package now has the ability to tell you.

Let's take a look at an example,

c, _ := semver.NewConstraint("<= 1.2.3, >= 1.4")
v, _ := semver.NewVersion("1.3")

// Validate a version against a constraint.
a, msgs := c.Validate(v)
// a is false because it failed validation
for _, m := range msgs {
    fmt.Println(m)

    // Loops over the errors which would read
    // "1.3 is greater than 1.2.3"
    // "1.3 is less than 1.4"
}

When testing constraints there is a new Validate method alongside Check on semver.Constraints instances that also returns reasons for a failure. The reasons are a slice of error instances outlining why a failed check happened.

This means it's possible to figure out why versions fail checks, report this to users, log reasons, and so forth.