validate

package module
v2.0.4+incompatible Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2019 License: MIT Imports: 4 Imported by: 191 modules, 246 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

github.com/gobuffalo/validate

Build Status GoDoc

This package provides a framework for writing validations for Go applications. It does provide you with few validators, but if you need others you can easly build them.

Installation

$ go get github.com/gobuffalo/validate

Usage

Using validate is pretty easy, just define some Validator objects and away you go.

Here is a pretty simple example:

package main

import (
	"log"

	v "github.com/gobuffalo/validate"
)

type User struct {
	Name  string
	Email string
}

func (u *User) IsValid(errors *v.Errors) {
	if u.Name == "" {
		errors.Add("name", "Name must not be blank!")
	}
	if u.Email == "" {
		errors.Add("email", "Email must not be blank!")
	}
}

func main() {
	u := User{Name: "", Email: ""}
	errors := v.Validate(&u)
	log.Println(errors.Errors)
  // map[name:[Name must not be blank!] email:[Email must not be blank!]]
}

In the previous example I wrote a single Validator for the User struct. To really get the benefit of using go-validator, as well as the Go language, I would recommend creating distinct validators for each thing you want to validate, that way they can be run concurrently.

package main

import (
	"fmt"
	"log"
	"strings"

	v "github.com/gobuffalo/validate"
)

type User struct {
	Name  string
	Email string
}

type PresenceValidator struct {
	Field string
	Value string
}

func (v *PresenceValidator) IsValid(errors *v.Errors) {
	if v.Value == "" {
		errors.Add(strings.ToLower(v.Field), fmt.Sprintf("%s must not be blank!", v.Field))
	}
}

func main() {
	u := User{Name: "", Email: ""}
	errors := v.Validate(&PresenceValidator{"Email", u.Email}, &PresenceValidator{"Name", u.Name})
	log.Println(errors.Errors)
        // map[name:[Name must not be blank!] email:[Email must not be blank!]]
}

That's really it. Pretty simple and straight-forward Just a nice clean framework for writing your own validators. Use in good health.

Built-in Validators

To make it even simpler, this package has a children package with some nice built-in validators.

package main

import (
	"log"

	"github.com/gobuffalo/validate"
	"github.com/gobuffalo/validate/validators"
)

type User struct {
	Name  string
	Email string
}


func main() {
	u := User{Name: "", Email: ""}
	errors := validate.Validate(
		&validators.EmailIsPresent{Name: "Email", Field: u.Email, Message: "Mail is not in the right format."},
		&validators.StringIsPresent{Field: u.Name, Name: "Name"},
	)
	log.Println(errors.Errors)
	// map[name:[Name can not be blank.] email:[Mail is not in the right format.]]
}

All fields are required for each validators, except Message (every validator has a default error message).

Available Validators

A full list of available validators can be found at https://godoc.org/github.com/gobuffalo/validate/validators.

Documentation

Index

Constants

View Source
const Version = "v0.0.1"

Version of validate

Variables

This section is empty.

Functions

This section is empty.

Types

type Errors

type Errors struct {
	Errors map[string][]string `json:"errors" xml:"errors"`
	Lock   *sync.RWMutex       `json:"-"`
}

Errors holds onto all of the error messages that get generated during the validation process.

func NewErrors

func NewErrors() *Errors

NewErrors returns a pointer to a Errors object that has been primed and ready to go.

func Validate

func Validate(validators ...Validator) *Errors

Validate takes in n number of Validator objects and will run them and return back a point to a Errors object that will contain any errors.

func (*Errors) Add

func (v *Errors) Add(key string, msg string)

Add will add a new message to the list of errors using the given key. If the key already exists the message will be appended to the array of the existing messages.

func (*Errors) Append

func (v *Errors) Append(ers *Errors)

Append concatenates two Errors objects together. This will modify the first object in place.

func (*Errors) Count

func (v *Errors) Count() int

Count returns the number of errors.

func (*Errors) Error

func (v *Errors) Error() string

Error implements the error interface

func (*Errors) Get

func (v *Errors) Get(key string) []string

Get returns an array of error messages for the given key.

func (*Errors) HasAny

func (v *Errors) HasAny() bool

HasAny returns true/false depending on whether any errors have been tracked.

func (*Errors) Keys

func (v *Errors) Keys() []string

Keys return all field names which have error

func (Errors) MarshalXML

func (e Errors) MarshalXML(enc *xml.Encoder, start xml.StartElement) error

func (*Errors) String

func (v *Errors) String() string

type Validator

type Validator interface {
	IsValid(errors *Errors)
}

Validator must be implemented in order to pass the validator object into the Validate function.

func ValidatorFunc

func ValidatorFunc(fn func(errors *Errors)) Validator

ValidatorFunc wraps any function in a "Validator" to make it easy to write custom ones.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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