// Path: src/main/java/net/nextpulse/jadmin/ColumnDefinition.java
// public class ColumnDefinition {
// 
//   /**
//    * Column name, case sensitive
//    */
//   private String name;
//   /**
//    * JAdmin-type of this column.
//    */
//   private ColumnType type;
// 
//   /**
//    * True if this column is part of the key identifying a row of the datasource containing this column.
//    */
//   private boolean keyColumn;
//   private boolean editable;
//   private List<InputValidationRule> validationRules = new ArrayList<>();
//   private InputTransformer inputTransformer;
//   private ColumnValueTransformer columnValueTransformer;
// 
//   public ColumnDefinition() {
//   }
// 
//   public ColumnDefinition(String name, ColumnType type) {
//     this.name = name;
//     this.type = type;
//   }
// 
//   public ColumnDefinition(String name, ColumnType type, boolean keyColumn, boolean editable) {
//     this.name = name;
//     this.type = type;
//     this.keyColumn = keyColumn;
//     this.editable = editable;
//   }
// 
//   public String getName() {
//     return name;
//   }
// 
//   public ColumnDefinition setName(String name) {
//     this.name = name;
//     return this;
//   }
// 
//   public ColumnType getType() {
//     return type;
//   }
// 
//   public void setType(ColumnType type) {
//     this.type = type;
//   }
// 
//   public boolean isKeyColumn() {
//     return keyColumn;
//   }
// 
//   public ColumnDefinition setKeyColumn(boolean keyColumn) {
//     this.keyColumn = keyColumn;
//     return this;
//   }
// 
//   public boolean isEditable() {
//     return editable;
//   }
// 
//   public ColumnDefinition setEditable(boolean editable) {
//     this.editable = editable;
//     return this;
//   }
// 
//   /**
//    * Adds the provided input validation rule to the list of rules.
//    *
//    * @param inputValidationRules  rule(s) that should be active for this column
//    */
//   public ColumnDefinition addValidationRules(InputValidationRule... inputValidationRules) {
//     validationRules.addAll(Arrays.asList(inputValidationRules));
//     return this;
//   }
//   
//   /**
//    * @return the configured validation rules
//    */
//   public List<InputValidationRule> getValidationRules() {
//     return validationRules;
//   }
//   
//   public InputTransformer getInputTransformer() {
//     return inputTransformer;
//   }
//   
//   public ColumnDefinition setInputTransformer(InputTransformer inputTransformer) {
//     this.inputTransformer = inputTransformer;
//     return this;
//   }
// 
//   public ColumnValueTransformer getColumnValueTransformer() {
//     return columnValueTransformer;
//   }
// 
//   public ColumnDefinition setColumnValueTransformer(ColumnValueTransformer columnValueTransformer) {
//     this.columnValueTransformer = columnValueTransformer;
//     return this;
//   }
// }


import net.nextpulse.jadmin.ColumnDefinition;
import spark.utils.StringUtils;
import java.util.function.Function;
import java.util.function.Supplier;
package net.nextpulse.jadmin.dsl;



/**
 * Functional interface that specifies a method that will validatePostData (and optionally transform) a user's input value.
 * If the user input is not valid, an exception is thrown, otherwise the (optionally transformed) user input is returned.
 *
 * @author yholkamp
 */
@SuppressWarnings("unused")
@FunctionalInterface
public interface InputValidationRule {
  
  /**
   * Validation rule that indicates a required field.
   */
  Supplier<InputValidationRule> REQUIRED = () -> (columnDefinition, userInput) -> {
    if (StringUtils.isBlank(userInput)) {
      throw new InvalidInputException(String.format("%s is empty but is required", columnDefinition.getName()));
    }
  };
  
  /**
   * Validation rule that indicates a minimum length
   */
  Function<Integer, InputValidationRule> MINIMUM_LENGTH = (minLength) -> (columnDefinition, userInput) -> {
    if (StringUtils.isBlank(userInput) || userInput.length() < minLength) {
      throw new InvalidInputException(String.format("%s is less than %d characters", columnDefinition.getName(), minLength));
    }
  };

  /**
   * Validation rule that indicates a minimum length or blank
   */
  Function<Integer, InputValidationRule> MINIMUM_LENGTH_OR_BLANK = (minLength) -> (columnDefinition, userInput) -> {
    if (!StringUtils.isBlank(userInput) && userInput.length() < minLength) {
      throw new InvalidInputException(String.format("%s is less than %d characters", columnDefinition.getName(), minLength));
    }
  };
  
  /**
   * Validation rule that indicates a minimum length
   */
  Function<Integer, InputValidationRule> MAXIMUM_LENGTH = (maxLength) -> (columnDefinition, userInput) -> {
    if (userInput.length() > maxLength) {
      throw new InvalidInputException(String.format("%s is more than %d characters long", columnDefinition.getName(), maxLength));
    }
  };
  
