validator

package
v1.26.0 Latest Latest
Warning

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

Go to latest
Published: Jan 4, 2023 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Validator

type Validator struct {

	// Field names in the struct.
	Fields []string
	// contains filtered or unexported fields
}

Validator validates input data and stores it in the output struct. The output struct may contain only string fields, where each field must be decorated with a `name` tag. An input value with matching name is then stored in its corresponding struct field. By default, the input data has to contain a value for each struct field.

Available tags:

  • name:"FIELD-NAME" : key of the value in the input map
  • value: modifies value requirements: value:"required" : field is required value:"optional" : field is optional value:"requiredIf:FIELD-NAME=REGEXP-PATTERN" : field is required if the value of FIELD-NAME matches REGEXP-PATTERN value:"optionalIf:FIELD-NAME=REGEXP-PATTERN" : field is optional if the value of FIELD-NAME matches REGEXP-PATTERN value:"default:VALUE" : field value defaults to VALUE
  • dependsOn:"FIELD-NAMES|,..." : if this field is not empty, the specified fields are required to be present operator ',' acts as AND operator '|' acts as XOR e.g.: dependsOn:"f1|f2|f3,f4,f5" : if this field is not empty, exactly one of {f1,f2,f3} is required to be present, and f4 and f5 is required to be present
  • precludes:"FIELD-NAMES,..." : if this field is not empty, all specified fields are required to be empty
  • matches:"REGEXP-PATTERN" : if this field is not empty, it's required to match REGEXP-PATTERN

func New

func New(stringStruct interface{}) *Validator

New creates a new instance of Validator. stringStruct is the struct used for validation, must contain only string fields, must be the same type used in Validator.Populate. May panic on invalid struct tags.

func (*Validator) Populate

func (v *Validator) Populate(data map[string]string, out interface{}) error

Populate validates input data and populates the output struct.

Jump to

Keyboard shortcuts

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