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

16
test.go
View File

@@ -65,9 +65,9 @@ func main() {
}
// Get the validators map
validators, err := manager.Validators(context.Background())
if err != nil {
log.Fatalf("Failed to get validators: %v", err)
validators, defErr := manager.Validators(context.Background())
if defErr != (definition.Error{}) {
log.Fatalf("Failed to get validators: %v", defErr)
}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
@@ -88,7 +88,7 @@ func validateHandler(w http.ResponseWriter, r *http.Request, validators map[stri
// Extract endpoint from request URL
requestURL := r.RequestURI
u, err := url.Parse(requestURL)
u, err := url.ParseRequestURI(requestURL)
if err != nil {
http.Error(w, "Failed to parse request URL", http.StatusBadRequest)
return
@@ -99,8 +99,6 @@ func validateHandler(w http.ResponseWriter, r *http.Request, validators map[stri
http.Error(w, "Failed to read payload data", http.StatusInternalServerError)
return
}
fmt.Println("printing payload data", string(payloadData))
var payload Payload
err = json.Unmarshal(payloadData, &payload)
if err != nil {
@@ -123,9 +121,9 @@ func validateHandler(w http.ResponseWriter, r *http.Request, validators map[stri
}
ctx := context.Background()
valid, err := validator.Validate(ctx, *u, payloadData)
if err != nil {
http.Error(w, fmt.Sprintf("Document validation failed: %v", err), http.StatusBadRequest)
valid, validationErr := validator.Validate(ctx, u, payloadData)
if validationErr != (definition.Error{}) {
http.Error(w, fmt.Sprintf("Document validation failed: %v", validationErr), http.StatusBadRequest)
} else if !valid {
http.Error(w, "Document validation failed", http.StatusBadRequest)
} else {