SkillAgentSearch skills...

Govalidator

A simple, efficient, type-safe, easy to use Golang validator without any struct tag or type assertion.

Install / Use

/learn @rezakhademix/Govalidator
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

Go Reference Go Report Card codecov License: MIT

GoValidator

GoValidator is a data validation package that can sanitize and validate your data to ensure its safety and integrity as much as possible. GoValidator will not use struct tags as validation and there is no need of them.

Our goal is to avoid using any type assertion or reflection for simplicity. We would be delighted if you were interested in helping us improve the GoValidator package. Feel free to make your pull request.

Getting Started

Go Validator includes a set of validation rules and a handy CustomRule() method to define any custom rule.

Installation

Run the following command to install the package:

go get github.com/rezakhademix/govalidator/v2

Import

Import package in your code-base by using:

import validator "github.com/rezakhademix/govalidator/v2"

Documentation

GoValidator package has the following features and method to use. Each validation rule in GoValidator has it's own default message, e.g: required rule for a required name has the default message of: name is required but you can define your custom field name and message.

Methods (also known as validation rules)


| Method | Description | | ---------------- | ------------------------------------------------------------------------------------------------------------------- | | RequiredInt | RequiredInt checks if an integer value is provided or not. | | RequiredFloat | RequiredFloat checks if a float value is provided or not. | | RequiredString | RequiredString checks if a string value is empty or not. | | RequiredSlice | RequiredSlice checks if a slice has any value or not. | | BetweenInt | BetweenInt checks whether value falls within the specified range or not. | | BetweenFloat | BetweenFloat checks whether value falls within the specified range or not. | | BetweenString | BetweenString checks whether string length falls within the specified range or not. | | Date | Date checks value to be a valid, non-relative date with given layout. | | Email | Email checks value to match EmailRegex regular expression. | | Exists | Exists checks given value exists in given table or not. | | NotExists | NotExists checks given value doesn't exist in given table, except. | | ExistsExceptSelf | ExistsExceptSelf checks given value exists in a specified column of a table, excluding a row identified by selfID | | LenString | LenString checks length of given string is equal to given size or not. | | LenInt | LenInt checks length of given integer is equal to given size or not. | | LenSlice | LenSlice checks length of given slice is equal to given size or not. | | MaxInt | MaxInt checks given integer is less than or equal given max value. | | MaxFloat | MaxFloat checks given float is less than or equal given max value. | | MaxString | MaxString checks length of given string is less than or equal given max value. | | MinInt | MinInt checks given integer is greater than or equal given min value. | | MinFloat | MinFloat checks given float is greater than or equal given min value. | | MinString | MinString checks length of given string is greater than or equal given min value. | | RegexMatches | RegexMatches checks given string matches given regular expression pattern. | | UUID | UUID checks given value is a valid universally unique identifier (UUID). | | When | When will execute given closure if given condition is true. | | URL | URL will check given string is a valid URL or not. | | Before | Before will check if given time instant t is before given time instant u. | | After | After will check if given time instant t is after given time instant u. | | IP4 | IP4 will check whether given string is a valid IPV4. | | DefaultInt | DefaultInt sets a default value for any pointer to an int that is passed. | | DefaultFloat | DefaultFloat sets a default value for any pointer to a float that is passed. | | DefaultString | DefaultString sets a default value for any pointer to a string that is passed. | | IsJSON | IsJSON will check if given string is a valid JSON. | | NumericString | NumericString will check if given value is a valid string of numbers. | | MAC | MAC will check if given value is a valid MAC address. | | Time | Time will check if given value is a non-relative time with given layout. | | CustomRule | CustomRule is a dynamic method to define any custom validation rule. | | RequiredFile | RequiredFile checks if a *multipart.FileHeader is present and non-nil. | | FileMimeType | FileMimeType checks the actual file content type against a list of allowed MIME types using content sniffing. | | FileMaxSize | FileMaxSize checks that the uploaded file size does not exceed the given maximum in bytes. | | FileMinSize | FileMinSize checks that the uploaded file size meets the given minimum in bytes. | | FileExtension | FileExtension checks the uploaded file's extension against a list of allowed extensions (case-insensitive). |

Functions (other common validation rules)


| Method | Description | | ------ | ------------------------------------------------------------------- | | In | In checks given value is included in the provided list of values. | | Unique | Unique checks whether values in the provided slice are unique. |


Examples:


  1. Simple usage: (Go Playground)

        type User struct {
            Name string `json:"name"`
            Age uint    `json:"age"`
        }
    
        var user User
       
        v := govalidator.New() // be sure to import govalidator/v2
    
        v.RequiredInt(user.Age, "age", "").         // age can not be null or 0
            MinInt(user.Age, 18, "age", "")         // minimum value for age is 18
    
        v.RequiredString(user.Name, "name", "")     // name can not be null, "" or " "
            MaxString(user.Name, 50, "name", "")    // maximum acceptable length for name field is 50
    
        if v.IsFailed() {
            return v.Errors()  // will return failed validation error messages
        }
    
  2. With custom field names and messages:

        type User struct {
            Name string `json:"name"`
        }
    
        var user User
    
        v := govalidator.New()
    
        v.RequiredString(user.Name, "first_name", "please fill first_name field") // with custom field name and custom validation error message
    
        if v.IsFailed() {
            return v.Errors()
         }
    
  3. Advanced usage with any custom validation rule: You can define any custom rules or any flexible rule that does not exist in default govalidator package. Simply use CustomRule() method to define your desired data validations:

        type Profile struct {
            Name   string
            Age    int
            Score  int
            Status []string
        }
    
        var profile Profile
    
        // after filling profile struct
    
        v := govalidator.New()
    
        v.CustomRule(profile.Name != "", "name", "name is required")  // CustomRule is a method to define a custom rule as first parameter and then pass field name and validation error message
    
        v.CustomRule(profile.Age > 18, "age", "age must be greater than 18")
    
        // we j
    
View on GitHub
GitHub Stars52
CategoryDevelopment
Updated2d ago
Forks8

Languages

Go

Security Score

100/100

Audited on Mar 30, 2026

No findings