Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Validating nested params #53

Closed
mattbeedle opened this issue Nov 21, 2014 · 5 comments
Closed

Validating nested params #53

mattbeedle opened this issue Nov 21, 2014 · 5 comments
Assignees
Milestone

Comments

@mattbeedle
Copy link

Hey,

It doesn't seem to be possible to validate nested params at the moment. I was hoping this syntax would work:

params do
  param :job do
    param :title, presence: true
  end
end
@jodosha
Copy link
Member

jodosha commented Nov 22, 2014

@mattbeedle Thanks for mentioning this idea. Our goal is to release a new version of Lotus in a month, so the deadline is really close and we can't have a look to this in the near future.

Have you tried to flatten the params to one level and let the lower components to separate those attributes?

@jodosha
Copy link
Member

jodosha commented Dec 8, 2014

@mattbeedle @joneslee85 Most of the times we have single level of attributes, but grouped per entity.

Given the following form:

<form action="/books" method="POST">
  <input type="text" name="book[title]" />
</form>

The raw Hash of params that comes from the Rack env is { "book" => { "title" => "Refactoring" } }.
It doesn't work well with the actual implementation of Action.params which only understands top level params.

Instead of having nested blocks, which adds complexity both in Action and in Validations, what if we introduce an optional param name like this:

params :book do
  param :title # ...
end

If present it will extract and consider only that part of the Rack input.
What do you think?

@jodosha jodosha modified the milestone: v0.3.0 Dec 19, 2014
@stevehodgkiss
Copy link
Contributor

It is possible to use Virtus for nested attributes. You'd probably need to use another validation library in the Job class though.

class Job
  include Virtus.model(strict: true)

  attribute :title, String, required: false
end

# action
params do
  param :job, type: Job
end

@jodosha I can imagine a lot of scenarios where you'd still need to access other top level parameters... for example a route parameter /jobs/:id.

@jodosha
Copy link
Member

jodosha commented Dec 26, 2014

@stevehodgkiss We use Lotus::Validations for the purpose. I would love to include nested capability there.

@jodosha
Copy link
Member

jodosha commented Jan 28, 2015

Resolved by #71

@jodosha jodosha closed this as completed Jan 28, 2015
@jodosha jodosha added this to the v0.3.2 milestone Jan 28, 2015
@jodosha jodosha self-assigned this Jan 28, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants