Validator: question: default value

Created on 28 Jun 2017  路  5Comments  路  Source: go-playground/validator

Package version eg. v8, v9:

v9

Issue, Question or Enhancement:

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

Code sample, to showcase or reproduce:

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"`
}
question

All 5 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Jake-Convictional picture Jake-Convictional  路  4Comments

anam-digicom picture anam-digicom  路  4Comments

romangithub1024 picture romangithub1024  路  5Comments

0505gonzalez picture 0505gonzalez  路  3Comments

donileo picture donileo  路  3Comments