dry-rb/dry-validation

Support for array items in rule paths

開放

#607 建立於 2019年12月5日

 (9 則留言) (2 個反應) (0 位負責人)Ruby (195 個分叉)github user discovery
featurehelp wanted

倉庫指標

星標
 (1,422 顆星)
PR 合併指標
 (30 天內沒有已合併 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

貢獻者指南