A type-safe validation framework for Go applications.
Use form to validate JSON requests, API payloads, and application input through reusable schemas, typed fields, and composable validation rules.
This package requires Go 1.22 or newer.
It is designed for modern Go projects and may use language and standard library features introduced in recent Go versions.
- Go:
1.22or newer - Dependencies: Standard library only
Add the package to your project using go get:
go get github.com/netlifeguru/formImport the package into your application:
import (
"github.com/netlifeguru/form"
"github.com/netlifeguru/form/rules"
)Additional optional modules are available through subpackages:
import (
"github.com/netlifeguru/form/conditional"
"github.com/netlifeguru/form/httpform"
"github.com/netlifeguru/form/optional"
)The package is designed to integrate naturally with Go structs, net/http handlers, JSON request processing, and reusable application validation workflows.
Once installed, continue with the Quick Start guide to create your first validation schema and validate incoming request data.
The example below demonstrates a complete validation workflow using:
- HTTP request binding
- Typed validation schemas
- Reusable validation rules
- JSON request validation
- Structured request processing
The incoming request is a standard HTTP POST request with a JSON payload:
{
"name": "abcdefd",
"age": 10
}The validation flow consists of:
- Defining a request structure
- Creating reusable typed form fields
- Building validation rules
- Binding and validating the HTTP request
- Returning structured JSON responses
The schema defines reusable typed fields and validation rules for the incoming request.
Schemas are typically defined through functions instead of global variables to keep validation definitions immutable and isolated between application components.
package main
import (
"github.com/netlifeguru/form"
"github.com/netlifeguru/form/rules"
)
type PostRequest struct {
Name string `json:"name"`
Age int `json:"age"`
}
func PostSchema() form.Schema[PostRequest] {
var PostForm = struct {
Name form.StringField[PostRequest]
Age form.IntField[PostRequest]
}{
Name: form.Str[PostRequest]("name", func(r *PostRequest) string {
return r.Name
}),
Age: form.Int[PostRequest]("age", func(r *PostRequest) int {
return r.Age
}),
}
var NameSchema = form.Schema[PostRequest]{
rules.Required(PostForm.Name),
rules.MinLen(PostForm.Name, 5),
}
var AgeSchema = form.Schema[PostRequest]{
rules.RequiredInt(PostForm.Age),
rules.Min(PostForm.Age, 8),
}
return form.Rules(
NameSchema,
AgeSchema,
)
}The HTTP handler binds the incoming JSON request into the PostRequest structure and validates it using the schema.
package main
import (
"encoding/json"
"fmt"
"log/slog"
"net/http"
"os"
"github.com/netlifeguru/form"
"github.com/netlifeguru/form/httpform"
"github.com/netlifeguru/router"
)
func main() {
r := router.New()
r.HandleFunc("/", "POST", func(w http.ResponseWriter, r *http.Request, ctx *router.Context) {
var in PostRequest
if !httpform.BindAndValidate(w, r, &in, PostSchema(), 1<<20) {
fmt.Println("form validation failed")
return
}
fmt.Println("request received:", in)
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(map[string]any{
"message": "request received",
"data": in,
})
})
form.SendTestPost(":8080/", map[string]any{
"name": "abcdefd",
"age": 10,
})
if err := r.ListenAndServe(8080); err != nil {
slog.Error("failed to start server", "error", err)
os.Exit(1)
}
}Note
The example uses the helper method:
form.SendTestPost(":8080/", map[string]any{ "name": "abcdefd", "age": 10, })to automatically send a test HTTP
POSTrequest to the local server after startup.This helper exists only to make the example self-contained and immediately runnable without requiring external tools such as
curlor Postman.
Start the application:
go run .The server starts on:
http://localhost:8080
Successful validation returns a structured JSON response:
{
"message": "request received",
"data": {
"name": "abcdefd",
"age": 10
}
}If validation fails, the package automatically generates structured validation error responses through the httpform module.
Continue with the next sections to learn about validation rules, optional fields, conditional validation, custom error messages, schema composition, and advanced HTTP request processing.
Full package documentation, guides, and examples are available at:
https://netlife.guru/docs/go/form
API reference is also available on pkg.go.dev:
https://pkg.go.dev/github.com/netlifeguru/form
- Review package-specific concurrency behavior before using it in highly parallel workloads.
- Check performance characteristics when using this package in latency-sensitive paths.
- See the package documentation and examples for limitations and recommended usage patterns.
This project follows Semantic Versioning.
See CHANGELOG.md for release history and breaking changes.
Community contributions, feedback, and improvements are welcome.
Please read CONTRIBUTING.md before submitting pull requests or opening issues.
This project follows a Code of Conduct.
Please read CODE_OF_CONDUCT.md before contributing or participating in discussions.
Created and maintained by NetLife Guru s.r.o.
- Documentation: https://netlife.guru/docs/go/form
- GitHub: https://github.com/netlifeguru
- Contact: info@netlife.guru
MIT License. See LICENSE.