validator

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jun 12, 2023 License: BSD-2-Clause Imports: 4 Imported by: 1

README

go-mod-struct-validator

Simple validator for Go struct

Example code
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 := &ValidationOptions{
	RestrictFields: map[string]bool{
		"FirstName": true,
		"LastName":  true,
		...
	},
	...
}

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

Documentation

Index

Constants

View Source
const Email = 16
View Source
const FailEmail = 128
View Source
const FailEmpty = 32
View Source
const FailLenMax = 4
View Source
const FailLenMin = 2

values for invalid field flags

View Source
const FailRegexp = 64
View Source
const FailValMax = 16
View Source
const FailValMin = 8
View Source
const FailZero = 256
View Source
const Required = 8
View Source
const VERSION = "0.3.0"
View Source
const ValMaxNotNil = 4
View Source
const ValMinNotNil = 2

values used with flags

Variables

This section is empty.

Functions

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 FieldValidation

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

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

Jump to

Keyboard shortcuts

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