Package sanitize provides an easy way to clean fields in structs: trimming, applying maximum string lengths, minimum numeric values, default values, and so on...
Sanitizing a struct will mutate the fields according to rules in the san
tag. The tags work for both pointers and basic types.
go get github.com/go-sanitize/sanitize
package main
import "github.com/go-sanitize/sanitize"
type Dog struct {
Name string `san:"max=5,trim,lower"`
Breed *string `san:"def=unknown"`
}
func main() {
d := Dog{
Name: "Borky Borkins",
Breed: nil,
}
s := sanitizer.New()
s.Sanitize(&d)
fmt.Printf("Name: %s, Breed: %s", d.Name, d.Breed)
// Name: borky, Breed: unknown
}
Default: san
Use this option to tell the sanitizer to use another tag name instead of "san".
s := sanitizer.New(sanitizer.OptionTagName{
Value: "mytag",
})
Default: Input = []
, Output = ""
, and KeepFormat = false
.
Use this option to specify which date format we should use.
The Input
field tells us which date formats we can accept (such as RFC3339 and RFC3339Nano).
The KeepFormat
field tells us if we should keep the date format unchanged, or if we want to force them into another format.
The Output
field tells us which format we should use for the output if KeepFormat
is set to false.
If a date can not be parsed by the formats specified in the Input
field, the field will be converted into an empty string.
Example:
- If
Input = [RFC1123, RFC3339Nano]
,KeepFormat: false
, andOutput = RFC3339
, we will accept dates in the RFC1123 and RFC3339Nano formats and convert them to RFC3339 format. Any other formats will be converted into an empty string. - If
Input = [RFC1123, RFC3339Nano]
andKeepFormat: true
, we will accept dates in the RFC1123 and RFC3339Nano formats and keep them in the same format. Any other formats will be converted into an empty string. - If
Input = []
, the field will be converted into an empty string, since there are no allowed input formats.
s := sanitizer.New(sanitizer.OptionDateFormat{
Input: []string{
time.RFC3339,
time.RFC3339Nano,
},
KeepFormat: false,
Output: time.RFC1123,
})
- max=
<n>
- Maximum string length. It will truncate the string to<n>
characters if this limit is exceeded - trim - Remove trailing spaces left and right
- lower - Lowercase all characters in the string
- upper - Uppercase all characters in the string
- title - First character of every word is changed to uppercase, the rest to lowercase. Uses Go's built in
strings.Title()
function. - cap - Only the first letter of the string will be changed to uppercase, the rest to lowercase
- def=
<n>
(only available for pointers) - Sets a default<n>
value in case the pointer isnil
- xss - Will remove brackets such as <>{} and the characters !=? from the string
- date - Will parse the string using the input formats provided in the options and print it using the output format provided in the options. If the string can not be parsed, it will be left empty.
The order of precedence will be: xss -> trim -> date -> max -> lower -> upper -> title -> cap
Available for: int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, and float64
- max=
<n>
- Highest value allowed. If the limit is exceeded, the value will be set to<n>
- min=
<n>
- Lowest value allowed. If the limit is exceeded, the value will be set to<n>
- def=
<n>
(only available for pointers) - Sets a default<n>
value in case the pointer isnil
- def=
<n>
(only available for pointers) - Sets a default<n>
value in case the pointer isnil
- maxsize=
<n>
- Maximum slice length. It will truncate the slice to<n>
elements if the limit is exceeded
Other tags will be applied for every element in the slice, not the slice itself. For example: a field of type []string
with the tag max=5
will have every string truncated to 5 characters at most.