-
Notifications
You must be signed in to change notification settings - Fork 2
constraint.StringValidUuid
marrow16 edited this page Jan 21, 2023
·
5 revisions
Check that a string value is a valid UUID
struuid
(see also StringPresetPattern presets uuid, UUID, uuid1, UUID1 etc.)
| Field | Type | Description |
|---|---|---|
MinVersion |
uint8 | the minimum UUID version (optional - if zero this is not checked) |
SpecificVersion |
uint8 | the specific UUID version (optional - if zero this is not checked) |
Message |
string | the violation message to be used if the constraint fails. If empty, the default message is used |
Stop |
bool | when set to true, Stop prevents further validation checks on the property if this constraint fails |
Programmatic example...
package main
import (
"fmt"
"github.com/marrow16/valix"
)
func main() {
validator := &valix.Validator{
UseNumber: true,
Properties: valix.Properties{
"foo": {
Type: valix.JsonAny,
Constraints: valix.Constraints{
&valix.StringValidUuid{
SpecificVersion: 4,
},
},
},
},
}
ok, violations, _ := validator.ValidateString(`{"foo": "not a uuid"}`)
fmt.Printf("Passed? %v\n", ok)
for i, v := range violations {
fmt.Printf("Violation[%d] Message: %s, Property: %s, Path: %s\n", i+1, v.Message, v.Property, v.Path)
}
ok, violations, _ = validator.ValidateString(`{"foo": "538c6b1a-5114-11ed-bdc3-0242ac120002"}`)
fmt.Printf("Passed? %v\n", ok)
for i, v := range violations {
fmt.Printf("Violation[%d] Message: %s, Property: %s, Path: %s\n", i+1, v.Message, v.Property, v.Path)
}
ok, violations, _ = validator.ValidateString(`{"foo": "6c395bc3-b0e4-474a-8736-d5157a441b0d"}`)
fmt.Printf("Passed? %v\n", ok)
for i, v := range violations {
fmt.Printf("Violation[%d] Message: %s, Property: %s, Path: %s\n", i+1, v.Message, v.Property, v.Path)
}
}Struct v8n tag example...
package main
import (
"fmt"
"github.com/marrow16/valix"
)
type MyStruct struct {
Foo string `json:"foo" v8n:"&struuid{SpecificVersion: 4}"`
}
var validator = valix.MustCompileValidatorFor(MyStruct{})
func main() {
my := &MyStruct{}
ok, violations, _ := validator.ValidateStringInto(`{"foo": "not a uuid"}`, my)
fmt.Printf("Passed? %v\n", ok)
for i, v := range violations {
fmt.Printf("Violation[%d] Message: %s, Property: %s, Path: %s\n", i+1, v.Message, v.Property, v.Path)
}
ok, violations, _ = validator.ValidateStringInto(`{"foo": "538c6b1a-5114-11ed-bdc3-0242ac120002"}`, my)
fmt.Printf("Passed? %v\n", ok)
for i, v := range violations {
fmt.Printf("Violation[%d] Message: %s, Property: %s, Path: %s\n", i+1, v.Message, v.Property, v.Path)
}
ok, violations, _ = validator.ValidateStringInto(`{"foo": "6c395bc3-b0e4-474a-8736-d5157a441b0d"}`, my)
fmt.Printf("Passed? %v\n", ok)
for i, v := range violations {
fmt.Printf("Violation[%d] Message: %s, Property: %s, Path: %s\n", i+1, v.Message, v.Property, v.Path)
}
}