spec/integration/schema/check_rules_spec.rb in dry-validation-0.7.1 vs spec/integration/schema/check_rules_spec.rb in dry-validation-0.7.2

- old
+ new

@@ -77,6 +77,43 @@ expect(schema.(login: true, email: nil).messages).to eql( email_presence: ['must be present when login is set to true'] ) end end + + describe 'with nested schemas' do + subject(:schema) do + Dry::Validation.Schema do + key(:command).required(:str?, inclusion?: %w(First Second)) + + key(:args).required(:hash?) + + rule(first_args: [:command, :args]) do |command, args| + command.eql?('First') + .then(args.schema { key(:first).required(:bool?) }) + end + + rule(second_args: [:command, :args]) do |command, args| + command.eql?('Second') + .then(args.schema { key(:second).required(:bool?) }) + end + end + end + + it 'generates check rule matching on value' do + expect(schema.(command: 'First', args: { first: true })).to be_success + expect(schema.(command: 'Second', args: { second: true })).to be_success + + expect(schema.(command: 'oops', args: { such: 'validation' }).messages).to eql( + command: ['must be one of: First, Second'] + ) + + expect(schema.(command: 'First', args: { second: true }).messages).to eql( + args: { first: ['is missing'] } + ) + + expect(schema.(command: 'Second', args: { first: true }).messages).to eql( + args: { second: ['is missing'] } + ) + end + end end