Sha256: 4fcad2fae07e774a75f2804fe01b921dac777636e0e421d44d8c358b57806add
Contents?: true
Size: 1.66 KB
Versions: 26
Compression:
Stored size: 1.66 KB
Contents
RSpec.describe 'Schema with each and set rules' do subject(:schema) do Dry::Validation.Schema do required(:payments).each do required(:method).filled(:str?) required(:amount).filled(:float?) end end end describe '#messages' do it 'validates using all rules' do expect(schema.(payments: [{}]).messages).to eql( { payments: { 0 => { method: ['is missing'], amount: ['is missing'] } }} ) end it 'validates each payment against its set of rules' do input = { payments: [ { method: 'cc', amount: 1.23 }, { method: 'wire', amount: 4.56 } ] } expect(schema.(input).messages).to eql({}) end it 'validates presence of the method key for each payment' do input = { payments: [ { method: 'cc', amount: 1.23 }, { amount: 4.56 } ] } expect(schema.(input).messages).to eql( payments: { 1 => { method: ['is missing'] } } ) end it 'validates type of the method value for each payment' do input = { payments: [ { method: 'cc', amount: 1.23 }, { method: 12, amount: 4.56 } ] } expect(schema.(input).messages).to eql( payments: { 1 => { method: ['must be a string'] } } ) end it 'validates type of the amount value for each payment' do input = { payments: [ { method: 'cc', amount: 1.23 }, { method: 'wire', amount: '4.56' } ] } expect(schema.(input).messages).to eql( payments: { 1 => { amount: ['must be a float'] } } ) end end end
Version data entries
26 entries across 26 versions & 1 rubygems