From 768edb2c0645208cb4549444ad0239779805b198 Mon Sep 17 00:00:00 2001 From: Piotr Solnica Date: Sat, 4 May 2019 19:12:59 +0200 Subject: [PATCH] Use locales for acceptance macro message --- config/errors.yml | 4 ++++ lib/dry/validation/contract.rb | 1 + lib/dry/validation/macros.rb | 4 +++- 3 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 config/errors.yml diff --git a/config/errors.yml b/config/errors.yml new file mode 100644 index 0000000..1545d7b --- /dev/null +++ b/config/errors.yml @@ -0,0 +1,4 @@ +en: + dry_validation: + errors: + acceptance: "must accept %{key}" diff --git a/lib/dry/validation/contract.rb b/lib/dry/validation/contract.rb index 3ebc499..37473f5 100644 --- a/lib/dry/validation/contract.rb +++ b/lib/dry/validation/contract.rb @@ -55,6 +55,7 @@ module Dry extend ClassInterface config.messages.top_namespace = 'dry_validation' + config.messages.load_paths << Pathname(__FILE__).join('../../../../config/errors.yml').realpath # @!attribute [r] config # @return [Config] diff --git a/lib/dry/validation/macros.rb b/lib/dry/validation/macros.rb index a15c391..2d3fd7a 100644 --- a/lib/dry/validation/macros.rb +++ b/lib/dry/validation/macros.rb @@ -9,8 +9,10 @@ module Dry module RuleMethods # @api public def acceptance + key_name = keys[0] + @block = proc do - key.failure('must accept terms') unless values[keys[0]].equal?(true) + key.failure(:acceptance, key: key_name) unless values[key_name].equal?(true) end end end