Add `startswith` and `endswith` validators

`HasPrefix` and `HasSuffix` are both part of the `strings` package.
These seem like generally useful validations to include and cover some
subset of the use-cases of a general regex validator without having any
of the problems outlined by the validator docs.
pull/459/head
Tyler Cipriani 5 years ago
parent b199fa0642
commit 3945da16ee
  1. 12
      baked_in.go
  2. 12
      doc.go
  3. 56
      validator_test.go

@ -112,6 +112,8 @@ var (
"excludes": excludes,
"excludesall": excludesAll,
"excludesrune": excludesRune,
"startswith": startsWith,
"endswith": endsWith,
"isbn": isISBN,
"isbn10": isISBN10,
"isbn13": isISBN13,
@ -650,6 +652,16 @@ func contains(fl FieldLevel) bool {
return strings.Contains(fl.Field().String(), fl.Param())
}
// StartsWith is the validation function for validating that the field's value starts with the text specified within the param.
func startsWith(fl FieldLevel) bool {
return strings.HasPrefix(fl.Field().String(), fl.Param())
}
// EndsWith is the validation function for validating that the field's value ends with the text specified within the param.
func endsWith(fl FieldLevel) bool {
return strings.HasSuffix(fl.Field().String(), fl.Param())
}
// FieldContains is the validation function for validating if the current field's value contains the field specified by the param's value.
func fieldContains(fl FieldLevel) bool {
field := fl.Field()

@ -714,6 +714,18 @@ This validates that a string value does not contain the supplied rune value.
Usage: excludesrune=@
Starts With
This validates that a string value starts with the supplied string value
Usage: startswith=hello
Ends With
This validates that a string value ends with the supplied string value
Usage: endswith=goodbye
International Standard Book Number
This validates that a string value contains a valid isbn10 or isbn13 value.

@ -8555,3 +8555,59 @@ func TestDirValidation(t *testing.T) {
validate.Var(2, "dir")
}, "Bad field type int")
}
func TestStartsWithValidation(t *testing.T) {
tests := []struct {
Value string `validate:"startswith=(/^ヮ^)/*:・゚✧"`
Tag string
ExpectedNil bool
}{
{Value: "(/^ヮ^)/*:・゚✧ glitter", Tag: "startswith=(/^ヮ^)/*:・゚✧", ExpectedNil: true},
{Value: "abcd", Tag: "startswith=(/^ヮ^)/*:・゚✧", ExpectedNil: false},
}
validate := New()
for i, s := range tests {
errs := validate.Var(s.Value, s.Tag)
if (s.ExpectedNil && errs != nil) || (!s.ExpectedNil && errs == nil) {
t.Fatalf("Index: %d failed Error: %s", i, errs)
}
errs = validate.Struct(s)
if (s.ExpectedNil && errs != nil) || (!s.ExpectedNil && errs == nil) {
t.Fatalf("Index: %d failed Error: %s", i, errs)
}
}
}
func TestEndsWithValidation(t *testing.T) {
tests := []struct {
Value string `validate:"endswith=(/^ヮ^)/*:・゚✧"`
Tag string
ExpectedNil bool
}{
{Value: "glitter (/^ヮ^)/*:・゚✧", Tag: "endswith=(/^ヮ^)/*:・゚✧", ExpectedNil: true},
{Value: "(/^ヮ^)/*:・゚✧ glitter", Tag: "endswith=(/^ヮ^)/*:・゚✧", ExpectedNil: false},
}
validate := New()
for i, s := range tests {
errs := validate.Var(s.Value, s.Tag)
if (s.ExpectedNil && errs != nil) || (!s.ExpectedNil && errs == nil) {
t.Fatalf("Index: %d failed Error: %s", i, errs)
}
errs = validate.Struct(s)
if (s.ExpectedNil && errs != nil) || (!s.ExpectedNil && errs == nil) {
t.Fatalf("Index: %d failed Error: %s", i, errs)
}
}
}

Loading…
Cancel
Save