-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Support custom validators autoloading in Rails >= 6 #2204
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
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a3ed42d
add the spec for "unknown validator" scenario
dm1try 29b86e6
allow to set a callback for unknown validators
dm1try bf93fd3
allow to use custom validators with zeitwerk
dm1try acb2d29
regenerate rubocop_todo
dm1try 7a1a549
update changelog
dm1try File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# frozen_string_literal: true | ||
|
||
module Grape | ||
class Railtie < Rails::Railtie | ||
if Rails.version.to_f >= 6.0 | ||
initializer 'grape, setup zeitwerk loader for custom validators' do |_app| | ||
Grape.config.on_unknown_validator << method(:on_grape_unknown_validator) if Rails.autoloaders.zeitwerk_enabled? | ||
end | ||
end | ||
|
||
def on_grape_unknown_validator(name) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the config option is |
||
validator = ::Grape::Validations::Validators.const_get(name.camelize) | ||
|
||
unless Rails.application.config.eager_load | ||
# support reloading validators in development | ||
Rails.autoloaders.main.on_unload(validator.to_s) do | ||
::Grape::Validations.deregister_validator(name) | ||
end | ||
end | ||
|
||
validator | ||
rescue NameError | ||
nil | ||
end | ||
end | ||
end |
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -431,7 +431,7 @@ def check_incompatible_option_values(default, values, except_values, excepts) | |
end | ||
|
||
def validate(type, options, attrs, doc_attrs, opts) | ||
validator_class = Validations.validators[type.to_s] | ||
validator_class = find_validator_class(type.to_s) | ||
|
||
raise Grape::Exceptions::UnknownValidator.new(type) unless validator_class | ||
|
||
|
@@ -497,6 +497,14 @@ def document_attribute(attrs, doc_attrs) | |
full_attrs = attrs.collect { |name| { name: name, full_name: full_name(name) } } | ||
@api.document_attribute(full_attrs, doc_attrs) | ||
end | ||
|
||
def find_validator_class(name) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
Validations.validators[name] || | ||
::Grape.config.on_unknown_validator.find do |callback| | ||
result = callback.call(name) | ||
break result if result | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.