Skip to content

A lightweight, extensible, and framework-agnostic validation library for Go. Easily validate request bodies, nested objects, and more with a simple and intuitive API.

License

Notifications You must be signed in to change notification settings

kthehatter/go-validator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go-validator

A custom validator package for Go applications.

Installation

go get github.com/kthehatter/go-validator/validator

Usage

Import the package in your Go code:

import "github.com/kthehatter/go-validator/validator"

For Gin adapter:

import "github.com/kthehatter/go-validator/validator/ginadapter"

Test the Package

Step 1: Basic usage

package main

import (

"github.com/gin-gonic/gin"

"github.com/kthehatter/go-validator/validator"

"github.com/kthehatter/go-validator/validator/ginadapter"

)

func main() {

    // Create a new Gin router

    r := gin.New()


    // Define validation rules

    validationOptions := []validator.ValidationOption{

        {
        
            Key: "username",
            
            IsOptional: false,
            
            Validators: []validator.Validator{
            
            validator.CreateValidator(validator.IsNotEmpty, "Username is required"),
            
            validator.CreateValidator(validator.IsAlphanumeric, "Username must be alphanumeric"),
        
            },
        
        },
        {
        
            Key: "email",
            
            IsOptional: false,
            
            Validators: []validator.Validator{
            
            validator.CreateValidator(validator.IsNotEmpty, "Email is required"),
            
            validator.CreateValidator(validator.IsEmail, "Invalid email address"),
        
            },
        
        },
        
        {
        
            Key: "password",
            
            IsOptional: false,
            
            Validators: []validator.Validator{
            
            validator.CreateValidator(validator.IsNotEmpty, "Password is required"),
            
            validator.CreateValidator(validator.MinLength(6), "Password must be at least 6 characters"),
        
            },
        
        },

    }

    // Apply the validation middleware to the POST /user endpoint

    r.POST("/user", ginadapter.Middleware(validationOptions), func(c *gin.Context) {

    // Retrieve the validated body from the context

    body := c.MustGet("validatedBody").(gin.H)

    // Process the data (for demonstration, just return it)

    c.JSON(200, gin.H{

        "message": "User created successfully",

        "data": body,

    })

    })  

    // Start the Gin server

    r.Run(":8080")

}

Step 2: Run the Application

  1. Build and run the application:
go run main.go
  1. Verify the server is running:

    • Open a web browser or use curl to send a POST request to http://localhost:8080/user with a JSON body.

Step 3: Test the Endpoint

Test with Valid Data:

curl -X POST http://localhost:8080/user -H "Content-Type: application/json" -d '{
    "username": "user123",
    "email": "[email protected]",
    "password": "password123"
}'

Expected response:

{
    "message": "User created successfully",
    "data": {
        "username": "user123",
        "email": "[email protected]",
        "password": "password123"
    }
}

Test with Invalid Data:

curl -X POST http://localhost:8080/user -H "Content-Type: application/json" -d '{
    "username": "",
    "email": "invalid-email",
    "password": "pass"
}'

Expected response:

{
    "message": "Username is required"
}

About

A lightweight, extensible, and framework-agnostic validation library for Go. Easily validate request bodies, nested objects, and more with a simple and intuitive API.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages