...

Source file src/github.com/go-playground/validator/v10/_examples/map-validation/main.go

Documentation: github.com/go-playground/validator/v10/_examples/map-validation

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/go-playground/validator/v10"
     6  )
     7  
     8  var validate *validator.Validate
     9  
    10  func main() {
    11  	validate = validator.New()
    12  
    13  	validateMap()
    14  	validateNestedMap()
    15  }
    16  
    17  func validateMap() {
    18  	user := map[string]interface{}{"name": "Arshiya Kiani", "email": "zytel3301@gmail.com"}
    19  
    20  	// Every rule will be applied to the item of the data that the offset of rule is pointing to.
    21  	// So if you have a field "email": "omitempty,required,email", the validator will apply these
    22  	// rules to offset of email in user data
    23  	rules := map[string]interface{}{"name": "required,min=8,max=32", "email": "omitempty,required,email"}
    24  
    25  	// ValidateMap will return map[string]error.
    26  	// The offset of every item in errs is the name of invalid field and the value
    27  	// is the message of error. If there was no error, ValidateMap method will
    28  	// return an EMPTY map of errors, not nil. If you want to check that
    29  	// if there was an error or not, you must check the length of the return value
    30  	errs := validate.ValidateMap(user, rules)
    31  
    32  	if len(errs) > 0 {
    33  		fmt.Println(errs)
    34  		// The user is invalid
    35  	}
    36  
    37  	// The user is valid
    38  }
    39  
    40  func validateNestedMap() {
    41  
    42  	data := map[string]interface{}{
    43  		"name":  "Arshiya Kiani",
    44  		"email": "zytel3301@gmail.com",
    45  		"details": map[string]interface{}{
    46  			"family_members": map[string]interface{}{
    47  				"father_name": "Micheal",
    48  				"mother_name": "Hannah",
    49  			},
    50  			"salary": "1000",
    51  			"phones": []map[string]interface{}{
    52  				{
    53  					"number": "11-111-1111",
    54  					"remark": "home",
    55  				},
    56  				{
    57  					"number": "22-222-2222",
    58  					"remark": "work",
    59  				},
    60  			},
    61  		},
    62  	}
    63  
    64  	// Rules must be set as the structure as the data itself. If you want to dive into the
    65  	// map, just declare its rules as a map
    66  	rules := map[string]interface{}{
    67  		"name":  "min=4,max=32",
    68  		"email": "required,email",
    69  		"details": map[string]interface{}{
    70  			"family_members": map[string]interface{}{
    71  				"father_name": "required,min=4,max=32",
    72  				"mother_name": "required,min=4,max=32",
    73  			},
    74  			"salary": "number",
    75  			"phones": map[string]interface{}{
    76  				"number": "required,min=4,max=32",
    77  				"remark": "required,min=1,max=32",
    78  			},
    79  		},
    80  	}
    81  
    82  	if len(validate.ValidateMap(data, rules)) == 0 {
    83  		// Data is valid
    84  	}
    85  
    86  	// Data is invalid
    87  }
    88  

View as plain text