Package validator implements value validations for structs and individual fields based on tags.
It has the following unique features:
- Cross Field and Cross Struct validations by using validation tags or custom validators.
- Slice, Array and Map diving, which allows any or all levels of a multidimensional field to be validated.
- Ability to dive into both map keys and values for validation
- Handles type interface by determining it's underlying type prior to validation.
- Handles custom field types such as sql driver Valuer see Valuer
- Alias validation tags, which allows for mapping of several validations to a single tag for easier defining of validations on structs
- Extraction of custom defined Field Name e.g. can specify to extract the JSON name while validating and have it available in the resulting FieldError
- Customizable i18n aware error messages.
- Default validator for the gin web framework; upgrading from v8 to v9 in gin see here
Use go get.
go get gopkg.in/go-playground/validator.v9
Then import the validator package into your own code.
import "gopkg.in/go-playground/validator.v9"
Validation functions return type error
They return type error to avoid the issue discussed in the following, where err is always != nil:
Validator only InvalidValidationError for bad validation input, nil or ValidationErrors as type error; so, in your code all you need to do is check if the error returned is not nil, and if it's not check if error is InvalidValidationError ( if necessary, most of the time it isn't ) type cast it to type ValidationErrors like so:
err:=validate.Struct(mystruct)
validationErrors:=err.(validator.ValidationErrors)Please see http://godoc.org/gopkg.in/go-playground/validator.v9 for detailed usage docs.
- Simple
- Custom Field Types
- Struct Level
- Translations & Custom Errors
- Gin upgrade and/or override validator
- wash - an example application putting it all together
goos: darwin
goarch: amd64
pkg: github.com/go-playground/validatorBenchmarkFieldSuccess-82000000083.6ns/op0B/op0allocs/opBenchmarkFieldSuccessParallel-85000000026.8ns/op0B/op0allocs/opBenchmarkFieldFailure-85000000291ns/op208B/op4allocs/opBenchmarkFieldFailureParallel-820000000107ns/op208B/op4allocs/opBenchmarkFieldArrayDiveSuccess-82000000623ns/op201B/op11allocs/opBenchmarkFieldArrayDiveSuccessParallel-810000000237ns/op201B/op11allocs/opBenchmarkFieldArrayDiveFailure-82000000859ns/op412B/op16allocs/opBenchmarkFieldArrayDiveFailureParallel-85000000335ns/op413B/op16allocs/opBenchmarkFieldMapDiveSuccess-810000001292ns/op432B/op18allocs/opBenchmarkFieldMapDiveSuccessParallel-83000000467ns/op432B/op18allocs/opBenchmarkFieldMapDiveFailure-810000001082ns/op512B/op16allocs/opBenchmarkFieldMapDiveFailureParallel-85000000425ns/op512B/op16allocs/opBenchmarkFieldMapDiveWithKeysSuccess-810000001539ns/op480B/op21allocs/opBenchmarkFieldMapDiveWithKeysSuccessParallel-83000000613ns/op480B/op21allocs/opBenchmarkFieldMapDiveWithKeysFailure-810000001413ns/op721B/op21allocs/opBenchmarkFieldMapDiveWithKeysFailureParallel-83000000575ns/op721B/op21allocs/opBenchmarkFieldCustomTypeSuccess-810000000216ns/op32B/op2allocs/opBenchmarkFieldCustomTypeSuccessParallel-82000000082.2ns/op32B/op2allocs/opBenchmarkFieldCustomTypeFailure-85000000274ns/op208B/op4allocs/opBenchmarkFieldCustomTypeFailureParallel-820000000116ns/op208B/op4allocs/opBenchmarkFieldOrTagSuccess-82000000740ns/op16B/op1allocs/opBenchmarkFieldOrTagSuccessParallel-83000000474ns/op16B/op1allocs/opBenchmarkFieldOrTagFailure-83000000471ns/op224B/op5allocs/opBenchmarkFieldOrTagFailureParallel-83000000414ns/op224B/op5allocs/opBenchmarkStructLevelValidationSuccess-810000000213ns/op32B/op2allocs/opBenchmarkStructLevelValidationSuccessParallel-82000000091.8ns/op32B/op2allocs/opBenchmarkStructLevelValidationFailure-83000000473ns/op304B/op8allocs/opBenchmarkStructLevelValidationFailureParallel-810000000234ns/op304B/op8allocs/opBenchmarkStructSimpleCustomTypeSuccess-85000000385ns/op32B/op2allocs/opBenchmarkStructSimpleCustomTypeSuccessParallel-810000000161ns/op32B/op2allocs/opBenchmarkStructSimpleCustomTypeFailure-82000000640ns/op424B/op9allocs/opBenchmarkStructSimpleCustomTypeFailureParallel-85000000318ns/op440B/op10allocs/opBenchmarkStructFilteredSuccess-82000000597ns/op288B/op9allocs/opBenchmarkStructFilteredSuccessParallel-810000000266ns/op288B/op9allocs/opBenchmarkStructFilteredFailure-83000000454ns/op256B/op7allocs/opBenchmarkStructFilteredFailureParallel-810000000214ns/op256B/op7allocs/opBenchmarkStructPartialSuccess-83000000502ns/op256B/op6allocs/opBenchmarkStructPartialSuccessParallel-810000000225ns/op256B/op6allocs/opBenchmarkStructPartialFailure-82000000702ns/op480B/op11allocs/opBenchmarkStructPartialFailureParallel-85000000329ns/op480B/op11allocs/opBenchmarkStructExceptSuccess-82000000793ns/op496B/op12allocs/opBenchmarkStructExceptSuccessParallel-810000000193ns/op240B/op5allocs/opBenchmarkStructExceptFailure-82000000639ns/op464B/op10allocs/opBenchmarkStructExceptFailureParallel-85000000300ns/op464B/op10allocs/opBenchmarkStructSimpleCrossFieldSuccess-83000000417ns/op72B/op3allocs/opBenchmarkStructSimpleCrossFieldSuccessParallel-810000000163ns/op72B/op3allocs/opBenchmarkStructSimpleCrossFieldFailure-82000000645ns/op304B/op8allocs/opBenchmarkStructSimpleCrossFieldFailureParallel-85000000285ns/op304B/op8allocs/opBenchmarkStructSimpleCrossStructCrossFieldSuccess-83000000588ns/op80B/op4allocs/opBenchmarkStructSimpleCrossStructCrossFieldSuccessParallel-810000000221ns/op80B/op4allocs/opBenchmarkStructSimpleCrossStructCrossFieldFailure-82000000868ns/op320B/op9allocs/opBenchmarkStructSimpleCrossStructCrossFieldFailureParallel-85000000337ns/op320B/op9allocs/opBenchmarkStructSimpleSuccess-85000000260ns/op0B/op0allocs/opBenchmarkStructSimpleSuccessParallel-82000000090.6ns/op0B/op0allocs/opBenchmarkStructSimpleFailure-82000000619ns/op424B/op9allocs/opBenchmarkStructSimpleFailureParallel-85000000296ns/op424B/op9allocs/opBenchmarkStructComplexSuccess-810000001454ns/op128B/op8allocs/opBenchmarkStructComplexSuccessParallel-83000000579ns/op128B/op8allocs/opBenchmarkStructComplexFailure-83000004140ns/op3041B/op53allocs/opBenchmarkStructComplexFailureParallel-810000002127ns/op3041B/op53allocs/opBenchmarkOneof-810000000140ns/op0B/op0allocs/opBenchmarkOneofParallel-82000000070.1ns/op0B/op0allocs/opHere is a list of software that complements using this library either pre or post validation.
- form - Decodes url.Values into Go value(s) and Encodes Go value(s) into url.Values. Dual Array and Full map support.
- mold - A general library to help modify or set data within data structures and other objects
Make a pull request...
Distributed under MIT License, please see license file within the code for more details.
