rdev/internal/adapter/templates/templates/skeleton/pkg/app/bind.go.tmpl
jordan 62460bf098 feat: complete template upgrade - chassis framework, UI library, auth, app-nextjs, OpenAPI, and cookbook
Weeks 1-7 of the template upgrade plan:
- pkg/api: typed HTTPError with sentinels, Wrap/WrapMiddleware, Bind, health probes, OpenAPI schema/param builders
- skeleton/packages: ui (design tokens, components), layout (DashboardShell), auth (AuthProvider, ProtectedRoute), api-client
- skeleton/pkg: httperror, app/handler, app/bind, app/health, auth (JWT/API key middleware)
- components/app-nextjs: Next.js 14 App Router template with dashboard, server actions, auth
- cookbooks/feature-development.md with test and validation scripts
- Handler tests for components, project management, and woodpecker webhook
- 3 rounds of code review fixes applied

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-02 00:46:51 -07:00

94 lines
2.6 KiB
Cheetah

package app
import (
"errors"
"net/http"
"{{GO_MODULE}}/pkg/httperror"
"{{GO_MODULE}}/pkg/httpresponse"
"{{GO_MODULE}}/pkg/httpvalidation"
)
// Bind decodes JSON from the request body into v.
// Returns an HTTPError on failure that can be returned directly from a HandlerFunc.
//
// Usage:
//
// func CreateUser(w http.ResponseWriter, r *http.Request) error {
// var req CreateUserRequest
// if err := app.Bind(r, &req); err != nil {
// return err // Returns typed HTTPError
// }
// // req is now decoded
// }
func Bind(r *http.Request, v any) error {
if err := httpresponse.DecodeJSON(r, v); err != nil {
if errors.Is(err, httpresponse.ErrEmptyBody) {
return httperror.BadRequest("request body is required")
}
return httperror.BadRequest("invalid request body")
}
return nil
}
// BindStrict decodes JSON from the request body with strict field checking.
// Unknown fields in the JSON will cause an error.
func BindStrict(r *http.Request, v any) error {
if err := httpresponse.DecodeJSONStrict(r, v); err != nil {
if errors.Is(err, httpresponse.ErrEmptyBody) {
return httperror.BadRequest("request body is required")
}
if errors.Is(err, httpresponse.ErrUnknownFields) {
return httperror.BadRequest("unknown fields in request body")
}
return httperror.BadRequest("invalid request body")
}
return nil
}
// BindAndValidate decodes JSON and validates using httpvalidation.
// Returns a validation error with field-level details if validation fails.
//
// Usage:
//
// type CreateUserRequest struct {
// Name string `json:"name" validate:"required,min=1,max=100"`
// Email string `json:"email" validate:"required,email"`
// }
//
// func CreateUser(w http.ResponseWriter, r *http.Request) error {
// var req CreateUserRequest
// if err := app.BindAndValidate(r, &req); err != nil {
// return err
// }
// // req is decoded and validated
// }
func BindAndValidate(r *http.Request, v any) error {
// Decode JSON
if err := Bind(r, v); err != nil {
return err
}
// Validate struct
if details := httpvalidation.ValidateStruct(v); len(details) > 0 {
return httperror.WithDetails(httperror.Validation("validation failed"), details)
}
return nil
}
// BindAndValidateStrict decodes JSON with strict field checking and validates.
func BindAndValidateStrict(r *http.Request, v any) error {
// Decode JSON strictly
if err := BindStrict(r, v); err != nil {
return err
}
// Validate struct
if details := httpvalidation.ValidateStruct(v); len(details) > 0 {
return httperror.WithDetails(httperror.Validation("validation failed"), details)
}
return nil
}