Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
Package go-validate-yourself
================
[![Build Status](https://travis-ci.org/bluesuncorp/go-validate-yourself.svg?branch=v4)](https://travis-ci.org/bluesuncorp/go-validate-yourself)
[![GoDoc](https://godoc.org/gopkg.in/bluesuncorp/go-validate-yourself.v4?status.svg)](https://godoc.org/gopkg.in/bluesuncorp/go-validate-yourself.v4)
[![Build Status](https://travis-ci.org/bluesuncorp/go-validate-yourself.svg?branch=v5)](https://travis-ci.org/bluesuncorp/go-validate-yourself)
[![GoDoc](https://godoc.org/gopkg.in/bluesuncorp/go-validate-yourself.v5?status.svg)](https://godoc.org/gopkg.in/bluesuncorp/go-validate-yourself.v5)

Package validator implements value validations for structs and individual fields based on tags.
It is even capable of Cross Field and even Cross Field Cross Struct validation.

Installation
============

Just use go get.

go get gopkg.in/bluesuncorp/go-validate-yourself.v4
go get gopkg.in/bluesuncorp/go-validate-yourself.v5

or to update

go get -u gopkg.in/bluesuncorp/go-validate-yourself.v4
go get -u gopkg.in/bluesuncorp/go-validate-yourself.v5

And then just import the package into your own code.

import "gopkg.in/bluesuncorp/go-validate-yourself.v4"
import "gopkg.in/bluesuncorp/go-validate-yourself.v5"

Usage
=====

Please see http://godoc.org/gopkg.in/bluesuncorp/go-validate-yourself.v4 for detailed usage docs.
Please see http://godoc.org/gopkg.in/bluesuncorp/go-validate-yourself.v5 for detailed usage docs.

Contributing
============
Expand Down
2 changes: 1 addition & 1 deletion baked_in.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
// BakedInValidators is the default map of ValidationFunc
// you can add, remove or even replace items to suite your needs,
// or even disregard and use your own map if so desired.
var BakedInValidators = map[string]ValidationFunc{
var BakedInValidators = map[string]Func{
"required": hasValue,
"len": hasLengthOf,
"min": hasMinOf,
Expand Down
46 changes: 23 additions & 23 deletions doc.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/*
Package validator implements value validations for structs and individual fields based on tags. It can also handle Cross Field validation and even Cross Field Cross Struct validation for nested structs.

Built In Validator
Validate

myValidator = validator.NewValidator("validate", validator.BakedInValidators)
validate := validator.New("validate", validator.BakedInValidators)

errs := myValidator.ValidateStruct(//your struct)
valErr := myValidator.ValidateFieldByTag(field, "omitempty,min=1,max=10")
errs := validate.Struct(//your struct)
valErr := validate.Field(field, "omitempty,min=1,max=10")

A simple example usage:

Expand All @@ -25,17 +25,17 @@ A simple example usage:
}

// errs will contain a hierarchical list of errors
// using the StructValidationErrors struct
// using the StructErrors struct
// or nil if no errors exist
errs := myValidator.ValidateStruct(user)
errs := validate.Struct(user)

// in this case 1 error Name is required
errs.Struct will be "User"
errs.StructErrors will be empty <-- fields that were structs
errs.Errors will have 1 error of type FieldValidationError
errs.Errors will have 1 error of type FieldError

NOTE: Anonymous Structs - they don't have names so expect the Struct name
within StructValidationErrors to be blank.
within StructErrors to be blank.

Error Handling

Expand All @@ -45,7 +45,7 @@ The error can be used like so
fieldErr.Field // "Name"
fieldErr.ErrorTag // "required"

Both StructValidationErrors and FieldValidationError implement the Error interface but it's
Both StructErrors and FieldError implement the Error interface but it's
intended use is for development + debugging, not a production error message.

fieldErr.Error() // Field validation for "Name" failed on the "required" tag
Expand All @@ -67,7 +67,7 @@ I needed to know the field and what validation failed so that I could provide an
}

The hierarchical error structure is hard to work with sometimes.. Agreed Flatten function to the rescue!
Flatten will return a map of FieldValidationError's but the field name will be namespaced.
Flatten will return a map of FieldError's but the field name will be namespaced.

// if UserDetail Details field failed validation
Field will be "Sub.Details"
Expand All @@ -89,19 +89,19 @@ Custom functions can be added
return true
}

myValidator.AddFunction("custom tag name", customFunc)
validate.AddFunction("custom tag name", customFunc)
// NOTES: using the same tag name as an existing function
// will overwrite the existing one

Cross Field Validation

Cross Field Validation can be implemented, for example Start & End Date range validation

// NOTE: when calling myValidator.validateStruct(val) val will be the top level struct passed
// NOTE: when calling validate.Struct(val) val will be the top level struct passed
// into the function
// when calling myValidator.ValidateFieldByTagAndValue(val, field, tag) val will be
// when calling validate.FieldWithValue(val, field, tag) val will be
// whatever you pass, struct, field...
// when calling myValidator.ValidateFieldByTag(field, tag) val will be nil
// when calling validate.Field(field, tag) val will be nil
//
// Because of the specific requirements and field names within each persons project that
// uses this library it is likely that custom functions will need to be created for your
Expand Down Expand Up @@ -225,29 +225,29 @@ Here is a list of the current built in validators:
Only valid for Numbers and time.Time types, this will validate the field value
against another fields value either within a struct or passed in field.
usage examples are for validation of a Start and End date:
Validation on End field using ValidateByStruct Usage(gtfield=Start)
Validating by field ValidateFieldByTagAndValue(start, end, "gtfield")
Validation on End field using validate.Struct Usage(gtfield=Start)
Validating by field validate.FieldWithValue(start, end, "gtfield")

gtefield
Only valid for Numbers and time.Time types, this will validate the field value
against another fields value either within a struct or passed in field.
usage examples are for validation of a Start and End date:
Validation on End field using ValidateByStruct Usage(gtefield=Start)
Validating by field ValidateFieldByTagAndValue(start, end, "gtefield")
Validation on End field using validate.Struct Usage(gtefield=Start)
Validating by field validate.FieldWithValue(start, end, "gtefield")

ltfield
Only valid for Numbers and time.Time types, this will validate the field value
against another fields value either within a struct or passed in field.
usage examples are for validation of a Start and End date:
Validation on End field using ValidateByStruct Usage(ltfield=Start)
Validating by field ValidateFieldByTagAndValue(start, end, "ltfield")
Validation on End field using validate.Struct Usage(ltfield=Start)
Validating by field validate.FieldWithValue(start, end, "ltfield")

ltefield
Only valid for Numbers and time.Time types, this will validate the field value
against another fields value either within a struct or passed in field.
usage examples are for validation of a Start and End date:
Validation on End field using ValidateByStruct Usage(ltefield=Start)
Validating by field ValidateFieldByTagAndValue(start, end, "ltefield")
Validation on End field using validate.Struct Usage(ltefield=Start)
Validating by field validate.FieldWithValue(start, end, "ltefield")

alpha
This validates that a strings value contains alpha characters only
Expand Down Expand Up @@ -329,6 +329,6 @@ This package panics when bad input is provided, this is by design, bad code like
TestField: "Test"
}

myValidator.ValidateStruct(t) // this will panic
validate.Struct(t) // this will panic
*/
package validator
Loading