Skip to content

Commit 305ff32

Browse files
author
Avish Porwal
committed
Refactor validation code
1 parent 01a6ce5 commit 305ff32

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
@@ -9,6 +9,7 @@ import (
99
"github.com/modelcontextprotocol/registry/internal/auth"
1010
"github.com/modelcontextprotocol/registry/internal/model"
1111
"github.com/modelcontextprotocol/registry/internal/service"
12+
"github.com/modelcontextprotocol/registry/internal/validators"
1213
)
1314

1415
// PublishServerInput represents the input for publishing a server
@@ -17,7 +18,6 @@ type PublishServerInput struct {
1718
Body model.PublishRequest
1819
}
1920

20-
2121
// RegisterPublishEndpoint registers the publish endpoint
2222
func RegisterPublishEndpoint(api huma.API, registry service.RegistryService, authService auth.Service) {
2323
huma.Register(api, huma.Operation{
@@ -42,13 +42,10 @@ func RegisterPublishEndpoint(api huma.API, registry service.RegistryService, aut
4242
// Convert PublishRequest body to ServerDetail
4343
serverDetail := input.Body.ServerDetail
4444

45-
// Huma handles validation automatically based on struct tags
46-
// But we can add custom validation if needed
47-
if serverDetail.Name == "" {
48-
return nil, huma.Error400BadRequest("Name is required")
49-
}
50-
if serverDetail.VersionDetail.Version == "" {
51-
return nil, huma.Error400BadRequest("Version is required")
45+
// Validate the server detail
46+
validator := validators.NewServerValidator()
47+
if err := validator.Validate(&serverDetail.Server); err != nil {
48+
return nil, huma.Error400BadRequest(err.Error())
5249
}
5350

5451
// Determine authentication method based on server name prefix

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)