rdev/pkg/api/bind.go
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

109 lines
3.0 KiB
Go

package api
import (
"errors"
"net/http"
"github.com/orchard9/rdev/internal/validate"
)
// Bind decodes JSON from the request body and validates it.
// This combines DecodeJSON and validation in a single call for convenience.
//
// Usage:
//
// func CreateUser(w http.ResponseWriter, r *http.Request) error {
// var req CreateUserRequest
// if err := api.Bind(r, &req); err != nil {
// return err // Returns typed HTTPError
// }
// // req is now decoded and validated
// }
//
// The struct should have validation tags:
//
// type CreateUserRequest struct {
// Name string `json:"name" validate:"required,min=1,max=100"`
// Email string `json:"email" validate:"required,email"`
// }
//
// Bind uses the internal/validate package for validation. For struct validation
// with go-playground/validator tags, see BindWithValidator.
func Bind(r *http.Request, v any) error {
// Decode JSON
if err := DecodeJSON(r, v); err != nil {
if errors.Is(err, ErrEmptyBody) {
return BadRequest("request body is required")
}
return 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 := DecodeJSONStrict(r, v); err != nil {
if errors.Is(err, ErrEmptyBody) {
return BadRequest("request body is required")
}
return BadRequest("invalid request body")
}
return nil
}
// BindAndValidate decodes JSON and validates using the validate package.
// Returns a validation error with field-level details if validation fails.
//
// Usage:
//
// func CreateUser(w http.ResponseWriter, r *http.Request) error {
// var req CreateUserRequest
// if err := api.BindAndValidate(r, &req, func(v *validate.Validator, req *CreateUserRequest) {
// v.Required(req.Name, "name")
// v.Required(req.Email, "email")
// v.StringLength(req.Name, "name", 1, 100)
// }); err != nil {
// return err
// }
// }
func BindAndValidate[T any](r *http.Request, v *T, validateFn func(*validate.Validator, *T)) error {
// Decode JSON
if err := DecodeJSON(r, v); err != nil {
if errors.Is(err, ErrEmptyBody) {
return BadRequest("request body is required")
}
return BadRequest("invalid request body")
}
// Run validation
validator := validate.New()
validateFn(validator, v)
if validator.HasErrors() {
return WithDetails(Validation("validation failed"), formatValidateErrors(validator.Errors()))
}
return nil
}
// ValidationDetail is the structure for field-level validation errors.
type ValidationDetail struct {
Field string `json:"field"`
Message string `json:"message"`
}
// formatValidateErrors converts validation errors to API-friendly details.
func formatValidateErrors(errs validate.ValidationErrors) []ValidationDetail {
details := make([]ValidationDetail, 0, len(errs))
for _, e := range errs {
details = append(details, ValidationDetail{
Field: e.Field,
Message: e.Message,
})
}
return details
}