validate
validate copied to clipboard
An explanation of how `required` works.
System (please complete the following information):
- OS:
linux
- GO Version:
1.20
- Pkg Version:
1.4.6
Describe the bug
Not necessarily a bug.
How exactly does required
work?
Suppose we have the following case
type Foo {
Name int `validate:"required|int|between:1,999" label: "name"`
}
foo := &Foo{Name: 4}
v := validate.Struct(&foo)
v.Validate()
I'm getting a validation error name is required to not be empty as if I did not specify the the field Name
. If I leave out the required
validator it works.
So the question is, when exactly to we need to use the required validator?
Because if I don't specify required are we saying that the field is optional? Or does required only work with pointer types?
Can you please explain the use cases and effects of this. I have just discovered that my validation rules were not running before because I made this PR #202 and that is why I'm confused now.
To Reproduce
Expected behavior
I expect validation to pass when required
is specified and when a field has a valid value.
Screenshots
n/a
Additional context
n/a
If not required
, it will only be validated if the value is not empty(nil,zero-value).
So without specifying required
, it can be said that the field is optional.
If not
required
, it will only be validated if the value is not empty(nil,zero-value).So without specifying
required
, it can be said that the field is optional.
Alright, but now I'm seeing that when I remove required from a string field, my custom filter and validator function does not get called and I'm ending up passing invalid data which gets accepted, which should not happen 😔