|
| 1 | +package org.grails.validation |
| 2 | + |
| 3 | +import grails.validation.ValidationErrors |
| 4 | +import org.grails.core.DefaultGrailsDomainClass |
| 5 | +import org.springframework.context.MessageSource |
| 6 | +import org.springframework.context.support.StaticMessageSource |
| 7 | +import org.springframework.validation.ObjectError |
| 8 | +import spock.lang.Issue |
| 9 | +import spock.lang.Specification |
| 10 | + |
| 11 | +/** |
| 12 | + * Created by graemerocher on 08/12/16. |
| 13 | + */ |
| 14 | +class GrailsDomainClassValidatorSpec extends Specification { |
| 15 | + |
| 16 | + @Issue('https://github.com/grails/grails-core/issues/10347') |
| 17 | + void "test validator constraint is only invoked once"() { |
| 18 | + when:"" |
| 19 | + GrailsDomainClassValidator validator = new GrailsDomainClassValidator() |
| 20 | + validator.setDomainClass( new DefaultGrailsDomainClass(Test)) |
| 21 | + validator.setMessageSource(new StaticMessageSource()) |
| 22 | + def test = new Test(name: "blah", foo: "two") |
| 23 | + def errors = new ValidationErrors(test) |
| 24 | + validator.validate(test, errors) |
| 25 | + |
| 26 | + then: |
| 27 | + errors.allErrors.size() == 2 |
| 28 | + errors.allErrors.find { ObjectError e -> e.code == 'invalid.name' } |
| 29 | + errors.allErrors.find { ObjectError e -> e.code == 'invalid.foo' } |
| 30 | + } |
| 31 | + |
| 32 | + |
| 33 | + static class Test { |
| 34 | + Long id,version |
| 35 | + String name |
| 36 | + String foo |
| 37 | + static transients = ['foo'] |
| 38 | + static constraints = { |
| 39 | + name validator: { val, obj, errors -> |
| 40 | + errors.reject("invalid.name", "Invalid Name") |
| 41 | + } |
| 42 | + foo validator: { val, obj, errors -> |
| 43 | + errors.reject("invalid.foo", "Invalid Foo") |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | +} |
0 commit comments