|
1
|
|
|
package it |
|
2
|
|
|
|
|
3
|
|
|
import ( |
|
4
|
|
|
"context" |
|
5
|
|
|
|
|
6
|
|
|
"github.com/muonsoft/validation" |
|
7
|
|
|
"github.com/muonsoft/validation/is" |
|
8
|
|
|
"github.com/muonsoft/validation/validate" |
|
9
|
|
|
) |
|
10
|
|
|
|
|
11
|
|
|
// IsULID validates whether the value is a valid ULID (Universally Unique Lexicographically Sortable Identifier). |
|
12
|
|
|
// See https://github.com/ulid/spec for ULID specifications. |
|
13
|
|
|
func IsULID() validation.StringFuncConstraint { |
|
14
|
|
|
return validation.OfStringBy(is.ULID). |
|
15
|
|
|
WithError(validation.ErrInvalidULID). |
|
16
|
|
|
WithMessage(validation.ErrInvalidULID.Message()) |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
// UUIDConstraint validates whether a string value is a valid UUID (also known as GUID). |
|
20
|
|
|
// |
|
21
|
|
|
// By default, it uses strict mode and checks the UUID as specified in RFC 4122. |
|
22
|
|
|
// To parse additional formats, use the [UUIDConstraint.NonCanonical] method. |
|
23
|
|
|
// |
|
24
|
|
|
// In addition, it checks if the UUID version matches one of |
|
25
|
|
|
// the registered versions: 1, 2, 3, 4, 5, 6 or 7. |
|
26
|
|
|
// Use [UUIDConstraint.WithVersions] to validate for a specific set of versions. |
|
27
|
|
|
// |
|
28
|
|
|
// Nil UUID ("00000000-0000-0000-0000-000000000000") values are considered as valid. |
|
29
|
|
|
// Use [UUIDConstraint.NotNil] to disallow nil value. |
|
30
|
|
|
// |
|
31
|
|
|
// See http://tools.ietf.org/html/rfc4122 for specifications. |
|
32
|
|
|
type UUIDConstraint struct { |
|
33
|
|
|
isIgnored bool |
|
34
|
|
|
groups []string |
|
35
|
|
|
options []func(o *validate.UUIDOptions) |
|
36
|
|
|
err error |
|
37
|
|
|
messageTemplate string |
|
38
|
|
|
messageParameters validation.TemplateParameterList |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
// IsUUID validates whether a string value is a valid UUID (also known as GUID). |
|
42
|
|
|
// See [UUIDConstraint] for more info. |
|
43
|
|
|
func IsUUID() UUIDConstraint { |
|
44
|
|
|
return UUIDConstraint{ |
|
45
|
|
|
err: validation.ErrInvalidUUID, |
|
46
|
|
|
messageTemplate: validation.ErrInvalidUUID.Message(), |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
// NotNil used to treat nil UUID ("00000000-0000-0000-0000-000000000000") value as invalid. |
|
51
|
|
|
func (c UUIDConstraint) NotNil() UUIDConstraint { |
|
52
|
|
|
c.options = append(c.options, validate.DenyNilUUID()) |
|
53
|
|
|
return c |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
// NonCanonical used to enable parsing UUID value from non-canonical formats. |
|
57
|
|
|
// |
|
58
|
|
|
// Following formats are supported: |
|
59
|
|
|
// - "6ba7b810-9dad-11d1-80b4-00c04fd430c8", |
|
60
|
|
|
// - "{6ba7b810-9dad-11d1-80b4-00c04fd430c8}", |
|
61
|
|
|
// - "urn:uuid:6ba7b810-9dad-11d1-80b4-00c04fd430c8" |
|
62
|
|
|
// - "6ba7b8109dad11d180b400c04fd430c8" |
|
63
|
|
|
// - "{6ba7b8109dad11d180b400c04fd430c8}", |
|
64
|
|
|
// - "urn:uuid:6ba7b8109dad11d180b400c04fd430c8". |
|
65
|
|
|
func (c UUIDConstraint) NonCanonical() UUIDConstraint { |
|
66
|
|
|
c.options = append(c.options, validate.AllowNonCanonicalUUIDFormats()) |
|
67
|
|
|
return c |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
// WithVersions used to set valid versions of the UUID value. |
|
71
|
|
|
// By default, the UUID will be checked for compliance with the default |
|
72
|
|
|
// registered versions: 1, 2, 3, 4, 5, 6 or 7. |
|
73
|
|
|
func (c UUIDConstraint) WithVersions(versions ...byte) UUIDConstraint { |
|
74
|
|
|
c.options = append(c.options, validate.AllowUUIDVersions(versions...)) |
|
75
|
|
|
return c |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
// WithError overrides default error for produced violation. |
|
79
|
|
|
func (c UUIDConstraint) WithError(err error) UUIDConstraint { |
|
80
|
|
|
c.err = err |
|
81
|
|
|
return c |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
// WithMessage sets the violation message template. You can set custom template parameters |
|
85
|
|
|
// for injecting its values into the final message. Also, you can use default parameters: |
|
86
|
|
|
// |
|
87
|
|
|
// {{ value }} - the current (invalid) value. |
|
88
|
|
|
func (c UUIDConstraint) WithMessage(template string, parameters ...validation.TemplateParameter) UUIDConstraint { |
|
89
|
|
|
c.messageTemplate = template |
|
90
|
|
|
c.messageParameters = parameters |
|
91
|
|
|
return c |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
// When enables conditional validation of this constraint. If the expression evaluates to false, |
|
95
|
|
|
// then the constraint will be ignored. |
|
96
|
|
|
func (c UUIDConstraint) When(condition bool) UUIDConstraint { |
|
97
|
|
|
c.isIgnored = !condition |
|
98
|
|
|
return c |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
// WhenGroups enables conditional validation of the constraint by using the validation groups. |
|
102
|
|
|
func (c UUIDConstraint) WhenGroups(groups ...string) UUIDConstraint { |
|
103
|
|
|
c.groups = groups |
|
104
|
|
|
return c |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
func (c UUIDConstraint) ValidateString(ctx context.Context, validator *validation.Validator, value *string) error { |
|
108
|
|
|
if c.isIgnored || validator.IsIgnoredForGroups(c.groups...) || value == nil || *value == "" { |
|
109
|
|
|
return nil |
|
110
|
|
|
} |
|
111
|
|
|
if is.UUID(*value, c.options...) { |
|
112
|
|
|
return nil |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
return validator.BuildViolation(ctx, c.err, c.messageTemplate). |
|
116
|
|
|
WithParameters( |
|
117
|
|
|
c.messageParameters.Prepend( |
|
118
|
|
|
validation.TemplateParameter{Key: "{{ value }}", Value: *value}, |
|
119
|
|
|
)..., |
|
120
|
|
|
). |
|
121
|
|
|
Create() |
|
122
|
|
|
} |
|
123
|
|
|
|