validate

package
v0.0.0-...-dc4534b Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2015 License: MIT Imports: 3 Imported by: 0

README

github.com/markbates/going/validate

This package provides a framework for writing validations for Go applications. It does not, however, provide you with any actual validators, that part is up to you.

Installation

$ go get https://github.com/markbates/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/markbates/going/validate"
)

type User struct {
	Name  string
	Email string
}

func (u *User) IsValid(errors *v.ValidationErrors) {
	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/markbates/going/validate"
)

type User struct {
	Name  string
	Email string
}

type PresenceValidator struct {
	Field string
	Value string
}

func (v *PresenceValidator) IsValid(errors *v.ValidationErrors) {
	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.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Errors

type Errors struct {
	Errors map[string][]string `json:"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) 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) 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.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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