Resolve custom validators from API namespace (#1178)#2658
Open
GulfGulfinson wants to merge 2 commits intoruby-grape:masterfrom
Open
Resolve custom validators from API namespace (#1178)#2658GulfGulfinson wants to merge 2 commits intoruby-grape:masterfrom
GulfGulfinson wants to merge 2 commits intoruby-grape:masterfrom
Conversation
When a validator short name (e.g. :uuid) is not in the global registry, Grape now looks up the constant in the API's namespace under Validators::<Name> (e.g. Api::V2::Validators::Uuid for Api::V2::Jobs). This fixes the require-order / double-load issue when using custom validators in Rails: params like 'uuid: true' can resolve to a validator class in the same API namespace without explicit require or global Grape::Validations.register, and without triggering 'already initialized constant' warnings. - Add optional scope: to require_validator; pass API from ParamsScope - Add find_custom_validator_in_scope and resolve_api_class (use mount instance's @base for named API class when present) - Add specs for scope-based lookup and integration with params Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
f1c8740 to
8bb87a3
Compare
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #1178
What
Custom validators (e.g. 'uuid: true') are resolved from the apis namespace, when they are not in the global registry. For example for an api under Api::V2::Jobs, Grape looks up 'Api::V2::Validators::Uuid' when it sees uuid: true
in params, so you no longer need to requirethe validator file yourself.Why
Previosly using a namespaced custom validator (e.g. 'Api::V2::Validators::Uuid) required manually requiring the file in the API (e.g. in base.rb). With Rails autoloading, that led to the file being loaded twice and already initialized constant warnings.
How
basefor remountable APIs) so lookup uses the correct namespace.Tests added in spec/grape/api/custom_validations_spec.rb for the namespace lookup behavior.