Skip to content

Commit

Permalink
Merge pull request #583 from metalinspired/master
Browse files Browse the repository at this point in the history
Add validators that check if string does not start/end with supplied parameter
  • Loading branch information
Dean Karn authored May 21, 2020
2 parents 07f2314 + f3b347c commit dbfcad2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
12 changes: 12 additions & 0 deletions baked_in.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ var (
"excludesrune": excludesRune,
"startswith": startsWith,
"endswith": endsWith,
"startsnotwith": startsNotWith,
"endsnotwith": endsNotWith,
"isbn": isISBN,
"isbn10": isISBN10,
"isbn13": isISBN13,
Expand Down Expand Up @@ -690,6 +692,16 @@ func endsWith(fl FieldLevel) bool {
return strings.HasSuffix(fl.Field().String(), fl.Param())
}

// StartsNotWith is the validation function for validating that the field's value does not start with the text specified within the param.
func startsNotWith(fl FieldLevel) bool {
return !startsWith(fl)
}

// EndsNotWith is the validation function for validating that the field's value does not end with the text specified within the param.
func endsNotWith(fl FieldLevel) bool {
return !endsWith(fl)
}

// 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()
Expand Down
12 changes: 12 additions & 0 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -814,6 +814,18 @@ This validates that a string value ends with the supplied string value
Usage: endswith=goodbye
Does Not Start With
This validates that a string value does not start with the supplied string value
Usage: startsnotwith=hello
Does Not End With
This validates that a string value does not end with the supplied string value
Usage: endsnotwith=goodbye
International Standard Book Number
This validates that a string value contains a valid isbn10 or isbn13 value.
Expand Down

0 comments on commit dbfcad2

Please sign in to comment.