3 ms·
I also wasn't a fan of how Rails handles params, so I ended up writing my own lib. And I'm actually working on open sourcing it and extracting into a gem.
by ezekg 4y ago
I also wasn't a fan of how Rails handles params, so I ended up writing my own lib. And I'm actually working on open sourcing it and extracting into a gem.
typed_params {
param :data, type: :hash do
param :first_name, type: :string, optional: true
param :last_name, type: :string, optional: true
param :email, type: :string, format: { with: /@/ }
param :password, type: :string, length: { minimum: 8 }
with if: -> { current_bearer&.admin? } do
param :metadata, type: :hash, allow_blank: true, optional: true
param :role, type: :string, inclusion: { in: %w[admin user] }, optional: true,
transform: -> (_, role) { [:role_attributes, { role: }] }
param :permissions, type: :array, optional: true, if: -> { current_account.ent? } do
items type: :string
end
end
end
}
def create
# ... controller code here
end
Each param schema has to be defined by source, e.g. request body vs query string. And params aren't intermixed (including routing params).
- revskill 4y agoWe want a generic interface: Think Rack for params parsing/serializing, then you could easily parse any params into your own data structure. Stringly types are out of scope.
- ezekg 4y agoWhy do you need something so generic? Can you share a use case?
- fknorangesite 4y agoI'm very ambivalent about Grape, but this very much reminds me of something I do really like about it: the param validation. https://github.com/ruby-grape/grape#parameters https://github.com/ruby-grape/grape#parameters
- ezekg 4y agoYep! I was very inspired by Grape’s param system.