Package verify uses struct field tags to verify data.
-
minSize
-- specifies the minimum allowable length of a field. This can only be used on the following types: string, slice, array, or map. -
maxSize
-- specifies the maximum allowable length of a field. This can only be used on the following types: string, slice, array, or map. -
min
-- specifies the minimum allowable value of a field. This should only be used on types that can be parsed into an int64 or float64. -
max
-- specifies the maximum allowable value of a field. This should only be used on types that can be parsed into an int64 or float64. -
required
-- specifies the field may not be set to the zero value for the given type. This may be used on any types except arrays and structs.
Here is an example of the usage of each tag:
type Foo struct {
A []string `verify:"minSize=5"`
B string `verify:"maxSize=10"`
C int8 `verify:"min=3"`
D float32 `verify:"max=1.2"`
E int64 `verify:"min=3,max=7"`
F *bool `verify:"required"`
}
- verify only supports working with flat structures at the moment; it will not work with inner/embedded structs. Also,
- Because this package makes use of reflection the tags may only be used on exported fields.
I wrote a blog post about how to make your own struct field tags. Check it out here!