forked from go-goyave/goyave
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrim.go
27 lines (21 loc) · 765 Bytes
/
trim.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package validation
import "strings"
// TrimValidator if the field under validation is a string, trims it using
// `strings.TrimSpace()`.
type TrimValidator struct{ BaseValidator }
// Validate always returns true. If the field under validation is a string,
// trims it using `strings.TrimSpace()`.
func (v *TrimValidator) Validate(ctx *Context) bool {
str, ok := ctx.Value.(string)
if ok {
ctx.Value = strings.TrimSpace(str)
}
// This rule is just transforming, so we always return true.
return true
}
// Name returns the string name of the validator.
func (v *TrimValidator) Name() string { return "trim" }
// Trim if the field under validation is a string, trims it using `strings.TrimSpace()`.
func Trim() *TrimValidator {
return &TrimValidator{}
}