Validation arguments

What do you think about the following way to specify validations?

validates name: {presence: true}, email: {uniqueness: true, presence: true}

or even

validates name: [:presence], email: [:uniqueness, :presence]

Right now this requires 2 calls to validates

validates :user, presence: true
validates :email, uniqueness: true, presence: true

I think they are more confusing that the two calls


The second version doesn’t allow to pass arguments to the validation type.

You can use this syntax :

with_options presence: true do |opts|

opts.validates :name

opts.validates :email, uniqueness: true


Best regards.

@Rafael Imo, this syntax resembles how arguments for ActiveRecord.joins are specified.

@Geoffrey I just didn’t provide an example, but it does allow:

validates name: [:presence, format: {with: /\s+/}]

I don’t like that join syntax either. :wink:

This would only be good
for simple validation requirements. If there were an :if or :unless parameter on a given validation,
for example, that would look kind of ugly in the one line


Sure. It’s possible to make it ugly with the existing syntax as well :slight_smile: This change would just add one more option, not disable all the existing ones.