Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add NotBlank validation function. #421

Merged
merged 4 commits into from
Mar 31, 2019
Merged

Conversation

andreiavrammsd
Copy link
Contributor

Change Details:

  • There are cases when a value needs to not be empty (like a string containing only spaces, slice not nil but with no elements) and I added a new tag to easily overcome this need.
type TestString struct {
	NotBlank  string `validate:"notblank"`
}

@go-playground/admins

@coveralls
Copy link

coveralls commented Nov 17, 2018

Coverage Status

Coverage remained the same at 100.0% when pulling f2ac4ef on andreiavrammsd:notblank into 774d09c on go-playground:v9.

@deankarn
Copy link
Contributor

Hey @andreiavrammsd sorry for the delay I responded a few days ago but I don't see it here, was having internet issues that day...anyways

I'm curious how this is different from combining required and gt like so required,gt=0?
if it's just that you want the nonblank tag as the error an alias can be registered for this.

@andreiavrammsd
Copy link
Contributor Author

Hello,

The value "" (empty string) is caught by required,gt=0, but " " (string made only of spaces) is not. In many situations, I need to make sure there is actually a value inside a string, not only spaces.
I'm using a custom registered tag now, but I see the situation as quite common.

Thanks!

@deankarn
Copy link
Contributor

Ah I see, I missed the TrimSpace in your validation; I'm not against this but also wanted to suggest an alternate approach; I too have seen that things like this are pretty common and so created another package to help sanitize the data prior to validation.

mold which will allow you to trim and sanitize data in a number of ways, even trimming values of maps.

My only concern about adding this to the validation lib is that I can see a great number of additional cases eg. streaming map string values or map of map string values... and don;t think it would make sense to have all of them in the std validation package.

What do you think about using the modifiers package of the mold lib prior to validation?

@andreiavrammsd
Copy link
Contributor Author

Regarding trimming, is it worth using a library for a simple built-in function?
For maps and slices, you could use "dive" in order to run the validation on everything.

@andreiavrammsd
Copy link
Contributor Author

andreiavrammsd commented Nov 22, 2018

If it feels too heavy for the standard library, no problem, we can close it.
It's just something I see as a standard for validations.

@deankarn
Copy link
Contributor

Sorry for the delay, super busy, my only concern is that the validation actually does more that one thing that corresponds https://github.com/go-playground/validator/pull/421/files#diff-e8dcd0f6e62fb5a9257bc99c0891f3f8R1197

eg. both len and required are being mixed together in this validation; the way I look at baked-in validations is that they are of singular purpose.

but I definitely see the value in this and other non-standard validations as well so I have a proposal, if we could create some sub-folders for this package non-standard/validators and non-standard/aliases

Then we can add these sorts of validators here and people can easily register them if they desire, but won't be registered by default.

What do you think?

@andreiavrammsd
Copy link
Contributor Author

This could be something. How would you see the non-standard validators registration: (1) including tags or (2) leaving the user the choose a custom tag?

  1. New register function: this would automatically register the validate function with the tag "notblank", so you'd use it as:
type User struct {
    Username `validate:"notblank"`
}
v := validator.New()
v.RegisterNonStd(validators.NotBlank) // please ignore the "RegisterNonStd" name, I'm just out of ideas for now
  1. Allow user to use the function with any tag:
type User struct {
    Username `validate:"customtag"`
}
v := validator.New()
v.RegisterValidation("customtag", validators.NotBlank)

@deankarn
Copy link
Contributor

deankarn commented Dec 8, 2018

@andreiavrammsd I like option #2 but I'd still want them in another package as described because there are still aliases and translations that can go along with the validation and I'm worried about cluttering up the validator namespace.

@andreiavrammsd
Copy link
Contributor Author

Is it starting to look as you thought?

@deankarn
Copy link
Contributor

deankarn commented Jan 9, 2019

@andreiavrammsd apologies for the delay, trying to catch up after the holidays, I have not forgotten

doc.go Show resolved Hide resolved
@deankarn
Copy link
Contributor

@andreiavrammsd looking great! if you could add a note about non-standard then I can merge :)

@deankarn
Copy link
Contributor

Awesome thanks!

@deankarn deankarn merged commit ce0e3d1 into go-playground:v9 Mar 31, 2019
@andreiavrammsd andreiavrammsd deleted the notblank branch March 31, 2019 17:32
fairyhunter13 added a commit to fairyhunter13/validator that referenced this pull request Jul 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants