import { LenConstraint } from "../api/ServerApi";

/**
 * Check if a constraint was respected or not
 *
 * @returns An error message appropriate for the constraint
 * violation, if any, or undefined otherwise
 */
export function checkConstraint(
  constraint: LenConstraint,
  value: string | undefined
): string | undefined {
  value = value ?? "";
  if (value.length < constraint.min)
    return `Veuillez indiquer au moins ${constraint.min} caractères !`;

  if (value.length > constraint.max)
    return `Veuillez indiquer au maximum ${constraint.max} caractères !`;

  return undefined;
}
