Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update validator registration mechanism #2508

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/grape.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
require 'active_support/core_ext/object/duplicable'
require 'active_support/core_ext/string/output_safety'
require 'active_support/core_ext/string/exclude'
require 'active_support/core_ext/string/inflections'
require 'active_support/deprecation'
require 'active_support/inflector'
require 'active_support/ordered_options'
require 'active_support/notifications'

Expand Down
28 changes: 10 additions & 18 deletions lib/grape/validations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,19 @@ module Grape
module Validations
module_function

def validators
@validators ||= {}
end

# Register a new validator, so it can be used to validate parameters.
# @param short_name [String] all lower-case, no spaces
# @param klass [Class] the validator class. Should inherit from
# Grape::Validations::Validators::Base.
def register_validator(short_name, klass)
validators[short_name] = klass
end

def deregister_validator(short_name)
validators.delete(short_name)
end

def require_validator(short_name)
str_name = short_name.to_s
validators.fetch(str_name) { Grape::Validations::Validators.const_get(:"#{str_name.camelize}Validator") }
ValidatorsRegistry[short_name]
rescue NameError
raise Grape::Exceptions::UnknownValidator, short_name
end

class ValidatorsRegistry < Grape::Util::Cache
def initialize
super
@cache = Hash.new do |h, name|
h[name] = Grape::Validations::Validators.const_get(:"#{name.to_s.camelize}Validator")
end
end
end
end
end
8 changes: 0 additions & 8 deletions lib/grape/validations/validators/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,6 @@ def validate!(params)
raise Grape::Exceptions::ValidationArrayErrors.new(array_errors) if array_errors.any?
end

def self.inherited(klass)
super
return if klass.name.blank?

short_validator_name = klass.name.demodulize.underscore.delete_suffix('_validator')
Validations.register_validator(short_validator_name, klass)
end

def message(default_key = nil)
options = instance_variable_get(:@option)
options_key?(:message) ? options[:message] : default_key
Expand Down
15 changes: 15 additions & 0 deletions spec/grape/validations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2026,5 +2026,20 @@ def validate_param!(attr_name, params)
expect { subject }.to raise_error(Grape::Exceptions::UnknownValidator)
end
end

context 'when custom' do
let(:short_name) { :custom }
let(:custom_validator) do
Class.new(Grape::Validations::Validators::Base) do
def validate_param!(_attr_name, _params); end
end
end

before do
stub_const('Grape::Validations::Validators::CustomValidator', custom_validator)
end

it { is_expected.to be(Grape::Validations::Validators::CustomValidator) }
end
end
end