structvalidator

package module
v0.6.1 Latest Latest
Warning

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

Go to latest
Published: Jun 5, 2025 License: MIT Imports: 6 Imported by: 0

README

structvalidator

Go Reference Go Report Card

Verify the values of struct fields using tags

Example code

Use the package with the following URL:

import "github.com/keenbytes/structvalidator"

And see below code snippet:

type Test1 struct {
	FirstName     string `validation:"req lenmin:5 lenmax:25"`
	LastName      string `validation:"req lenmin:2 lenmax:50"`
	Age           int    `validation:"req valmin:18 valmax:150"`
	Price         int    `validation:"req valmin:0 valmax:9999"`
	PostCode      string `validation:"req" validation_regexp:"^[0-9][0-9]-[0-9][0-9][0-9]$"`
	Email         string `validation:"req email"`
	BelowZero     int    `validation:"valmin:-6 valmax:-2"`
	DiscountPrice int    `validation:"valmin:0 valmax:8000"`
	Country       string `validation_regexp:"^[A-Z][A-Z]$"`
	County        string `validation:"lenmax:40"`
}

s := &Test1{
	FirstName: "Name that is way too long and certainly not valid",
	...
}

o := structvalidator.&ValidationOptions{
	RestrictFields: map[string]bool{
		"FirstName": true,
		"LastName":  true,
		...
	},
	...
}

isValid, fieldsWithInvalidValue := structvalidator.Validate(s, &o)

Documentation

Index

Constants

View Source
const (
	FailLenMin = 1 << iota
	FailLenMax
	FailValMin
	FailValMax
	FailEmpty
	FailRegexp
	FailEmail
	FailZero
)

values for invalid field flags

View Source
const (
	ValMinNotNil = 1 << iota
	ValMaxNotNil
	Required
	Email
)

values used with flags

View Source
const TypeEmail = 4
View Source
const TypePassword = 3
View Source
const TypeText = 1
View Source
const TypeTextarea = 2
View Source
const VERSION = "0.6.1"

Variables

This section is empty.

Functions

func GenerateHTML

func GenerateHTML(obj interface{}, options *HTMLOptions) map[string]string

GenerateHTMLInput takes a struct and generates HTML inputs for each of the fields, eg. <input> or <textarea>

func Validate

func Validate(obj interface{}, options *ValidationOptions) (bool, map[string]int)

Validate validates fields of a struct. Currently only fields which are string or int (any) are validated. Func returns boolean value that determines whether value is true or false, and a map of fields that failed validation. See Fail* constants for the values.

Types

type HTMLOptions

type HTMLOptions struct {
	RestrictFields     map[string]bool
	ExcludeFields      map[string]bool
	OverwriteFieldTags map[string]map[string]string
	OverwriteTagName   string
	ValidateWhenSuffix bool
	IDPrefix           string
	NamePrefix         string
	OverwriteValues    map[string]string
	FieldValues        bool
}

Optional configuration for validation: * RestrictFields defines what struct fields should be generated * ExcludeFields defines fields that should be skipped (also from RestrictFields) * OverwriteFieldTags can be used to overwrite tags for specific fields * OverwriteTagName sets tag used to define validation (default is "validation") * ValidateWhenSuffix will validate certain fields based on their name, eg. "PrimaryEmail" field will need to be a valid email * OverwriteFieldValues is to use overwrite values for fields, so these values are validated not the ones in struct * IDPrefix - if added, an element will contain an 'id' attribute in form of prefix + field name * NamePrefix - use this to put a prefix in the 'name' attribute * OverwriteValues - fill inputs with the specified values * FieldValues - when true then fill inputs with struct instance values

type ValidationOptions

type ValidationOptions struct {
	RestrictFields       map[string]bool
	OverwriteFieldTags   map[string]map[string]string
	OverwriteTagName     string
	ValidateWhenSuffix   bool
	OverwriteFieldValues map[string]interface{}
}

Optional configuration for validation: * RestrictFields defines what struct fields should be validated * OverwriteFieldTags can be used to overwrite tags for specific fields * OverwriteTagName sets tag used to define validation (default is "validation") * ValidateWhenSuffix will validate certain fields based on their name, eg. "PrimaryEmail" field will need to be a valid email * OverwriteFieldValues is to use overwrite values for fields, so these values are validated not the ones in struct

type ValueValidation

type ValueValidation struct {
	LenMin int
	LenMax int
	ValMin int64
	ValMax int64
	Regexp *regexp.Regexp
	Flags  int64
}

func NewValueValidation

func NewValueValidation() *ValueValidation

func (*ValueValidation) ValidateReflectValue

func (v *ValueValidation) ValidateReflectValue(value reflect.Value) (ok bool, failureFlags int)

Jump to

Keyboard shortcuts

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