validation

package
v0.28.0 Latest Latest
Warning

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

Go to latest
Published: Sep 5, 2025 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

internal/gateway/validation/routes.go

internal/gateway/validation/validator.go

Index

Constants

This section is empty.

Variables

View Source
var RouteValidationRules = map[string]*ValidationRules{
	"PUT /v1/containers/{container}/artifacts/{artifact}": {
		ContentTypes: []string{"application/octet-stream", "multipart/form-data"},
		MaxBodySize:  5 * 1024 * 1024 * 1024,
		Headers: HeaderRules{
			Required: []string{"X-Tenant-ID"},
		},
	},
	"GET /v1/containers/{container}/artifacts": {
		Query: QueryRules{
			Types: map[string]ParamType{
				"limit":  ParamTypeInt,
				"offset": ParamTypeInt,
			},
			Ranges: map[string]Range{
				"limit": {Min: 1, Max: 1000},
			},
		},
	},
	"POST /v1/users": {
		ContentTypes: []string{"application/json"},
		MaxBodySize:  1024 * 1024,
		JSONSchema: `{
			"type": "object",
			"required": ["email", "password"],
			"properties": {
				"email": {"type": "string", "format": "email"},
				"password": {"type": "string", "minLength": 8},
				"name": {"type": "string", "maxLength": 100}
			}
		}`,
	},
}

RouteValidationRules defines validation rules for different routes

Functions

func ValidationMiddleware

func ValidationMiddleware(validator *RequestValidator, rules *ValidationRules) func(http.Handler) http.Handler

ValidationMiddleware creates an HTTP middleware for request validation

Types

type HeaderRules

type HeaderRules struct {
	Required []string
	Patterns map[string]string
}

HeaderRules defines validation rules for headers

type ParamType

type ParamType string

ParamType represents the type of a query parameter

const (
	ParamTypeString ParamType = "string"
	ParamTypeInt    ParamType = "int"
	ParamTypeBool   ParamType = "bool"
	ParamTypeFloat  ParamType = "float"
)

type QueryRules

type QueryRules struct {
	Required []string
	Types    map[string]ParamType
	Patterns map[string]string
	Ranges   map[string]Range
}

QueryRules defines validation rules for query parameters

type Range

type Range struct {
	Min float64
	Max float64
}

Range defines min and max values for numeric parameters

type RequestValidator

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

RequestValidator handles request validation

func NewRequestValidator

func NewRequestValidator() *RequestValidator

NewRequestValidator creates a new request validator

func (*RequestValidator) Validate

func (v *RequestValidator) Validate(r *http.Request, rules *ValidationRules) error

Validate performs all configured validations

func (*RequestValidator) ValidateContentLength

func (v *RequestValidator) ValidateContentLength(r *http.Request, maxSize int64) error

ValidateContentLength validates that the request body doesn't exceed max size

func (*RequestValidator) ValidateContentType

func (v *RequestValidator) ValidateContentType(r *http.Request, allowed []string) error

ValidateContentType validates the request content type

func (*RequestValidator) ValidateHeaders

func (v *RequestValidator) ValidateHeaders(r *http.Request, rules HeaderRules) error

ValidateHeaders validates request headers against rules

func (*RequestValidator) ValidateJSONSchema

func (v *RequestValidator) ValidateJSONSchema(r *http.Request, schemaStr string) error

ValidateJSONSchema validates request body against a JSON schema

func (*RequestValidator) ValidateQueryParams

func (v *RequestValidator) ValidateQueryParams(r *http.Request, rules QueryRules) error

ValidateQueryParams validates query parameters against rules

type ValidationRules

type ValidationRules struct {
	ContentTypes []string
	MaxBodySize  int64
	Headers      HeaderRules
	Query        QueryRules
	JSONSchema   string
}

ValidationRules defines all validation rules for a request

func GetValidationRules

func GetValidationRules(method, path string) *ValidationRules

GetValidationRules returns validation rules for a given route

Jump to

Keyboard shortcuts

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