Giter Club home page Giter Club logo

golvalidator's Introduction

golvalidator

A golang laravel inspired validation

Installation

Install the package using

$ go get github.com/silasrm/golvalidator

Usage

To use the package import it in your *.go code

import "github.com/silasrm/golvalidator"

Example

Validate struct only at the moment

package main

import (
	"github.com/gofiber/fiber/v2"
	"github.com/silasrm/golvalidator"
)

func main() {
	app := fiber.New()

	app.Get("/", Test)

	app.Listen(":3002")
}

type UserType struct {
	FirstName  string `json:"first_name" validate:"required_with:last_name"  validate_message:"required_with:O campo %s é obrigatório quando %s está presente."``
	MiddleName string `json:"middle_name" validate:"required|string|min:3|same:last_name"`
	LastName   string `json:"last_name" validate:"required|string|min:3"`
}

func Test(c *fiber.Ctx) error {
	user := UserType{
		FirstName:  "",
		LastName:   "e1",
		MiddleName: "3",
	}

	errors := golvalidator.ValidateStructs(user)
	if len(errors) > 0 {
		return c.JSON(fiber.Map{
			"errors": errors,
		})
	}

	return c.SendString("No Errors")
}

Errors

{
    "errors": {
        "first_name": [
            "The first name field is required when last name is present."
        ],
        "last_name": [
            "The last name must only contain letters.",
            "The last name must be at least 3 characters."
        ],
        "middle_name": [
            "The middle name must only contain letters.",
            "The middle name must be at least 3 characters.",
            "The middle name and last name must match."
        ]
    }
}

Available Validation

alpha
string
numeric
alpha_num
alpha_space
alpha_dash
date
email
same
digits
digits_between

min,max,between:
-string length
-numeric

lt,gt,lte,gte:
-string length
-numeric

nullable
required
required_if
required_with
ip
ipv4
ipv6
url
credit_card

Pending

Unique -> (Gorm Dependency)
File Size
File Extension

golvalidator's People

Contributors

kmacute avatar silasrm avatar

Watchers

 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.