diff --git a/urns/urns.go b/urns/urns.go index a956a02..d95ca8d 100644 --- a/urns/urns.go +++ b/urns/urns.go @@ -23,6 +23,9 @@ const ( // FCMScheme is the scheme used for Firebase Cloud Messaging identifiers FCMScheme string = "fcm" + // FreshChatScheme is the scheme used for FreshChat Cloud Messaging identifiers + FreshChatScheme string = "freshchat" + // JiochatScheme is the scheme used for Jiochat identifiers JiochatScheme string = "jiochat" @@ -60,6 +63,7 @@ var ValidSchemes = map[string]bool{ ExternalScheme: true, FacebookScheme: true, FCMScheme: true, + FreshChatScheme: true, JiochatScheme: true, LineScheme: true, TelegramScheme: true, @@ -84,6 +88,7 @@ var emailRegex = regexp.MustCompile(`^[^\s@]+@[^\s@]+$`) var viberRegex = regexp.MustCompile(`^[a-zA-Z0-9_=/+]{1,24}$`) var lineRegex = regexp.MustCompile(`^[a-zA-Z0-9_]{1,36}$`) var allDigitsRegex = regexp.MustCompile(`^[0-9]+$`) +var freshchatRegex = regexp.MustCompile(`^[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}/[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}$`) // URN represents a Universal Resource Name, we use this for contact identifiers like phone numbers etc.. type URN string @@ -249,9 +254,15 @@ func (u URN) Validate() error { if !allDigitsRegex.MatchString(path) { return fmt.Errorf("invalid whatsapp id: %s", path) } - } + case FreshChatScheme: + // validate path and query is a uuid + if !freshchatRegex.MatchString(path) { + return fmt.Errorf("invalid freshchat id: %s", path) + } + } return nil // anything goes for external schemes + } // Scheme returns the scheme portion for the URN diff --git a/urns/urns_test.go b/urns/urns_test.go index 9d0c8d5..f9ac8bd 100644 --- a/urns/urns_test.go +++ b/urns/urns_test.go @@ -212,7 +212,7 @@ func TestValidate(t *testing.T) { // invalid tel numbers {"tel:07883 83383", "invalid tel number"}, // can't have spaces - {"tel:", "cannot be empty"}, // need a path + {"tel:", "cannot be empty"}, // need a path // twitter handles {"twitter:jimmyjo", ""}, @@ -259,6 +259,11 @@ func TestValidate(t *testing.T) { {"whatsapp:12354", ""}, {"whatsapp:abcde", "invalid whatsapp id"}, {"whatsapp:+12067799294", "invalid whatsapp id"}, + + // freschat has to be two uuids separated by a colon + {"freshchat:6a2f41a3-c54c-fce8-32d2-0324e1c32e22/6a2f41a3-c54c-fce8-32d2-0324e1c32e22", ""}, + {"freshchat:6a2f41a3-c54c-fce8-32d2-0324e1c32e22", "invalid freshchat id"}, + {"freshchat:+12067799294", "invalid freshchat id"}, } for _, tc := range testCases {