From 83d61bacef1c9fceef8f5fc18f586aac2d68824b Mon Sep 17 00:00:00 2001 From: Piotr Solnica Date: Sun, 19 Jun 2016 19:00:40 +0200 Subject: [PATCH] Add deprecation warnings when type spec is missing --- lib/dry/validation/deprecations.rb | 23 ++++++++++++++++++++++ lib/dry/validation/error_compiler/input.rb | 8 ++++++-- lib/dry/validation/schema/dsl.rb | 3 +++ lib/dry/validation/schema/rule.rb | 6 +++++- lib/dry/validation/schema/value.rb | 4 +++- log/.gitkeep | 0 spec/spec_helper.rb | 4 ++++ 7 files changed, 44 insertions(+), 4 deletions(-) create mode 100644 lib/dry/validation/deprecations.rb create mode 100644 log/.gitkeep diff --git a/lib/dry/validation/deprecations.rb b/lib/dry/validation/deprecations.rb new file mode 100644 index 0000000..d903411 --- /dev/null +++ b/lib/dry/validation/deprecations.rb @@ -0,0 +1,23 @@ +require 'logger' + +module Dry + module Validation + module Deprecations + extend Dry::Configurable + + setting :logger, Logger.new($stdout) + + def self.format(msg, caller) + "#{msg} [#{caller[1].split(':')[0..1].join(' line ')}]" + end + + def logger + @logger ||= Deprecations.config.logger + end + + def warn(msg) + logger.warn(Deprecations.format(msg, ::Kernel.caller)) + end + end + end +end diff --git a/lib/dry/validation/error_compiler/input.rb b/lib/dry/validation/error_compiler/input.rb index 8d59400..8411200 100644 --- a/lib/dry/validation/error_compiler/input.rb +++ b/lib/dry/validation/error_compiler/input.rb @@ -1,6 +1,10 @@ +require 'dry/validation/deprecations' + module Dry module Validation class ErrorCompiler::Input < ErrorCompiler + extend Deprecations + attr_reader :name, :input, :rule, :val_type def initialize(messages, options) @@ -67,12 +71,12 @@ module Dry end def options_for_inclusion?(args) - ::Kernel.warn 'inclusion is deprecated - use included_in instead.' + warn 'inclusion is deprecated - use included_in instead.' options_for_included_in?(args) end def options_for_exclusion?(args) - ::Kernel.warn 'exclusion is deprecated - use excluded_from instead.' + warn 'exclusion is deprecated - use excluded_from instead.' options_for_excluded_from?(args) end diff --git a/lib/dry/validation/schema/dsl.rb b/lib/dry/validation/schema/dsl.rb index abd6c4c..3b83169 100644 --- a/lib/dry/validation/schema/dsl.rb +++ b/lib/dry/validation/schema/dsl.rb @@ -1,9 +1,12 @@ require 'dry/validation/schema/rule' +require 'dry/validation/deprecations' module Dry module Validation class Schema class DSL < BasicObject + include ::Dry::Validation::Deprecations + attr_reader :name, :registry, :rules, :checks, :parent, :options def self.[](name, options = {}) diff --git a/lib/dry/validation/schema/rule.rb b/lib/dry/validation/schema/rule.rb index 3a1c4e3..060ed53 100644 --- a/lib/dry/validation/schema/rule.rb +++ b/lib/dry/validation/schema/rule.rb @@ -1,7 +1,11 @@ +require 'dry/validation/deprecations' + module Dry module Validation class Schema class Rule < BasicObject + extend ::Dry::Validation::Deprecations + INVALID_PREDICATES = { value: [], maybe: [:empty?, :none?], @@ -48,7 +52,7 @@ module Dry end def required(*predicates) - ::Kernel.warn 'required is deprecated - use filled instead.' + warn 'required is deprecated - use filled instead.' filled(*predicates) end diff --git a/lib/dry/validation/schema/value.rb b/lib/dry/validation/schema/value.rb index e827e06..0a761df 100644 --- a/lib/dry/validation/schema/value.rb +++ b/lib/dry/validation/schema/value.rb @@ -15,7 +15,7 @@ module Dry end def key(name, &block) - ::Kernel.warn 'key is deprecated - use required instead.' + warn 'key is deprecated - use required instead.' required(name, &block) end @@ -25,6 +25,8 @@ module Dry if type_spec type_map[name] = type_spec + else + warn "Missing type spec for #{name.inspect} #{type}" end rule diff --git a/log/.gitkeep b/log/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index efd747f..94efff3 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -23,6 +23,10 @@ module Types include Dry::Types.module end +Dry::Validation::Deprecations.configure do |config| + config.logger = Logger.new(SPEC_ROOT.join('../log/deprecations.log')) +end + RSpec.configure do |config| config.disable_monkey_patching!