{"version":3,"sources":["../../../src/api/records.ts"],"sourcesContent":["import type { PipeInput, PipeOutput, Pipe } from './base'\nimport { compileNested, context, schema, standard } from './base/pipes'\nimport { getRandomValue } from '../utils/functions'\n\ntype ObjectPipe<T extends Record<string, Pipe<any, any>>> = Pipe<{ [K in keyof T]: PipeInput<T[K]> }, { [K in keyof T]: PipeOutput<T[K]> }>\n\nconst objCompile: (branches: Record<string, Pipe<any, any>>) => Parameters<typeof standard>[0] =\n\t(branches) =>\n\t({ input, path }, opts) => {\n\t\tconst resVarname = `res_${getRandomValue()}`\n\t\tconst errorsVarname = `errors_${getRandomValue()}`\n\t\tconst validatedVarname = `validated_${getRandomValue()}`\n\t\treturn [\n\t\t\topts.wrapError(\n\t\t\t\t`typeof ${input} !== 'object' || ${input} === null || Array.isArray(${input})`,\n\t\t\t\t`PipeError.root('is not an object', ${input}, ${path})`,\n\t\t\t),\n\t\t\t`const ${resVarname} = {}`,\n\t\t\topts.failEarly ? '' : `const ${errorsVarname} = []`,\n\t\t\t`let ${validatedVarname}`,\n\t\t\t...Object.entries(branches).flatMap(([k, branch]) => [\n\t\t\t\t`${validatedVarname} = ${input}['${k}']`,\n\t\t\t\t...compileNested({ opts, pipe: branch, input: validatedVarname, key: k, errorType: 'assign' }),\n\t\t\t\topts.failEarly\n\t\t\t\t\t? opts.wrapError(`${validatedVarname} instanceof PipeError`, validatedVarname)\n\t\t\t\t\t: `if (${validatedVarname} instanceof PipeError) ${errorsVarname}.push(${validatedVarname})`,\n\t\t\t\t`${resVarname}['${k}'] = ${validatedVarname}`,\n\t\t\t]),\n\t\t\topts.failEarly ? '' : opts.wrapError(`${errorsVarname}.length`, `PipeError.rootFrom(${errorsVarname})`),\n\t\t\t`${input} = ${resVarname}`,\n\t\t]\n\t}\n\nexport const object = <T extends Record<string, Pipe<any, any>>>(branches: T) =>\n\tstandard<PipeInput<ObjectPipe<T>>, PipeOutput<ObjectPipe<T>>>(objCompile(branches), {\n\t\tschema: () => ({\n\t\t\ttype: 'object',\n\t\t\tproperties: Object.fromEntries(\n\t\t\t\tObject.entries(branches ?? {})\n\t\t\t\t\t.filter(([_, pipe]) => !context(pipe).jsonRedacted)\n\t\t\t\t\t.map(([key, pipe]) => [key, schema(pipe)]),\n\t\t\t),\n\t\t\trequired: Object.entries(branches ?? {})\n\t\t\t\t.filter(([, pipe]) => !context(pipe).optional && !context(pipe).jsonRedacted)\n\t\t\t\t.map(([key]) => key),\n\t\t\tadditionalProperties: false,\n\t\t}),\n\t\tcontext: { branches },\n\t})\n\nexport const objectPick = <T extends ObjectPipe<Record<string, Pipe<any, any>>>, S extends ReadonlyArray<keyof PipeInput<T> & string>>(\n\tbranch: T,\n\tkeys: S,\n) => {\n\tconst ctx = context(branch)\n\tconst sch = schema(branch)\n\tconst branches = Object.fromEntries(\n\t\tObject.entries((ctx.branches as Record<string, Pipe<any, any>>) ?? {}).filter(([key]) => keys.includes(key as S[number])),\n\t)\n\treturn standard<Pick<PipeInput<T>, S[number]>, Pick<PipeOutput<T>, S[number]>>(objCompile(branches), {\n\t\tschema: () => ({\n\t\t\t...sch,\n\t\t\tproperties: Object.fromEntries(Object.entries(sch.properties ?? {}).filter(([key]) => keys.includes(key as S[number]))),\n\t\t\trequired: (sch.required ?? []).filter((key) => keys.includes(key as S[number])),\n\t\t}),\n\t\tcontext: { ...ctx, branches },\n\t})\n}\n\nexport const objectOmit = <T extends ObjectPipe<Record<string, Pipe<any, any>>>, S extends ReadonlyArray<keyof PipeInput<T> & string>>(\n\tbranch: T,\n\tkeys: S,\n) => {\n\tconst ctx = context(branch)\n\tconst sch = schema(branch)\n\tconst branches = Object.fromEntries(\n\t\tObject.entries((ctx.branches as Record<string, Pipe<any, any>>) ?? {}).filter(([key]) => !keys.includes(key as S[number])),\n\t)\n\treturn standard<Omit<PipeInput<T>, S[number]>, Omit<PipeOutput<T>, S[number]>>(objCompile(branches), {\n\t\tschema: () => ({\n\t\t\t...sch,\n\t\t\tproperties: Object.fromEntries(Object.entries(sch.properties ?? {}).filter(([key]) => !keys.includes(key as S[number]))),\n\t\t\trequired: (sch.required ?? []).filter((key) => !keys.includes(key as S[number])),\n\t\t}),\n\t\tcontext: { ...ctx, branches },\n\t})\n}\n\nexport const record = <K extends Pipe<any, PropertyKey>, V extends Pipe<any, any>>(kPipe: K, vPipe: V) => {\n\tconst kValidatedVarname = `kValidated_${getRandomValue()}`\n\tconst vValidatedVarname = `vValidated_${getRandomValue()}`\n\tconst resVarname = `res_${getRandomValue()}`\n\tconst errorsVarname = `errors_${getRandomValue()}`\n\treturn standard<Record<PipeInput<K>, PipeInput<V>>, Record<PipeOutput<K>, PipeOutput<V>>>(\n\t\t({ input, path }, opts) => [\n\t\t\topts.wrapError(\n\t\t\t\t`typeof ${input} !== 'object' || ${input} === null || Array.isArray(${input})`,\n\t\t\t\t`PipeError.root(['is not an object'], ${input}, ${path})`,\n\t\t\t),\n\t\t\t`const ${resVarname} = {}`,\n\t\t\topts.failEarly ? '' : `const ${errorsVarname} = []`,\n\t\t\t`for (let [k, v] of Object.entries(${input})) {`,\n\t\t\t...[\n\t\t\t\t`let ${kValidatedVarname} = k`,\n\t\t\t\t`let ${vValidatedVarname} = v`,\n\t\t\t\t...compileNested({ opts, pipe: kPipe, input: kValidatedVarname, errorType: 'assign', key: kValidatedVarname }),\n\t\t\t\topts.failEarly\n\t\t\t\t\t? opts.wrapError(\n\t\t\t\t\t\t\t`${kValidatedVarname} instanceof PipeError`,\n\t\t\t\t\t\t\t`PipeError.path(k, '${kValidatedVarname}', ${kValidatedVarname})`,\n\t\t\t\t\t\t)\n\t\t\t\t\t: `\tif (${kValidatedVarname} instanceof PipeError) ${errorsVarname}.push(PipeError.path(k, '${kValidatedVarname}', ${kValidatedVarname}))`,\n\t\t\t\t...compileNested({ opts, pipe: vPipe, input: vValidatedVarname, errorType: 'assign', key: kValidatedVarname }),\n\t\t\t\topts.failEarly\n\t\t\t\t\t? opts.wrapError(\n\t\t\t\t\t\t\t`${vValidatedVarname} instanceof PipeError`,\n\t\t\t\t\t\t\t`PipeError.path(k, '${kValidatedVarname}', ${vValidatedVarname})`,\n\t\t\t\t\t\t)\n\t\t\t\t\t: `\tif (${vValidatedVarname} instanceof PipeError) ${errorsVarname}.push(PipeError.path(k, '${kValidatedVarname}', ${vValidatedVarname}))`,\n\t\t\t\t`\tif (!(${kValidatedVarname} instanceof PipeError) && !(${vValidatedVarname} instanceof PipeError)) ${resVarname}[${kValidatedVarname}] = ${vValidatedVarname};`,\n\t\t\t],\n\t\t\t`}`,\n\t\t\topts.failEarly ? '' : opts.wrapError(`${errorsVarname}.length`, `PipeError.rootFrom(${errorsVarname})`),\n\t\t\t`${input} = ${resVarname}`,\n\t\t],\n\t\t{\n\t\t\tschema: () => ({\n\t\t\t\ttype: 'object',\n\t\t\t\tpropertyNames: schema(kPipe),\n\t\t\t\tadditionalProperties: schema(vPipe),\n\t\t\t}),\n\t\t},\n\t)\n}\n\nexport const asMap = <K extends PropertyKey, V>() =>\n\tstandard<Record<K, V>, Map<K, V>>(({ input }) => [`${input} = new Map(Object.entries(${input}))`])\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AACA,mBAAyD;AACzD,uBAA+B;AAI/B,MAAM,aACL,CAAC,aACD,CAAC,EAAE,OAAO,KAAK,GAAG,SAAS;AAC1B,QAAM,aAAa,WAAO,iCAAe,CAAC;AAC1C,QAAM,gBAAgB,cAAU,iCAAe,CAAC;AAChD,QAAM,mBAAmB,iBAAa,iCAAe,CAAC;AACtD,SAAO;AAAA,IACN,KAAK;AAAA,MACJ,UAAU,KAAK,oBAAoB,KAAK,8BAA8B,KAAK;AAAA,MAC3E,sCAAsC,KAAK,KAAK,IAAI;AAAA,IACrD;AAAA,IACA,SAAS,UAAU;AAAA,IACnB,KAAK,YAAY,KAAK,SAAS,aAAa;AAAA,IAC5C,OAAO,gBAAgB;AAAA,IACvB,GAAG,OAAO,QAAQ,QAAQ,EAA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