Created
June 8, 2021 19:29
-
-
Save moofkit/c1d7c564dc1d12c9758c118585324590 to your computer and use it in GitHub Desktop.
Error message path for rule with nested params reproducing bug
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
en: | |
dry_validation: | |
errors: | |
rules: | |
data: | |
attributes: | |
phone_number: | |
invalid_format: 'is of invalid format' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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