{"version":3,"sources":["../src/internal/utils.ts"],"sourcesContent":["/**\n * @file Util functions for validators\n * @name utils.js\n * @author Nick Krause\n * @license MIT\n */\nimport { ValidationCheck, ValidationM, ValidationRuleset } from '../types'\n\nclass ValidationError extends Error {}\n\n/**\n * id function - returns whatever its input is.\n * @param {Any} x - Any param supplied to this function.\n * @returns {Any} The param supplied to this function.\n */\nconst id = (x: any) => x\n\nconst pipeValidators: (\n  fns: ValidationRuleset,\n  values?: any\n) => ValidationCheck = (fns, values) => (value: any) => {\n  const [first, ...rest] = fns\n\n  const firstCheck = (\n    typeof first === `function` ? first(values) : first\n  ).check(value)\n\n  // starting with the first function that returns a monad,\n  // we chain through the rest of the functions\n  // in order to combine them all into a single check.\n  return rest.reduce(\n    (prevM, nextM) =>\n      // eslint-disable-next-line @typescript-eslint/unbound-method\n      prevM.chain(\n        typeof nextM === `function` ? nextM(values)[`check`] : nextM.check,\n      ),\n    firstCheck,\n  )\n}\n\n/**\n * returnConstant is a convenience method that can be used as an argument to functions that require a function,\n * but ultimately do nothing but return a primitive value.\n * @param {Any} x - Primitive value to be returned.\n */\nfunction returnConstant<T>(x: T): () => T {\n  return () => x\n}\n\n/**\n * a customized error message to catch incorrect types in Validator.using\n * @param {Function} fn - The incorrect function supplied to using().\n * @returns {String} - A tailored error message that attempts to pinpoint the error.\n */\nconst validationErrorMessage = (fn: (v: any) => any): string => {\n  return `\nChaining validation only works if every function has a .chain() method that takes a Success or Fail object.\nCheck the type of ${fn.constructor.name} - was it written using createRule?\n`\n}\n\n/**\n * Function that checks whether the supplied validator is, itself, valid.\n * @param {Function} validator - Parameter description.\n * @throws {Exception Type} Exception description.\n */\nconst checkIsValidationM = (validator: ValidationM): void => {\n  if (\n    !(\n      validator.hasOwnProperty(`value`) && validator.hasOwnProperty(`isSuccess`)\n    )\n  ) {\n    throw new ValidationError(validationErrorMessage(validator as any))\n  }\n}\n\n/**\n * Function that takes an object with field names and rules, then applies those rules to the fields\n * of another object with the same field names. Great for validation of entire objects at once, like\n * forms or API responses.\n */\n\nconst validateObject =\n  <Rules extends Record<string, ValidationRuleset>>(fieldRules: Rules) =>\n  <Vals extends Record<keyof Rules, any>>(\n      values: Vals,\n    ): {\n    values: Vals\n    hasErrors: boolean\n    errors: Partial<Record<keyof Rules, string[]>>\n  } => {\n    const errors = Object.keys(fieldRules).reduce((errs, fieldName) => {\n      if (!(fieldName in values)) {\n        throw new Error(\n          `Field ${fieldName} is not in the object being validated`,\n        )\n      }\n\n      const applyFieldChecks: ValidationCheck = pipeValidators(\n        fieldRules[fieldName],\n        values,\n      )\n      const checkResults: ValidationM = applyFieldChecks(\n        values[fieldName],\n        values,\n      )\n\n      if (!checkResults.isSuccess) {\n        errs[fieldName] = checkResults.errors\n      }\n\n      return errs\n    }, {})\n\n    return {\n      values,\n      hasErrors: Object.keys(errors).length > 0,\n      errors,\n    }\n  }\n\nexport {\n  checkIsValidationM,\n  id,\n  pipeValidators,\n  returnConstant,\n  validateObject,\n  ValidationError,\n}\n"],"mappings":";AAAA,AAQA,oCAA8B,MAAM;AAAC;AAOrC,IAAM,KAAK,CAAC,MAAW;AAEvB,IAAM,iBAGiB,CAAC,KAAK,WAAW,CAAC,UAAe;AACtD,QAAM,CAAC,UAAU,QAAQ;AAEzB,QAAM,aACJ,QAAO,UAAU,aAAa,MAAM,MAAM,IAAI,OAC9C,MAAM,KAAK;AAKb,SAAO,KAAK,OACV,CAAC,OAAO,UAEN,MAAM,MACJ,OAAO,UAAU,aAAa,MAAM,MAAM,EAAE,WAAW,MAAM,KAC/D,GACF,UACF;AACF;AAOA,wBAA2B,GAAe;AACxC,SAAO,MAAM;AACf;AAOA,IAAM,yBAAyB,CAAC,OAAgC;AAC9D,SAAO;AAAA;AAAA,oBAEW,GAAG,YAAY;AAAA;AAEnC;AAOA,IAAM,qBAAqB,CAAC,cAAiC;AAC3D,MACE,CACE,WAAU,eAAe,OAAO,KAAK,UAAU,eAAe,WAAW,IAE3E;AACA,UAAM,IAAI,gBAAgB,uBAAuB,SAAgB,CAAC;AAAA,EACpE;AACF;AAQA,IAAM,iBACJ,CAAkD,eAClD,CACI,WAKC;AACH,QAAM,SAAS,OAAO,KAAK,UAAU,EAAE,OAAO,CAAC,MAAM,cAAc;AACjE,QAAI,CAAE,cAAa,SAAS;AAC1B,YAAM,IAAI,MACR,SAAS,gDACX;AAAA,IACF;AAEA,UAAM,mBAAoC,eACxC,WAAW,YACX,MACF;AACA,UAAM,eAA4B,iBAChC,OAAO,YACP,MACF;AAEA,QAAI,CAAC,aAAa,WAAW;AAC3B,WAAK,aAAa,aAAa;AAAA,IACjC;AAEA,WAAO;AAAA,EACT,GAAG,CAAC,CAAC;AAEL,SAAO;AAAA,IACL;AAAA,IACA,WAAW,OAAO,KAAK,MAAM,EAAE,SAAS;AAAA,IACxC;AAAA,EACF;AACF;","names":[]}