feat: generate fields validation and use it when sending requests to API (#7)
* feat: generate fields validation and use it when issuing requests to API with soft (default) or hard modes * chore: apply linter fixes * feat: enable field validation on int fields * feat: add validation for ^[\w]+$ fields * feat: add validation for MAC address fields * fix: trim wrappers for all comments * feat: add validation for IPv4, IPv6 and IP(IPv4/IPv6) fields * feat: add validation for numeric, non-zero based fields * fix: one of validation can contain dot (.) sign in values * feat: add second notation of MAC address validation * fix: one of validation can start with ^( and end with )$ * feat: add option to disable validation and use soft validation by default * chore: fix test * docs: add readme about client-side validation
This commit is contained in:
committed by
GitHub
parent
9f4fe33d07
commit
53bb1a13b9
@@ -579,3 +579,46 @@ func TestUrlValidation(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
type validateableBody struct {
|
||||
Data string `json:"data" validate:"required"`
|
||||
}
|
||||
|
||||
func TestValidationModes(t *testing.T) {
|
||||
t.Parallel()
|
||||
testCases := []struct {
|
||||
validationMode validationMode
|
||||
expectedError string
|
||||
expectRequest bool
|
||||
}{
|
||||
{SoftValidation, "dial tcp", true},
|
||||
{HardValidation, "validation failed", false},
|
||||
{DisableValidation, "dial tcp", true},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(string(tc.validationMode), func(t *testing.T) {
|
||||
t.Parallel()
|
||||
a := assert.New(t)
|
||||
// given
|
||||
interceptor := NewTestInterceptor()
|
||||
c, _ := NewClient(&ClientConfig{
|
||||
URL: testUrl,
|
||||
APIKey: "test-key",
|
||||
Interceptors: []ClientInterceptor{interceptor},
|
||||
ValidationMode: tc.validationMode,
|
||||
})
|
||||
c.apiPaths = &NewStyleAPI
|
||||
// when
|
||||
err := c.Get(context.Background(), "", validateableBody{}, nil)
|
||||
|
||||
// then
|
||||
require.ErrorContains(t, err, tc.expectedError)
|
||||
if tc.expectRequest {
|
||||
a.NotNil(interceptor.request)
|
||||
} else {
|
||||
a.Nil(interceptor.request)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user