updated the interface to return a Error struct and modified the function and tests accordingly

This commit is contained in:
AshwiniK-protean
2025-03-13 11:16:45 +05:30
parent 0488eccc3b
commit e9cc670e3d
8 changed files with 88 additions and 80 deletions

View File

@@ -11,11 +11,11 @@ import (
type ValidatorProvider struct{}
// New initializes a new Verifier instance.
func (vp ValidatorProvider) New(ctx context.Context, config map[string]string) (map[string]definition.Validator, error) {
func (vp ValidatorProvider) New(ctx context.Context, config map[string]string) (map[string]definition.Validator, definition.Error) {
// Create a new Validator instance with the provided configuration
validators, err := validator.New(ctx, config)
if err != nil {
return nil, err
if err != (definition.Error{}) {
return nil, definition.Error{Path: "", Message: err.Message}
}
// Convert the map to the expected type
@@ -24,8 +24,8 @@ func (vp ValidatorProvider) New(ctx context.Context, config map[string]string) (
result[key] = val
}
return result, nil
return result, definition.Error{}
}
// Provider is the exported symbol that the plugin manager will look for.
var Provider definition.ValidatorProvider = ValidatorProvider{}
var Provider definition.ValidatorProvider = &ValidatorProvider{}

View File

@@ -12,8 +12,8 @@ import (
// MockValidator is a mock implementation of the Validator interface for testing.
type MockValidator struct{}
func (m *MockValidator) Validate(ctx context.Context, u url.URL, data []byte) (bool, error) {
return true, nil
func (m *MockValidator) Validate(ctx context.Context, u *url.URL, data []byte) (bool, definition.Error) {
return true, (definition.Error{})
}
// Mock New function for testing
@@ -70,7 +70,7 @@ func TestValidatorProvider_New(t *testing.T) {
// Check for expected error
if tt.expectedError != "" {
if err == nil || err.Error() != tt.expectedError {
if err == (definition.Error{}) || err.Message != tt.expectedError {
t.Errorf("expected error %q, got %v", tt.expectedError, err)
}
return