import { isObject } from "@/helper/utils";

// TODO 简单判断字段空值
const useValidate = <T extends Record<string, any>>(fields: T): T => {
  const flagArray: boolean[] = [];
  Object.keys(fields).forEach((field) => {
    if (isObject(fields[field])) useValidate(fields[field]);
    else flagArray.push(!!fields[field]);
  });

  const hasEmpty = flagArray.some((flag) => !flag);

  if (hasEmpty) throw new Error("字段不能为空");

  return fields as T;
};

export default useValidate;
