{"version":3,"file":"validate-config.cjs","names":["CollectionRequiresConfigError","CollectionRequiresGetKeyError","InvalidGetKeyError","CollectionRequiresSyncConfigError","InvalidSyncConfigError","InvalidSyncFunctionError","InvalidCallbackOptionError","InvalidOptionTypeError"],"sources":["../../../src/collection/validate-config.ts"],"sourcesContent":["import {\n  CollectionRequiresConfigError,\n  CollectionRequiresSyncConfigError,\n} from '../errors'\nimport { devBuild } from '../error-message'\nimport {\n  CollectionRequiresGetKeyError,\n  InvalidCallbackOptionError,\n  InvalidGetKeyError,\n  InvalidOptionTypeError,\n  InvalidSyncConfigError,\n  InvalidSyncFunctionError,\n} from './config-errors'\n\n/**\n * Known top-level config properties for createCollection.\n * Extra properties are allowed; these names are only used for typo suggestions.\n */\nconst KNOWN_CONFIG_KEYS = new Set([\n  `id`,\n  `schema`,\n  `getKey`,\n  `sync`,\n  `gcTime`,\n  `startSync`,\n  `autoIndex`,\n  `defaultIndexType`,\n  `compare`,\n  `syncMode`,\n  `defaultStringCollation`,\n  `onInsert`,\n  `onUpdate`,\n  `onDelete`,\n  `utils`,\n  `singleResult`,\n  // Metadata returned by persistedCollectionOptions.\n  `persistence`,\n])\n\n// Limit suggestions to casing mistakes and adjacent swaps in longer names.\n// Broader edit-distance matching can mistake adapter fields for core options.\nfunction findLikelyTypo(\n  key: string,\n  config: Record<string, unknown>,\n): string | undefined {\n  for (const knownKey of KNOWN_CONFIG_KEYS) {\n    if (knownKey in config) continue\n    if (key.toLowerCase() === knownKey.toLowerCase()) return knownKey\n    if (key.length < 5 || key.length !== knownKey.length) continue\n\n    for (let i = 0; i < key.length - 1; i++) {\n      const swapped = key.slice(0, i) + key[i + 1] + key[i] + key.slice(i + 2)\n      if (swapped === knownKey) return knownKey\n    }\n  }\n  return undefined\n}\n\nfunction describeType(value: unknown): string {\n  if (value === null) return `null`\n  if (value === undefined) return `undefined`\n  if (Array.isArray(value)) return `an array`\n  return typeof value\n}\n\n/**\n * Validates the collection config at runtime, providing clear error messages\n * for common misconfiguration mistakes that would otherwise surface as\n * unreadable TypeScript errors.\n *\n * This runs before the config is passed to CollectionImpl, catching issues\n * early with actionable error messages.\n */\nexport function validateCollectionConfig(config: unknown): void {\n  // Check config exists and is an object\n  if (!config || typeof config !== `object` || Array.isArray(config)) {\n    throw new CollectionRequiresConfigError()\n  }\n\n  const configObj = config as Record<string, unknown>\n\n  // Adapter metadata is valid. Warn only when an extra key is likely a typo.\n  // The guard keeps the typo search and its text out of a CommonJS production\n  // bundle, which cannot drop this module.\n  if (devBuild() && process.env.NODE_ENV !== `production`)\n    for (const key of Object.keys(configObj)) {\n      if (KNOWN_CONFIG_KEYS.has(key) || key.startsWith(`_`)) continue\n      const suggestion = findLikelyTypo(key, configObj)\n      if (suggestion) {\n        console.warn(\n          `Possible misspelling in collection config: \"${key}\". Did you mean \"${suggestion}\"?`,\n        )\n      }\n    }\n\n  // Validate getKey\n  if (!(`getKey` in configObj) || configObj.getKey === undefined) {\n    throw new CollectionRequiresGetKeyError()\n  }\n  if (typeof configObj.getKey !== `function`) {\n    throw new InvalidGetKeyError(describeType(configObj.getKey))\n  }\n\n  // Validate sync\n  if (!configObj.sync) {\n    throw new CollectionRequiresSyncConfigError()\n  }\n  if (typeof configObj.sync !== `object` || Array.isArray(configObj.sync)) {\n    throw new InvalidSyncConfigError(describeType(configObj.sync))\n  }\n  const syncObj = configObj.sync as Record<string, unknown>\n  if (typeof syncObj.sync !== `function`) {\n    throw new InvalidSyncFunctionError(describeType(syncObj.sync))\n  }\n\n  // Validate callback options\n  const callbackOptions = [\n    `onInsert`,\n    `onUpdate`,\n    `onDelete`,\n    `compare`,\n  ] as const\n  for (const optionName of callbackOptions) {\n    if (\n      optionName in configObj &&\n      configObj[optionName] !== undefined &&\n      typeof configObj[optionName] !== `function`\n    ) {\n      throw new InvalidCallbackOptionError(\n        optionName,\n        describeType(configObj[optionName]),\n      )\n    }\n  }\n\n  // Validate id\n  if (`id` in configObj && configObj.id !== undefined) {\n    if (typeof configObj.id !== `string`) {\n      throw new InvalidOptionTypeError(\n        `id`,\n        `a string`,\n        describeType(configObj.id),\n      )\n    }\n  }\n\n  // Validate gcTime\n  if (`gcTime` in configObj && configObj.gcTime !== undefined) {\n    if (typeof configObj.gcTime !== `number`) {\n      throw new InvalidOptionTypeError(\n        `gcTime`,\n        `a number`,\n        describeType(configObj.gcTime),\n      )\n    }\n  }\n\n  // Validate startSync\n  if (`startSync` in configObj && configObj.startSync !== undefined) {\n    if (typeof configObj.startSync !== `boolean`) {\n      throw new InvalidOptionTypeError(\n        `startSync`,\n        `a boolean`,\n        describeType(configObj.startSync),\n      )\n    }\n  }\n\n  // Validate autoIndex\n  if (`autoIndex` in configObj && configObj.autoIndex !== undefined) {\n    if (configObj.autoIndex !== `off` && configObj.autoIndex !== `eager`) {\n      throw new InvalidOptionTypeError(\n        `autoIndex`,\n        `\"off\" or \"eager\"`,\n        String(configObj.autoIndex),\n      )\n    }\n  }\n\n  // Validate syncMode\n  if (`syncMode` in configObj && configObj.syncMode !== undefined) {\n    if (configObj.syncMode !== `eager` && configObj.syncMode !== `on-demand`) {\n      throw new InvalidOptionTypeError(\n        `syncMode`,\n        `\"eager\" or \"on-demand\"`,\n        String(configObj.syncMode),\n      )\n    }\n  }\n\n  // Validate utils\n  if (`utils` in configObj && configObj.utils !== undefined) {\n    if (\n      typeof configObj.utils !== `object` ||\n      configObj.utils === null ||\n      Array.isArray(configObj.utils)\n    ) {\n      throw new InvalidOptionTypeError(\n        `utils`,\n        `an object`,\n        describeType(configObj.utils),\n      )\n    }\n  }\n}\n"],"mappings":";;;AAkBA,IAAM,oCAAoB,IAAI,IAAI;CAChC;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CAEA;AACF,CAAC;AAID,SAAS,eACP,KACA,QACoB;CACpB,KAAA,MAAW,YAAY,mBAAmB;EACxC,IAAI,YAAY,QAAQ;EACxB,IAAI,IAAI,YAAY,MAAM,SAAS,YAAY,GAAG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