Validator: Allow empty fields for optionals

Created on 26 Dec 2018  路  1Comment  路  Source: go-playground/validator

Package version eg. v8, v9:

I'm using the v9 version

Issue, Question or Enhancement:

I have a struct in which I have a string field, which is optional: I allow it to be empty, but if it isn't empty, I want to enforce the URL format.

I simply did a validate:"url" without putting the required tag, thinking that it'd allow empty data, but it doesn't.

How can I handle that ?

Code sample, to showcase or reproduce:

type Contributor struct {
    LastName        string `validate:"required,min=3"`
    FirstName       string `validate:"required,min=3"`
    Email           string `validate:"required,email"`
    PersonalWebsite string `validate:"url"` // If it's not required, it'll still crash as it isn't URL format.
    PhoneNumber     string
    Role            string `validate:"" gorm:"size:50"`
}
question

Most helpful comment

Hey @IvanDelsinne as the documentation states I think you're looking for omitempty

>All comments

Hey @IvanDelsinne as the documentation states I think you're looking for omitempty

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dkostenko picture dkostenko  路  5Comments

ivybridge-3c33 picture ivybridge-3c33  路  3Comments

gwan284 picture gwan284  路  4Comments

scisci picture scisci  路  5Comments

anam-digicom picture anam-digicom  路  4Comments