asaskevich / govalidator

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

Wrong error msg when required field is not set #220

Open sclu1034 opened 7 years ago

sclu1034 commented 7 years ago

When govalidator.SetFieldsRequiredByDefault is enabled, the error message for an unset field is misleading. It reports "validation missing" for any validation except valid:"-":

All fields are required to at least have one validation defined;

Full Test Case

package main

import (
    "log"

    "github.com/asaskevich/govalidator"
)

type S struct {
    Url string `valid:"url"`
}

func main() {
    govalidator.SetFieldsRequiredByDefault(true)
    s := &S{}

    _, err := govalidator.ValidateStruct(s)
    if err != nil {
        log.Print(err)
    }
}
jasonlam604 commented 7 years ago

I think this is expected behavior, unless I'm not understanding the documentation correctly. See the main README. Search for Activate behavior to require all fields have a validation tag by default. It explains what you are seeing and yes "-" would mark a specific field except from the check.

sergeyglazyrindev commented 3 years ago

Hello guys! I forked this package cause owner disappeared. Hope, he will be back, but it would be easier to merge these changes back if he is back Link to my repo: create issue there and we'll discuss it.