package validator_test import ( "testing" "github.com/fiskerinc/cloud-services/pkg/testhelper" "github.com/fiskerinc/cloud-services/pkg/validator" ) type TestCertSerial struct { Name string `validate:"serial"` Expected string } var serialValidTests = []TestCertSerial{ {Name: "01:01:7c:d2:d0:6b:ff:30:c5:66:9a:0e:dd:19:0a:61:7d:ca:95:33"}, {Name: "01:03:68:f1:b7:1f:e1:27:90:01:9c:b0:0d:ed:7d:a1:b9:c0:e1:3a"}, {Name: "01:03:68:f1:b7:1f:e1:27:90:01:9c:b0:0d:ed:7d:a1:b9:c0:e1"}, } func TestValidateSerial(t *testing.T) { var tests = []TestCertSerial{ { Name: "", Expected: "Key: 'TestCertSerial.Name' Error:Field validation for 'Name' failed on the 'serial' tag", }, { Name: "testing123", Expected: "Key: 'TestCertSerial.Name' Error:Field validation for 'Name' failed on the 'serial' tag", }, { Name: "01:03:68:f1:b7:1f:e1:27:90:01:9c:b0:0d:ed:7d:a1:b9:c0", Expected: "Key: 'TestCertSerial.Name' Error:Field validation for 'Name' failed on the 'serial' tag", }, { Name: "01:03:68:f1:b7:1f:e1:27:90:01:9c:b0:0d:ed7da1b9c0e13a", Expected: "Key: 'TestCertSerial.Name' Error:Field validation for 'Name' failed on the 'serial' tag", }, } tests = append(tests, serialValidTests...) for _, test := range tests { err := validator.ValidateStruct(test) if err == nil && test.Expected != "" { t.Errorf(testhelper.TestErrorTemplate, test.Name, test.Expected, err) } else if err != nil && err.Error() != test.Expected { t.Errorf(testhelper.TestErrorTemplate, test.Name, test.Expected, err.Error()) } } }