dry-rb/dry-validation

Support for array items in rule paths

Offen

#607 geöffnet am 05.12.2019

 (9 Kommentare) (2 Reaktionen) (0 zugewiesene Personen)Ruby (196 Forks)github user discovery
featurehelp wanted

Repository-Metriken

Stars
 (1.422 Sterne)
PR-Merge-Metriken
 (Keine gemergten PRs in 30 T)

Beschreibung

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[].email").each do
    key.failure("email not valid") unless value.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

Contributor Guide