From 8c72d66d872a14d0139f6c8a43b53b1a4c866985 Mon Sep 17 00:00:00 2001 From: Hayden <64056131+hay-kot@users.noreply.github.com> Date: Mon, 6 Mar 2023 20:55:29 -0900 Subject: [PATCH] add custom validator --- backend/internal/sys/validate/validate.go | 41 ++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/backend/internal/sys/validate/validate.go b/backend/internal/sys/validate/validate.go index 4a4b7a9..3943b48 100644 --- a/backend/internal/sys/validate/validate.go +++ b/backend/internal/sys/validate/validate.go @@ -1,11 +1,50 @@ package validate -import "github.com/go-playground/validator/v10" +import ( + "strings" + + "github.com/go-playground/validator/v10" +) var validate *validator.Validate func init() { validate = validator.New() + + validate.RegisterValidation("shoutrrr", func(fl validator.FieldLevel) bool { + prefixes := [...]string{ + "discord://", + "smtp://", + "gotify://", + "googlechat://", + "ifttt://", + "join://", + "mattermost://", + "matrix://", + "opsgenie://", + "pushbullet://", + "pushover://", + "rocketchat://", + "slack://", + "teams://", + "telegram://", + "zulip://", + } + + str := fl.Field().String() + if str == "" { + return false + } + + for _, prefix := range prefixes { + if strings.HasPrefix(str, prefix) { + return true + } + } + + return false + }) + } // Checks a struct for validation errors and returns any errors the occur. This