github go-playground/validator v8.0.1
Release 8.0.1

latest releases: v10.20.0, v10.19.0, v10.18.0...
8 years ago

What v8 Why?

  • v8 was needed to update a bunch of code to basically spit polish this library. READ more below for details.
  • Added Tag alias logic.

Changes

  • Validator now returns type error instead of type ValidationErrors, this was due to some confusion about interfaces and them never being nil and hence the error, is some situations would never be nil either. This change actually now behaves a little more like packages in the standard library, for example: https://golang.org/pkg/os/#Open file.Open returns nil if everything is ok and *PathError if not; now this library return nil if no errors and ValidationErrors if there are. to type cast just do this to the returned error err.(validator.ValidationErrors) for a more detailed explanation of why this change was made see the following issue and post:
  • BakedInValidators is no longer exposed; it allowed for the map to be manipulated externally, potentially while validator was running, which could cause unknown issues. baked in validators are now automatically added when creating a new validator.
  • You can no longer pass a map of validation functions when creating a new validator instance; now you must use the register function RegisterValidation https://godoc.org/gopkg.in/bluesuncorp/validator.v8#Validate.RegisterValidation
  • Now validating custom tags when adding new validations to ensure core tags that are needed for the library to operate correctly are not overwritten and that invalid characters are not present.
  • Config is now passed in as a pointer when initializing a new instance.

What do I need to change to update?

not that much

// for Config
//before
config := validator.Config{TagName:"validate", Validators: validator.BakedInValidators}
// now
config := &validator.Config{TagName:"validate"}

// for getting at individual FieldError's within the ValidationErrors map
// before
errs := validate.Struct(mystruct)
if errs != nil {
  range k, fieldError := range errs {
    // build your own error message
  }
}

// now
errs := validate.Struct(mystruct)
if errs != nil {
  range k, fieldError := range errs.(validator.ValidationErrors) {
    // build your own error message
  }
}

Additions

  • Added ability to add a validation tag that map to a predefined set of validations; maybe because you use it allot, or want to be able to update globally. For example there is one default alias validator baked in, the tag is "iscolor" and when validator see's that tag on the struct it runs the following validations "hexcolor|rgb|rgba|hsl|hsla". NOTE: when an error occurs you will get the aliased error i.e. "iscolor"; the only exception is the "dive" tag. Any validations defined after a "dive" tag returns the actual validation tag, the reason for this is that not tag can apply to both the top level and a sub element, say of a slice, without being defined twice; the same rule applied for alias. This is documented in detail within the godocs https://godoc.org/gopkg.in/bluesuncorp/validator.v8

New Benchmarks

ns/op changed ever so slightly, but the minor tradeoff for ability to use alias is well worth it.

$ go test -cpu=4 -bench=. -benchmem=true
PASS
BenchmarkFieldSuccess-4                              5000000           296 ns/op          16 B/op          1 allocs/op
BenchmarkFieldFailure-4                              5000000           294 ns/op          16 B/op          1 allocs/op
BenchmarkFieldDiveSuccess-4                           500000          2529 ns/op         384 B/op         19 allocs/op
BenchmarkFieldDiveFailure-4                           500000          3056 ns/op         768 B/op         23 allocs/op
BenchmarkFieldCustomTypeSuccess-4                    3000000           443 ns/op          32 B/op          2 allocs/op
BenchmarkFieldCustomTypeFailure-4                    2000000           753 ns/op         384 B/op          4 allocs/op
BenchmarkFieldOrTagSuccess-4                         1000000          1334 ns/op          32 B/op          2 allocs/op
BenchmarkFieldOrTagFailure-4                         1000000          1172 ns/op         416 B/op          6 allocs/op
BenchmarkStructSimpleCustomTypeSuccess-4             1000000          1206 ns/op          80 B/op          5 allocs/op
BenchmarkStructSimpleCustomTypeFailure-4             1000000          1737 ns/op         592 B/op         11 allocs/op
BenchmarkStructPartialSuccess-4                      1000000          1367 ns/op         400 B/op         11 allocs/op
BenchmarkStructPartialFailure-4                      1000000          1914 ns/op         800 B/op         16 allocs/op
BenchmarkStructExceptSuccess-4                       2000000           909 ns/op         368 B/op          9 allocs/op
BenchmarkStructExceptFailure-4                       1000000          1350 ns/op         400 B/op         11 allocs/op
BenchmarkStructSimpleCrossFieldSuccess-4             1000000          1218 ns/op         128 B/op          6 allocs/op
BenchmarkStructSimpleCrossFieldFailure-4             1000000          1783 ns/op         544 B/op         11 allocs/op
BenchmarkStructSimpleCrossStructCrossFieldSuccess-4  1000000          1806 ns/op         160 B/op          8 allocs/op
BenchmarkStructSimpleCrossStructCrossFieldFailure-4  1000000          2369 ns/op         576 B/op         13 allocs/op
BenchmarkStructSimpleSuccess-4                       1000000          1161 ns/op          48 B/op          3 allocs/op
BenchmarkStructSimpleFailure-4                       1000000          1813 ns/op         592 B/op         11 allocs/op
BenchmarkStructSimpleSuccessParallel-4               5000000           353 ns/op          48 B/op          3 allocs/op
BenchmarkStructSimpleFailureParallel-4               2000000           656 ns/op         592 B/op         11 allocs/op
BenchmarkStructComplexSuccess-4                       200000          7637 ns/op         432 B/op         27 allocs/op
BenchmarkStructComplexFailure-4                       100000         12775 ns/op        3128 B/op         69 allocs/op
BenchmarkStructComplexSuccessParallel-4              1000000          2270 ns/op         432 B/op         27 allocs/op
BenchmarkStructComplexFailureParallel-4               300000          4328 ns/op        3128 B/op         69 allocs/op

Author Notes

I know that I have created a few new versions lately and just wanted to touch on that.

  • I only create a new version when the new code can cause breaking changes because ensuring I don't cause your code to break is of top importance.
  • I use this library myself not only in my personal projects but in some commercial ones as well and am implementing changes that not only I will use, but that I think others will too. Making this library better and better will benefit everyone not just myself

Don't miss a new validator release

NewReleases is sending notifications on new releases.