Array validations

45 views
Skip to first unread message

Frank

unread,
May 7, 2013, 6:35:53 PM5/7/13
to ruby-...@googlegroups.com
Hey!

Started up with Grape today and am looking to enable a user to POST a request for an order which contains a variable number of line items. Was wondering your suggestions on the cleanest way to handle this, as I'm not sure exactly how to handle the validations.

Was thinking something along the lines of:

params do
   group :line_item do
          requires :item_id
          requires :quantity
   end
end

Where the user could include multiple line items, but will Grape check each of them for the required fields?

Daniel Doubrovkine

unread,
May 8, 2013, 10:38:33 AM5/8/13
to ruby-...@googlegroups.com
You should try it. If it doesn't work, I think we should support validation across an array of items. You might also want to just use model-level validations and wrap the creating of line items in a transaction, so if the N-th one fails because, say, quantity is missing, you get a standard validation exception, everything is rolled back, etc.

Finally, personally I would think twice before implementing something like this. I would create a single item POST and make 10 calls if I have to. These days having made all possible mistakes in API design one could have possibly make, I would model any new API on some good examples. What does Github do in similar scenarios?



--
You received this message because you are subscribed to the Google Groups "Grape Framework Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ruby-grape+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--

dB. | Moscow - Geneva - Seattle - New York
dblock.org - @dblockdotorg

Reply all
Reply to author
Forward
0 new messages