house/backend/middleware/param.go
2025-06-14 23:47:44 -04:00

33 lines
782 B
Go

package middleware
import (
"strconv"
"git.red-panda.pet/pandaware/house/backend/router"
"github.com/google/uuid"
)
func ValidateUUIDParam(ctx *router.Context, key string) error {
param := ctx.Parameter(key)
if err := uuid.Validate(param); err != nil {
return ctx.Error(err, 400, "Bad path parameter "+key)
}
return nil
}
func ValidateIDParam(ctx *router.Context, key string) error {
param := ctx.Parameter(key)
v, err := strconv.ParseInt(param, 10, 64)
if err != nil {
return ctx.Error(err, 400, "Bad path parameter "+key)
}
ctx.With(paramsKey, v)
return nil
}
// TODO: expand this kinda api for more than just one param, better validation, etc.
func ParamID(r router.ValidatedParamRoute, ctx *router.Context) int64 {
return ctx.Value(paramsKey).(int64)
}