forked from hashicorp/terraform-provider-azurerm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uuid.go
38 lines (30 loc) · 834 Bytes
/
uuid.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package validate
import (
"fmt"
"regexp"
"github.com/hashicorp/go-uuid"
)
//uuid regex helper
var UUIDRegExp = regexp.MustCompile("^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-4[a-fA-F0-9]{3}-[8|9|aA|bB][a-fA-F0-9]{3}-[a-fA-F0-9]{12}$")
func UUID(i interface{}, k string) (warnings []string, errors []error) {
v, ok := i.(string)
if !ok {
errors = append(errors, fmt.Errorf("expected type of %q to be string", k))
return
}
if _, err := uuid.ParseUUID(v); err != nil {
errors = append(errors, fmt.Errorf("%q isn't a valid UUID (%q): %+v", k, v, err))
}
return warnings, errors
}
func UUIDOrEmpty(i interface{}, k string) (warnings []string, errors []error) {
v, ok := i.(string)
if !ok {
errors = append(errors, fmt.Errorf("expected type of %q to be string", k))
return
}
if v == "" {
return
}
return UUID(i, k)
}