Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Some cleanups to extended validation, extra test for null return and …

…update documentation.

Signed-off-by: Geoff Lane <geoff@zorched.net>
  • Loading branch information...
commit 7cf0434b32f814c695a87036f5ae4b3b66c159a8 1 parent 2d1f10c
Geoff Lane authored
16 README.markdown
Source Rendered
@@ -63,8 +63,20 @@ e.g.
63 63 }
64 64
65 65 ### validate closure (required) ###
66   -The validate closure is the main part of the algorithm where validation is performed. It should return true if the
67   -validation was successful and false if the validation did not succeed.
  66 +The validate closure is the main part of the algorithm where validation is performed. It should return a value to indicate
  67 +if the validation was successful.
  68 +
  69 +Successful validation is indicated by the return of:
  70 +
  71 +1. true
  72 +2. null
  73 +
  74 +An unsuccessful validation is indicated by the return of:
  75 +
  76 +1. false
  77 +2. A String which is used as the error message to show the user
  78 +3. A Collection with first element being message code, and following elements being message parameters
  79 +4. An Array with first element being message code, and following elements being message parameters
68 80
69 81 The validate closure takes up to 3 parameters:
70 82
6 grails-app/domain/net/zorched/test/HasExtendedValidators.groovy
@@ -3,9 +3,11 @@ package net.zorched.test
3 3 class HasExtendedValidators {
4 4 String foo
5 5 String bar
  6 + String baz
6 7
7 8 static constraints = {
8   - foo(errorCode: true)
9   - bar(errorParams: true)
  9 + foo(nullable: true, errorCode: true)
  10 + bar(nullable: true, errorParams: true)
  11 + baz(nullable: true, nullReturning: true)
10 12 }
11 13 }
7 grails-app/utils/net/zorched/test/NullReturningConstraint.groovy
... ... @@ -0,0 +1,7 @@
  1 +package net.zorched.test
  2 +
  3 +class NullReturningConstraint {
  4 + def validate = { val ->
  5 + return null
  6 + }
  7 +}
62 src/java/net/zorched/grails/plugins/validation/CustomConstraintFactory.java
@@ -87,51 +87,45 @@ protected void processValidate(Object target, Object propertyValue, Errors error
87 87 applicationContext.getAutowireCapableBeanFactory().autowireBeanProperties(constraint.getReferenceInstance(), AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, false);
88 88
89 89 Object result = constraint.validate(params.toArray());
90   - boolean bad = false;
  90 + boolean isValid = false;
91 91 String errmsg = constraint.getFailureCode();
92 92 Object[] args = null;
93 93
94   - if (result != null) {
95   - if (result instanceof Boolean) {
96   - bad = !((Boolean)result).booleanValue();
97   - }
98   - else if (result instanceof CharSequence) {
99   - bad = true;
100   - errmsg = result.toString();
101   - }
102   - else if ((result instanceof Collection<?>) || result.getClass().isArray()) {
103   - bad = true;
104   - Object[] values = (result instanceof Collection<?>) ? ((Collection<?>)result).toArray() : (Object[])result;
105   - if (!(values[0] instanceof String)) {
106   - throw new IllegalArgumentException("Return value from validation closure [" +
107   - ConstrainedProperty.VALIDATOR_CONSTRAINT+"] of property ["+constraintPropertyName+"] of class [" +
108   - constraintOwningClass+"] is returning a list but the first element must be a string " +
109   - "containing the error message code");
110   - }
111   - errmsg = (String)values[0];
112   - args = new Object[values.length - 1 + 3];
113   - int i = 0;
114   - args[i++] = constraintPropertyName;
115   - args[i++] = constraintOwningClass;
116   - args[i++] = propertyValue;
117   - System.arraycopy(values, 1, args, i, values.length - 1);
118   - }
119   - else {
  94 + if (result == null) {
  95 + isValid = true;
  96 + } else if (result instanceof Boolean) {
  97 + isValid = (Boolean) result;
  98 + } else if (result instanceof CharSequence) {
  99 + isValid = false;
  100 + errmsg = result.toString();
  101 + } else if ((result instanceof Collection<?>) || result.getClass().isArray()) {
  102 + isValid = false;
  103 + Object[] values = (result instanceof Collection<?>) ? ((Collection<?>)result).toArray() : (Object[])result;
  104 + if (!(values[0] instanceof String)) {
120 105 throw new IllegalArgumentException("Return value from validation closure [" +
121   - ConstrainedProperty.VALIDATOR_CONSTRAINT+"] of property [" + constraintPropertyName +
122   - "] of class [" + constraintOwningClass +
123   - "] must be a boolean, a string, an array or a collection");
  106 + ConstrainedProperty.VALIDATOR_CONSTRAINT+"] of property ["+constraintPropertyName+"] of class [" +
  107 + constraintOwningClass+"] is returning a list but the first element must be a string " +
  108 + "containing the error message code");
124 109 }
  110 + errmsg = (String)values[0];
  111 + args = new Object[values.length - 1 + 3];
  112 + int i = 0;
  113 + args[i++] = constraintPropertyName;
  114 + args[i++] = constraintOwningClass;
  115 + args[i++] = propertyValue;
  116 + System.arraycopy(values, 1, args, i, values.length - 1);
  117 + } else {
  118 + throw new IllegalArgumentException("Return value from validation closure [" +
  119 + ConstrainedProperty.VALIDATOR_CONSTRAINT+"] of property [" + constraintPropertyName +
  120 + "] of class [" + constraintOwningClass +
  121 + "] must be a boolean, a string, an array or a collection");
125 122 }
126 123
127   - if (bad) {
  124 + if (! isValid) {
128 125 if (args == null) {
129 126 args = new Object[] { constraintPropertyName, constraintOwningClass, propertyValue };
130 127 }
131   - /*super.rejectValue(target, errors, ConstrainedProperty.DEFAULT_INVALID_VALIDATOR_MESSAGE_CODE,
132   - errmsg == null ? ConstrainedProperty.VALIDATOR_CONSTRAINT + ConstrainedProperty.INVALID_SUFFIX: errmsg, args);*/
133 128
134   - /*Object[] args = new Object[] { constraintPropertyName, constraintOwningClass, propertyValue, constraintParameter };*/
135 129 super.rejectValue(target, errors, constraint.getDefaultMessageCode(), errmsg, args);
136 130 }
137 131 }
10 test/integration/ExtendedValidatorTests.groovy
@@ -3,7 +3,7 @@ import org.junit.Test
3 3
4 4 class ExtendedValidatorTests {
5 5 @Test
6   - void testCodeReturnedFromValidator() {
  6 + void test_code_returned_from_validator() {
7 7 def a = new HasExtendedValidators(foo:"bar")
8 8 a.validate()
9 9
@@ -12,7 +12,7 @@ class ExtendedValidatorTests {
12 12 }
13 13
14 14 @Test
15   - void testCodeAndArgumentsFromValidator() {
  15 + void test_code_and_arguments_from_validator() {
16 16 def a = new HasExtendedValidators(bar:"foo")
17 17 a.validate()
18 18
@@ -26,4 +26,10 @@ class ExtendedValidatorTests {
26 26 assert error.arguments[3] == "foo" // custom param #1
27 27 assert error.arguments[4] == "bar" // custom param #2
28 28 }
  29 +
  30 + @Test
  31 + void test_null_returned_from_validator() {
  32 + def a = new HasExtendedValidators(baz:"foo")
  33 + assert a.validate() // Null returned from validator means valid
  34 + }
29 35 }

0 comments on commit 7cf0434

Please sign in to comment.
Something went wrong with that request. Please try again.