govalidatorunique

package module
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Sep 29, 2020 License: MIT Imports: 4 Imported by: 0

README

govalidator-gorm-unique

Unique validator extensions for thedevsaddam/govalidator. Inspired by Laravel's unique validation rule & go-unique-validator.

Installation

Before use this validator, you need to install thedevsaddam/govalidator first, and then install this package.

go get github.com/mashurimansur/govalidator-gorm-unique

Usage

Import this package to your code

import uniquevalidator "github.com/mashurimansur/govalidator-gorm-unique"

Create db instance

db, err := gorm.Open("mysql", "username:password@tcp(127.0.0.1:3306)/dbname?parseTime=true")
if err != nil {
  log.Fatal(err)
}
defer db.Close()

Add as custom rule to govalidator

uniqueRule := uniquevalidator.NewUniqueRule(db, "unique")
govalidator.AddCustomRule("unique", uniqueRule.Rule)
Example Rule

Format: unique:table,column,except,idColumn

To check if attribute is unique:

rules := govalidator.MapData{
	"email": []string{"required", "email", "unique:users,email"},
}

Forcing A Unique Rule To Ignore A Given ID:

rules := govalidator.MapData{
	"email": []string{"required", "email", "unique:users,email,id,123"},
}
Example Usage
package main

import (
	"encoding/json"
	"fmt"
	"log"
	"net/http"

	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/mysql"
	uniquevalidator "github.com/mashurimansur/govalidator-gorm-unique"
	"github.com/thedevsaddam/govalidator"
)

func handler(w http.ResponseWriter, r *http.Request) {
	rules := govalidator.MapData{
		"email": []string{"required", "email", "unique:users,email"},
	}

	opts := govalidator.Options{
		Request:         r,     // request object
		Rules:           rules, // rules map
		RequiredDefault: true,  // all the field to be pass the rules
	}
	v := govalidator.New(opts)
	e := v.Validate()
	err := map[string]interface{}{"validationError": e}
	w.Header().Set("Content-type", "application/json")
	json.NewEncoder(w).Encode(err)
}

func main() {
	db, err := gorm.Open("mysql", "username:password@tcp(127.0.0.1:3306)/dbname?parseTime=true")
	if err != nil {
		log.Fatal(err)
	}
	defer db.Close()

	uniqueRule := uniquevalidator.NewUniqueRule(db, "unique")
	govalidator.AddCustomRule("unique", uniqueRule.Rule)

	http.HandleFunc("/", handler)
	fmt.Println("Listening on port: 9000")
	http.ListenAndServe(":9000", nil)
}

Send request to the server using curl or postman: curl GET "http://localhost:9000?email=your@email.com"

Response

{
    "validationError": {
        "email": [
            "The email has already been taken"
        ]
    }
}

License

The govalidator-gorm-unique is an open-source software licensed under the MIT License.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type UniqueRule

type UniqueRule struct {
	// contains filtered or unexported fields
}

UniqueRule to check data is unique or not in db

func NewUniqueRule

func NewUniqueRule(db *gorm.DB, ruleName string) *UniqueRule

NewUniqueRule to create instance

func (*UniqueRule) Rule

func (r *UniqueRule) Rule(field string, rule string, message string, value interface{}) error

Rule is func to register as custom rule

Jump to

Keyboard shortcuts

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