From f3b347cc2936cf035fc2b703f34fd087cdc8dd07 Mon Sep 17 00:00:00 2001 From: Milan Divkovic Date: Wed, 19 Feb 2020 10:15:03 +0100 Subject: [PATCH] Add validators that check if string does not start/end with supplied parameter --- baked_in.go | 12 ++++++++++++ doc.go | 12 ++++++++++++ 2 files changed, 24 insertions(+) diff --git a/baked_in.go b/baked_in.go index 4124c62..b504bff 100644 --- a/baked_in.go +++ b/baked_in.go @@ -120,6 +120,8 @@ var ( "excludesrune": excludesRune, "startswith": startsWith, "endswith": endsWith, + "startsnotwith": startsNotWith, + "endsnotwith": endsNotWith, "isbn": isISBN, "isbn10": isISBN10, "isbn13": isISBN13, @@ -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() diff --git a/doc.go b/doc.go index 291c629..380e148 100644 --- a/doc.go +++ b/doc.go @@ -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.