-
Notifications
You must be signed in to change notification settings - Fork 5
/
uuid.go
32 lines (27 loc) · 960 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
package govalidator
import "github.com/google/uuid"
const (
// UUID represents rule name which will be used to find the default error message.
UUID = "uuid"
// UUIDMsg is the default error message format for fields with UUID validation rule.
UUIDMsg = "%s is not a valid UUID"
)
// UUID validates that the field under validation is a valid RFC 4122 universally unique identifier (UUID).
// It accepts non-standard strings such as raw hex encoding xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
// and 38 byte "Microsoft style" encodings, e.g., {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}.
//
// Example:
//
// v := validator.New()
// v.UUID("f47ac10b-58cc-4372-a567-0e02b2c3d479", "uuid", "Invalid UUID format.")
// if v.IsFailed() {
// fmt.Printf("validation errors: %#v\n", v.Errors())
// }
func (v Validator) UUID(u, field, msg string) Validator {
_, err := uuid.Parse(u)
if err != nil {
v.check(false, field, v.msg(UUID, msg, field))
return v
}
return v
}