Documentation
¶
Index ¶
- Constants
- func ArrayOfInterfaceToArrayOf[T comparable](in []interface{}) ([]T, error)
- func Contains[V comparable](list []V, v V) bool
- func RemoveWhere[V comparable](list []V, f func(V) bool) []V
- func UnmarshalAndValidate(data []byte, v any) error
- func UnmarshalValidateAndUpdate(jsonInput []byte, structToUpdate interface{}) error
- func Validate(v any) error
- func ValidateAndUpdate(jsonInput map[string]interface{}, structToUpdate interface{}) error
- type JsonMap
- type StructValue
Constants ¶
Variables ¶
This section is empty.
Functions ¶
func ArrayOfInterfaceToArrayOf ¶
func ArrayOfInterfaceToArrayOf[T comparable](in []interface{}) ([]T, error)
func Contains ¶
func Contains[V comparable](list []V, v V) bool
func RemoveWhere ¶
func RemoveWhere[V comparable](list []V, f func(V) bool) []V
func UnmarshalAndValidate ¶
UnmarshalAndValidate unmarshals given json ([]byte) into pointer v. For more information to Validate look at Validate(value any).
func Validate ¶
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 ¶
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.