nicolasblanco / rails_param

Parameter Validation & Type Coercion for Rails
MIT License
942 stars 89 forks source link

Idea : migrate all validations to ActiveModel #2

Open nicolasblanco opened 10 years ago

nicolasblanco commented 10 years ago

Instead of using its own rules, rails_param could use a ActiveModel class to validate the params. This would be a major rewrite of the library. The benefit is that the developer will be able to use all the known validations he already uses, like:

param! :phone_number, String, presence: true, length: { minimum: 3 }

Typecasting and transformation will still be implemented internally (I could use some other gems like Virtus but for the moment I don't want dependencies).

It will also be possible for the developper to access the errors Hash.

willian commented 9 years ago

I liked this one and I'm willing to help you if you need.

ilyazub commented 2 years ago

A bit related — it can be done like this with Mongoid:

Search.fields.map { |name, field| "param! :#{field.name}, #{field.type}" }

Output

["param! :_id, BSON::ObjectId",
 "param! :created_at, Time",
 "param! :updated_at, Time",
 "param! :engine, String",
 "param! :q, String",
 "param! :output, String",
 "param! :location_requested, String",
 "param! :lat, String",
 "param! :lon, String",
 "param! :device, String",
 "param! :source, String",
 "param! :requester_ip, String",
 "param! :api_request, String",
 "param! :md5, String",
 "param! :proxy_provider, String",
 "param! :google_domain, String",

(It's a string array to show the code would be executed.)