Open tiagomelo opened 3 years ago
Hey @tiagomelo, good question.
So the first required
is applied on the array itself []*Address
. So ensuring there is at least one value in the array.
The second required
is applied on each individual element within the array eg. *Address
The reason the example is like this is to show that you could have an array with one entry that is nil
because *Address
is a pointer.
Does that explain it?
Am I calling it right?
the Addresses fields like street, planet, phone and city have already their value now, but the "dive" is not diving in that json array?
then... when I remove the said values in the addresses.. ok, it tells us all inside that json array are required,
but when, I put values in planet and city, still the "dive" is not diving to check which fields have already the value or not:
what's the problem here?
we have the same issue here https://stackoverflow.com/questions/62522747/golang-validator-with-custom-structs and I am landed here finding the solution...
Hey @empaguia84 it looks like your JSON does not match your Go struct definitions.
The Go struct is expecting an Array of Addresses but your sending an JSON Object instead and so unmarshaling into those structs isn't happing.
You'll need to adjust your payload to something like:
{
...
"Addresses": [{"street":"","planet":"",...}]
}
@deankarn The dive validator does not work correctly. Here is a simple example of a required,dive,required
Created a struct of nested pointers to structs. Defined it as empty. The first required is not nil. Then it should dive into it and because there are no pointers in that slice it should fail on the second required. However, it passes with no errors. Thats because dive is not triggered for empty.
package main
import (
"fmt"
"github.com/go-playground/validator/v10"
)
type NestedStruct struct{}
type MyStruct struct {
nested []*NestedStruct `validate:"required,dive,required"`
}
func main() {
s := MyStruct{
nested: []*NestedStruct{},
}
validate := validator.New()
err := validate.Struct(s)
fmt.Println(err) // should be error about missing nested items in the slice of pointers, but its nil
}
Go playground code: https://go.dev/play/p/Axsg3EA3g-F
The only way I accomplished the desired logic was to validate the slice length before diving into it
nested []*NestedStruct `validate:"required,min=1,dive,required"`
This is functioning correctly @Oscar-nesto .
The first required, and any other tag before the dive, is applied at the slice level. After the dive the required or any other tag is validating against each element of the slice.
And so because there are no items in the slice, those validations have nothing to be applied to.
If your trying to ensure the slice is present and has at least one entry then add the gt=0 before the dive and after the required.
Package version eg. v9, v10:
v10
Issue, Question or Enhancement:
Following the example provided,
Why do I need to specify
required,dive,required
inAddresses
? Not sure about the need of the secondrequired
, if I remove it the result will be the same if I miss any of the required fields inAddress
. What am I missing?Code sample, to showcase or reproduce: