Grape::Exceptions::UnknownValidator
Nobody has claimed this yet.
- Dominant language
- Ruby
- Stars
- 10k
- Forks
- 1.2k
- Avg merge
- 14h 38m
- Merged PRs (30d)
- 92
Description
I have the following files:
# app/controllers/api/v2/base.rb
module Api
module V2
class Base < Grape::API
mount V2::Jobs
end
end
end
# app/controllers/api/v2/jobs.rb
module Api
module V2
class Jobs < Grape::API
resources :jobs do
params do
requires :id, type: String, uuid: true
end
get ':id' do
end
end
end
end
end
# app/controllers/api/v2/validators/uuid.rb
module Api
module V2
module Validators
class Uuid < ::Grape::Validations::Base
REGEX = %r{[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}}
def validate_param!(attr_name, params)
unless (params[attr_name] =~ REGEX).zero?
fail ::Grape::Exceptions::Validation, params: [@scope.full_name(attr_name)], message: 'must be a valid UUID'
end
end
end
end
end
end
If I want the above to work, I have to manually require the validator class in base.rb file, like so:
require_relative './validators/uuid'
which result in throwing warnings:
app/controllers/api/v2/validators/uuid.rb:6: warning: already initialized constant Api::V2::Validators::Uuid::REGEX
app/controllers/api/v2/validators/uuid.rb:6: warning: previous definition of REGEX was here
since rails will automatically require the validator as well. Any idea why grape is not able to pick up the custom validator? Looks like a require order issue or something. Any clue?
Thanks
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by comparing app/controllers/api/v2/base.rb, jobs.rb, and validators/uuid.rb with Rails' automatic loading behavior and Grape's custom validator lookup. Reproduce the UUID validation path without the manual require, then confirm the validator is discovered once and no duplicate-constant warning is emitted.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rails, ruby
- Domain
- api, backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 35/100