You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
65 lines
1.2 KiB
65 lines
1.2 KiB
package validators
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/go-playground/validator/v10"
|
|
"github.com/go-playground/assert/v2"
|
|
)
|
|
|
|
type test struct {
|
|
String string `validate:"notblank"`
|
|
Array []int `validate:"notblank"`
|
|
Pointer *int `validate:"notblank"`
|
|
Number int `validate:"notblank"`
|
|
Interface interface{} `validate:"notblank"`
|
|
Func func() `validate:"notblank"`
|
|
}
|
|
|
|
func TestNotBlank(t *testing.T) {
|
|
v := validator.New()
|
|
err := v.RegisterValidation("notblank", NotBlank)
|
|
assert.Equal(t, nil, err)
|
|
|
|
// Errors
|
|
var x *int
|
|
invalid := test{
|
|
String: " ",
|
|
Array: []int{},
|
|
Pointer: x,
|
|
Number: 0,
|
|
Interface: nil,
|
|
Func: nil,
|
|
}
|
|
fieldsWithError := []string{
|
|
"String",
|
|
"Array",
|
|
"Pointer",
|
|
"Number",
|
|
"Interface",
|
|
"Func",
|
|
}
|
|
|
|
errors := v.Struct(invalid).(validator.ValidationErrors)
|
|
var fields []string
|
|
for _, err := range errors {
|
|
fields = append(fields, err.Field())
|
|
}
|
|
|
|
assert.Equal(t, fieldsWithError, fields)
|
|
|
|
// No errors
|
|
y := 1
|
|
x = &y
|
|
valid := test{
|
|
String: "str",
|
|
Array: []int{1},
|
|
Pointer: x,
|
|
Number: 1,
|
|
Interface: "value",
|
|
Func: func() {},
|
|
}
|
|
|
|
err = v.Struct(valid)
|
|
assert.Equal(t, nil, err)
|
|
}
|
|
|