Giter Site home page Giter Site logo

Validating nested params about controller HOT 5 CLOSED

hanami avatar hanami commented on May 18, 2024
Validating nested params

from controller.

Comments (5)

jodosha avatar jodosha commented on May 18, 2024

@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?

from controller.

jodosha avatar jodosha commented on May 18, 2024

@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?

from controller.

stevehodgkiss avatar stevehodgkiss commented on May 18, 2024

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.

from controller.

jodosha avatar jodosha commented on May 18, 2024

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

from controller.

jodosha avatar jodosha commented on May 18, 2024

Resolved by #71

from controller.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.