Skip to content
New issue

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

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

Already on GitHub? # to your account

Validators should return their success or failure #624

Closed
BrucePerens opened this issue Jan 29, 2021 · 3 comments · Fixed by #744
Closed

Validators should return their success or failure #624

BrucePerens opened this issue Jan 29, 2021 · 3 comments · Fixed by #744

Comments

@BrucePerens
Copy link

BrucePerens commented Jan 29, 2021

Validators currently don't return a defined value. They should return true for valid and false for invalid. That way, we could run validators conditionally depending on the success of others. For example:

if validate_required(name)
  validate_size_of(name, 3, 12)
end

It would then also be possible to change validate_size_of, etc. to raise an exception if they are called upon a nil value, rather than silently ignoring it, which just seems wrong for validators.

@jwoertink
Copy link
Member

I like that idea, though, not too sure about raising an exception. The actual error handling shouldn't be done in operations. That should be handled outside which is most likely the Action so that way the ErrorHandler picks it all up.

The only main thing to consider is if you have an attribute that is allowed to be nil, but you pass it to validate_size_of, what would that return? If it returns false, you might think it failed. Maybe it returns nil if it's ignored? 🤔

@BrucePerens
Copy link
Author

What I want to do with the UI is something like this:

fail = if validate_required(password)
  if validate_length_of(password, 7, 24)
    ...
  end
end
if fail
  password.add_error <<-EOT
  A password should be between 7 and 24 characters in size,
  should contain at least one of each of a letter, a number,
  and a character that is neither a letter nor a number.
  EOT
end

@BrucePerens
Copy link
Author

BrucePerens commented Jan 29, 2021

I am dubious about validators ignoring conditions that aren't true. Consider that validators with ! raise on nil:

if name.value
  validate_size_of!(name, 3, 12)
end

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants