Skip to content

Commit 269b060

Browse files
author
Avish Porwal
committed
Refactor validation code
1 parent fe52766 commit 269b060

File tree

2 files changed

+35
-8
lines changed

2 files changed

+35
-8
lines changed

internal/api/handlers/v0/publish.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/modelcontextprotocol/registry/internal/database"
1313
"github.com/modelcontextprotocol/registry/internal/model"
1414
"github.com/modelcontextprotocol/registry/internal/service"
15+
"github.com/modelcontextprotocol/registry/internal/validators"
1516
"golang.org/x/net/html"
1617
)
1718

@@ -48,15 +49,11 @@ func PublishHandler(registry service.RegistryService, authService auth.Service)
4849
http.Error(w, "Invalid server detail payload: "+err.Error(), http.StatusBadRequest)
4950
return
5051
}
51-
// Validate required fields
52-
if serverDetail.Name == "" {
53-
http.Error(w, "Name is required", http.StatusBadRequest)
54-
return
55-
}
5652

57-
// Version is required
58-
if serverDetail.VersionDetail.Version == "" {
59-
http.Error(w, "Version is required", http.StatusBadRequest)
53+
// Validate the server detail
54+
validator := validators.NewServerValidator()
55+
if err := validator.Validate(&serverDetail.Server); err != nil {
56+
http.Error(w, err.Error(), http.StatusBadRequest)
6057
return
6158
}
6259

internal/validators/validators.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package validators
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/modelcontextprotocol/registry/internal/model"
7+
)
8+
9+
type Validator interface {
10+
// Validate checks if the input is valid according to the validator's rules
11+
Validate(obj *model.Server) error
12+
}
13+
14+
type ServerValidator struct{}
15+
16+
func (v *ServerValidator) Validate(obj *model.Server) error {
17+
if obj.Name == "" {
18+
return fmt.Errorf("Name is required")
19+
}
20+
21+
// Version is required
22+
if obj.VersionDetail.Version == "" {
23+
return fmt.Errorf("Version is required")
24+
}
25+
return nil
26+
}
27+
28+
func NewServerValidator() Validator {
29+
return &ServerValidator{}
30+
}

0 commit comments

Comments
 (0)