github go-playground/validator v6.7
Release 6.7

latest releases: v10.23.0, v10.22.1, v10.22.0...
9 years ago

Summary

Backported v7 updates + additions from the near released v7.

Changes

  • updated code to determine field type into a reusable function, making the code a little more DRY and allowing for future use in v7.

Additions

  • Added StructPartial and StructExcept functions + tests + benchmarks

both StructPartial and StructExcept allow for conditional validation of a struct in order to ignore or explicitly define which fields that are to be validated for a given situation.

Although using separate structs or more than one validator with different TagName can solve this problem, and is usually the recommended approach, there are times where this would be necessary. An example would be that you are upgrading your current language to Go, but cannot redesign some of the data structure, essentially being stuck with some legacy structure.

Thanks @jtriber for the idea and help! fo request #149

Benchmarks

Splitting the type determination into a function did affect the ns/op ever so slightly.

go test -cpu=4 -bench=. -benchmem=true
PASS
BenchmarkFieldSuccess-4                      5000000           337 ns/op          16 B/op          1 allocs/op
BenchmarkFieldFailure-4                      5000000           331 ns/op          16 B/op          1 allocs/op
BenchmarkFieldCustomTypeSuccess-4            3000000           497 ns/op          32 B/op          2 allocs/op
BenchmarkFieldCustomTypeFailure-4            2000000           842 ns/op         416 B/op          6 allocs/op
BenchmarkFieldOrTagSuccess-4                  500000          2432 ns/op          20 B/op          2 allocs/op
BenchmarkFieldOrTagFailure-4                 1000000          1323 ns/op         384 B/op          6 allocs/op
BenchmarkStructSimpleCustomTypeSuccess-4     1000000          1409 ns/op          56 B/op          5 allocs/op
BenchmarkStructSimpleCustomTypeFailure-4     1000000          1876 ns/op         577 B/op         13 allocs/op
BenchmarkStructPartialSuccess-4              1000000          1438 ns/op         384 B/op         13 allocs/op
BenchmarkStructPartialFailure-4              1000000          2040 ns/op         785 B/op         18 allocs/op
BenchmarkStructExceptSuccess-4               1000000          1000 ns/op         368 B/op         11 allocs/op
BenchmarkStructExceptFailure-4               1000000          1431 ns/op         384 B/op         13 allocs/op
BenchmarkStructSimpleSuccess-4               1000000          1375 ns/op          24 B/op          3 allocs/op
BenchmarkStructSimpleFailure-4               1000000          1893 ns/op         529 B/op         11 allocs/op
BenchmarkStructSimpleSuccessParallel-4       5000000           362 ns/op          24 B/op          3 allocs/op
BenchmarkStructSimpleFailureParallel-4       2000000           883 ns/op         529 B/op         11 allocs/op
BenchmarkStructComplexSuccess-4               200000          8237 ns/op         368 B/op         30 allocs/op
BenchmarkStructComplexFailure-4               100000         12617 ns/op        2861 B/op         72 allocs/op
BenchmarkStructComplexSuccessParallel-4      1000000          2398 ns/op         368 B/op         30 allocs/op
BenchmarkStructComplexFailureParallel-4       300000          5733 ns/op        2862 B/op         72 allocs/op

Don't miss a new validator release

NewReleases is sending notifications on new releases.