Skip to content

Releases: go-playground/validator

Release 8.5

16 Oct 01:22

Choose a tag to compare

What Changed?

  • Exposed Baked In functions for use within custom functions ( why reinvent the wheel right ) this was prompted by question in issue #195

Notes

  • Exposing these functions adds allot to the godoc, but is necessary to get the extra functionality we want.

Release 8.4

06 Oct 20:44

Choose a tag to compare

Whats New?

Added new validators:

  • cidr
  • cidrv4
  • cidrv6

see docs for more details https://godoc.org/gopkg.in/go-playground/validator.v8

thanks for the pull request! @krhubert #193

Release 8.3

30 Sep 12:47

Choose a tag to compare

What New?

  • Added ability to extract a custom name from any tag e.g. can specify FieldNameTag in the config set it to say "json" and validator will extract the JSON name and added it the the resulting FieldError in the Name field.

Thanks @ilgooz for the pull request!

Release 8.2

24 Sep 11:56

Choose a tag to compare

update organization from bluesuncorp to go-playground

Release 7.2

24 Sep 11:55

Choose a tag to compare

update organization from bluesuncorp to go-playground

Release 6.8

24 Sep 11:55

Choose a tag to compare

update organization from bluesuncorp to go-playground

Release 5.12

24 Sep 11:54

Choose a tag to compare

update organization from bluesuncorp to go-playground

Release 8.1

03 Sep 11:26

Choose a tag to compare

Updates

Exposed previously unexposed functions for use within custom functions.

  • ExtractType
  • GetStructFieldOK

Release 8.0.1

03 Sep 02:29

Choose a tag to compare

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

Release 7.1

03 Sep 00:20

Choose a tag to compare

Changes

Add check in Validator methods to ensure that it has been initialized, because it is a pointer methods can still be called even though it's not initialized.

thanks @flowonyx for the pull request!