Skip to content

Commit

Permalink
clean up some validation code
Browse files Browse the repository at this point in the history
  • Loading branch information
asolntsev committed Nov 1, 2022
1 parent d59c0e0 commit 503dae6
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 17 deletions.
12 changes: 12 additions & 0 deletions framework/src/play/data/validation/Error.java
Original file line number Diff line number Diff line change
@@ -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
*/
Expand Down Expand Up @@ -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);
}
}
9 changes: 3 additions & 6 deletions framework/src/play/data/validation/ValidCheck.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Required> {

Expand Down Expand Up @@ -63,23 +64,19 @@ public boolean isSatisfied(Object validatedObject, Object value, OValContext con
boolean validateObject(String key, Object value) {
ValidationPlugin.keys.get().put(value, key);
List<ConstraintViolation> violations = new Validator().validate(value);
//

if (violations.isEmpty()) {
return true;
} else {
for (ConstraintViolation violation : violations) {
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;
}
}

}
24 changes: 13 additions & 11 deletions framework/src/play/data/validation/ValidationPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -56,28 +57,29 @@ 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<ConstraintViolation> violations = new Validator().validateAction(request, session, actionMethod);
List<Error> errors = new ArrayList<>();
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);
Expand Down

0 comments on commit 503dae6

Please sign in to comment.