Skip to content

dariomatias-dev/go-validators

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation


Go

Go Validators

A powerful, lightweight, and dependency-free validation package for Go.
Explore the docs »

pkg.go.dev · Report Bug · Request Feature

Table of Contents

About The Project

Go Validators is a validation package for Go designed to be powerful, lightweight, and dependency-free. It provides a flexible way to handle data validation, allowing developers to declaratively validate structs from JSON payloads using validates tags or use individual validation functions for granular control.

This package is easily integrated into any Go project without introducing external dependencies, offering a simple yet extensible solution for ensuring data integrity.

Features

  • Struct Validation: Declaratively validate structs from JSON payloads using validates tags.
  • Standalone Functions: Use individual validation functions for granular control over any value.
  • Zero Dependencies: A lightweight package that can be integrated into any project without external dependencies.
  • Customizable: Extend the library with your own logic using the Custom validator.

Built With

This project was developed using the following core technologies:

  • Go – An open-source programming language designed for simplicity, reliability, and efficiency in building scalable software.

Getting Started

To get a local copy up and running, follow these steps.

Installation

go get github.com/dariomatias-dev/go-validators

Execution Order

Validators should be organized in a three-stage order within validates tags:

  1. Presence Validators (required): Confirm the field exists.
  2. Type Validators (isString, isInt, etc.): Ensure the value has the correct data type.
  3. Value Validators (minLength, email, etc.): Apply specific rules to the value's content.

This order prevents runtime errors by ensuring that value validators never receive an unexpected type.

Usage

Import the package using an alias for cleaner code:

import v "github.com/dariomatias-dev/go-validators"

Struct Validation (via Tags)

Validate a JSON payload by mapping it to a struct with validates tags. The Validate function returns a map of errors or nil on success.

type UserModel struct {
    Name  string `json:"name"  validates:"required;isString;minLength=3;maxLength=20"`
    Age   int    `json:"age"   validates:"required;isInt;min=18;max=100"`
    Email string `json:"email" validates:"required;email"`
}

user := UserModel{}

jsonPayload := `{
    "name":  "Name",
    "age":   16,
    "email": "emailexample@gmail.com"
}`

err := v.Validate(&user, jsonPayload)
if err != nil {
    fmt.Println(err)
    return
}

Standalone Validation

Validators are configurable higher-order functions. The first parentheses configure the rule, the second receives the value.

value := 4
err, _ := v.Min(3, "Value must be at least 3.")(value)
if err != nil {
    fmt.Println(err)
}

value = 2
err, _ = v.Min(3)(value)
if err != nil {
    fmt.Println(err)
    return
}

Available Validators

All parameters marked with (*) are mandatory. Custom error messages are always optional.

Validator Category Parameters Applicable Value Type
Required Presence any
IsString Type string
IsNumber Type int, float
IsInt Type int
IsFloat Type float
IsBool Type bool
IsArray Type field validators string* slice, array
IsNullString Type nil, string
IsNullNumber Type nil, int, float
IsNullInt Type nil, int
IsNullFloat Type nil, float
IsNullBool Type nil, bool
IsNullArray Type field validators string* nil, slice, array
Email Value string
Password Value string
Min Value minimum value (number)* int, int32, int64, float32, float64
Max Value maximum value (number)* int, int32, int64, float32, float64
Length Value size int* string, slice, array
MinLength Value minimum size int* string, slice, array
MaxLength Value maximum size int* string, slice, array
IsAlpha Value string
IsAlphaNum Value string
IsAlphaSpace Value string
IsAlphaNumSpace Value string
StartsWith Value prefix string* string
StartsNotWith Value prefix string* string
EndsWith Value suffix string* string
EndsNotWith Value suffix string* string
Regex Value regex string* string
Url Value string
Uuid Value version int (Default: 5) string
OneOf Value options []string* string
Custom Value custom validator function* any

Contributing

Contributions make the open-source community an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.

  1. Fork the Project
  2. Create your Feature Branch
git checkout -b feature/AmazingFeature
  1. Commit your Changes
git commit -m 'Add some AmazingFeature'
  1. Push to the Branch
git push origin feature/AmazingFeature
  1. Open a Pull Request

License

Distributed under the MIT License. See the LICENSE file for more information.

Author

Developed by Dário Matias: