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

com.cosium.hal_mock_mvc.ValidatedFormProperty Maven / Gradle / Ivy

The newest version!
package com.cosium.hal_mock_mvc;

import static java.util.Objects.requireNonNull;

import java.util.Map;
import java.util.Optional;
import org.springframework.lang.Nullable;

/**
 * @author Réda Housni Alaoui
 */
record ValidatedFormProperty(
    FormProperty property, @Nullable ValidationError firstValidationError) {

  public static  ValidatedFormProperty markAsValid(FormProperty formProperty) {
    return new ValidatedFormProperty<>(formProperty, null);
  }

  public static  DeferrableToServerSideValidationBuilder invalidBuilder(
      FormProperty formProperty) {
    return deferrableToServerSideValidation ->
        errorMessage ->
            new ValidatedFormProperty<>(
                formProperty, new ValidationError(errorMessage, deferrableToServerSideValidation));
  }

  public ValidatedFormProperty populateRequestPayload(Map requestPayload) {
    property.populateRequestPayload(requestPayload);
    return this;
  }

  public Optional serverSideVerifiableErrorMessage() {
    return Optional.ofNullable(firstValidationError)
        .filter(ValidationError::serverSideVerifiable)
        .map(ValidationError::reason);
  }

  public  ValidatedFormProperty mapTo(FormProperty formProperty) {
    return new ValidatedFormProperty<>(formProperty, firstValidationError);
  }

  record ValidationError(String reason, boolean serverSideVerifiable) {
    ValidationError {
      requireNonNull(reason);
    }
  }

  interface DeferrableToServerSideValidationBuilder {
    ErrorMessageBuilder serverSideVerifiable(boolean serverSideVerifiable);
  }

  interface ErrorMessageBuilder {
    ValidatedFormProperty reason(String reason);
  }
}