Last active
September 25, 2017 05:58
-
-
Save cherring/9a27643946b6d31d1624567577fdacc6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module ProfileValidator | |
def self.validate(profile, scentregroup_id) | |
profile_schema.call(profile.merge(scentregroup_id: scentregroup_id)) | |
end | |
def self.profile_schema | |
required(:centre).filled | |
required(:number_plates).each do | |
schema do | |
required(:plate).filled | |
required(:registered_in).filled | |
validate(plate_available: %i[plate]) do |plate| | |
### How can I use the centre attribute in here? | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm not sure you can refer to attributes outside of the sub-schema like you're hoping to.
Perhaps simplest thing is to have a data mapper that you run before calling the schema, which would copy the
centre
attribute into each of thenumber_plates
hashes? Then you could addrequired(:centre).filled
inside that sub-schema and use thecentre
attribute inside your validate block.I admit this is a bit awkward, but it'd get the job done here, at least.
I'd encourage you to file this as an issue on the GitHub repo, though. We're going to put our attention back onto dry-validation at some point before too long, and getting this as an issue here would at least mean we can consider this use case :)