I'm using a command object to perform the data binding on a Grails restfull controller like in the example above:
class BookController {
// ...
def save(BookCommand command) {
if(command.hasErrors()) {
respond command.errors
return
}
// Save a new instance of book
}
// ...
}
@Validateable
class BookCommand {
String title
Author author
static constraints = {
title nullable: false
pet nullable: false, validator: { author ->
author.enabled
}
}
}
When an invalid author is passed a Json is rendered containing a "rejectedValue" property that has all the author's information nested.
We can not expose those to end users.
Is it possible to filter those fields or the entire rejectedValue property?
Obs: I'm still using Grails 2.5.6 because of migration issues. I don't know if Grails 3+ behaves diferently in this case.
So far using the "excludes" parameter on respod has not worked.
I tried the following options:
respond command.errors, [excludes: ['rejectedValue']]
respond command.errors, [excludes: ['rejected-value']]
respond command.errors, [excludes: ['errors.rejectedValue']]
================================= EDIT ==================================
I wrote an example above showing the request body sent and the responses I'm currently receiving, the one that would be ideal and one that just solves the problem.
Example request body:
{
"title": "How to Spy",
"author": 7
}
Current response body:
{
"errors": [
{
"object": "com.example.Author",
"field": "author",
"rejected-value": {
"id": 7
"secretField": "Sekret info this"
"errors": {
"errors": []
},
"version": 1
},
"message": "Custom validator failed"
},
]
}
Ideal response body:
{
"errors": [
{
"object": "com.example.Author",
"field": "author",
"rejected-value": {
"id": 7
},
"message": "Custom validator failed"
},
]
}
Okay response body:
{
"errors": [
{
"object": "com.example.Author",
"field": "author",
"message": "Custom validator failed"
},
]
}