Documentation
¶
Index ¶
- func UnmapOrUnmarshalRequestValidateAndUpdate(request *http.Request, structToUpdate interface{}) error
- func UnmapUrlValuesAndValidateWithValidation(values url.Values, validation ...model.Validation) (model.JsonMap, error)
- func UnmapUrlValuesToJsonMap(values url.Values) (model.JsonMap, error)
- func UnmapValidateAndUpdate(values url.Values, structToUpdate interface{}) error
- func UnmarshalAndValidate(data []byte, v any) error
- func UnmarshalJsonToJsonMap(jsonInput []byte) (model.JsonMap, error)
- func UnmarshalJsonToJsonMapAndValidate(jsonInput []byte, validation ...model.Validation) (model.JsonMap, error)
- func UnmarshalValidateAndUpdate(jsonInput []byte, structToUpdate interface{}) error
- func Validate(v any) error
- func ValidateAndUpdate(jsonInput model.JsonMap, structToUpdate interface{}) error
- func ValidateJsonMap(jsonMap model.JsonMap, validations ...model.Validation) error
- func ValidateValueWithParser(input reflect.Value, validation *model.Validation) (interface{}, error)
- type StructValue
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func UnmapOrUnmarshalRequestValidateAndUpdate ¶ added in v0.2.1
func UnmapOrUnmarshalRequestValidateAndUpdate(request *http.Request, structToUpdate interface{}) error
UnmapOrAnmarshalValidateAndUpdate unmarshals given json ([]byte) or given url.Values (from request.Form), validates them and updates the given struct.
func UnmapUrlValuesAndValidateWithValidation ¶ added in v0.2.1
func UnmapUrlValuesToJsonMap ¶ added in v0.2.1
func UnmapValidateAndUpdate ¶ added in v0.2.1
UnmapValidateAndUpdate unmaps given url.Values into pointer jsonMap. For more information to ValidateAndUpdate look at ValidateAndUpdate(jsonInput model.JsonMap, structToUpdate interface{}) error.
func UnmarshalAndValidate ¶
UnmarshalAndValidate unmarshals given json ([]byte) into pointer v. For more information to Validate look at [Validate(v any) error].
func UnmarshalJsonToJsonMap ¶ added in v0.2.1
func UnmarshalJsonToJsonMapAndValidate ¶ added in v0.2.1
func UnmarshalValidateAndUpdate ¶
UnmarshalValidateAndUpdate unmarshals given json ([]byte) into pointer v. For more information to ValidateAndUpdate look at ValidateAndUpdate(jsonInput model.JsonMap, structToUpdate interface{}) error.
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/bool == condition, len(array) == condition
neq - int/float/string/bool != 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.
func ValidateJsonMap ¶ added in v0.1.9
func ValidateJsonMap(jsonMap model.JsonMap, validations ...model.Validation) error
func ValidateValueWithParser ¶ added in v0.2.0
func ValidateValueWithParser(input reflect.Value, validation *model.Validation) (interface{}, error)