0

我正在使用命令对象在 Grails restfull 控制器上执行数据绑定,如上例所示:

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
        }
    }
}

当传递一个无效的作者时,会呈现一个包含“rejectedValue”属性的 Json,该属性嵌套了所有作者的信息。

我们不能将这些暴露给最终用户。

是否可以过滤这些字段或整个 deniedValue 属性?

Obs:由于迁移问题,我仍在使用 Grails 2.5.6。我不知道 Grails 3+ 在这种情况下是否表现不同。

到目前为止,在 respod 上使用“excludes”参数还没有奏效。

我尝试了以下选项:

respond command.errors, [excludes: ['rejectedValue']]
respond command.errors, [excludes: ['rejected-value']]
respond command.errors, [excludes: ['errors.rejectedValue']]

================================== 编辑================ ===================

我在上面写了一个示例,显示了发送的请求正文和我当前收到的响应,一个是理想的,一个是解决问题的。

示例请求正文:

{
    "title": "How to Spy",
    "author": 7
}

当前响应正文:

{
    "errors": [
        {
            "object": "com.example.Author",
            "field": "author",
            "rejected-value": {
                "id": 7
                "secretField": "Sekret info this"
                "errors": {
                    "errors": []
                },
                "version": 1
            },
            "message": "Custom validator failed"
        },
    ]
}

理想反应体:

{
    "errors": [
        {
            "object": "com.example.Author",
            "field": "author",
            "rejected-value": {
                "id": 7
            },
            "message": "Custom validator failed"
        },
    ]
}

好的响应正文:

{
    "errors": [
        {
            "object": "com.example.Author",
            "field": "author",
            "message": "Custom validator failed"
        },
    ]
}
4

0 回答 0