How do I write the correct params
validations for this hash parameter:
{
"files": {
"main.c": {
"contents": "#include <stdio.h> ...",
"foo": "bar"
},
"main.h": {
"contents": "#define BLAH ...",
"foo": "baz"
},
... more files here ...
}
}
files
is the hash parameter that I want to validate. Each key of files
can be anything (a string); the values are hashes with a specific format that also needs to be validated (requires contents
and foo
). I'm using grape 0.9.0.
This is kind of what I want to have:
params do
optional :files, type: Hash do
requires <any key>, type: Hash do
requires :contents, type: String
requires :foo, type: String
end
end
end
I've read the documentation but I couldn't see how to achieve this kind of validation. Is it even possible? Do I need to write a custom validator?
An alternative is to have this instead:
{
"files":
[
{
"name": "main.c",
"contents": "#include <stdio.h> ...",
"foo": "bar"
},
{
"name": "main.h",
"contents": "#define BLAH ...",
"foo": "baz"
}
]
}
which can be easily validated like this:
params do
optional :files, type: Array do
requires :name, type: String
requires :contents, type: String
requires :foo, type: String
end
end
but now I lose the ability to have unique file names.