Skip to content

Added non_empty parameter validator to validate non-empty strings#743

Closed
elado wants to merge 9 commits into
ruby-grape:masterfrom
elado:master
Closed

Added non_empty parameter validator to validate non-empty strings#743
elado wants to merge 9 commits into
ruby-grape:masterfrom
elado:master

Conversation

@elado

@elado elado commented Aug 27, 2014

Copy link
Copy Markdown
Contributor

The PresenceValidator validates only that the param is sent, but not if it's empty. Using Regexp validator seems too cumbersome for such a common task.

An additional NonEmpty validator is very useful.

Also, I allowed myself to reorganize README validation section. I hope it's fine.

@dblock

dblock commented Aug 27, 2014

Copy link
Copy Markdown
Member

I am really not a fan of the non_empty name. What do you think of allow_nil: false for example?

@dblock

dblock commented Aug 27, 2014

Copy link
Copy Markdown
Member

The code looks good otherwise, could be squashed. Lets talk about how to call this option, I prefer "positive" names rather than negative names.

@elado

elado commented Aug 27, 2014

Copy link
Copy Markdown
Contributor Author

I agree, non_empty sounds weird, I actually started it with empty but empty: false looks weirder. allow_nil: false sounds better. If I change the name, would this PR be merged? Or should I squash also?

Thanks

@dblock

dblock commented Aug 27, 2014

Copy link
Copy Markdown
Member

You should squash too, please, we like single nice looking commits. LMK if you need help with that.

@elado

elado commented Aug 27, 2014

Copy link
Copy Markdown
Contributor Author

Actually, allow_nil is confusing. It's usually used when you want to treat nils specifically, but here, it depends on requires vs optional. Also, since it's an HTTP request, nils aren't real values. Either a key was sent with a string (that could be empty) as value, or it wasn't sent at all.

Thoughts? Is empty: false better?

@dm1try

dm1try commented Aug 28, 2014

Copy link
Copy Markdown
Member

allow_blank?:grimacing:

Anyway, there is a little problem with optional group and non_empty validator:

    params do
      optional :user, type: Hash do
        requires :name, non_empty: true
      end
    end
    get '/allow_empty_in_optional_group'

    it '...' do
      get '/allow_empty_in_optional_group'
      expect(last_response.status).to eq(200) # fails
    end

See similar #635, #688, #723

I think, this behaviour should be documented before someone provides a common solution.

@elado

elado commented Aug 28, 2014

Copy link
Copy Markdown
Contributor Author

OK. Renamed non_empty to allow_blank. Also took care of nested scopes. The condition that generates should_validate is quite complex, maybe should be extracted out of there, but it's ok for now.

Tried to use @scope.should_validate?(attr_name) but it failed with some cases.

Let me know if it's good now, I'll squash.

@dblock

dblock commented Aug 29, 2014

Copy link
Copy Markdown
Member

Perfect. Merged squashed via dfbeb22.

@dblock dblock closed this Aug 29, 2014
@dm1try

dm1try commented Aug 29, 2014

Copy link
Copy Markdown
Member

@elado , thanks!

@elado

elado commented Aug 29, 2014

Copy link
Copy Markdown
Contributor Author

Thank you guys!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants