Skip to content

Instantly share code, notes, and snippets.

@moofkit
Created June 8, 2021 19:29
Show Gist options
  • Save moofkit/c1d7c564dc1d12c9758c118585324590 to your computer and use it in GitHub Desktop.
Save moofkit/c1d7c564dc1d12c9758c118585324590 to your computer and use it in GitHub Desktop.
Error message path for rule with nested params reproducing bug
en:
dry_validation:
errors:
rules:
data:
attributes:
phone_number:
invalid_format: 'is of invalid format'
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'rspec'
gem 'i18n', '~> 1.6', require: false
gem 'dry-validation', '~> 1.6', require: false
end
require 'rspec/autorun'
require 'i18n'
require 'dry-validation'
RSpec.describe 'Error message path for rule with nested params ' do
let(:contract) do
Dry::Validation::Contract.build do
config.messages.backend = :i18n
config.messages.load_paths << Pathname(__dir__).join('errors.en.yml').realpath
params do
optional(:data).hash do
required(:attributes).hash do
required(:phone_number).maybe(:string)
end
end
end
rule('data.attributes.phone_number') do
key.failure(:invalid_format) unless value == "123"
end
end
end
it 'works with deep nested rules' do
expect(contract.call(data: {attributes: {phone_number: '321'}}).errors.to_h).to eq(data: {attributes: {phone_number: ['is of invalid format']}})
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment