|
| 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.ServerDetail) error |
| 12 | +} |
| 13 | + |
| 14 | +// ServerValidator validates server details |
| 15 | +type ServerValidator struct { |
| 16 | + *RespositoryValidator // Embedded RespositoryValidator for repository validation |
| 17 | +} |
| 18 | + |
| 19 | +// Validate checks if the server details are valid |
| 20 | +func (v *ServerValidator) Validate(obj *model.Server) error { |
| 21 | + if obj.Name == "" { |
| 22 | + return ErrNameRequired |
| 23 | + } |
| 24 | + |
| 25 | + // TODO: Add format validation according to https://github.com/modelcontextprotocol/modelcontextprotocol/issues/1086 |
| 26 | + if len(obj.Name) > MaxLengthForServerName { |
| 27 | + return ErrServerNameTooLong |
| 28 | + } |
| 29 | + |
| 30 | + // Version is required |
| 31 | + if obj.VersionDetail.Version == "" { |
| 32 | + return ErrVersionRequired |
| 33 | + } |
| 34 | + |
| 35 | + if len(obj.Description) > MaxLengthForDescription { |
| 36 | + return ErrDescriptionTooLong |
| 37 | + } |
| 38 | + |
| 39 | + if err := v.RespositoryValidator.Validate(&obj.Repository); err != nil { |
| 40 | + return err |
| 41 | + } |
| 42 | + return nil |
| 43 | +} |
| 44 | + |
| 45 | +// NewServerValidator creates a new ServerValidator instance |
| 46 | +func NewServerValidator() *ServerValidator { |
| 47 | + return &ServerValidator{ |
| 48 | + RespositoryValidator: NewRepositoryValidator(), |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +// RepositoryValidator validates repository details |
| 53 | +type RespositoryValidator struct { |
| 54 | + validSources map[RepositorySource]bool |
| 55 | +} |
| 56 | + |
| 57 | +// Validate checks if the repository details are valid |
| 58 | +func (rv *RespositoryValidator) Validate(obj *model.Repository) error { |
| 59 | + // validate the repository URL |
| 60 | + repoSource := RepositorySource(obj.Source) |
| 61 | + if _, ok := rv.validSources[repoSource]; !ok { |
| 62 | + return fmt.Errorf("%w: %s", ErrInvalidRepositorySource, obj.Source) |
| 63 | + } |
| 64 | + if !IsValidRepositoryURL(repoSource, obj.URL) { |
| 65 | + return fmt.Errorf("%w: %s", ErrInvalidRepositoryURL, obj.URL) |
| 66 | + } |
| 67 | + |
| 68 | + // TODO: Add validator for repo ID |
| 69 | + return nil |
| 70 | +} |
| 71 | + |
| 72 | +// NewRepositoryValidator creates a new RespositoryValidator instance |
| 73 | +func NewRepositoryValidator() *RespositoryValidator { |
| 74 | + return &RespositoryValidator{ |
| 75 | + validSources: map[RepositorySource]bool{SourceGitHub: true, SourceGitLab: true}, |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +// PackageValidator validates package details |
| 80 | +type PackageValidator struct{} |
| 81 | + |
| 82 | +// Validate checks if the package details are valid |
| 83 | +func (pv *PackageValidator) Validate(obj *model.Package) error { |
| 84 | + if len(obj.Name) > MaxLengthForPackageName { |
| 85 | + return ErrPackageNameTooLong |
| 86 | + } |
| 87 | + |
| 88 | + if !HasNoSpaces(obj.Name) { |
| 89 | + return ErrPackageNameHasSpaces |
| 90 | + } |
| 91 | + |
| 92 | + return nil |
| 93 | +} |
| 94 | + |
| 95 | +// NewPackageValidator creates a new PackageValidator instance |
| 96 | +func NewPackageValidator() *PackageValidator { |
| 97 | + return &PackageValidator{} |
| 98 | +} |
| 99 | + |
| 100 | +// RemoteValidator validates remote connection details |
| 101 | +type RemoteValidator struct{} |
| 102 | + |
| 103 | +// Validate checks if the remote connection details are valid |
| 104 | +func (rv *RemoteValidator) Validate(obj *model.Remote) error { |
| 105 | + if !IsValidURL(obj.URL) { |
| 106 | + return fmt.Errorf("%w: %s", ErrInvalidRemoteURL, obj.URL) |
| 107 | + } |
| 108 | + return nil |
| 109 | +} |
| 110 | + |
| 111 | +// NewRemoteValidator creates a new RemoteValidator instance |
| 112 | +func NewRemoteValidator() *RemoteValidator { |
| 113 | + return &RemoteValidator{} |
| 114 | +} |
| 115 | + |
| 116 | +// ObjectValidator aggregates multiple validators for different object types |
| 117 | +// This allows for a single entry point to validate complex objects that may contain multiple fields |
| 118 | +// that need validation. |
| 119 | +type ObjectValidator struct { |
| 120 | + ServerValidator *ServerValidator |
| 121 | + PackageValidator *PackageValidator |
| 122 | + RemoteValidator *RemoteValidator |
| 123 | +} |
| 124 | + |
| 125 | +func NewObjectValidator() Validator { |
| 126 | + return &ObjectValidator{ |
| 127 | + ServerValidator: NewServerValidator(), |
| 128 | + PackageValidator: NewPackageValidator(), |
| 129 | + RemoteValidator: NewRemoteValidator(), |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +func (ov *ObjectValidator) Validate(obj *model.ServerDetail) error { |
| 134 | + if err := ov.ServerValidator.Validate(&obj.Server); err != nil { |
| 135 | + return err |
| 136 | + } |
| 137 | + |
| 138 | + for _, pkg := range obj.Packages { |
| 139 | + if err := ov.PackageValidator.Validate(&pkg); err != nil { |
| 140 | + return err |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + for _, remote := range obj.Remotes { |
| 145 | + if err := ov.RemoteValidator.Validate(&remote); err != nil { |
| 146 | + return err |
| 147 | + } |
| 148 | + } |
| 149 | + return nil |
| 150 | +} |
0 commit comments