{"version":3,"sources":["../src/validate.string.utils.ts","../src/validate.string.plugin.ts"],"names":["commonRegexPatterns","validateAndTransformString","value","config","transformedValue","error","pattern","txt","validateString","listener","recordHook","record","field","newValue"],"mappings":";;;;AAoBO,IAAMA,EAAsB,CACjC,KAAA,CAAO,4BACP,CAAA,KAAA,CAAO,qBACP,CAAA,GAAA,CAAK,gEACP,EAOO,SAASC,CACdC,CAAAA,CAAAA,CACAC,CACkB,CAAA,CAClB,IAAIC,CAAAA,CAAmBF,CACnBG,CAAAA,CAAAA,CAAuB,KAE3B,GAAI,CAACF,CAAO,CAAA,kBAAA,EAAsBD,EAAM,IAAK,EAAA,GAAM,EACjD,CAAA,OAAO,CAAE,KAAAA,CAAAA,CAAAA,CAAO,KAAO,CAAA,uBAAwB,CAGjD,CAAA,IAAMI,CACJ,CAAA,OAAOH,EAAO,OAAY,EAAA,QAAA,CACtBH,CAAoBG,CAAAA,CAAAA,CAAO,OAAO,CAClCA,CAAAA,CAAAA,CAAO,OACb,CAAA,GAAIG,GAAW,CAACA,CAAAA,CAAQ,IAAKJ,CAAAA,CAAK,CAChC,CAAA,OAAAG,CAAQF,CAAAA,CAAAA,CAAO,eAAe,OAAW,EAAA,gBAAA,CAClC,CAAE,KAAA,CAAAD,EAAO,KAAAG,CAAAA,CAAM,CAGxB,CAAA,GAAIF,EAAO,SAAaD,EAAAA,CAAAA,CAAM,MAASC,CAAAA,CAAAA,CAAO,SAC5C,CAAA,OAAAE,CACEF,CAAAA,CAAAA,CAAO,eAAe,MAAU,EAAA,CAAA,kBAAA,EAAqBA,CAAO,CAAA,SAAS,CAChE,CAAA,CAAA,CAAE,KAAAD,CAAAA,CAAAA,CAAO,MAAAG,CAAM,CAAA,CAGxB,GAAIF,CAAAA,CAAO,SAAaD,EAAAA,CAAAA,CAAM,MAASC,CAAAA,CAAAA,CAAO,UAC5C,OAAAE,CAAAA,CACEF,CAAO,CAAA,aAAA,EAAe,QAAU,CAAqBA,kBAAAA,EAAAA,CAAAA,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EAAe,MACtB,gDACK,CAAA,CAAE,KAAOC,CAAAA,CAAAA,CAAkB,KAAAC,CAAAA,CAAM,CAIrC,EAAA,CAAE,MAAOD,CAAkB,CAAA,KAAA,CAAO,IAAK,CAChD,CCtGO,SAASI,CAAeL,CAAAA,CAAAA,CAAgC,CAC7D,OAAQM,CAAAA,EAA+B,CACrCA,CAAAA,CAAS,GACPC,CAAAA,2BAAAA,CAAWP,CAAO,CAAA,SAAA,EAAa,KAAOQ,CAAW,EAAA,CAC/C,IAAWC,IAAAA,CAAAA,IAAST,EAAO,MAAQ,CAAA,CACjC,IAAMD,CAAAA,CAAQS,EAAO,GAAIC,CAAAA,CAAK,CAC9B,CAAA,GAAIV,CAAU,EAAA,IAAA,CAA6B,CACzC,GAAM,CAAE,KAAOW,CAAAA,CAAAA,CAAU,KAAAR,CAAAA,CAAM,EAAIJ,CACjCC,CAAAA,CAAAA,CACAC,CACF,CAAA,CACIE,GACFM,CAAO,CAAA,QAAA,CAASC,CAAOP,CAAAA,CAAK,CAE1BQ,CAAAA,CAAAA,GAAaX,CACfS,EAAAA,CAAAA,CAAO,IAAIC,CAAOC,CAAAA,CAAQ,EAE9B,CACF,CACA,OAAOF,CACT,CAAC,CACH,EACF,CACF","file":"index.browser.cjs","sourcesContent":["export interface StringValidationConfig {\n  fields: string[]\n  sheetSlug?: string\n  pattern?: keyof typeof commonRegexPatterns | RegExp\n  minLength?: number\n  maxLength?: number\n  exactLength?: number\n  caseType?: 'lowercase' | 'uppercase' | 'titlecase'\n  trim?: {\n    leading?: boolean\n    trailing?: boolean\n  }\n  emptyStringAllowed?: boolean\n  errorMessages?: {\n    pattern?: string\n    length?: string\n    case?: string\n    trim?: string\n  }\n}\nexport const commonRegexPatterns = {\n  email: /^[^\\s@]+@[^\\s@]+\\.[^\\s@]+$/,\n  phone: /^\\+?[\\d\\s-]{10,14}$/,\n  url: /^(https?:\\/\\/)?([\\da-z\\.-]+)\\.([a-z\\.]{2,6})([\\/\\w \\.-]*)*\\/?$/,\n}\n\nexport interface ValidationResult {\n  value: string\n  error: string | null\n}\n\nexport function validateAndTransformString(\n  value: string,\n  config: StringValidationConfig\n): ValidationResult {\n  let transformedValue = value\n  let error: string | null = null\n\n  if (!config.emptyStringAllowed && value.trim() === '') {\n    return { value, error: 'Field cannot be empty' }\n  }\n\n  const pattern =\n    typeof config.pattern === 'string'\n      ? commonRegexPatterns[config.pattern]\n      : config.pattern\n  if (pattern && !pattern.test(value)) {\n    error = config.errorMessages?.pattern || 'Invalid format'\n    return { value, error }\n  }\n\n  if (config.minLength && value.length < config.minLength) {\n    error =\n      config.errorMessages?.length || `Minimum length is ${config.minLength}`\n    return { value, error }\n  }\n\n  if (config.maxLength && value.length > config.maxLength) {\n    error =\n      config.errorMessages?.length || `Maximum length is ${config.maxLength}`\n    return { value, error }\n  }\n\n  if (config.exactLength && value.length !== config.exactLength) {\n    error =\n      config.errorMessages?.length ||\n      `Exact length must be ${config.exactLength}`\n    return { value, error }\n  }\n\n  if (config.caseType) {\n    switch (config.caseType) {\n      case 'lowercase':\n        transformedValue = value.toLowerCase()\n        break\n      case 'uppercase':\n        transformedValue = value.toUpperCase()\n        break\n      case 'titlecase':\n        transformedValue = value.replace(\n          /\\w\\S*/g,\n          (txt) => txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase()\n        )\n        break\n    }\n    if (value !== transformedValue) {\n      error =\n        config.errorMessages?.case ||\n        `Field value must be in ${config.caseType}`\n      return { value: transformedValue, error }\n    }\n  }\n\n  if (config.trim) {\n    if (config.trim.leading) {\n      transformedValue = transformedValue.trimStart()\n    }\n    if (config.trim.trailing) {\n      transformedValue = transformedValue.trimEnd()\n    }\n    if (value !== transformedValue) {\n      error =\n        config.errorMessages?.trim ||\n        'Field value has leading or trailing whitespace'\n      return { value: transformedValue, error }\n    }\n  }\n\n  return { value: transformedValue, error: null }\n}\n","import { FlatfileListener } from '@flatfile/listener'\nimport { recordHook } from '@flatfile/plugin-record-hook'\nimport {\n  StringValidationConfig,\n  validateAndTransformString,\n} from './validate.string.utils'\n\nexport function validateString(config: StringValidationConfig) {\n  return (listener: FlatfileListener) => {\n    listener.use(\n      recordHook(config.sheetSlug || '**', (record) => {\n        for (const field of config.fields) {\n          const value = record.get(field) as string\n          if (value !== null && value !== undefined) {\n            const { value: newValue, error } = validateAndTransformString(\n              value,\n              config\n            )\n            if (error) {\n              record.addError(field, error)\n            }\n            if (newValue !== value) {\n              record.set(field, newValue)\n            }\n          }\n        }\n        return record\n      })\n    )\n  }\n}\n\nexport default validateString\n"]}