validate

package module
Version: v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Oct 5, 2020 License: MIT Imports: 5 Imported by: 0

README

Validate Go

A struct validator which group in a map and custom errors messages from a simplified Go Validator.

Usage

Registering custom validation func
Example
    validate.RegisterValidation("underAge", underAge, "the :field is too low")
Registering
    // name is the validation name where will be used on struct tags
    // function is the validation function that will receive the field
    // value to be validate
    // message is the custom message when validation failed, use :field
    // in the message and will be replaced by failed field name
   validate.RegisterValidation(name string, function ValidationFunc, message string)
ValidationFunc
    type ValidationFunc func(field IField) bool
IField
   type IField interface {
   	Field() reflect.Value
   }
Validation name usage example
   type Student struct {
   	Name     string `validate:"required"`
   	Email    string `validate:"required,email"`
   	Password string `validate:"required"`
   	Age      int    `validate:"underAge"`
   }
Registering custom message to a validation
Example
    validate.RegisterValidationMessage("underAge", "the :field is too low")
Registering
    validate.RegisterValidationMessage(name string, message string)
Validation
    // data is struct that will be under validation
    // errors is a map[string]string with the failed field 
    // name as key and error message as value
    // ok is a boolean warning the validation status
    errors, ok := validate.Validate(data interface{})

Documentation

Index

Constants

View Source
const ApplicationName = "validate-go"
View Source
const Version = "0.2.2"

Variables

View Source
var GitCommit string
View Source
var VersionPrerelease = ""

Functions

func RegisterValidation added in v0.2.0

func RegisterValidation(name string, function ValidationFunc, message ...string)

func RegisterValidationMessage added in v0.2.0

func RegisterValidationMessage(name string, message string)

func Validate added in v0.2.0

func Validate(data interface{}) (map[string]string, bool)

Types

type Field

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

func (*Field) Field

func (f *Field) Field() reflect.Value

type IField

type IField interface {
	Field() reflect.Value
}

type ValidationFunc

type ValidationFunc func(field IField) bool

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL