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

export interface IQuantityDTO {
  amount: number | null;
  scale: number;
}

export function ensureQuantityDTOIsValid(quantity: IQuantityRequestDTO) {
  const schema = Joi.object({
    amount: Joi.number(),
    scale: Joi.number(),
  });

  const result = RuleValidator.validate<IQuantityRequestDTO>(schema, quantity);

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

  return Result.ok(true);
}

export interface IQuantityRequestDTO extends IQuantityDTO {}
export interface IQuantityResponseDTO extends IQuantityDTO {}
