import Joi from "joi";
import { Result, RuleValidator } from "../../../common";

export interface ILogin_DTO {
  email: string;
  password: string;
}

export function ensureLogin_DTOIsValid(
  loginDTO: ILogin_DTO,
): Result<boolean, Error> {
  const schema = Joi.object({
    email: Joi.string().email().required(),
    password: Joi.string().min(4).alphanum().required(),
  });

  let result = RuleValidator.validate<ILogin_DTO>(schema, loginDTO);

  if (result.isFailure) {
    return Result.fail(result.error);
  }

  return Result.ok(true);
}
