bindv

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 6, 2025 License: MIT Imports: 8 Imported by: 0

README

bindv

Test

A tiny Go library for binding and validating HTTP request data. Uses encoding/json for JSON decoding, go-playground/form for form decoding, and go-playground/validator for validation.

Usage

package main

import (
    "net/http"
    "github.com/fivethirty/bindv"
)

type User struct {
    Name  string `json:"name" form:"name" validate:"required"`
    Email string `json:"email" form:"email" validate:"required,email"`
}

func handleJSON(w http.ResponseWriter, r *http.Request) {
    var user User
    if err := bindv.JSON(r.Body, &user); err != nil {
        http.Error(w, err.Error(), http.StatusBadRequest)
        return
    }
    // Use validated user data...
}

func handleQuery(w http.ResponseWriter, r *http.Request) {
    var user User
    if err := bindv.Query(r.URL.Query(), &user); err != nil {
        http.Error(w, err.Error(), http.StatusBadRequest)
        return
    }
    // Use validated user data...
}

func handleForm(w http.ResponseWriter, r *http.Request) {
    var user User
    if err := bindv.PostForm(r, &user); err != nil {
        http.Error(w, err.Error(), http.StatusBadRequest)
        return
    }
    // Use validated user data...
}

func validateUser() error {
    user := User{Name: "John", Email: "invalid-email"}
    return bindv.Validate(&user)
}

Error Types

  • bindv.ErrDecoding - Returned when data cannot be decoded
  • bindv.ErrValidating - Returned when validation fails

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrDecoding   error = errors.New("can't decode")
	ErrValidating error = errors.New("can't validate")
)

Functions

func JSON

func JSON(body io.ReadCloser, target any) error

func PostForm

func PostForm(r *http.Request, target any) error

func Query

func Query(values url.Values, target any) error

func Validate

func Validate(target any) error

Types

This section is empty.

Jump to

Keyboard shortcuts

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