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

export interface IMoney_DTO {
  amount: number | null;
  scale: number;
  currency_code: string;
}

export interface IMoney_Request_DTO extends IMoney_DTO {}
export interface IMoney_Response_DTO extends IMoney_DTO {}

export function ensureMoney_DTOIsValid(money: IMoney_Request_DTO) {
  const schema = Joi.object({
    amount: Joi.number(),
    scale: Joi.number(),
    currency_code: Joi.string(),
  });

  const result = RuleValidator.validate<IMoney_Request_DTO>(schema, money);

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

  return Result.ok(true);
}
