179 lines
3.1 KiB
Go
179 lines
3.1 KiB
Go
package validator
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
// testStruct is a sample struct with validation tags for testing.
|
|
type testStruct struct {
|
|
Name string `validate:"required"`
|
|
Email string `validate:"required,email"`
|
|
Age int `validate:"gte=0,lte=130"`
|
|
}
|
|
|
|
func TestInit(t *testing.T) {
|
|
// Should not panic
|
|
Init()
|
|
// validate should be set
|
|
assert.NotNil(t, validate)
|
|
}
|
|
|
|
func TestValidateStruct_Valid(t *testing.T) {
|
|
// Ensure initialized
|
|
Init()
|
|
|
|
s := testStruct{
|
|
Name: "Alice",
|
|
Email: "alice@example.com",
|
|
Age: 30,
|
|
}
|
|
err := ValidateStruct(s)
|
|
assert.NoError(t, err)
|
|
}
|
|
|
|
func TestValidateStruct_Invalid_MissingRequired(t *testing.T) {
|
|
Init()
|
|
|
|
s := testStruct{
|
|
Name: "",
|
|
Email: "alice@example.com",
|
|
Age: 30,
|
|
}
|
|
err := ValidateStruct(s)
|
|
assert.Error(t, err)
|
|
}
|
|
|
|
func TestValidateStruct_Invalid_BadEmail(t *testing.T) {
|
|
Init()
|
|
|
|
s := testStruct{
|
|
Name: "Alice",
|
|
Email: "not-an-email",
|
|
Age: 30,
|
|
}
|
|
err := ValidateStruct(s)
|
|
assert.Error(t, err)
|
|
}
|
|
|
|
func TestValidateStruct_Invalid_AgeOutOfRange(t *testing.T) {
|
|
Init()
|
|
|
|
s := testStruct{
|
|
Name: "Alice",
|
|
Email: "alice@example.com",
|
|
Age: 200,
|
|
}
|
|
err := ValidateStruct(s)
|
|
assert.Error(t, err)
|
|
}
|
|
|
|
func TestValidateStruct_Invalid_NegativeAge(t *testing.T) {
|
|
Init()
|
|
|
|
s := testStruct{
|
|
Name: "Alice",
|
|
Email: "alice@example.com",
|
|
Age: -1,
|
|
}
|
|
err := ValidateStruct(s)
|
|
assert.Error(t, err)
|
|
}
|
|
|
|
func TestValidateStruct_AutoInit(t *testing.T) {
|
|
// Reset validate to nil to test auto-initialization
|
|
validate = nil
|
|
|
|
s := testStruct{
|
|
Name: "Bob",
|
|
Email: "bob@example.com",
|
|
Age: 25,
|
|
}
|
|
err := ValidateStruct(s)
|
|
assert.NoError(t, err)
|
|
// validate should have been auto-initialized
|
|
assert.NotNil(t, validate)
|
|
}
|
|
|
|
func TestVar_Valid(t *testing.T) {
|
|
Init()
|
|
|
|
err := Var("test@example.com", "required,email")
|
|
assert.NoError(t, err)
|
|
}
|
|
|
|
func TestVar_Invalid(t *testing.T) {
|
|
Init()
|
|
|
|
err := Var("not-an-email", "email")
|
|
assert.Error(t, err)
|
|
}
|
|
|
|
func TestVar_Required_Empty(t *testing.T) {
|
|
Init()
|
|
|
|
err := Var("", "required")
|
|
assert.Error(t, err)
|
|
}
|
|
|
|
func TestVar_MinLength(t *testing.T) {
|
|
Init()
|
|
|
|
err := Var("ab", "min=3")
|
|
assert.Error(t, err)
|
|
|
|
err = Var("abc", "min=3")
|
|
assert.NoError(t, err)
|
|
}
|
|
|
|
func TestVar_AutoInit(t *testing.T) {
|
|
// Reset validate to nil to test auto-initialization
|
|
validate = nil
|
|
|
|
err := Var("test@example.com", "email")
|
|
assert.NoError(t, err)
|
|
assert.NotNil(t, validate)
|
|
}
|
|
|
|
func TestValidateStruct_Pointer(t *testing.T) {
|
|
Init()
|
|
|
|
s := &testStruct{
|
|
Name: "Alice",
|
|
Email: "alice@example.com",
|
|
Age: 30,
|
|
}
|
|
err := ValidateStruct(s)
|
|
assert.NoError(t, err)
|
|
}
|
|
|
|
func TestValidateStruct_InvalidPointer(t *testing.T) {
|
|
Init()
|
|
|
|
s := &testStruct{
|
|
Name: "",
|
|
Email: "bad",
|
|
Age: 500,
|
|
}
|
|
err := ValidateStruct(s)
|
|
assert.Error(t, err)
|
|
}
|
|
|
|
func TestValidateStruct_NonStruct(t *testing.T) {
|
|
Init()
|
|
|
|
// Validating a non-struct with Struct() should return an error
|
|
err := ValidateStruct("just a string")
|
|
require.Error(t, err)
|
|
}
|
|
|
|
func TestValidateStruct_Nil(t *testing.T) {
|
|
Init()
|
|
|
|
// validate.Struct(nil) returns an error
|
|
err := ValidateStruct(nil)
|
|
assert.Error(t, err)
|
|
}
|