Array of hashes not failing correctly when array is empty
Nobody has claimed this yet.
- Dominant language
- Ruby
- Stars
- 10k
- Forks
- 1.2k
- Avg merge
- 14h 38m
- Merged PRs (30d)
- 92
Description
grape: 1.0.1
ruby: 2.4.2
When you have:
params do
requires :paired_values, type: Array do
requires :name, allow_blank: false, type: String
requires :semver, allow_blank: false, type: String, semver_constraint: true
end
end
And semver_constraint custom validator looks like
class SemverConstraint < Grape::Validations::Base
def validate_param!(attr_name, params)
Semverse::Constraint.new(params[attr_name])
rescue Semverse::InvalidConstraintFormat => e
raise Grape::Exceptions::Validation, params: [@scope.full_name(attr_name)], message: e.message
end
end
When you call into the api_endpoint with params: { paired_values: [] } grape ends up calling validate_param! with attr_name being :semver and params being "", so this throws the exception TypeError: no implicit conversion of Symbol into Integer. Because these values are required shouldn't the grape validation for required fail out before this is even called?
I can easily work around this but before i upgraded to 1.0.0/1.0.1 this was not throwing this exception.
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 reproducing the issue from the parameter declaration and custom SemverConstraint validator shown in the report with paired_values set to an empty array. Trace the validation order for the required fields and confirm that the required validation fails before the custom validator runs; done means the expected validation error is raised instead of the TypeError.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- ruby
- Domain
- api, backend
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 30/100