mirror of
https://github.com/rancher/steve.git
synced 2025-09-06 09:51:02 +00:00
Refactor
This commit is contained in:
47
pkg/schemaserver/parse/validate.go
Normal file
47
pkg/schemaserver/parse/validate.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package parse
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/rancher/steve/pkg/schemaserver/httperror"
|
||||
"github.com/rancher/steve/pkg/schemaserver/types"
|
||||
"github.com/rancher/wrangler/pkg/schemas/validation"
|
||||
)
|
||||
|
||||
var (
|
||||
supportedMethods = map[string]bool{
|
||||
http.MethodPost: true,
|
||||
http.MethodGet: true,
|
||||
http.MethodPut: true,
|
||||
http.MethodPatch: true,
|
||||
http.MethodDelete: true,
|
||||
}
|
||||
)
|
||||
|
||||
func ValidateMethod(request *types.APIRequest) error {
|
||||
if request.Action != "" && request.Method == http.MethodPost {
|
||||
return nil
|
||||
}
|
||||
|
||||
if !supportedMethods[request.Method] {
|
||||
return httperror.NewAPIError(validation.MethodNotAllowed, fmt.Sprintf("Invalid method %s not supported", request.Method))
|
||||
}
|
||||
|
||||
if request.Type == "" || request.Schema == nil || request.Link != "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
allowed := request.Schema.ResourceMethods
|
||||
if request.Name == "" {
|
||||
allowed = request.Schema.CollectionMethods
|
||||
}
|
||||
|
||||
for _, method := range allowed {
|
||||
if method == request.Method {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
return httperror.NewAPIError(validation.MethodNotAllowed, fmt.Sprintf("Method %s not supported", request.Method))
|
||||
}
|
Reference in New Issue
Block a user