-
Notifications
You must be signed in to change notification settings - Fork 2
constraint.StringStartsWith
marrow16 edited this page Jan 21, 2023
·
6 revisions
Check that a string starts with a given prefix
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)
starts
Field | Type | Description |
---|---|---|
Value |
string | the value to check that the string starts with |
Values |
[]string | multiple additional values that the string may start with |
CaseInsensitive |
bool | whether the check is case-insensitive (by default, the check is case-sensitive) |
Not |
bool | whether the check is NOT-ed (i.e. checks that the string does not start with) |
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 |
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.StringStartsWith{
Value: "foo",
CaseInsensitive: true,
},
},
},
},
}
ok, violations, _ := validator.ValidateString(`{"foo": "there isn't a foo at the start of this"}`)
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": "Foo is at the start"}`)
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:"&starts{Value:'foo', insensitive:true}"`
}
var validator = valix.MustCompileValidatorFor(MyStruct{}, nil)
func main() {
my := &MyStruct{}
ok, violations, _ := validator.ValidateStringInto(`{"foo": "there isn't a foo at the start of this"}`, 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": "Foo is at the start"}`, 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)
}
}