validator

package module
v0.1.8 Latest Latest
Warning

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

Go to latest
Published: Aug 2, 2024 License: Apache-2.0 Imports: 11 Imported by: 0

README

validator

Description

Validator for structs to validate fields by tag

Validate validates a given struct by vld tags. Validate needs a struct as input.

All fields in the struct need a vld tag. If you want to ignore one field in the validator you can add `vld:"-"`. If you don't add the vld tag to every field the function will fail with an error.

If you want to use multiple conditions you can add them with a space in between them.

A complex example for a password check (min length 8, max length 30, at least one capital letter, one small letter, one digit and one special character) would be: `vld:"min8 max30 rex^(.*[A-Z])+(.*)$ rex^(.*[a-z])+(.*)$ rex^(.*\\d)+(.*)$ rex^(.*[\x60!@#$%^&*()_+={};':\"|\\,.<>/?~-])+(.*)$"`

Condition types

- - ignores the field

equ - equal (value or length)

neq - not equal (value or length)

min - min (value or length)

max - max (value or length)

con - contains

rex - regular expression

Usage

Conditions have different usages per variable type:

equ - int/float/string == condition, len(array) == condition

neq - int/float/string != condition, len(array) != condition

min - int/float >= condition, len(strings.TrimSpace(string)/array) >= condition

max - int/float <= condition, len(strings.TrimSpace(string)/array) <= condition

con - strings.Contains(string, condition), contains(array, condition), int/float ignored

rex - regexp.MatchString(condition, strconv.Itoa(int)/strconv.FormatFloat(float, 'f', 3, 64)/string), array ignored

For con you need to put in a condition that is convertable to the underlying type of the arrary. Eg. for an array of int the condition must be convertable to int (bad: `vld:"conA"`, good: `vld:"con1"`).

In the case of rex the int and float input will get converted to a string (strconv.Itoa(int) and fmt.Sprintf("%f", f)). If you want to check more complex cases you can obviously replace equ, neq, min, max and con with one regular expression.

Testing

To run the tests run go test ..

Benchmark

To run benchmarks run go test -bench . -count 100 > bench.txt (with memory allocation would be go test -bench . -benchmem -count 100 > bench.txt but they are 0). To see the results in a nice way after the run install go install golang.org/x/perf/cmd/benchstat@latest and log the results to the console: benchstat bench.txt.

Creating a new version

To create a new tagged version run eg. git tag v0.1.3. To push and publish it run eg. git push origin v0.1.3.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func InterfaceFromString added in v0.1.7

func InterfaceFromString(in string, inType model.ValidatorType, condition string) (interface{}, error)

func NewJsonMapFromUrlValues added in v0.1.5

func NewJsonMapFromUrlValues(values url.Values, validation ...model.Validation) (model.JsonMap, error)

func UnmarshalAndValidate

func UnmarshalAndValidate(data []byte, v any) error

UnmarshalAndValidate unmarshals given json ([]byte) into pointer v. For more information to Validate look at [Validate(v any) error].

func UnmarshalValidateAndUpdate

func UnmarshalValidateAndUpdate(jsonInput []byte, structToUpdate interface{}) error

UnmarshalValidateAndUpdate unmarshals given json ([]byte) into pointer v. For more information to ValidateAndUpdate look at [ValidateAndUpdate(jsonInput map[string]interface{}, structToUpdate interface{}) error].

func Validate

func Validate(v any) error

Validate validates a given struct by vld tags. Validate needs a struct as input.

All fields in the struct need a vld tag. If you want to use multiple conditions you can add them with a space in between them.

A complex example for password would be: `vld:"min8 max30 rex^(.*[A-Z])+(.*)$ rex^(.*[a-z])+(.*)$ rex^(.*\\d)+(.*)$ rex^(.*[\x60!@#$%^&*()_+={};':\"|\\,.<>/?~-])+(.*)$"`

If you want to ignore one field in the validator you can add `vld:"-"`. If you don't add the vld tag to every field the function will fail with an error.

Conditions have different usages per variable type:

equ - int/float/string == condition, len(array) == condition

neq - int/float/string != condition, len(array) != condition

min - int/float >= condition, len(string/array) >= condition

max - int/float <= condition, len(string/array) <= condition

con - strings.Contains(string, condition), contains(array, condition), int/float ignored

rex - regexp.MatchString(condition, int/float/string), array ignored

For con you need to put in a condition that is convertable to the underlying type of the arrary. Eg. for an array of int the condition must be convertable to int (bad: `vld:"conA"`, good: `vld:"con1"`).

In the case of rex the int and float input will get converted to a string (strconv.Itoa(int) and fmt.Sprintf("%f", f)). If you want to check more complex cases you can obviously replace equ, neq, min, max and con with one regular expression.

func ValidateAndUpdate

func ValidateAndUpdate(jsonInput map[string]interface{}, structToUpdate interface{}) error

ValidateAndUpdate validates a given struct by upd tags. ValidateAndUpdate needs a struct pointer and a json map as input. The given struct is updated by the values in the json map.

All fields in the struct need a upd tag. The tag has to contain the key value for the json struct. If no tag is present the field in the struct is ignored and does not get updated.

The second part of the tag contains the conditions for the validation.

If you want to use multiple conditions you can add them with a space in between them.

A complex example for password would be: `upd:"password, min8 max30 rex^(.*[A-Z])+(.*)$ rex^(.*[a-z])+(.*)$ rex^(.*\\d)+(.*)$ rex^(.*[\x60!@#$%^&*()_+={};':\"|\\,.<>/?~-])+(.*)$"`

If you want don't want to validate the field you can add `upd:"json_key, -"`. If you don't add the upd tag to every field the function will fail with an error.

Conditions have different usages per variable type:

equ - int/float/string == condition, len(array) == condition

neq - int/float/string != condition, len(array) != condition

min - int/float >= condition, len(string/array) >= condition

max - int/float <= condition, len(string/array) <= condition

con - strings.Contains(string, condition), contains(array, condition), int/float ignored

rex - regexp.MatchString(condition, int/float/string), array ignored

For con you need to put in a condition that is convertable to the underlying type of the arrary. Eg. for an array of int the condition must be convertable to int (bad: `upd:"array, conA"`, good: `upd:"array, con1"`).

In the case of rex the int and float input will get converted to a string (strconv.Itoa(int) and fmt.Sprintf("%f", f)). If you want to check more complex cases you can obviously replace equ, neq, min, max and con with one regular expression.

Types

type StructValue

type StructValue struct {
	Error  error
	Groups []string
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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