asaskevich / govalidator

[Go] Package of validators and sanitizers for strings, numerics, slices and structs
MIT License
6.04k stars 555 forks source link

README example error #379

Closed MonsterRob closed 4 years ago

MonsterRob commented 4 years ago

Your example:

var mapTemplate = map[string]interface{}{
        "name":       "required,alpha",
        "family":     "required,alpha",
        "email":      "required,email",
        "cell-phone": "numeric",
        "address": map[string]interface{}{
            "line1":       "required,alphanum",
            "line2":       "alphanum",
            "postal-code": "numeric",
        },
    }

    var inputMap = map[string]interface{}{
        "name":   "Bob",
        "family": "Smith",
        "email":  "foo@bar.baz",
        "address": map[string]interface{}{
            "line1":       "",
            "line2":       "",
            "postal-code": "",
        },
    }
result, err := govalidator.ValidateMap(mapTemplate, inputMap)
    if err != nil {
        println("error: " + err.Error())
    }
    println(result)

The output is :

error: name: The following validator is invalid or can't be applied to the field: "Bob";family: The following validator is invalid or can't be applied to the field: "Smith";email: The following validator is invalid or can't be applied to the field: "foo@bar.baz";all map keys has to be present in the validation map; got cell-phone
false

It should be validated like this:

result, err := govalidator.ValidateMap(inputMap, mapTemplate)
    if err != nil {
        println("error: " + err.Error())
    }
    println(result)

Output:

true