1 // Package validation provides format validation functions.
2 package validation
3
4 import (
5 "net/url"
6 "regexp"
7 )
8
9 var (
10 isUUIDRegexp = 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}$")
11 isRegionRegex = regexp.MustCompile("^[a-z]{2}-[a-z]{3,7}$")
12 isZoneRegex = regexp.MustCompile("^[a-z]{2}-[a-z]{3,7}-[0-9]{1,2}$")
13 isAccessKey = regexp.MustCompile("^SCW[A-Z0-9]{17}$")
14 isEmailRegexp = regexp.MustCompile("^.+@.+$")
15 )
16
17 // IsUUID returns true if the given string has a valid UUID format.
18 func IsUUID(s string) bool {
19 return isUUIDRegexp.MatchString(s)
20 }
21
22 // IsAccessKey returns true if the given string has a valid Scaleway access key format.
23 func IsAccessKey(s string) bool {
24 return isAccessKey.MatchString(s)
25 }
26
27 // IsSecretKey returns true if the given string has a valid Scaleway secret key format.
28 func IsSecretKey(s string) bool {
29 return IsUUID(s)
30 }
31
32 // IsOrganizationID returns true if the given string has a valid Scaleway organization ID format.
33 func IsOrganizationID(s string) bool {
34 return IsUUID(s)
35 }
36
37 // IsProjectID returns true if the given string has a valid Scaleway project ID format.
38 func IsProjectID(s string) bool {
39 return IsUUID(s)
40 }
41
42 // IsRegion returns true if the given string has a valid region format.
43 func IsRegion(s string) bool {
44 return isRegionRegex.MatchString(s)
45 }
46
47 // IsZone returns true if the given string has a valid zone format.
48 func IsZone(s string) bool {
49 return isZoneRegex.MatchString(s)
50 }
51
52 // IsURL returns true if the given string has a valid URL format.
53 func IsURL(s string) bool {
54 if s == "" {
55 return false
56 }
57
58 _, err := url.Parse(s)
59 return err == nil
60 }
61
62 // IsEmail returns true if the given string has an email format.
63 func IsEmail(v string) bool {
64 return isEmailRegexp.MatchString(v)
65 }
66