Giter VIP home page Giter VIP logo

jubaerhossain / validation Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 11 KB

Go validation package is a package for validating Go structs and their fields based on configurable tags. The package offers various validation rules such as required, email, max, min, and many more to validate the fields of the struct. The package provides a simple and efficient way to validate data before inserting it into the database or using

Home Page: https://github.com/JubaerHossain/validation/wiki

License: MIT License

Go 100.00%
go go-validation go-validator graphql-validation graphql-validator validation golang-validation

validation's Introduction

Validation Package

GoDoc Go Report Card codecov License: MIT

This is a simple Go package for validating structs based on a set of validation rules. It can be used to ensure that incoming data from clients, databases or other sources meets certain criteria before being processed further.

Installation

To use this package in your Go project, you can install it using the go get command:

go get github.com/JubaerHossain/validation

Getting Started

Here's an example of how to use this package to validate a User struct:

package main

import (
	"fmt"

	"github.com/JubaerHossain/validation"
)

type User struct {
	Name     string `json:"first_name"`
	Password string `json:"password"`
}

func main() {

	userInput := User{
		Name:     "John",
		Password: "123456",
	}
	validationErrors := ValidateUser(userInput)
	if validationErrors != nil {
		var errorMsgs []string
		for _, validationErr := range validationErrors {
			errorMsgs = append(errorMsgs, validationErr.Field+" : "+validationErr.Message)
		}
		fmt.Println(errorMsgs)
	}
}

func ValidateUser(user User) []validation.ValidationErrorItem {

	rules := []validation.ValidationRule{
		{
			Field:       "name",
			Description: "Name",
			Validations: []func(interface{}) validation.ValidationErrorItem{
				validation.RequiredValidation,
				validation.MinLengthValidation(3),
				validation.MaxLengthValidation(50),
			},
		},
		{
			Field:       "password",
			Description: "Password",
			Validations: []func(interface{}) validation.ValidationErrorItem{
				validation.RequiredValidation,
				validation.MinLengthValidation(6),
				validation.MaxLengthValidation(50),
			},
		},
	}

	return validation.Validate(user, rules)
}

In this example, we define a User struct with two fields: Name and Password. We then define an array of ValidationRule structs that describe the validation rules for each field. Each ValidationRule has a Field name, a human-readable Description of the field, and an array of validation functions that take a value and return a ValidationErrorItem if the value is invalid.

We then call the Validate function with the User struct and the array of validation rules. This function returns an array of ValidationErrorItems if there are any validation errors, or an empty array if the input is valid.

Available Validations

Here are the available validation functions that can be used in a ValidationRule:

  • RequiredValidation - Returns an error if the value is nil or an empty string.
  • MinLengthValidation(min int) - Returns an error if the string length is less than the minimum value.
  • MaxLengthValidation(max int) - Returns an error if the string length is greater than the maximum value.
  • You can also define your own custom validation functions by implementing the func(interface{}) ValidationErrorItem signature.

License

This package is licensed under the MIT License. See the LICENSE file for details.

validation's People

Contributors

jubaerhossain avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.