| 1 |
package yaml_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"strings" |
| 5 |
"testing" |
| 6 |
|
| 7 |
"github.com/go-playground/validator/v10" |
| 8 |
|
| 9 |
"github.com/goccy/go-yaml" |
| 10 |
) |
| 11 |
|
| 12 |
func TestStructValidator(t *testing.T) { |
| 13 |
type Inner struct { |
| 14 |
Required string `validate:"required"` |
| 15 |
Lt10 int `validate:"lt=10"` |
| 16 |
} |
| 17 |
|
| 18 |
cases := []struct { |
| 19 |
TestName string |
| 20 |
YAMLContent string |
| 21 |
ExpectedErr string |
| 22 |
Instance interface{} |
| 23 |
}{ |
| 24 |
{ |
| 25 |
TestName: "Test Simple Validation", |
| 26 |
YAMLContent: `--- |
| 27 |
- name: john |
| 28 |
age: 20 |
| 29 |
- name: tom |
| 30 |
age: -1 |
| 31 |
- name: ken |
| 32 |
age: 10`, |
| 33 |
ExpectedErr: `[5:8] Key: 'Age' Error:Field validation for 'Age' failed on the 'gte' tag |
| 34 |
2 | - name: john |
| 35 |
3 | age: 20 |
| 36 |
4 | - name: tom |
| 37 |
> 5 | age: -1 |
| 38 |
^ |
| 39 |
6 | - name: ken |
| 40 |
7 | age: 10`, |
| 41 |
Instance: &[]struct { |
| 42 |
Name string `yaml:"name" validate:"required"` |
| 43 |
Age int `yaml:"age" validate:"gte=0,lt=120"` |
| 44 |
}{}, |
| 45 |
}, |
| 46 |
{ |
| 47 |
TestName: "Test Missing Required Field", |
| 48 |
YAMLContent: `--- |
| 49 |
- name: john |
| 50 |
age: 20 |
| 51 |
- age: 10`, |
| 52 |
ExpectedErr: `[4:1] Key: 'Name' Error:Field validation for 'Name' failed on the 'required' tag |
| 53 |
1 | --- |
| 54 |
2 | - name: john |
| 55 |
3 | age: 20 |
| 56 |
> 4 | - age: 10 |
| 57 |
^ |
| 58 |
`, |
| 59 |
Instance: &[]struct { |
| 60 |
Name string `yaml:"name" validate:"required"` |
| 61 |
Age int `yaml:"age" validate:"gte=0,lt=120"` |
| 62 |
}{}, |
| 63 |
}, |
| 64 |
{ |
| 65 |
TestName: "Test Nested Validation Missing Internal Required", |
| 66 |
YAMLContent: `--- |
| 67 |
name: john |
| 68 |
age: 10 |
| 69 |
addr: |
| 70 |
number: seven`, |
| 71 |
ExpectedErr: `[4:5] Key: 'State' Error:Field validation for 'State' failed on the 'required' tag |
| 72 |
1 | --- |
| 73 |
2 | name: john |
| 74 |
3 | age: 10 |
| 75 |
> 4 | addr: |
| 76 |
^ |
| 77 |
5 | number: seven`, |
| 78 |
Instance: &struct { |
| 79 |
Name string `yaml:"name" validate:"required"` |
| 80 |
Age int `yaml:"age" validate:"gte=0,lt=120"` |
| 81 |
Addr struct { |
| 82 |
Number string `yaml:"number" validate:"required"` |
| 83 |
State string `yaml:"state" validate:"required"` |
| 84 |
} `yaml:"addr"` |
| 85 |
}{}, |
| 86 |
}, |
| 87 |
{ |
| 88 |
TestName: "Test nested Validation with unknown field", |
| 89 |
YAMLContent: `--- |
| 90 |
name: john |
| 91 |
age: 20 |
| 92 |
addr: |
| 93 |
number: seven |
| 94 |
state: washington |
| 95 |
error: error |
| 96 |
`, |
| 97 |
ExpectedErr: `[7:3] unknown field "error" |
| 98 |
4 | addr: |
| 99 |
5 | number: seven |
| 100 |
6 | state: washington |
| 101 |
> 7 | error: error |
| 102 |
^ |
| 103 |
`, |
| 104 |
Instance: &struct { |
| 105 |
Name string `yaml:"name" validate:"required"` |
| 106 |
Age int `yaml:"age" validate:"gte=0,lt=120"` |
| 107 |
Addr *struct { |
| 108 |
Number string `yaml:"number" validate:"required"` |
| 109 |
State string `yaml:"state" validate:"required"` |
| 110 |
} `yaml:"addr" validate:"required"` |
| 111 |
}{}, |
| 112 |
}, |
| 113 |
{ |
| 114 |
TestName: "Test Validation with wrong field type", |
| 115 |
YAMLContent: `--- |
| 116 |
name: myDocument |
| 117 |
roles: |
| 118 |
name: myRole |
| 119 |
permissions: |
| 120 |
- hello |
| 121 |
- how |
| 122 |
- are |
| 123 |
- you |
| 124 |
`, |
| 125 |
ExpectedErr: `[4:7] mapping was used where sequence is expected |
| 126 |
1 | --- |
| 127 |
2 | name: myDocument |
| 128 |
3 | roles: |
| 129 |
> 4 | name: myRole |
| 130 |
^ |
| 131 |
5 | permissions: |
| 132 |
6 | - hello |
| 133 |
7 | - how |
| 134 |
8 | `, |
| 135 |
Instance: &struct { |
| 136 |
Name string `yaml:"name"` |
| 137 |
Roles []struct { |
| 138 |
Name string `yaml:"name"` |
| 139 |
Permissions []string `yaml:"permissions"` |
| 140 |
} `yaml:"roles"` |
| 141 |
}{}, |
| 142 |
}, |
| 143 |
{ |
| 144 |
TestName: "Test inline validation missing required", |
| 145 |
YAMLContent: `--- |
| 146 |
name: john |
| 147 |
age: 20 |
| 148 |
`, |
| 149 |
ExpectedErr: `Key: 'Inner.Required' Error:Field validation for 'Required' failed on the 'required' tag`, |
| 150 |
Instance: &struct { |
| 151 |
Name string `yaml:"name" validate:"required"` |
| 152 |
Age int `yaml:"age" validate:"gte=0,lt=120"` |
| 153 |
Inner `yaml:",inline"` |
| 154 |
}{}, |
| 155 |
}, |
| 156 |
{ |
| 157 |
TestName: "Test inline validation field error", |
| 158 |
YAMLContent: `--- |
| 159 |
name: john |
| 160 |
age: 20 |
| 161 |
required: present |
| 162 |
lt10: 20 |
| 163 |
`, |
| 164 |
ExpectedErr: `[5:7] Key: 'Inner.Lt10' Error:Field validation for 'Lt10' failed on the 'lt' tag |
| 165 |
2 | name: john |
| 166 |
3 | age: 20 |
| 167 |
4 | required: present |
| 168 |
> 5 | lt10: 20 |
| 169 |
^ |
| 170 |
`, |
| 171 |
Instance: &struct { |
| 172 |
Name string `yaml:"name" validate:"required"` |
| 173 |
Age int `yaml:"age" validate:"gte=0,lt=120"` |
| 174 |
Inner `yaml:",inline"` |
| 175 |
}{}, |
| 176 |
}, |
| 177 |
} |
| 178 |
|
| 179 |
for _, tc := range cases { |
| 180 |
tc := tc // NOTE: https://github.com/golang/go/wiki/CommonMistakes#using-goroutines-on-loop-iterator-variables |
| 181 |
t.Run(tc.TestName, func(t *testing.T) { |
| 182 |
validate := validator.New() |
| 183 |
dec := yaml.NewDecoder( |
| 184 |
strings.NewReader(tc.YAMLContent), |
| 185 |
yaml.Validator(validate), |
| 186 |
yaml.Strict(), |
| 187 |
) |
| 188 |
err := dec.Decode(tc.Instance) |
| 189 |
switch { |
| 190 |
case tc.ExpectedErr != "" && err == nil: |
| 191 |
t.Fatal("expected error") |
| 192 |
case tc.ExpectedErr == "" && err != nil: |
| 193 |
t.Fatalf("unexpected error: %v", err) |
| 194 |
case tc.ExpectedErr != "" && tc.ExpectedErr != err.Error(): |
| 195 |
t.Fatalf("expected `%s` but actual `%s`", tc.ExpectedErr, err.Error()) |
| 196 |
} |
| 197 |
}) |
| 198 |
} |
| 199 |
} |