validation

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Apr 8, 2023 License: MIT Imports: 6 Imported by: 0

README

Validation Package

GoDoc Go Report Card codecov

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.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FileSizeValidation

func FileSizeValidation(maxSize int64) func(interface{}) ValidationErrorItem

func MaxLengthValidation

func MaxLengthValidation(maxLength int) func(interface{}) ValidationErrorItem

func MinLengthValidation

func MinLengthValidation(minLength int) func(interface{}) ValidationErrorItem

Types

type ValidationErrorItem

type ValidationErrorItem struct {
	Field   string `json:"field"`
	Message string `json:"message"`
}

func DateValidation

func DateValidation(value interface{}) ValidationErrorItem

func EmailValidation

func EmailValidation(value interface{}) ValidationErrorItem

func FileTypeValidation

func FileTypeValidation(value interface{}, validTypes []string) ValidationErrorItem

func FileValidation

func FileValidation(value interface{}) ValidationErrorItem

func ImageMimeValidation

func ImageMimeValidation(value interface{}) ValidationErrorItem

func ImageValidation

func ImageValidation(value interface{}) ValidationErrorItem

func NumericValidation

func NumericValidation(value interface{}) ValidationErrorItem

func PhoneValidation

func PhoneValidation(value interface{}) ValidationErrorItem

func RequiredValidation

func RequiredValidation(value interface{}) ValidationErrorItem

func StringValidation

func StringValidation(value interface{}) ValidationErrorItem

func URLValidation

func URLValidation(value interface{}) ValidationErrorItem

func Validate

func Validate(data interface{}, rules []ValidationRule) []ValidationErrorItem

type ValidationFunc

type ValidationFunc func(interface{}) ValidationErrorItem

type ValidationRule

type ValidationRule struct {
	Field       string
	Description string
	Validations []func(interface{}) ValidationErrorItem
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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