{
  "version": 3,
  "sources": ["index.js"],
  "sourcesContent": ["// Copyright 2026 will Farrell, and datastream contributors.\n// SPDX-License-Identifier: MIT\nimport { createTransformStream } from \"@datastream/core\";\nimport { compile } from \"ajv-cmd\";\n\nconst ajvDefaults = {\n\tstrict: true,\n\tcoerceTypes: true,\n\tallErrors: true,\n\tuseDefaults: \"empty\",\n\tmessages: true, // needs to be true to allow multi-locale errorMessage to work\n};\n\n// This is pulled out due to it's performance cost (50-100ms on cold start)\n// Precompile your schema during a build step is recommended.\nexport const transpileSchema = (schema, ajvOptions) => {\n\tconst options = { ...ajvDefaults, ...ajvOptions };\n\treturn compile(schema, options);\n};\n\nexport const validateStream = (\n\t{\n\t\tschema,\n\t\tidxStart,\n\t\tonErrorEnqueue,\n\t\tallowCoerceTypes,\n\t\tresultKey,\n\t\tmaxErrorRows = Infinity,\n\t},\n\tstreamOptions = {},\n) => {\n\tidxStart ??= 0;\n\n\tif (typeof schema !== \"function\") {\n\t\tschema = transpileSchema(schema);\n\t}\n\n\tconst value = {}; // aka errors\n\tlet idx = idxStart - 1;\n\tconst transform = (chunk, enqueue) => {\n\t\tidx += 1;\n\t\tconst data =\n\t\t\tallowCoerceTypes === false ? structuredClone(chunk) : undefined;\n\t\tconst chunkValid = schema(chunk);\n\t\tif (!chunkValid) {\n\t\t\tfor (const error of schema.errors) {\n\t\t\t\tconst { id, keys, message } = processError(error);\n\n\t\t\t\tif (!value[id]) {\n\t\t\t\t\tvalue[id] = { id, keys, message, idx: [] };\n\t\t\t\t}\n\t\t\t\tif (value[id].idx.length < maxErrorRows) {\n\t\t\t\t\tvalue[id].idx.push(idx);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif (chunkValid || onErrorEnqueue) {\n\t\t\tenqueue(data ?? chunk);\n\t\t}\n\t};\n\tconst stream = createTransformStream(transform, streamOptions);\n\tstream.result = () => ({ key: resultKey ?? \"validate\", value });\n\treturn stream;\n};\n\nconst processError = (error) => {\n\tconst message = error.message || \"\";\n\n\tlet id = error.schemaPath;\n\n\tlet keys = [];\n\tif (error.keyword === \"errorMessage\") {\n\t\terror.params.errors.forEach((error) => {\n\t\t\tconst value = makeKeys(error);\n\t\t\tif (value) keys.push(value);\n\t\t});\n\t\tkeys = [...new Set(keys.sort())];\n\t} else {\n\t\tkeys.push(makeKeys(error));\n\t}\n\tif (!error.instancePath && keys.length) {\n\t\tid += `/${keys.join(\"|\")}`;\n\t}\n\treturn { id, keys, message };\n};\n\nconst makeKeys = (error) => {\n\t// deps groups columns that are related in anyOf/oneOf.\n\t/* error.params.deps ?? */\n\treturn (\n\t\terror.params.missingProperty ||\n\t\terror.params.additionalProperty ||\n\t\terror.instancePath.replace(\"/\", \"\")\n\t);\n};\n\nexport default validateStream;\n"],
  "mappings": "AAEA,SAAS,6BAA6B;AACtC,SAAS,eAAe;AAExB,MAAM,cAAc;AAAA,EACnB,QAAQ;AAAA,EACR,aAAa;AAAA,EACb,WAAW;AAAA,EACX,aAAa;AAAA,EACb,UAAU;AAAA;AACX;AAIO,MAAM,kBAAkB,CAAC,QAAQ,eAAe;AACtD,QAAM,UAAU,EAAE,GAAG,aAAa,GAAG,WAAW;AAChD,SAAO,QAAQ,QAAQ,OAAO;AAC/B;AAEO,MAAM,iBAAiB,CAC7B;AAAA,EACC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA,eAAe;AAChB,GACA,gBAAgB,CAAC,MACb;AACJ,eAAa;AAEb,MAAI,OAAO,WAAW,YAAY;AACjC,aAAS,gBAAgB,MAAM;AAAA,EAChC;AAEA,QAAM,QAAQ,CAAC;AACf,MAAI,MAAM,WAAW;AACrB,QAAM,YAAY,CAAC,OAAO,YAAY;AACrC,WAAO;AACP,UAAM,OACL,qBAAqB,QAAQ,gBAAgB,KAAK,IAAI;AACvD,UAAM,aAAa,OAAO,KAAK;AAC/B,QAAI,CAAC,YAAY;AAChB,iBAAW,SAAS,OAAO,QAAQ;AAClC,cAAM,EAAE,IAAI,MAA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