-
-
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
Support for array items in rule paths #607
Comments
@solnic shouldn't the rule be without rule("contacts[].email") do
key.failure("email not valid") unless value.include?('@')
end
rule("contacts[].emails").each do
key.failure("email not valid") unless value.include?('@')
end |
@skryukov I think it should be with |
@solnic I see your point, but how we will differentiate, should we iterate over |
@skryukov wait, I think I’m missing something - differentiate what? |
@solnic I'll try to explain =) For example, we have a schema like this: require 'dry-validation'
class PropsContract < Dry::Validation::Contract
json do
required(:contacts).value(:array, min_size?: 1).each do
hash do
required(:name).filled(:string)
required(:email).filled(:string)
required(:phone).filled(:string)
required(:additional_emails).each(:string)
end
end
end
rule("contacts[].email").each do
key.failure("email not valid") unless value.include?('@')
end
rule("contacts[].additional_emails").each do
key.failure("email not valid") unless value.include?('@')
end
end
c = PropsContract.new
c.(
contacts: [
{ name: 'Jane', email: '[email protected]', phone: '123', additional_emails: ['oops'] },
{ name: 'John', email: 'oops', phone: '123', additional_emails: ['[email protected]'] }
]).errors.to_h.inspect
# => {:contacts =>{0=>{:additional_emails =>{0=>["email not valid"]}}},1=>{:email=>["email not valid"]}} So how should we know when a rule needs to be applied to the item itself ( rule("contacts[].additional_emails").each(index:) do # or maybe even indexes in this case? =)
key.failure("additional emails contain primary email") if value.include?(values[:contacts][index][:email])
end |
One more thing, |
I don't know. Please feel free to figure it out however you think is best 😄 |
Hello, I think my question suits this issue as well I have a macro:
and I have to check in the same file both a single case such as the following:
and for a separate case also array of numeric strings:
the last one does not work is there a right way how to do that? Many thanks in advance |
@TimoMoss for now you need to handle it manually, well implement a nice DSL for it at some point. For now I'm adding this to 1.7.0 milestone. |
Examples
Resources
Refs #603
The text was updated successfully, but these errors were encountered: