github go-playground/validator v9.0.0
Release 9.0.0

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

Release 9.0.0

After much work, tweaking and design updates validator v9 has been released!

It's even faster, more efficient and built for ease of use!

Quick overview of changes

documentation has been updated to reflect all of these changes, see them for more details.

  • ValidationErrors is now an []FieldError instead of a map as before.
  • exists tag has been removed, now required tag handles pointer values as well.
  • Much of the design has been updated to use interfaces allowing for even less breaking changes going forward, including FieldError.
  • change name for validating individual fields, I think that some people were associating validation using validate.Field or validate.FieldWithValue to a struct field only, but that is not the case, any variable can be validated and so the functions have been changed to:
var age int

err := validate.Var(age, "gte=0,lte=130")

var parentsAge int

err := validate.VarWithValue(age, parentsAge, "ltcsfield")

// side note: reason validator doesn't have strict sanitizers like IsEmail(..) bool is so many validation can be run  on a field in one run eg.

var settings []string

// this will validate that settings exists, there is at least one entry in array 
// and that each string within the array is set...
validate.Var(settings, "required,gte=1,dive,required")
  • can register any custom tag name function instead of before having to specify the name of the tag...
validate.RegisterTagNameFunc(func(fld reflect.StructField) string {
    name := strings.SplitN(fld.Tag.Get("json"), ",", 2)[0]

    if name == "-" {
        return ""
    }

    return name
})
  • Validation function + Struct Level validation function signatures have changed to use interface and should be infinity easier to remember and implement:
// field level validation func
type Func func(fl FieldLevel) bool

// struct level validation func
type StructLevelFunc func(sl StructLevel)

And now what veryone wants to see, benchmarks compared to v8

benchmark                                               old ns/op     new ns/op     delta
BenchmarkFieldSuccess-8                                 118           108           -8.47%
BenchmarkFieldFailure-8                                 758           320           -57.78%
BenchmarkFieldDiveSuccess-8                             2471          726           -70.62%
BenchmarkFieldDiveFailure-8                             3172          939           -70.40%
BenchmarkFieldCustomTypeSuccess-8                       300           268           -10.67%
BenchmarkFieldCustomTypeFailure-8                       775           310           -60.00%
BenchmarkFieldOrTagSuccess-8                            1122          889           -20.77%
BenchmarkFieldOrTagFailure-8                            1167          546           -53.21%
BenchmarkStructLevelValidationSuccess-8                 548           336           -38.69%
BenchmarkStructLevelValidationFailure-8                 558           611           +9.50%
BenchmarkStructSimpleCustomTypeSuccess-8                623           555           -10.91%
BenchmarkStructSimpleCustomTypeFailure-8                1381          811           -41.27%
BenchmarkStructPartialSuccess-8                         1036          676           -34.75%
BenchmarkStructPartialFailure-8                         1734          1001          -42.27%
BenchmarkStructExceptSuccess-8                          888           1038          +16.89%
BenchmarkStructExceptFailure-8                          1036          863           -16.70%
BenchmarkStructSimpleCrossFieldSuccess-8                773           549           -28.98%
BenchmarkStructSimpleCrossFieldFailure-8                1487          783           -47.34%
BenchmarkStructSimpleCrossStructCrossFieldSuccess-8     1261          837           -33.62%
BenchmarkStructSimpleCrossStructCrossFieldFailure-8     2055          1110          -45.99%
BenchmarkStructSimpleSuccess-8                          519           380           -26.78%
BenchmarkStructSimpleFailure-8                          1429          760           -46.82%
BenchmarkStructSimpleSuccessParallel-8                  146           114           -21.92%
BenchmarkStructSimpleFailureParallel-8                  551           353           -35.93%
BenchmarkStructComplexSuccess-8                         3269          2100          -35.76%
BenchmarkStructComplexFailure-8                         8436          5080          -39.78%
BenchmarkStructComplexSuccessParallel-8                 1024          662           -35.35%
BenchmarkStructComplexFailureParallel-8                 3536          2159          -38.94%

benchmark                                               old allocs     new allocs     delta
BenchmarkFieldSuccess-8                                 0              0              +0.00%
BenchmarkFieldFailure-8                                 4              4              +0.00%
BenchmarkFieldDiveSuccess-8                             28             11             -60.71%
BenchmarkFieldDiveFailure-8                             32             16             -50.00%
BenchmarkFieldCustomTypeSuccess-8                       2              2              +0.00%
BenchmarkFieldCustomTypeFailure-8                       4              4              +0.00%
BenchmarkFieldOrTagSuccess-8                            1              1              +0.00%
BenchmarkFieldOrTagFailure-8                            6              5              -16.67%
BenchmarkStructLevelValidationSuccess-8                 5              2              -60.00%
BenchmarkStructLevelValidationFailure-8                 5              8              +60.00%
BenchmarkStructSimpleCustomTypeSuccess-8                3              2              -33.33%
BenchmarkStructSimpleCustomTypeFailure-8                9              9              +0.00%
BenchmarkStructPartialSuccess-8                         9              6              -33.33%
BenchmarkStructPartialFailure-8                         14             11             -21.43%
BenchmarkStructExceptSuccess-8                          7              12             +71.43%
BenchmarkStructExceptFailure-8                          9              10             +11.11%
BenchmarkStructSimpleCrossFieldSuccess-8                4              3              -25.00%
BenchmarkStructSimpleCrossFieldFailure-8                9              8              -11.11%
BenchmarkStructSimpleCrossStructCrossFieldSuccess-8     7              4              -42.86%
BenchmarkStructSimpleCrossStructCrossFieldFailure-8     12             9              -25.00%
BenchmarkStructSimpleSuccess-8                          1              0              -100.00%
BenchmarkStructSimpleFailure-8                          9              9              +0.00%
BenchmarkStructSimpleSuccessParallel-8                  1              0              -100.00%
BenchmarkStructSimpleFailureParallel-8                  9              9              +0.00%
BenchmarkStructComplexSuccess-8                         15             8              -46.67%
BenchmarkStructComplexFailure-8                         60             53             -11.67%
BenchmarkStructComplexSuccessParallel-8                 15             8              -46.67%
BenchmarkStructComplexFailureParallel-8                 60             53             -11.67%

benchmark                                               old bytes     new bytes     delta
BenchmarkFieldSuccess-8                                 0             0             +0.00%
BenchmarkFieldFailure-8                                 432           192           -55.56%
BenchmarkFieldDiveSuccess-8                             464           201           -56.68%
BenchmarkFieldDiveFailure-8                             896           396           -55.80%
BenchmarkFieldCustomTypeSuccess-8                       32            32            +0.00%
BenchmarkFieldCustomTypeFailure-8                       432           192           -55.56%
BenchmarkFieldOrTagSuccess-8                            4             16            +300.00%
BenchmarkFieldOrTagFailure-8                            448           208           -53.57%
BenchmarkStructLevelValidationSuccess-8                 160           32            -80.00%
BenchmarkStructLevelValidationFailure-8                 160           288           +80.00%
BenchmarkStructSimpleCustomTypeSuccess-8                36            32            -11.11%
BenchmarkStructSimpleCustomTypeFailure-8                640           392           -38.75%
BenchmarkStructPartialSuccess-8                         272           256           -5.88%
BenchmarkStructPartialFailure-8                         730           464           -36.44%
BenchmarkStructExceptSuccess-8                          250           480           +92.00%
BenchmarkStructExceptFailure-8                          272           448           +64.71%
BenchmarkStructSimpleCrossFieldSuccess-8                80            72            -10.00%
BenchmarkStructSimpleCrossFieldFailure-8                536           288           -46.27%
BenchmarkStructSimpleCrossStructCrossFieldSuccess-8     112           80            -28.57%
BenchmarkStructSimpleCrossStructCrossFieldFailure-8     576           304           -47.22%
BenchmarkStructSimpleSuccess-8                          4             0             -100.00%
BenchmarkStructSimpleFailure-8                          640           392           -38.75%
BenchmarkStructSimpleSuccessParallel-8                  4             0             -100.00%
BenchmarkStructSimpleFailureParallel-8                  640           392           -38.75%
BenchmarkStructComplexSuccess-8                         244           128           -47.54%
BenchmarkStructComplexFailure-8                         3609          2833          -21.50%
BenchmarkStructComplexSuccessParallel-8                 244           128           -47.54%
BenchmarkStructComplexFailureParallel-8                 3609          2833          -21.50%

Don't miss a new validator release

NewReleases is sending notifications on new releases.