From 503dae6eda8857c584365e69634c23af7163a360 Mon Sep 17 00:00:00 2001 From: Andrei Solntsev Date: Tue, 1 Nov 2022 22:33:21 +0200 Subject: [PATCH] clean up some validation code --- framework/src/play/data/validation/Error.java | 12 ++++++++++ .../src/play/data/validation/ValidCheck.java | 9 +++---- .../data/validation/ValidationPlugin.java | 24 ++++++++++--------- 3 files changed, 28 insertions(+), 17 deletions(-) diff --git a/framework/src/play/data/validation/Error.java b/framework/src/play/data/validation/Error.java index e1c1dfde..e9a45c1e 100644 --- a/framework/src/play/data/validation/Error.java +++ b/framework/src/play/data/validation/Error.java @@ -1,9 +1,14 @@ package play.data.validation; +import net.sf.oval.ConstraintViolation; import play.i18n.Messages; +import javax.annotation.CheckReturnValue; +import javax.annotation.Nonnull; import java.util.Collection; +import static java.util.Collections.emptyList; + /** * A validation error */ @@ -56,4 +61,11 @@ public String toString() { String getMessageKey() { return message; } + + @Nonnull + @CheckReturnValue + static Error toValidationError(String key, ConstraintViolation violation) { + Collection variables = violation.getMessageVariables() == null ? emptyList() : violation.getMessageVariables().values(); + return new Error(key, violation.getMessage(), variables); + } } diff --git a/framework/src/play/data/validation/ValidCheck.java b/framework/src/play/data/validation/ValidCheck.java index 163baa9f..9e05b43e 100644 --- a/framework/src/play/data/validation/ValidCheck.java +++ b/framework/src/play/data/validation/ValidCheck.java @@ -13,6 +13,7 @@ import java.util.List; import static java.util.Collections.emptyList; +import static play.data.validation.Error.toValidationError; public class ValidCheck extends AbstractAnnotationCheck { @@ -63,7 +64,7 @@ public boolean isSatisfied(Object validatedObject, Object value, OValContext con boolean validateObject(String key, Object value) { ValidationPlugin.keys.get().put(value, key); List violations = new Validator().validate(value); - // + if (violations.isEmpty()) { return true; } else { @@ -71,15 +72,11 @@ boolean validateObject(String key, Object value) { if (violation.getContext() instanceof FieldContext) { FieldContext ctx = (FieldContext) violation.getContext(); String fkey = (key == null ? "" : key + ".") + ctx.getField().getName(); - Error error = new Error( - fkey, - violation.getMessage(), - violation.getMessageVariables() == null ? emptyList() : violation.getMessageVariables().values()); + Error error = toValidationError(fkey, violation); Validation.current().errors.add(error); } } return false; } } - } diff --git a/framework/src/play/data/validation/ValidationPlugin.java b/framework/src/play/data/validation/ValidationPlugin.java index 3f392692..93f21aed 100644 --- a/framework/src/play/data/validation/ValidationPlugin.java +++ b/framework/src/play/data/validation/ValidationPlugin.java @@ -36,6 +36,7 @@ import static java.nio.charset.StandardCharsets.UTF_8; import static java.util.Collections.emptyList; +import static play.data.validation.Error.toValidationError; public class ValidationPlugin extends PlayPlugin { @@ -56,14 +57,7 @@ public void beforeInvocation() { public void beforeActionInvocation(Request request, Response response, Session session, RenderArgs renderArgs, Scope.Flash flash, Method actionMethod) { Validation.current.set(restore(request)); - boolean verify = false; - for (Annotation[] annotations : actionMethod.getParameterAnnotations()) { - if (annotations.length > 0) { - verify = true; - break; - } - } - if (!verify) { + if (!needsValidation(actionMethod)) { return; } List violations = new Validator().validateAction(request, session, actionMethod); @@ -71,13 +65,21 @@ public void beforeActionInvocation(Request request, Response response, Session s String[] paramNames = Java.parameterNames(actionMethod); for (ConstraintViolation violation : violations) { String key = paramNames[((MethodParameterContext) violation.getContext()).getParameterIndex()]; - Collection variables = violation.getMessageVariables() == null ? emptyList() - : violation.getMessageVariables().values(); - errors.add(new Error(key, violation.getMessage(), variables)); + Error error = toValidationError(key, violation); + errors.add(error); } Validation.current.get().errors.addAll(errors); } + private boolean needsValidation(Method actionMethod) { + for (Annotation[] annotations : actionMethod.getParameterAnnotations()) { + if (annotations.length > 0) { + return true; + } + } + return false; + } + @Override public void onActionInvocationResult(@Nonnull Request request, @Nonnull Response response, @Nonnull Session session, @Nonnull RenderArgs renderArgs, Result result) { save(request, response);