summaryrefslogtreecommitdiff
path: root/internal/configuration/schema/validator_test.go
blob: 2e1371b20299dc623ddcfd3990d051ea73cb1063 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package schema_test

import (
	"fmt"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"

	"github.com/authelia/authelia/internal/configuration/schema"
)

type TestNestedStruct struct {
	MustBe5 int
}

func (tns *TestNestedStruct) Validate(validator *schema.StructValidator) {
	if tns.MustBe5 != 5 {
		validator.Push(fmt.Errorf("MustBe5 must be 5"))
	}
}

type TestStruct struct {
	MustBe10       int
	ShouldBeAbove5 int
	NotEmpty       string
	SetDefault     string
	Nested         TestNestedStruct
	Nested2        TestNestedStruct
	NilPtr         *int
	NestedPtr      *TestNestedStruct
}

func (ts *TestStruct) Validate(validator *schema.StructValidator) {
	if ts.MustBe10 != 10 {
		validator.Push(fmt.Errorf("MustBe10 must be 10"))
	}

	if ts.NotEmpty == "" {
		validator.Push(fmt.Errorf("NotEmpty must not be empty"))
	}

	if ts.ShouldBeAbove5 <= 5 {
		validator.PushWarning(fmt.Errorf("ShouldBeAbove5 should be above 5"))
	}

	if ts.SetDefault == "" {
		ts.SetDefault = "xyz"
	}
}

func TestValidator(t *testing.T) {
	validator := schema.NewValidator()

	s := TestStruct{
		MustBe10:  5,
		NotEmpty:  "",
		NestedPtr: &TestNestedStruct{},
	}

	err := validator.Validate(&s)
	if err != nil {
		panic(err)
	}

	errs := validator.Errors()
	assert.Equal(t, 4, len(errs))

	assert.Equal(t, 2, len(errs["root"]))
	assert.ElementsMatch(t, []error{
		fmt.Errorf("MustBe10 must be 10"),
		fmt.Errorf("NotEmpty must not be empty")}, errs["root"])

	assert.Equal(t, 1, len(errs["root.Nested"]))
	assert.ElementsMatch(t, []error{
		fmt.Errorf("MustBe5 must be 5")}, errs["root.Nested"])

	assert.Equal(t, 1, len(errs["root.Nested2"]))
	assert.ElementsMatch(t, []error{
		fmt.Errorf("MustBe5 must be 5")}, errs["root.Nested2"])

	assert.Equal(t, 1, len(errs["root.NestedPtr"]))
	assert.ElementsMatch(t, []error{
		fmt.Errorf("MustBe5 must be 5")}, errs["root.NestedPtr"])

	assert.Equal(t, "xyz", s.SetDefault)
}

func TestStructValidator(t *testing.T) {
	validator := schema.NewStructValidator()
	s := TestStruct{
		MustBe10:       5,
		ShouldBeAbove5: 2,
		NotEmpty:       "",
		NestedPtr:      &TestNestedStruct{},
	}
	s.Validate(validator)

	assert.True(t, validator.HasWarnings())
	assert.True(t, validator.HasErrors())

	require.Len(t, validator.Warnings(), 1)
	require.Len(t, validator.Errors(), 2)

	assert.EqualError(t, validator.Warnings()[0], "ShouldBeAbove5 should be above 5")
	assert.EqualError(t, validator.Errors()[0], "MustBe10 must be 10")
	assert.EqualError(t, validator.Errors()[1], "NotEmpty must not be empty")

	validator.Clear()

	assert.False(t, validator.HasWarnings())
	assert.False(t, validator.HasErrors())

	assert.Len(t, validator.Warnings(), 0)
	assert.Len(t, validator.Errors(), 0)
}