Plugins You can find out about all the publicly available Grails plugins.

Groovyschema Grails plugin

  • Tags: /
  • Latest: 0.4
  • Last Updated: 13 April 2018
  • Grails version: 2.4 > *
0 vote
Dependency:
compile "org.grails.plugins:groovyschema:0.4"

 Documentation  Source  Issues

Summary

Declaratively validate deserialized JSON objects.

Description

Groovy object validation library that tries to emulate the JSON Schema specification. It is vastly inspired by @tdegrunt's implementation.

It is meant to be used with JsonSlurper when validating incoming JSON content in REST API implementations.

class SomeController {

static final CONTACT_SCHEMA = [ type: 'object', required: ['givenName', 'familyName', 'email'], properties: [ honorificPrefix: [enum:['Ms.', 'Mr.', 'Dr.']], givenName: [type:'string'], additionalName: [type:'string'], familyName: [type:'string'], honorificSuffix: [enum:['Ph.D.', 'Esq.']], email: [format:'email'], ] ]

def groovySchemaService

def someAction() { def payload = request.JSON def validationErrors = groovySchemaService.validate(payload, CONTACT_SCHEMA) if (validationErrors) { render([ status: 400, contentType: 'application/json', encoding: 'UTF-8', text: new groovy.json.JsonBuilder(validationErrors).toString() ]) } else { // … } } }