validgo

package module
v1.1.10 Latest Latest
Warning

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

Go to latest
Published: Sep 28, 2026 License: MIT Imports: 1 Imported by: 0

README

validgo

Go Version License Status

Validation for Golang.

validgo helps simplify request parsing and validation in Go by wrapping go-playground/validator with a cleaner developer experience.

Features

  • Request body parsing
  • Automatic validation
  • Automatic JSON validation response
  • Generic typed request parsing
  • Clean Fiber integration
  • Built on top of go-playground/validator

Installation

go get github.com/MRizki28/validgo

Install validgo CLI:

go install github.com/MRizki28/validgo/cmd/validgo@latest

Make sure your Go binary path is added to PATH:

export PATH=$PATH:$(go env GOPATH)/bin

CLI Commands

Generate Request

validgo make:request RegisterUser

Generated file:

app/requests/register_user_request.go

Quick Start

Request Struct

package main

type RegisterRequest struct {
    Name     string `json:"name" validate:"required"`
    Email    string `json:"email" validate:"required,email"`
    Password string `json:"password" validate:"required,min=6"`
}

Example

package main

import (
	"github.com/MRizki28/validgo"
	"github.com/gofiber/fiber/v2"
)

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

func main() {
	app := fiber.New()

	app.Post(
		"/register",
		func(c *fiber.Ctx) error {
			var body RegisterRequest

			if err := c.BodyParser(&body); err != nil {
				return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
					"status":  false,
					"message": err.Error(),
				})
			}

			if err := validgo.Validate(body); err != nil {
				if valErr, ok := err.(*validgo.ValidationError); ok {
					return c.Status(fiber.StatusUnprocessableEntity).JSON(fiber.Map{
						"status":  false,
						"message": "validation failed",
						"errors":  valErr.Errors,
					})
				}

				return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
					"status":  false,
					"message": err.Error(),
				})
			}

			return c.Status(200).JSON(fiber.Map{
				"message": "success",
			})
		},
	)

	app.Listen(":3000")
}


Validation Error Response

{
  "status": false,
  "message": "validation failed",
  "errors": {
    "email": [
      "email must be a valid email"
    ],
    "name": [
      "name is required"
    ]
  }
}

Invalid Body Response

{
  "status": false,
  "message": "invalid request body"
}

Example

go run examples/fiber/main.go

Testing

Run tests:

go test -v ./...

Roadmap

  • Custom validation messages
  • Localization support
  • Database unique validation
  • Snake case field formatter
  • Query validation
  • Form-data validation

License

MIT


Author

Muhammad Rizki

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Validate added in v1.1.7

func Validate[T any](data T) error

Types

type ValidationError

type ValidationError struct {
	Errors interface{}
}

func (*ValidationError) Error

func (e *ValidationError) Error() string

Directories

Path Synopsis
cmd
validgo command
examples
fiber command

Jump to

Keyboard shortcuts

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