package validator_test import ( "testing" "github.com/fiskerinc/cloud-services/pkg/testhelper" "github.com/fiskerinc/cloud-services/pkg/validator" ) type TestCANID struct { Name string `validate:"can_id"` Expected string } var canIDValidatorValidTests = []TestCANID{ {Name: "1"}, {Name: "123"}, {Name: "123-456"}, } func TestValidateCANID(t *testing.T) { var tests = []TestCANID{ { Name: "", Expected: "Key: 'TestCANID.Name' Error:Field validation for 'Name' failed on the 'can_id' tag", }, { Name: "-", Expected: "Key: 'TestCANID.Name' Error:Field validation for 'Name' failed on the 'can_id' tag", }, { Name: "-123", Expected: "Key: 'TestCANID.Name' Error:Field validation for 'Name' failed on the 'can_id' tag", }, { Name: "abc", Expected: "Key: 'TestCANID.Name' Error:Field validation for 'Name' failed on the 'can_id' tag", }, { Name: "ab12", Expected: "Key: 'TestCANID.Name' Error:Field validation for 'Name' failed on the 'can_id' tag", }, { Name: "123-123-123", Expected: "Key: 'TestCANID.Name' Error:Field validation for 'Name' failed on the 'can_id' tag", }, } tests = append(tests, canIDValidatorValidTests...) 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()) } } }