-
Notifications
You must be signed in to change notification settings - Fork 2
constraint.LessThan
marrow16 edited this page Jan 21, 2023
·
5 revisions
Check that a numeric value is less than a specified value
Note: This constraint is stricter than Minimum, Maximum and Range constraints in that if the property value is not a numeric then this constraint fails
lt
Field | Type | Description |
---|---|---|
Value |
flot64 | the value to compare against |
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{
Properties: valix.Properties{
"foo": {
Type: valix.JsonNumber,
Constraints: valix.Constraints{
&valix.LessThan{
Value: 10,
},
},
},
},
}
ok, violations, _ := validator.ValidateString(`{"foo": 15.0}`)
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": 10}`)
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": 9.999}`)
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 float64 `json:"foo" v8n:"<{10}"`
}
var validator = valix.MustCompileValidatorFor(MyStruct{}, nil)
func main() {
my := &MyStruct{}
ok, violations, _ := validator.ValidateStringInto(`{"foo": 15.0}`, 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": 10}`, 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": 9.999}`, 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)
}
}