Merge pull request #459 from thcipriani/v9

Add `startswith` and `endswith` validators
pull/480/head
Dean Karn 6 years ago committed by GitHub
commit 9980009549
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 12
      baked_in.go
  2. 12
      doc.go
  3. 56
      validator_test.go

@ -112,6 +112,8 @@ var (
"excludes": excludes, "excludes": excludes,
"excludesall": excludesAll, "excludesall": excludesAll,
"excludesrune": excludesRune, "excludesrune": excludesRune,
"startswith": startsWith,
"endswith": endsWith,
"isbn": isISBN, "isbn": isISBN,
"isbn10": isISBN10, "isbn10": isISBN10,
"isbn13": isISBN13, "isbn13": isISBN13,
@ -650,6 +652,16 @@ func contains(fl FieldLevel) bool {
return strings.Contains(fl.Field().String(), fl.Param()) 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. // 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 { func fieldContains(fl FieldLevel) bool {
field := fl.Field() field := fl.Field()

@ -714,6 +714,18 @@ This validates that a string value does not contain the supplied rune value.
Usage: excludesrune=@ 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 International Standard Book Number
This validates that a string value contains a valid isbn10 or isbn13 value. 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") validate.Var(2, "dir")
}, "Bad field type int") }, "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