-
Notifications
You must be signed in to change notification settings - Fork 2
constraint.IsNotNull
marrow16 edited this page Jan 21, 2023
·
2 revisions
Is a utility constraint to check that a value is not null
Normally, null checking would be performed by the PropertyValidator.NotNull setting - however, it may be the case that null is only disallowed under certain conditions
notnull
| Field | Type | Description |
|---|---|---|
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.JsonAny,
Constraints: valix.Constraints{
&valix.IsNotNull{},
},
},
},
}
ok, violations, _ := validator.ValidateString(`{"foo": null}`)
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": "not null"}`)
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 interface{} `json:"foo" v8n:"¬null"`
}
var validator = valix.MustCompileValidatorFor(MyStruct{})
func main() {
my := &MyStruct{}
ok, violations, _ := validator.ValidateStringInto(`{"foo": null}`, 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": "not null"}`, 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)
}
}