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

ai.promoted.delivery.client.DefaultDeliveryRequestValidator Maven / Gradle / Ivy

There is a newer version: 3.2.0
Show newest version
package ai.promoted.delivery.client;

import java.util.ArrayList;
import java.util.List;
import ai.promoted.proto.delivery.Request;
import ai.promoted.proto.delivery.Insertion;
import ai.promoted.proto.event.CohortMembership;

/**
 * Implements the default delivery request validation logic.
 */
public class DefaultDeliveryRequestValidator implements DeliveryRequestValidator {

  public static final DefaultDeliveryRequestValidator INSTANCE =
      new DefaultDeliveryRequestValidator();

  /**
   * @see DeliveryRequestValidator#validate(DeliveryRequest)
   */
  public List validate(DeliveryRequest request) {
    List validationErrors = new ArrayList<>();

    Request.Builder reqBuilder = request.getRequestBuilder();
    if (reqBuilder == null) {
      validationErrors.add("Request builder must be set");
      return validationErrors;
    }

    // Check the ids.
    validationErrors.addAll(validateIds(request.getRequestBuilder(), request.getExperiment()));

    // Insertion start should be >= 0.
    if (request.getRetrievalInsertionOffset() < 0) {
      validationErrors.add("Insertion start must be greater or equal to 0");
    }

    return validationErrors;
  }

  private List validateIds(Request.Builder reqBuilder, CohortMembership experiment) {
    List validationErrors = new ArrayList<>();


    if (!reqBuilder.getRequestId().isBlank()) {
      validationErrors.add("Request.requestId should not be set");
    }

    if (!reqBuilder.hasUserInfo()) {
      validationErrors.add("Request.userInfo should be set");
    } else if (reqBuilder.getUserInfo().getAnonUserId().isBlank()) {
      validationErrors.add("Request.userInfo.anonUserId should be set");
    }

    for (Insertion ins : reqBuilder.getInsertionList()) {
      if (ins.getContentId().isBlank()) {
        validationErrors.add("Insertion.contentId should be set");
      }
    }

    return validationErrors;
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy