dry-rb/dry-validation

Support for array items in rule paths

オープン

#607 opened on 2019/12/05

 (9 件のコメント) (2 件のリアクション) (0 人の担当者)Ruby (196 件のフォーク)github user discovery
featurehelp wanted

Repository metrics

Stars
 (1,422 個のスター)
PR merge metrics
 (30d に merged PR はありません)

説明

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

コントリビューターガイド