  /**
   * Method that will validatePostData (and optionally transform) a user's input value. If the user input is not valid, an
   * exception is thrown, otherwise the (optionally transformed) user input is returned.
   *
   * @param columnDefinition
   * @param userInput   raw (unsafe!) user data
   * @throws InvalidInputException  if the user input is invalid
   */
// Path: src/main/java/net/nextpulse/jadmin/ColumnDefinition.java
// public class ColumnDefinition {
// 
//   /**
//    * Column name, case sensitive
//    */
//   private String name;
//   /**
//    * JAdmin-type of this column.
//    */
//   private ColumnType type;
// 
//   /**
//    * True if this column is part of the key identifying a row of the datasource containing this column.
//    */
//   private boolean keyColumn;
//   private boolean editable;
//   private List<InputValidationRule> validationRules = new ArrayList<>();
//   private InputTransformer inputTransformer;
//   private ColumnValueTransformer columnValueTransformer;
// 
//   public ColumnDefinition() {
//   }
// 
//   public ColumnDefinition(String name, ColumnType type) {
//     this.name = name;
//     this.type = type;
//   }
// 
//   public ColumnDefinition(String name, ColumnType type, boolean keyColumn, boolean editable) {
//     this.name = name;
//     this.type = type;
//     this.keyColumn = keyColumn;
//     this.editable = editable;
//   }
// 
//   public String getName() {
//     return name;
//   }
// 
//   public ColumnDefinition setName(String name) {
//     this.name = name;
//     return this;
//   }
// 
//   public ColumnType getType() {
//     return type;
//   }
// 
//   public void setType(ColumnType type) {
//     this.type = type;
//   }
// 
//   public boolean isKeyColumn() {
//     return keyColumn;
//   }
// 
//   public ColumnDefinition setKeyColumn(boolean keyColumn) {
//     this.keyColumn = keyColumn;
//     return this;
//   }
// 
//   public boolean isEditable() {
//     return editable;
//   }
// 
//   public ColumnDefinition setEditable(boolean editable) {
//     this.editable = editable;
//     return this;
//   }
// 
//   /**
//    * Adds the provided input validation rule to the list of rules.
//    *
//    * @param inputValidationRules  rule(s) that should be active for this column
//    */
//   public ColumnDefinition addValidationRules(InputValidationRule... inputValidationRules) {
//     validationRules.addAll(Arrays.asList(inputValidationRules));
//     return this;
//   }
//   
//   /**
//    * @return the configured validation rules
//    */
//   public List<InputValidationRule> getValidationRules() {
//     return validationRules;
//   }
//   
//   public InputTransformer getInputTransformer() {
//     return inputTransformer;
//   }
//   
//   public ColumnDefinition setInputTransformer(InputTransformer inputTransformer) {
//     this.inputTransformer = inputTransformer;
//     return this;
//   }
// 
//   public ColumnValueTransformer getColumnValueTransformer() {
//     return columnValueTransformer;
//   }
// 
//   public ColumnDefinition setColumnValueTransformer(ColumnValueTransformer columnValueTransformer) {
//     this.columnValueTransformer = columnValueTransformer;
//     return this;
//   }
// }


// Path: src/main/java/net/nextpulse/jadmin/dsl/InputValidationRule.java
import net.nextpulse.jadmin.ColumnDefinition;
import spark.utils.StringUtils;
import java.util.function.Function;
import java.util.function.Supplier;

package net.nextpulse.jadmin.dsl;



/**
 * Functional interface that specifies a method that will validatePostData (and optionally transform) a user's input value.
 * If the user input is not valid, an exception is thrown, otherwise the (optionally transformed) user input is returned.
 *
 * @author yholkamp
 */
@SuppressWarnings("unused")
@FunctionalInterface
public interface InputValidationRule {
  
  /**
   * Validation rule that indicates a required field.
   */
  Supplier<InputValidationRule> REQUIRED = () -> (columnDefinition, userInput) -> {
    if (StringUtils.isBlank(userInput)) {
      throw new InvalidInputException(String.format("%s is empty but is required", columnDefinition.getName()));
    }
  };
  
  /**
   * Validation rule that indicates a minimum length
   */
  Function<Integer, InputValidationRule> MINIMUM_LENGTH = (minLength) -> (columnDefinition, userInput) -> {
    if (StringUtils.isBlank(userInput) || userInput.length() < minLength) {
      throw new InvalidInputException(String.format("%s is less than %d characters", columnDefinition.getName(), minLength));
    }
  };

  /**
   * Validation rule that indicates a minimum length or blank
   */
  Function<Integer, InputValidationRule> MINIMUM_LENGTH_OR_BLANK = (minLength) -> (columnDefinition, userInput) -> {
    if (!StringUtils.isBlank(userInput) && userInput.length() < minLength) {
      throw new InvalidInputException(String.format("%s is less than %d characters", columnDefinition.getName(), minLength));
    }
  };
  
  /**
   * Validation rule that indicates a minimum length
   */
  Function<Integer, InputValidationRule> MAXIMUM_LENGTH = (maxLength) -> (columnDefinition, userInput) -> {
    if (userInput.length() > maxLength) {
      throw new InvalidInputException(String.format("%s is more than %d characters long", columnDefinition.getName(), maxLength));
    }
  };
  
  /**
   * Method that will validatePostData (and optionally transform) a user's input value. If the user input is not valid, an
   * exception is thrown, otherwise the (optionally transformed) user input is returned.
   *
   * @param columnDefinition
   * @param userInput   raw (unsafe!) user data
   * @throws InvalidInputException  if the user input is invalid
   */
  void validate(ColumnDefinition columnDefinition, String userInput) throws InvalidInputException;