https://github.com/osoco/grails-extra-runtime-validation
Extra runtime validation grails plugin
https://github.com/osoco/grails-extra-runtime-validation
Last synced: about 1 year ago
JSON representation
Extra runtime validation grails plugin
- Host: GitHub
- URL: https://github.com/osoco/grails-extra-runtime-validation
- Owner: osoco
- License: apache-2.0
- Created: 2012-11-30T12:56:21.000Z (over 13 years ago)
- Default Branch: master
- Last Pushed: 2012-12-09T21:29:54.000Z (over 13 years ago)
- Last Synced: 2025-03-27T07:35:47.799Z (about 1 year ago)
- Language: Groovy
- Size: 150 KB
- Stars: 5
- Watchers: 4
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Summary
Allows performing additional validations on Grails domain objects at runtime. Adds an overloaded ``validate(Closure extraConstraints)`` method to every domain class.
# Installation
Include the plugin in ``BuildConfig.groovy``:
```groovy
plugins {
compile ":extra-runtime-validation:0.1"
}
```
or if you are somehow old school and like deprecated features:
```
grails install-plugin extra-runtime-validation
```
# Usage
## Typical scenario
### Given
a domain class ``Author`` with a nullable ``firstName`` field:
```groovy
class Author {
String firstName
String lastName
static constraints = {
firstName nullable: true
lastName blank: false
}
}
```
### When
we want to validate that the ``firstName`` is neither null nor blank (as required by a specific use case).
### Then
validate the domain object with the standard ``validate`` method:
```groovy
anAuthor.validate()
```
#### And
perform some additional validations with extra constraints:
```groovy
anAuthor.validate {
firstName nullable: false, blank: false
}
```
#### And
do something with validation errors.
# Unit testing
The plugin doesn't provide a direct support for mocking the ``validate(Closure)``. The reason behind this is that the plugin should remain compatible with both Grails 1.3 and 2.x (which implement mocking in a different way).
However, the plugin includes a helper method ``ValidationUtils.addValidateWithExtraConstraints(Object, ApplicationContext)`` which adds the new validation method to a concrete **instance**. Dependending on the Grails version and assuming you are using Spock, you mock ``validate(Closure)`` in the following way:
### Grails 1.3
```
import es.osoco.grails.plugins.extraval.ValidationUtils
import org.codehaus.groovy.grails.support.MockApplicationContext
mockDomain(Author)
def o = new Author(...)
// With JUnit and a subclass of GrailsUnitTestCase, you don't have to create the MockApplicationContext. Take it from the parent class (applicationContext property)
def ctx = new MockApplicationContext()
ctx.registerMockBean('messageSource', null)
ValidationUtils.addValidateWithExtraConstraints(o, ctx)
```
### Grails 2.x
```
import es.osoco.grails.plugins.extraval.ValidationUtils
@TestMixin(DomainClassUnitTestMixin)
mockDomain(Author)
def o = new Author(...)
ValidationUtils.addValidateWithExtraConstraints(o, applicationContext)
```
## Final notes
* ``validate(Closure)`` must be called after the standard ``validate`` method so that additional errors are retained. If called before, the built-in validation cleans the error list.
* Validation of extra constraints adds errors and doesn't replace them.
* Extra constraints neither replace nor modify intrinsic constraints