soveran / scrivener

Validation frontend for models.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Assert Present accepts empty string with spaces

jigyasam opened this issue · comments

assert_present considers " " valid. It should strip the value before testing for empty? I'll submit a PR soon, if I can.

Hey! I understand your point, it looks like it you may need a different assertion, for example assert_format or even something more specific like assert_email, assert_url, etc. I'm not sure assert_present should change its behavior, as it is just following Ruby's rules regarding what is and what is not empty. Otherwise you can use a simple assert like this:

assert !foo.strip.empty?, [:foo, :empty]

Let me know if you are OK using something like assert_format or a bare assert.

I understand what you mean. I was drawing a parallel with ActiveModel Validations:
validates :attr, presence: true which uses the ActiveSupport method blank? and not empty?. I did already define a custom validator to do it, though.

We run into the same issue as well when a user was testing our application. We ended-up monkey patching assert_present.

class Scrivener
  BLANK = /\A[[:space:]]*\z/

  def assert_present(att, error = [att, :not_present])
    assert(!(BLANK === send(att).to_s), error)
  end
end

In my opinion, systems should be designed so that they don't require precise input from users. In most cases, all strings should be trimmed, maybe double spaces converted to a single one, etc. But I think this step should come before the validation layer provided by Scrivener, because you'll want to keep those transformations before passing it to another layer (the database, a service, whatever).

Closing this issue for now. I think eventually we can create a different assertion if the requirement is widespread.