Home Grails: How do a validate POST body consisting of a list of items using a command object?
Reply: 0

Grails: How do a validate POST body consisting of a list of items using a command object?

user1629
1#
user1629 Published in July 19, 2018, 9:44 am

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?

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.36176 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO