-
-
Notifications
You must be signed in to change notification settings - Fork 190
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Calling a macro on a deeply nested rule #578
Comments
I just hit the same kind of issue, where my params look like: {
"first_name": "Francois",
"last_name": "Beausoleil",
"email_locators": [
{
"name": "professional",
"email": "[email protected]"
}
]
} I have a macro named class ApplicationContract < Dry::Validation::Contract
config.messages.backend = :i18n
register_macro(:email_format) do
if value.present? && !/\A[-\w+.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i.match?(value)
key.failure(:email_format)
end
end
end
class ManuallyRegisterPersonContract < ApplicationContract
params do
required(:first_name).maybe(Types::StrippedString, min_size?: 1)
required(:last_name).maybe(Types::StrippedString, min_size?: 1)
required(:date_of_birth).maybe(:date)
required(:email_locators).maybe(:array).each do
hash do
required(:name).filled(Types::StrippedString, min_size?: 1)
required(:email).filled(Types::StrippedString, min_size?: 1)
end
end
optional(:metadata).hash(MetadataContract)
end
rule(email_locators: [:email]).validate(:email_format)
end
RSpec.describe ManuallyRegisterPersonContract do
describe "a contract with one email locator that has an invalid email address format" do
fit "fails validation" do
params = {
first_name: nil,
last_name: nil,
date_of_birth: nil,
email_locators: [
{
name: "personal",
email: "baden@"
}
]
}
result = subject.call(params)
expect(result).to be_failure
end
end
end
I fixed my immediate problem by using the solution in Calling a macro on a depply nested rule. It would be really nice if dry-validation handled it for us. |
So, for that, we want to come up with an explicit key path syntax that points to arrays. It's partially implemented in some places in dry-schema but we really need to properly encapsulate it in |
I'd like to be able to specify a macro in the following manner:
rule(foo: :bar).each
where bothfoo
andbar
are arrays.Examples
As an example please have a look at this spec that's currently failing: master...DawidJanczak:macro-on-deeply-nested-rule
Resources
Discussed with @solnic here: https://discourse.dry-rb.org/t/calling-a-macro-on-a-depply-nested-rule/842
The text was updated successfully, but these errors were encountered: