validator

package
v0.33.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 25, 2024 License: Apache-2.0 Imports: 22 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// This comes from the path: /Users/vinayada/radius/radius/swagger/specification/ucp/resource-manager/UCP/preview/2023-10-01-preview/openapi.json
	// This spec path is parsed and this string needs to be provider/resourceType.
	// For UCP, the provider is UCP and since all UCP resource types are in a single json, the file is named openapi.json.
	// Therefore, resourceType = ucp
	UCPEndpointType = "ucp/openapi"
	UCPApiVersion   = "2023-10-01-preview"
)
View Source
const (
	APIVersionQueryKey = "api-version"
)

Variables

View Source
var (
	ErrSpecDocumentNotFound = errors.New("not found OpenAPI specification document")
)
View Source
var (
	ErrUndefinedRoute = errors.New("undefined route path")
)

Functions

func APIMethodNotAllowedHandler

func APIMethodNotAllowedHandler() http.HandlerFunc

APIMethodNotAllowedHandler is the handler when the request method does not match the route. It handles requests with invalid methods by returning a MethodNotAllowedResponse and an error if the response fails to be applied.

r := mux.NewRouter()
r.MethodNotAllowedHandler = APIMethodNotAllowedHandler()

func APINotFoundHandler

func APINotFoundHandler() http.HandlerFunc

APINotFoundHandler is the handler when the request url route does not exist. It handles requests that are invalid and returns a NotFoundMessageResponse. If an error occurs, it is handled by the handleError function.

r := mux.NewRouter()
r.NotFoundHandler = APINotFoundHandler()

func APIValidator

func APIValidator(options Options) func(h http.Handler) http.Handler

APIValidator is the middleware to validate incoming request with OpenAPI spec. It wraps a handler to validate requests against a given spec loader and resource type getter.

func RadiusResourceTypeGetter

func RadiusResourceTypeGetter(r *http.Request) (string, error)

RadiusResourceTypeGetter parses the request URL and method to get the resource type and returns it, returning an error if parsing fails.

func UCPResourceTypeGetter

func UCPResourceTypeGetter(r *http.Request) (string, error)

UCPResourceTypeGetter returns the UCPEndpointType string and no error.

Types

type Loader

type Loader struct {
	// contains filtered or unexported fields
}

Loader is the OpenAPI spec loader implementation.

func LoadSpec

func LoadSpec(ctx context.Context, providerName string, specs fs.FS, rootScopePrefixes []string, rootScopeParam string) (*Loader, error)

LoadSpec loads OpenAPI spec documents from the given FS and returns a Loader instance. If no spec documents are found, an error is returned.

func (*Loader) GetValidator

func (l *Loader) GetValidator(resourceType, version string) (Validator, bool)

GetValidator returns the cached validator.

func (*Loader) Name

func (l *Loader) Name() string

Name returns the name of loader.

func (*Loader) SupportedVersions

func (l *Loader) SupportedVersions(resourceType string) []string

// SupportedVersions returns a list of supported versions for the given resource type, or an empty list if the resource type is not supported.

type Options

type Options struct {
	// SpecLoader is the loader to load the OpenAPI spec.
	SpecLoader *Loader

	// ResourceType is the function to get the resource type from the request.
	ResourceTypeGetter func(*http.Request) (string, error)
}

Options represents the options for APIValidator.

type ValidationError

type ValidationError struct {
	// Code represents the code of validation error.
	Code string

	// Message contains the error message, e.g. "location is required".
	Message string
}

ValidationError represents a validation error.

type Validator

type Validator interface {
	// ValidateRequest validates a http request and returns all the errors.
	ValidateRequest(req *http.Request) []ValidationError
}

Validator validates HTTP request.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL