I'm using Ruby on Rails 7 to build a REST API that accepts a payload via HTTP PATCH that looks like this:
{
"answers": [
{"question_id": 1, "content": "My answer content ONE"},
{"question_id": 2, "content": "My answer content TWO"}
]
}
I want to validate the structure of this response in my controller using strong parameters.
I've tried a bunch of similar SO posts but none of them seem to work for me.
Here is what I've tried so far:
params = ActionController::Parameters.new(answers: [{question_id: 1, content: 'some answer'}])
params.require(answers: [:question_id, :content])
# I get: `require': param is missing or the value is empty: {:answers=>[:question_id, :content]} (ActionController::ParameterMissing)
params.require(:answers).require([:question_id, :content])
# I get: NoMethodError on the second .require
params.require(answers: [[:question_id, :content]])
# I get: param is missing or the value is empty: {:answers=>[[:question_id, :content]]}
Any pointers on what I am doing wrong would be greatly appreciated.
Why insist on validating the params only through
ActionController::Parameters? I have always found limitations with the params API for nested structures (to be specific more deep structures for API I have built) like you have and in such cases I always validate the data in the most basic manner. For e.g. for the data structure you have shown I would validate it in following manner:Params API is definitely useful for whitelisting data but when it poses limitations for a particular data structure, then instead of trying to insist on implementing the validation through only that API, it is better to implement through pure Ruby constructs which is the option always open.
Hope you find this suggestion helpful.