like the struct below, I want to validate: if User.Address is not empty, then validate the nested struct fileds (City and Street), if User.Address is empty, omit.
Any idea? or is this supported ?
type User struct {
Name string
Address Address
}
type Address struct {
City string `validate:"required"`
Street string `validate:"required"`
}
@joeybloggs any comment is appreciated.
Hello @wenchma
there are a few options, first off just need to point out that because Go is statically typed, the way Address is defined within the User struct it will never be empty.
Option 1:
change address definition in user to Address *Address and then can add the omitempty tag.
Option 2
change address definition to allow for multiple addresses `Address []Address's and when no items exist in the array it won't get validated.
Option 3
this one is probably not desired but could also set omitempty tag on each field withing Address and allow a blank address.
Sorry for the lack of code, answering from my phone, please let me know if this makes sense or need any more help.
@joeybloggs Awesome, :+1: your options are very helpful for me, I think them will work, let me try.
Much thanks!
@joeybloggs Awesome, 馃憤 your options are very helpful for me, I think them will work, let me try.
Much thanks!
How do I validate if one of the nested struct field (city and street) is empty, the whole Address field will be omitted ? @deankarn
@roychowdhuryrohit-dev if I'm not sure I understand your question, can you provide more details?
@roychowdhuryrohit-dev if I'm not sure I understand your question, can you provide more details?
I am using Gin which uses validator V8. Suppose in the above example
type User struct {
Name string
Address Address
}
type Address struct {
City string `validate:"required"`
Street string `validate:"required"`
}
I want a behaviour in which if any one of the field (City, Street) is empty(""), the Address field in User will be omitted/empty. @deankarn
Thanks @roychowdhuryrohit-dev but still a little confused.
I want a behaviour in which if any one of the field (City, Street) is empty(""), the Address field in User will be omitted/empty.
If this is the case then why validate the fields at all? in what case would you want/need validation?
Thanks @roychowdhuryrohit-dev but still a little confused.
I want a behaviour in which if any one of the field (City, Street) is empty(""), the Address field in User will be omitted/empty.
If this is the case then why validate the fields at all? in what case would you want/need validation?
I am storing the Address field as jsonb field in Postgres. I am looking for a way by which incomplete/missing data doesn't get stored as Gorm doesn't handle this for jsonb types. @deankarn
Most helpful comment
Hello @wenchma
there are a few options, first off just need to point out that because Go is statically typed, the way Address is defined within the User struct it will never be empty.
Option 1:
change address definition in user to
Address *Addressand then can add theomitemptytag.Option 2
change address definition to allow for multiple addresses `Address []Address's and when no items exist in the array it won't get validated.
Option 3
this one is probably not desired but could also set
omitemptytag on each field withing Address and allow a blank address.Sorry for the lack of code, answering from my phone, please let me know if this makes sense or need any more help.