NetLife Guru

Open source Go packages for fast, maintainable web systems. Built with a documentation-first approach.

Product
OverviewGolang packagesNews
Documentation
DocumentationGo LoggerGo RouterGo DB Form
Company
OverviewContactNewsGitHub
Community / Support
Supportinfo@netlife.guru
© 2026 NetLife Guru. All rights reserved.
GitHubinfo@netlife.guru
NetLife GuruNetLife GuruNetLife Guru
NetLife GuruNetLife GuruNetLife Guru
OverviewDocumentationNewsSupportContact

Golang packages

About
InstallationGetting StartedValidation Philosophy
Form ValidatorGetting Started

Getting Started

Learn how to validate JSON HTTP requests using reusable schemas, typed validation fields, and structured validation rules with the NLG Form package.

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:

  1. Defining a request structure
  2. Creating reusable typed form fields
  3. Building validation rules
  4. Binding and validating the HTTP request
  5. Returning structured JSON responses

Create the Request Schema

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,
	)
}

Create the HTTP Server

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 POST request 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 curl or Postman.


Run the Application

Start the application:

go run .

The server starts on:

http://localhost:8080

Example Response

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.

Installation

Install the NLG Form package for Go applications and integrate reusable validation schemas, rules, HTTP request validation, and structured form processing into your project.

Validation Philosophy

Learn how NLG Form uses explicit, type-safe, reusable validation schemas instead of struct tags and reflection-heavy validation pipelines.

On this page

Create the Request SchemaCreate the HTTP ServerRun the ApplicationExample Response