All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ph.com.nightowlstudios.resource.Validators Maven / Gradle / Ivy

There is a newer version: 6.21.1
Show newest version
package ph.com.nightowlstudios.resource;

import io.netty.handler.codec.http.HttpResponseStatus;
import io.vertx.core.Handler;
import io.vertx.core.http.HttpServerRequest;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.RoutingContext;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * @author Joseph Harvey Angeles - @yev
 * @since 7/6/20
 */
public final class Validators {

  private Validators() {
  }

  public static Handler requiredBodyParams(String... params) {
    List> validators = Arrays
      .stream(params)
      .map(param -> new ValidatorImpl<>(
        RoutingContext::getBodyAsJson,
        body -> body.containsKey(param),
        HttpResponseStatus.PRECONDITION_REQUIRED))
      .collect(Collectors.toList());
    return ofList(validators);
  }

  public static Handler requiredRequestParams(String... params) {
    List> validators = Arrays
      .stream(params)
      .map(param -> new ValidatorImpl<>(
        RoutingContext::request,
        request -> Optional.ofNullable(request.getParam(param)).isPresent(),
        HttpResponseStatus.PRECONDITION_REQUIRED))
      .collect(Collectors.toList());
    return ofList(validators);
  }

  public static Handler stringEqualBodyParams(String paramOne, String paramTwo) {
    Predicate check = body -> body.getString(paramOne).equals(body.getString(paramTwo));
    return Validators.of(new ValidatorImpl<>(RoutingContext::getBodyAsJson, check, "Passwords do not match."));
  }

  public static  Handler of(Function requestMapper, Predicate predicate, String errorMessage) {
    return Validators.of(new ValidatorImpl<>(requestMapper, predicate, errorMessage));
  }

  public static  Handler of(Validator validator) {
    return ctx -> {
      if (!validator.predicate().test(validator.requestMapper().apply(ctx))) {
        ctx.response().setStatusCode(HttpResponseStatus.PRECONDITION_FAILED.code()).end(validator.errorMessage());
        ctx.fail(HttpResponseStatus.PRECONDITION_FAILED.code());
        return;
      }
      ctx.next();
    };
  }

  public static  Handler ofList(List> validators) {
    return ctx -> {
      for (Validator validator : validators) {
        if (!validator.predicate().test(validator.requestMapper().apply(ctx))) {
          ctx.fail(validator.responseStatus().code());
          return;
        }
      }
      ctx.next();
    };
  }

  static class ValidatorImpl implements Validator {
    private final Predicate predicate;
    private final Function requestMapper;
    private final HttpResponseStatus responseStatus;
    private String errorMessage;

    ValidatorImpl(Function mapper, Predicate predicate, String errorMessage) {
      this(mapper, predicate, HttpResponseStatus.PRECONDITION_FAILED);
      this.errorMessage = String.format("Error %d: %s", this.responseStatus.code(), errorMessage);
    }

    ValidatorImpl(Function mapper, Predicate predicate, HttpResponseStatus responseStatus) {
      this.requestMapper = mapper;
      this.predicate = predicate;
      this.responseStatus = responseStatus;
      this.errorMessage = responseStatus.reasonPhrase();
    }

    @Override
    public Predicate predicate() {
      return this.predicate;
    }

    @Override
    public Function requestMapper() {
      return this.requestMapper;
    }

    @Override
    public HttpResponseStatus responseStatus() {
      return this.responseStatus;
    }

    @Override
    public String errorMessage() {
      return this.errorMessage;
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy