Validating Grails Configurations

When externalizing grails app configurations for multiple environments I want to ensure values are provided for all the required/expected properties.  So I wrote a plugin to help.

Validating Expected and Required Properties

Simply add something like this to Config.groovy

validate  {
    required = [ "a", "b", "c" ]
    expected = [ "p":123, "d":"foobar", "q":"/dev/null" ]
}

Then (usually in BootStrap.groovy ) to set defaults for missing expected properties call

grailsApplication.config.validateExpectedProperties()

To check for required properties call

grailsApplication.config.validateRequiredProperties()

A ConfigurationException will be thrown when required properties are missing.

The validate.expected and validate.required data can be specified at lower levels too…

grails {
    mongo {
        validate {
            required = [
                "grails.mongo.host",
                "grails.mongo.databaseName"
            ]
            expected = [
                "grails.mongo.port": 27017,
                "grails.mongo.bucket": "project"
            ]
        }
    }
}

This way you can validate portions of the config by calling

grailsApplication.config.grails.mongo.validateExpectedProperties()

and so on.

Validating Existence of External Files

The ConfigUtils.validateExternalFiles method will check that a list of files does exist.  Use it like this in Config.groovy.

grails.config.locations << "file:${userHome}/.emacs"
grails.config.locations < "file:${userHome}/.grails/${appName}-config.groovy"
ConfigUtils.validateExternalFiles(grails.config.locations)

A `ConfigurationException` will be thrown when any of the files does not exist.

Resources

 

About the Author

Object Partners profile.

One thought on “Validating Grails Configurations

  1. amiller says:

    danmcharness pointed out that the sample app is not backward compatible with 2.1.4 or 2.0.4. So I created a couple new samples. Now github has example apps for 2.0.4, 2.1.4, and 2.2.1.

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Blog Posts
Performance Test Liquibase Update
When doing a liquibase update to a database if you’re having performance issues, it can be hard to find out which updates are causing problems. If you need to measure the time to apply each […]
TICK Stack Monitoring for the Non-Technical
TICK – Telegraf, Influx, Chronograf, and Kapacitor – is a method of monitoring your systems and applications. In this article, I discuss in non-technical terms what the difference is between TICK and Prometheus Grafana A […]
Design Systems, Part 1 • Introduction
Business leaders need a practical guide to plan and execute Design System Initiatives. The aim of this series is to be that guide. This installment introduces terms and definitions as a primer on Design Systems.
ML for Translating Dysarthria Speech (Pre-Part 1)
What is Dysarthria? Per the Mayo Clinic, Dysarthria occurs when the muscles you use for speech are weak or you have difficulty controlling them. Dysarthria often causes slurred or slow speech that can be difficult […]