I am facing some issues with writing custom validators (Commands) in grails 3.3.3. Specifically, I am trying to validate POST request whose body is composed of a list of items. This is what I have...
The Command:
class VoteCommand implements Validateable {
List<VoteItem> postList = [].withLazyDefault { new ListItem() }
static constraints = {
postList nullable: false
}
class ListItem implements Validateable {
String tag
String some_id
static constraints = {
some_id nullable: false, blank: false
tag nullable: false, blank: false
tag inList: Tag.values() as List
}
}
}
AND the Payload:
{
"noteVotesButWorks": [
{
"tag": "good"
},
{
"tag": "bad"
}
]
}
This payload passes the validation check in my controller action.
def save(VoteCommand command) {
println(command.errors) //grails.validation.ValidationErrors: 0 errors
if (command.hasErrors()) {
respond params.errors, view: 'create'
} else {
withFormat {
'*' { render status: CREATED }
}
}
}
After making the POST request to this action, I get a 201 and grails.validation.ValidationErrors: 0 errors printed to stdout.
Please, can someone here give me some pointers?
I don't think you want
nullable: falsefor postList. An empty list is not null. I think you wantminSize: 1.