v9
Question:
Is there a way to define a default value for ID, in case if its not passed? If the ID is present it needs to be >= 1
type Settings struct {
Low uint `json:"low" validate:"omitempty,min=0,max=98,ltfield=High"`
High uint `json:"high" validate:"omitempty,min=1,max=99"`
ID uint `json:"id" validate:"required,min=1"`
Namespace string `json:"namespace" validate:"required,min=1"`
}
Hey @geraldstanje no unfortunately not, validator is just not configured to do that, it's been asked about before, it's seems simple at first look, but way more complicated to do. I'd rather keep validator doing one thing really well.
However there is another library that can help, it's actually in the README, https://github.com/leebenson/conform that is designed to do exactly that.
So you'd just call that prior to validation.
Please let me know if that helps :)
@joeybloggs I have recently started using this library through gin. I just wanted to say that the way you handle issues and provide support is nothing short of amazing. Thank you! Don't mind the comment unrelated to the issue.
hi @joeybloggs i wonder if something has changed in the meantime?
also how can i define 2 ranges of allowed values or will i need to put all allowed vlues into validate:"omitempty,oneof=1 2 3 4 5 6 ... 1000 && 10000 10001 ... 20000"
@Arnold1 Would min=1,max=1000 work for you case? If you have gaping ranges, that's a complicated enough issue that you would have to create e custom validator or just don't use it for that specific struct field (and write your own code that checks its validity).
yeah that worked for now.