Hello. I have a validation that relies on some external data. Imagine we’re validating the creation of a meeting room booking, where every user has a maximum duration they are allowed.
I want to check the provided user_id belongs to someone who exists, and then that the duration is below their configured maximum. For example:
Dry::Validation.Form do required(:user_id).filled required(:duration).filled(:int?) validate(valid_user: %[user_id]) do |user_id| !User.find(user_id).nil? end validate(valid_duration: %[user_id duration]) do |user_id, duration| user = User.find(user_id) duration < user.max_duration end end
Is there a good way to avoid calling
User.find twice? This is actually a call to an HTTP API, so it would be great if I could reduce this to just one call. Thanks!