Skip to content
Browse files

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

…update documentation.

Signed-off-by: Geoff Lane <geoff@zorched.net>
  • Loading branch information...
1 parent 2d1f10c commit 7cf0434b32f814c695a87036f5ae4b3b66c159a8 @geofflane committed Jan 28, 2012
View
16 README.markdown
@@ -63,8 +63,20 @@ e.g.
}
### validate closure (required) ###
-The validate closure is the main part of the algorithm where validation is performed. It should return true if the
-validation was successful and false if the validation did not succeed.
+The validate closure is the main part of the algorithm where validation is performed. It should return a value to indicate
+if the validation was successful.
+
+Successful validation is indicated by the return of:
+
+1. true
+2. null
+
+An unsuccessful validation is indicated by the return of:
+
+1. false
+2. A String which is used as the error message to show the user
+3. A Collection with first element being message code, and following elements being message parameters
+4. An Array with first element being message code, and following elements being message parameters
The validate closure takes up to 3 parameters:
View
6 grails-app/domain/net/zorched/test/HasExtendedValidators.groovy
@@ -3,9 +3,11 @@ package net.zorched.test
class HasExtendedValidators {
String foo
String bar
+ String baz
static constraints = {
- foo(errorCode: true)
- bar(errorParams: true)
+ foo(nullable: true, errorCode: true)
+ bar(nullable: true, errorParams: true)
+ baz(nullable: true, nullReturning: true)
}
}
View
7 grails-app/utils/net/zorched/test/NullReturningConstraint.groovy
@@ -0,0 +1,7 @@
+package net.zorched.test
+
+class NullReturningConstraint {
+ def validate = { val ->
+ return null
+ }
+}
View
62 src/java/net/zorched/grails/plugins/validation/CustomConstraintFactory.java
@@ -87,51 +87,45 @@ protected void processValidate(Object target, Object propertyValue, Errors error
applicationContext.getAutowireCapableBeanFactory().autowireBeanProperties(constraint.getReferenceInstance(), AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, false);
Object result = constraint.validate(params.toArray());
- boolean bad = false;
+ boolean isValid = false;
String errmsg = constraint.getFailureCode();
Object[] args = null;
- if (result != null) {
- if (result instanceof Boolean) {
- bad = !((Boolean)result).booleanValue();
- }
- else if (result instanceof CharSequence) {
- bad = true;
- errmsg = result.toString();
- }
- else if ((result instanceof Collection<?>) || result.getClass().isArray()) {
- bad = true;
- Object[] values = (result instanceof Collection<?>) ? ((Collection<?>)result).toArray() : (Object[])result;
- if (!(values[0] instanceof String)) {
- throw new IllegalArgumentException("Return value from validation closure [" +
- ConstrainedProperty.VALIDATOR_CONSTRAINT+"] of property ["+constraintPropertyName+"] of class [" +
- constraintOwningClass+"] is returning a list but the first element must be a string " +
- "containing the error message code");
- }
- errmsg = (String)values[0];
- args = new Object[values.length - 1 + 3];
- int i = 0;
- args[i++] = constraintPropertyName;
- args[i++] = constraintOwningClass;
- args[i++] = propertyValue;
- System.arraycopy(values, 1, args, i, values.length - 1);
- }
- else {
+ if (result == null) {
+ isValid = true;
+ } else if (result instanceof Boolean) {
+ isValid = (Boolean) result;
+ } else if (result instanceof CharSequence) {
+ isValid = false;
+ errmsg = result.toString();
+ } else if ((result instanceof Collection<?>) || result.getClass().isArray()) {
+ isValid = false;
+ Object[] values = (result instanceof Collection<?>) ? ((Collection<?>)result).toArray() : (Object[])result;
+ if (!(values[0] instanceof String)) {
throw new IllegalArgumentException("Return value from validation closure [" +
- ConstrainedProperty.VALIDATOR_CONSTRAINT+"] of property [" + constraintPropertyName +
- "] of class [" + constraintOwningClass +
- "] must be a boolean, a string, an array or a collection");
+ ConstrainedProperty.VALIDATOR_CONSTRAINT+"] of property ["+constraintPropertyName+"] of class [" +
+ constraintOwningClass+"] is returning a list but the first element must be a string " +
+ "containing the error message code");
}
+ errmsg = (String)values[0];
+ args = new Object[values.length - 1 + 3];
+ int i = 0;
+ args[i++] = constraintPropertyName;
+ args[i++] = constraintOwningClass;
+ args[i++] = propertyValue;
+ System.arraycopy(values, 1, args, i, values.length - 1);
+ } else {
+ throw new IllegalArgumentException("Return value from validation closure [" +
+ ConstrainedProperty.VALIDATOR_CONSTRAINT+"] of property [" + constraintPropertyName +
+ "] of class [" + constraintOwningClass +
+ "] must be a boolean, a string, an array or a collection");
}
- if (bad) {
+ if (! isValid) {
if (args == null) {
args = new Object[] { constraintPropertyName, constraintOwningClass, propertyValue };
}
- /*super.rejectValue(target, errors, ConstrainedProperty.DEFAULT_INVALID_VALIDATOR_MESSAGE_CODE,
- errmsg == null ? ConstrainedProperty.VALIDATOR_CONSTRAINT + ConstrainedProperty.INVALID_SUFFIX: errmsg, args);*/
- /*Object[] args = new Object[] { constraintPropertyName, constraintOwningClass, propertyValue, constraintParameter };*/
super.rejectValue(target, errors, constraint.getDefaultMessageCode(), errmsg, args);
}
}
View
10 test/integration/ExtendedValidatorTests.groovy
@@ -3,7 +3,7 @@ import org.junit.Test
class ExtendedValidatorTests {
@Test
- void testCodeReturnedFromValidator() {
+ void test_code_returned_from_validator() {
def a = new HasExtendedValidators(foo:"bar")
a.validate()
@@ -12,7 +12,7 @@ class ExtendedValidatorTests {
}
@Test
- void testCodeAndArgumentsFromValidator() {
+ void test_code_and_arguments_from_validator() {
def a = new HasExtendedValidators(bar:"foo")
a.validate()
@@ -26,4 +26,10 @@ class ExtendedValidatorTests {
assert error.arguments[3] == "foo" // custom param #1
assert error.arguments[4] == "bar" // custom param #2
}
+
+ @Test
+ void test_null_returned_from_validator() {
+ def a = new HasExtendedValidators(baz:"foo")
+ assert a.validate() // Null returned from validator means valid
+ }
}

0 comments on commit 7cf0434

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