Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to enforce Grails command objects have been validated?
    primarykey
    data
    text
    <p>We use the following general pattern with Grails controllers and command objects</p> <pre><code>SomeController { def someAction() { SomeCommandObject co = SomeCommandObject.valueOf(params) if(!co.validate()) { // return bad request } someService.doWork(co) // return ok } SomeService { def doWork(SomeCommandObject co) { notTrue(!co.hasErrors(), 'cant have errors') // Commons validation // do actual work } } </code></pre> <p>Apparently, if co.validate() has not been called, .hasErrors() will always return false. Is there a better way to enforce that .validate() has been called before a command object is passed between application layers? We don't want to pass around invalid command objects but we don't want to force every new method to re-validate the command object either.</p> <p>Note: We aren't using the default controller/command object creation pattern because we need to do some custom parameter map checking, we use a static valueOf method instead to create the command object. Answers that change that practice are also welcome.</p> <p>EDIT: A little more info on why we aren't using the 'default' controller/command object creation. Specifically why we aren't doing ..</p> <pre><code>def someAction(SomeCommandObject co) { } </code></pre> <p>We have a requirement to disallow random query parameters, eg. endpoint/object?color=blue. To do that we need access to the parameter map in the command object to verify that it doesn't contain any 'unexpected' parameter keys. As I understand it, the default way would just create a member on the CO named color, and I don't see how to prevent arbitrary members using even custom validators. I'd happily entertain suggestions for doing so, thereby allowing us to use this default means.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload