-
Notifications
You must be signed in to change notification settings - Fork 2
constraint.StringExactLength
marrow16 edited this page Jan 21, 2023
·
7 revisions
Check that a string has an exact length
Note: By default, this constraint is non-strict - if the value being checked is not a string, this constraint does not fail (unless the Strict field is set)
strxlen
| Field | Type | Description |
|---|---|---|
Value |
int | the exact length expected |
UseRuneLen |
bool | if set to true, uses the rune length (true Unicode length) to check length of string |
NormalisationForm |
string | is the optional unicode normalisation form to be used prior to checking length (no unicode normalisation is performed if this is empty or unknown form) |
Stop |
bool | when set to true, Stop prevents further validation checks on the property if this constraint fails |
Strict |
bool | when set to true, fails if the value being checked is not a correct type |
Programmatic example...
package main
import (
"fmt"
"github.com/marrow16/valix"
)
func main() {
validator := &valix.Validator{
Properties: valix.Properties{
"foo": {
Type: valix.JsonString,
Constraints: valix.Constraints{
&valix.StringExactLength{
Value: 1,
UseRuneLen: true,
NormalisationForm: "NFC",
},
},
},
},
}
// NB. "\u0063\u0327" is 'c' followed by combining cedilla
ok, violations, _ := validator.ValidateString("{\"foo\": \"\u0063\u0327\"}")
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)
}
// NB. "\u00e7" is a 'c' with cedilla
ok, violations, _ = validator.ValidateString("{\"foo\": \"\u00e7\"}")
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)
}
// NB. "\u00b8" is a cedilla (non-combining)
ok, violations, _ = validator.ValidateString("{\"foo\": \"c\u00b8\"}")
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:"&strxlen{Value:1, UseRuneLen:true, NormalisationForm: 'NFC'}"`
}
var validator = valix.MustCompileValidatorFor(MyStruct{}, nil)
func main() {
my := &MyStruct{}
// NB. "\u0063\u0327" is 'c' followed by combining cedilla
ok, violations, _ := validator.ValidateStringInto("{\"foo\": \"\u0063\u0327\"}", 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)
}
// NB. "\u00e7" is a 'c' with cedilla
ok, violations, _ = validator.ValidateStringInto("{\"foo\": \"\u00e7\"}", 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)
}
// NB. "\u00b8" is a cedilla (non-combining)
ok, violations, _ = validator.ValidateStringInto("{\"foo\": \"c\u00b8\"}", 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)
}
}