Skip to content
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

Expose index within "each" rules #606

Closed
solnic opened this issue Dec 5, 2019 · 3 comments
Closed

Expose index within "each" rules #606

solnic opened this issue Dec 5, 2019 · 3 comments
Labels

Comments

@solnic
Copy link
Member

solnic commented Dec 5, 2019

Examples

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)
      end
    end
  end

  rule(:contacts).each do |index:|
      key([:email, index]).failure("email not valid")  unless value[:email].include?('@')
  end
end

c = PropsContract.new

c.(
  contacts: [
    { name: 'Jane', email: 'jane@doe.org', phone: '123' },
    { name: 'John', email: 'oops', phone: '123' }
  ]).errors.to_h.inspect
# {:email=>{1=>["email not valid"]}}

Resources

Refs #603

@solnic solnic added the feature label Dec 5, 2019
@solnic solnic added this to the 1.4.0 milestone Dec 5, 2019
@solnic solnic modified the milestones: 1.4.0, 1.5.0 Dec 13, 2019
@solnic solnic removed this from the 1.5.0 milestone Mar 11, 2020
@mrbongiolo
Copy link
Contributor

mrbongiolo commented Jul 14, 2020

@solnic could I try solving this one? Currently in the process of upgrading our application from 0.x to 1.5.x and this would help in some cases.
Right now I have to create rules like that:

  rule(:input).each do
    key([:input, values[:input].index(value), :name]).failure(:not_exists, value: value[:name]) unless value[:name].in?(benchmark_rate_names)
  end

@solnic
Copy link
Member Author

solnic commented Jul 15, 2020

@mrbongiolo sure, you're more than welcome to help with this feature :)

@morgoth
Copy link

morgoth commented Mar 15, 2022

looks like this can be closed, as it's implemented and released

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants