Skip to content

Instantly share code, notes, and snippets.

@cherring
Last active September 25, 2017 05:58
Show Gist options
  • Save cherring/9a27643946b6d31d1624567577fdacc6 to your computer and use it in GitHub Desktop.
Save cherring/9a27643946b6d31d1624567577fdacc6 to your computer and use it in GitHub Desktop.
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
@timriley
Copy link

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 the number_plates hashes? Then you could add required(:centre).filled inside that sub-schema and use the centre 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 :)

@cherring
Copy link
Author

Ah ok. I am a bit relieved that I was not missing something super obvious, thanks @timriley :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment