valleyed 4.5.16 → 4.5.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +7 -0
- package/dist/cjs/api/junctions.cjs +4 -4
- package/dist/cjs/api/junctions.cjs.map +1 -1
- package/dist/cjs/api/junctions.min.cjs +1 -1
- package/dist/cjs/api/junctions.min.cjs.map +1 -1
- package/dist/esm/api/junctions.min.mjs +1 -1
- package/dist/esm/api/junctions.min.mjs.map +1 -1
- package/dist/esm/api/junctions.mjs +4 -4
- package/dist/esm/api/junctions.mjs.map +1 -1
- package/dist/types/api/junctions.js +4 -4
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,13 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
|
|
4
4
|
|
|
5
|
+
### [4.5.17](https://github.com/kevinand11/valleyed/compare/v4.5.16...v4.5.17) (2025-08-12)
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
### Bug Fixes
|
|
9
|
+
|
|
10
|
+
* fn not defined in v.recursive ([54cc05e](https://github.com/kevinand11/valleyed/commit/54cc05ea9e7b8ba503160a5b890c3859aa32e2af))
|
|
11
|
+
|
|
5
12
|
### [4.5.16](https://github.com/kevinand11/valleyed/compare/v4.5.15...v4.5.16) (2025-08-09)
|
|
6
13
|
|
|
7
14
|
|
|
@@ -131,8 +131,8 @@ const recursive = (pipeFn, $refId) => {
|
|
|
131
131
|
return (0, import_pipes.standard)(
|
|
132
132
|
({ input }, opts) => {
|
|
133
133
|
const common = [`${input} = ${fnVarname}(${input})`, opts.wrapError(`${input} instanceof PipeError`, input)];
|
|
134
|
-
|
|
135
|
-
compiledBefore
|
|
134
|
+
compiledBefore = !compiledBefore;
|
|
135
|
+
if (!compiledBefore) return common;
|
|
136
136
|
return [
|
|
137
137
|
`function ${fnVarname}(node) {`,
|
|
138
138
|
...(0, import_pipes.compileNested)({ opts: { ...opts, failEarly: true }, pipe: pipeFn(), input: "node", errorType: "return" }).map(
|
|
@@ -145,8 +145,8 @@ const recursive = (pipeFn, $refId) => {
|
|
|
145
145
|
},
|
|
146
146
|
{
|
|
147
147
|
schema: () => {
|
|
148
|
-
|
|
149
|
-
schemedBefore
|
|
148
|
+
schemedBefore = !schemedBefore;
|
|
149
|
+
if (!schemedBefore) return { $refId };
|
|
150
150
|
return (0, import_pipes.schema)(pipeFn(), { $refId });
|
|
151
151
|
}
|
|
152
152
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/api/junctions.ts"],"sourcesContent":["import { Pipe, PipeInput, PipeOutput } from './base'\nimport { merge as differMerge } from '../utils/differ'\nimport { getRandomValue, wrapInTryCatch } from '../utils/functions'\nimport { compileNested, context, standard, schema, define, validate } from './base/pipes'\n\nexport const or = <T extends Pipe<any, any>[]>(branches: T) => {\n\tconst validatedVarname = `validated_${getRandomValue()}`\n\tconst errorsVarname = `errors_${getRandomValue()}`\n\treturn standard<PipeInput<T[number]>, PipeOutput<T[number]>>(\n\t\t({ input }, opts) =>\n\t\t\tbranches.length === 0\n\t\t\t\t? []\n\t\t\t\t: [\n\t\t\t\t\t\t`const ${errorsVarname} = []`,\n\t\t\t\t\t\t`let ${validatedVarname}`,\n\t\t\t\t\t\t...branches\n\t\t\t\t\t\t\t.map((branch, idx) => (lines: string[]) => [\n\t\t\t\t\t\t\t\t`${validatedVarname} = ${input}`,\n\t\t\t\t\t\t\t\t...compileNested({\n\t\t\t\t\t\t\t\t\topts: { ...opts, failEarly: true },\n\t\t\t\t\t\t\t\t\tpipe: branch,\n\t\t\t\t\t\t\t\t\tinput: validatedVarname,\n\t\t\t\t\t\t\t\t\terrorType: 'assign',\n\t\t\t\t\t\t\t\t\tkey: validatedVarname,\n\t\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t\t\t`if (${validatedVarname} instanceof PipeError) {`,\n\t\t\t\t\t\t\t\t`\t${errorsVarname}.push(PipeError.path(${idx}, '${validatedVarname}', ${validatedVarname}))`,\n\t\t\t\t\t\t\t\t...lines.map((l) => `\t${l}`),\n\t\t\t\t\t\t\t\tidx === branches.length - 1 ? `\t${opts.wrapError.format(`PipeError.rootFrom(${errorsVarname})`)}` : '',\n\t\t\t\t\t\t\t\t`}`,\n\t\t\t\t\t\t\t\t`else ${input} = ${validatedVarname}`,\n\t\t\t\t\t\t\t])\n\t\t\t\t\t\t\t.reduceRight<string[]>((acc, cur) => cur(acc), []),\n\t\t\t\t\t\topts.wrapError(`${input} instanceof PipeError`, input),\n\t\t\t\t\t],\n\t\t{\n\t\t\tschema: () => ({ oneOf: branches.map((branch) => schema(branch)) }),\n\t\t},\n\t)\n}\n\nexport const merge = <T1 extends Pipe<any, any>, T2 extends Pipe<any, any>>(branch1: T1, branch2: T2) => {\n\tconst inputVarname = `input_${getRandomValue()}`\n\treturn standard<PipeInput<T1> & PipeInput<T2>, PipeOutput<T1> & PipeOutput<T2>>(\n\t\t({ input, context }, opts) => [\n\t\t\t`let ${inputVarname}A = ${input}`,\n\t\t\t`let ${inputVarname}B = ${input}`,\n\t\t\t...compileNested({ opts, pipe: branch1, input: `${inputVarname}A` }),\n\t\t\topts.wrapError(`${inputVarname}A instanceof PipeError`, `${inputVarname}A`),\n\t\t\t...compileNested({ opts, pipe: branch2, input: `${inputVarname}B` }),\n\t\t\topts.wrapError(`${inputVarname}B instanceof PipeError`, `${inputVarname}B`),\n\t\t\t`${input} = ${context}.differMerge(${inputVarname}A, ${inputVarname}B)`,\n\t\t],\n\t\t{\n\t\t\tcontext: { differMerge },\n\t\t\tschema: () => ({ allOf: [schema(branch1), schema(branch2)] }),\n\t\t},\n\t)\n}\n\nexport const discriminate = <T extends Record<PropertyKey, Pipe<any, any>>>(\n\tdiscriminator: (val: PipeInput<T[keyof T]>) => PropertyKey,\n\tbranches: T,\n\terr = 'doesnt match any of the schema',\n) =>\n\tstandard<PipeInput<T[keyof T]>, PipeOutput<T[keyof T]>>(\n\t\t({ input, context, path }, opts) => [\n\t\t\t`switch (${context}.wrapInTryCatch(() => ${context}.discriminator(${input}))) {`,\n\t\t\t...Object.entries(branches).flatMap(([key, branch]) => [\n\t\t\t\t`\tcase ('${key}'): {`,\n\t\t\t\t...compileNested({ opts, pipe: branch, input }).map((l) => `\t\t${l}`),\n\t\t\t\t`\t\tbreak`,\n\t\t\t\t`\t}`,\n\t\t\t]),\n\t\t\t`\tdefault: ${opts.wrapError.format(`PipeError.root(\"${err}\", ${input}, ${path})`)}`,\n\t\t\t`}`,\n\t\t],\n\t\t{\n\t\t\tcontext: { wrapInTryCatch, discriminator },\n\t\t\tschema: () => ({ oneOf: Object.values(branches).map((s) => schema(s)) }),\n\t\t},\n\t)\n\nexport const fromJson = <T extends Pipe<any, any>>(branch: T) => {\n\tconst validatedVarname = `validated_${getRandomValue()}`\n\treturn standard<PipeInput<T>, PipeOutput<T>>(\n\t\t({ input, context }, opts) => [\n\t\t\t`let ${validatedVarname} = ${input}`,\n\t\t\t...compileNested({ opts, pipe: branch, input: validatedVarname, errorType: 'assign' }),\n\t\t\t`if (${validatedVarname} instanceof PipeError) {`,\n\t\t\topts.wrapError(`${input}?.constructor?.name !== 'String'`, validatedVarname),\n\t\t\t`\t${validatedVarname} = ${context}.wrapInTryCatch(() => JSON.parse(${input}), ${validatedVarname})`,\n\t\t\topts.wrapError(`${validatedVarname} instanceof PipeError`, validatedVarname),\n\t\t\t...compileNested({ opts, pipe: branch, input: validatedVarname, errorType: 'assign' }).map((l) => `\t${l}`),\n\t\t\topts.wrapError(`${validatedVarname} instanceof PipeError`, validatedVarname),\n\t\t\t`}`,\n\t\t\t`${input} = ${validatedVarname}`,\n\t\t],\n\t\t{\n\t\t\tcontext: { ...context(branch), wrapInTryCatch },\n\t\t\tschema: branch.schema,\n\t\t},\n\t)\n}\n\nexport const lazy = <T extends Pipe<any, any>>(pipeFn: () => T) =>\n\tdefine<PipeInput<T>, PipeOutput<T>>(\n\t\t(input) => {\n\t\t\tconst result = validate(pipeFn(), input)\n\t\t\treturn result.valid ? result.value : result.error\n\t\t},\n\t\t{\n\t\t\tschema: () => schema(pipeFn()),\n\t\t},\n\t)\n\nexport const recursive = <T extends Pipe<any, any>>(pipeFn: () => T, $refId: string) => {\n\tconst fnVarname = `fn_${getRandomValue()}`\n\tlet compiledBefore = false\n\tlet schemedBefore = false\n\treturn standard<PipeInput<T>, PipeOutput<T>>(\n\t\t({ input }, opts) => {\n\t\t\tconst common = [`${input} = ${fnVarname}(${input})`, opts.wrapError(`${input} instanceof PipeError`, input)]\n\t\t\
|
|
1
|
+
{"version":3,"sources":["../../../src/api/junctions.ts"],"sourcesContent":["import { Pipe, PipeInput, PipeOutput } from './base'\nimport { merge as differMerge } from '../utils/differ'\nimport { getRandomValue, wrapInTryCatch } from '../utils/functions'\nimport { compileNested, context, standard, schema, define, validate } from './base/pipes'\n\nexport const or = <T extends Pipe<any, any>[]>(branches: T) => {\n\tconst validatedVarname = `validated_${getRandomValue()}`\n\tconst errorsVarname = `errors_${getRandomValue()}`\n\treturn standard<PipeInput<T[number]>, PipeOutput<T[number]>>(\n\t\t({ input }, opts) =>\n\t\t\tbranches.length === 0\n\t\t\t\t? []\n\t\t\t\t: [\n\t\t\t\t\t\t`const ${errorsVarname} = []`,\n\t\t\t\t\t\t`let ${validatedVarname}`,\n\t\t\t\t\t\t...branches\n\t\t\t\t\t\t\t.map((branch, idx) => (lines: string[]) => [\n\t\t\t\t\t\t\t\t`${validatedVarname} = ${input}`,\n\t\t\t\t\t\t\t\t...compileNested({\n\t\t\t\t\t\t\t\t\topts: { ...opts, failEarly: true },\n\t\t\t\t\t\t\t\t\tpipe: branch,\n\t\t\t\t\t\t\t\t\tinput: validatedVarname,\n\t\t\t\t\t\t\t\t\terrorType: 'assign',\n\t\t\t\t\t\t\t\t\tkey: validatedVarname,\n\t\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t\t\t`if (${validatedVarname} instanceof PipeError) {`,\n\t\t\t\t\t\t\t\t`\t${errorsVarname}.push(PipeError.path(${idx}, '${validatedVarname}', ${validatedVarname}))`,\n\t\t\t\t\t\t\t\t...lines.map((l) => `\t${l}`),\n\t\t\t\t\t\t\t\tidx === branches.length - 1 ? `\t${opts.wrapError.format(`PipeError.rootFrom(${errorsVarname})`)}` : '',\n\t\t\t\t\t\t\t\t`}`,\n\t\t\t\t\t\t\t\t`else ${input} = ${validatedVarname}`,\n\t\t\t\t\t\t\t])\n\t\t\t\t\t\t\t.reduceRight<string[]>((acc, cur) => cur(acc), []),\n\t\t\t\t\t\topts.wrapError(`${input} instanceof PipeError`, input),\n\t\t\t\t\t],\n\t\t{\n\t\t\tschema: () => ({ oneOf: branches.map((branch) => schema(branch)) }),\n\t\t},\n\t)\n}\n\nexport const merge = <T1 extends Pipe<any, any>, T2 extends Pipe<any, any>>(branch1: T1, branch2: T2) => {\n\tconst inputVarname = `input_${getRandomValue()}`\n\treturn standard<PipeInput<T1> & PipeInput<T2>, PipeOutput<T1> & PipeOutput<T2>>(\n\t\t({ input, context }, opts) => [\n\t\t\t`let ${inputVarname}A = ${input}`,\n\t\t\t`let ${inputVarname}B = ${input}`,\n\t\t\t...compileNested({ opts, pipe: branch1, input: `${inputVarname}A` }),\n\t\t\topts.wrapError(`${inputVarname}A instanceof PipeError`, `${inputVarname}A`),\n\t\t\t...compileNested({ opts, pipe: branch2, input: `${inputVarname}B` }),\n\t\t\topts.wrapError(`${inputVarname}B instanceof PipeError`, `${inputVarname}B`),\n\t\t\t`${input} = ${context}.differMerge(${inputVarname}A, ${inputVarname}B)`,\n\t\t],\n\t\t{\n\t\t\tcontext: { differMerge },\n\t\t\tschema: () => ({ allOf: [schema(branch1), schema(branch2)] }),\n\t\t},\n\t)\n}\n\nexport const discriminate = <T extends Record<PropertyKey, Pipe<any, any>>>(\n\tdiscriminator: (val: PipeInput<T[keyof T]>) => PropertyKey,\n\tbranches: T,\n\terr = 'doesnt match any of the schema',\n) =>\n\tstandard<PipeInput<T[keyof T]>, PipeOutput<T[keyof T]>>(\n\t\t({ input, context, path }, opts) => [\n\t\t\t`switch (${context}.wrapInTryCatch(() => ${context}.discriminator(${input}))) {`,\n\t\t\t...Object.entries(branches).flatMap(([key, branch]) => [\n\t\t\t\t`\tcase ('${key}'): {`,\n\t\t\t\t...compileNested({ opts, pipe: branch, input }).map((l) => `\t\t${l}`),\n\t\t\t\t`\t\tbreak`,\n\t\t\t\t`\t}`,\n\t\t\t]),\n\t\t\t`\tdefault: ${opts.wrapError.format(`PipeError.root(\"${err}\", ${input}, ${path})`)}`,\n\t\t\t`}`,\n\t\t],\n\t\t{\n\t\t\tcontext: { wrapInTryCatch, discriminator },\n\t\t\tschema: () => ({ oneOf: Object.values(branches).map((s) => schema(s)) }),\n\t\t},\n\t)\n\nexport const fromJson = <T extends Pipe<any, any>>(branch: T) => {\n\tconst validatedVarname = `validated_${getRandomValue()}`\n\treturn standard<PipeInput<T>, PipeOutput<T>>(\n\t\t({ input, context }, opts) => [\n\t\t\t`let ${validatedVarname} = ${input}`,\n\t\t\t...compileNested({ opts, pipe: branch, input: validatedVarname, errorType: 'assign' }),\n\t\t\t`if (${validatedVarname} instanceof PipeError) {`,\n\t\t\topts.wrapError(`${input}?.constructor?.name !== 'String'`, validatedVarname),\n\t\t\t`\t${validatedVarname} = ${context}.wrapInTryCatch(() => JSON.parse(${input}), ${validatedVarname})`,\n\t\t\topts.wrapError(`${validatedVarname} instanceof PipeError`, validatedVarname),\n\t\t\t...compileNested({ opts, pipe: branch, input: validatedVarname, errorType: 'assign' }).map((l) => `\t${l}`),\n\t\t\topts.wrapError(`${validatedVarname} instanceof PipeError`, validatedVarname),\n\t\t\t`}`,\n\t\t\t`${input} = ${validatedVarname}`,\n\t\t],\n\t\t{\n\t\t\tcontext: { ...context(branch), wrapInTryCatch },\n\t\t\tschema: branch.schema,\n\t\t},\n\t)\n}\n\nexport const lazy = <T extends Pipe<any, any>>(pipeFn: () => T) =>\n\tdefine<PipeInput<T>, PipeOutput<T>>(\n\t\t(input) => {\n\t\t\tconst result = validate(pipeFn(), input)\n\t\t\treturn result.valid ? result.value : result.error\n\t\t},\n\t\t{\n\t\t\tschema: () => schema(pipeFn()),\n\t\t},\n\t)\n\nexport const recursive = <T extends Pipe<any, any>>(pipeFn: () => T, $refId: string) => {\n\tconst fnVarname = `fn_${getRandomValue()}`\n\tlet compiledBefore = false\n\tlet schemedBefore = false\n\treturn standard<PipeInput<T>, PipeOutput<T>>(\n\t\t({ input }, opts) => {\n\t\t\tconst common = [`${input} = ${fnVarname}(${input})`, opts.wrapError(`${input} instanceof PipeError`, input)]\n\t\t\tcompiledBefore = !compiledBefore\n\t\t\tif (!compiledBefore) return common\n\t\t\treturn [\n\t\t\t\t`function ${fnVarname}(node) {`,\n\t\t\t\t...compileNested({ opts: { ...opts, failEarly: true }, pipe: pipeFn(), input: 'node', errorType: 'return' }).map(\n\t\t\t\t\t(l) => `\t${l}`,\n\t\t\t\t),\n\t\t\t\t`\treturn node`,\n\t\t\t\t'}',\n\t\t\t\t...common,\n\t\t\t]\n\t\t},\n\t\t{\n\t\t\tschema: () => {\n\t\t\t\tschemedBefore = !schemedBefore\n\t\t\t\tif (!schemedBefore) return { $refId }\n\t\t\t\treturn schema(pipeFn(), { $refId })\n\t\t\t},\n\t\t},\n\t)\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AACA,oBAAqC;AACrC,uBAA+C;AAC/C,mBAA2E;AAEpE,MAAM,KAAK,CAA6B,aAAgB;AAC9D,QAAM,mBAAmB,iBAAa,iCAAe,CAAC;AACtD,QAAM,gBAAgB,cAAU,iCAAe,CAAC;AAChD,aAAO;AAAA,IACN,CAAC,EAAE,MAAM,GAAG,SACX,SAAS,WAAW,IACjB,CAAC,IACD;AAAA,MACA,SAAS,aAAa;AAAA,MACtB,OAAO,gBAAgB;AAAA,MACvB,GAAG,SACD,IAA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,EAAE,MAAM,EAAE,GAAG,MAAM,WAAW,KAAK,GAAG,MAAM,OAAO,GAAG,OAAO,QAAQ,WAAW,SAAS,CAAC,EAAE;AAAA,UAC5G,CAAC,MAAM,IAAI,CAAC;AAAA,QACb;AAAA,QACA;AAAA,QACA;AAAA,QACA,GAAG;AAAA,MACJ;AAAA,IACD;AAAA,IACA;AAAA,MACC,QAAQ,MAAM;AACb,wBAAgB,CAAC;AACjB,YAAI,CAAC,cAAe,QAAO,EAAE,OAAO;AACpC,mBAAO,qBAAO,OAAO,GAAG,EAAE,OAAO,CAAC;AAAA,MACnC;AAAA,IACD;AAAA,EACD;AACD;","names":["context","differMerge"]}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var c=Object.defineProperty;var T=Object.getOwnPropertyDescriptor;var P=Object.getOwnPropertyNames;var d=Object.prototype.hasOwnProperty;var l=(t,e)=>{for(var r in e)c(t,r,{get:e[r],enumerable:!0})},y=(t,e,r,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of P(e))!d.call(t,o)&&o!==r&&c(t,o,{get:()=>e[o],enumerable:!(n=T(e,o))||n.enumerable});return t};var E=t=>y(c({},"__esModule",{value:!0}),t);var v={};l(v,{discriminate:()=>O,fromJson:()=>w,lazy:()=>h,merge:()=>x,or:()=>g,recursive:()=>I});module.exports=E(v);var u=require('../utils/differ.min.cjs'),$=require('../utils/functions/index.min.cjs'),a=require('./base/pipes.min.cjs');const g=t=>{const e=`validated_${(0,$.getRandomValue)()}`,r=`errors_${(0,$.getRandomValue)()}`;return(0,a.standard)(({input:n},o)=>t.length===0?[]:[`const ${r} = []`,`let ${e}`,...t.map((p,i)=>s=>[`${e} = ${n}`,...(0,a.compileNested)({opts:{...o,failEarly:!0},pipe:p,input:e,errorType:"assign",key:e}),`if (${e} instanceof PipeError) {`,` ${r}.push(PipeError.path(${i}, '${e}', ${e}))`,...s.map(m=>` ${m}`),i===t.length-1?` ${o.wrapError.format(`PipeError.rootFrom(${r})`)}`:"","}",`else ${n} = ${e}`]).reduceRight((p,i)=>i(p),[]),o.wrapError(`${n} instanceof PipeError`,n)],{schema:()=>({oneOf:t.map(n=>(0,a.schema)(n))})})},x=(t,e)=>{const r=`input_${(0,$.getRandomValue)()}`;return(0,a.standard)(({input:n,context:o},p)=>[`let ${r}A = ${n}`,`let ${r}B = ${n}`,...(0,a.compileNested)({opts:p,pipe:t,input:`${r}A`}),p.wrapError(`${r}A instanceof PipeError`,`${r}A`),...(0,a.compileNested)({opts:p,pipe:e,input:`${r}B`}),p.wrapError(`${r}B instanceof PipeError`,`${r}B`),`${n} = ${o}.differMerge(${r}A, ${r}B)`],{context:{differMerge:u.merge},schema:()=>({allOf:[(0,a.schema)(t),(0,a.schema)(e)]})})},O=(t,e,r="doesnt match any of the schema")=>(0,a.standard)(({input:n,context:o,path:p},i)=>[`switch (${o}.wrapInTryCatch(() => ${o}.discriminator(${n}))) {`,...Object.entries(e).flatMap(([s,m])=>[` case ('${s}'): {`,...(0,a.compileNested)({opts:i,pipe:m,input:n}).map(f=>` ${f}`)," break"," }"]),` default: ${i.wrapError.format(`PipeError.root("${r}", ${n}, ${p})`)}`,"}"],{context:{wrapInTryCatch:$.wrapInTryCatch,discriminator:t},schema:()=>({oneOf:Object.values(e).map(n=>(0,a.schema)(n))})}),w=t=>{const e=`validated_${(0,$.getRandomValue)()}`;return(0,a.standard)(({input:r,context:n},o)=>[`let ${e} = ${r}`,...(0,a.compileNested)({opts:o,pipe:t,input:e,errorType:"assign"}),`if (${e} instanceof PipeError) {`,o.wrapError(`${r}?.constructor?.name !== 'String'`,e),` ${e} = ${n}.wrapInTryCatch(() => JSON.parse(${r}), ${e})`,o.wrapError(`${e} instanceof PipeError`,e),...(0,a.compileNested)({opts:o,pipe:t,input:e,errorType:"assign"}).map(p=>` ${p}`),o.wrapError(`${e} instanceof PipeError`,e),"}",`${r} = ${e}`],{context:{...(0,a.context)(t),wrapInTryCatch:$.wrapInTryCatch},schema:t.schema})},h=t=>(0,a.define)(e=>{const r=(0,a.validate)(t(),e);return r.valid?r.value:r.error},{schema:()=>(0,a.schema)(t())}),I=(t,e)=>{const r=`fn_${(0,$.getRandomValue)()}`;let n=!1,o=!1;return(0,a.standard)(({input:p},i)=>{const s=[`${p} = ${r}(${p})`,i.wrapError(`${p} instanceof PipeError`,p)];return n=!n,n?[`function ${r}(node) {`,...(0,a.compileNested)({opts:{...i,failEarly:!0},pipe:t(),input:"node",errorType:"return"}).map(m=>` ${m}`)," return node","}",...s]:s},{schema:()=>(o=!o,o?(0,a.schema)(t(),{$refId:e}):{$refId:e})})};0&&(module.exports={discriminate,fromJson,lazy,merge,or,recursive});
|
|
2
2
|
//# sourceMappingURL=junctions.min.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/api/junctions.ts"],"sourcesContent":["import { Pipe, PipeInput, PipeOutput } from './base'\nimport { merge as differMerge } from '../utils/differ'\nimport { getRandomValue, wrapInTryCatch } from '../utils/functions'\nimport { compileNested, context, standard, schema, define, validate } from './base/pipes'\n\nexport const or = <T extends Pipe<any, any>[]>(branches: T) => {\n\tconst validatedVarname = `validated_${getRandomValue()}`\n\tconst errorsVarname = `errors_${getRandomValue()}`\n\treturn standard<PipeInput<T[number]>, PipeOutput<T[number]>>(\n\t\t({ input }, opts) =>\n\t\t\tbranches.length === 0\n\t\t\t\t? []\n\t\t\t\t: [\n\t\t\t\t\t\t`const ${errorsVarname} = []`,\n\t\t\t\t\t\t`let ${validatedVarname}`,\n\t\t\t\t\t\t...branches\n\t\t\t\t\t\t\t.map((branch, idx) => (lines: string[]) => [\n\t\t\t\t\t\t\t\t`${validatedVarname} = ${input}`,\n\t\t\t\t\t\t\t\t...compileNested({\n\t\t\t\t\t\t\t\t\topts: { ...opts, failEarly: true },\n\t\t\t\t\t\t\t\t\tpipe: branch,\n\t\t\t\t\t\t\t\t\tinput: validatedVarname,\n\t\t\t\t\t\t\t\t\terrorType: 'assign',\n\t\t\t\t\t\t\t\t\tkey: validatedVarname,\n\t\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t\t\t`if (${validatedVarname} instanceof PipeError) {`,\n\t\t\t\t\t\t\t\t`\t${errorsVarname}.push(PipeError.path(${idx}, '${validatedVarname}', ${validatedVarname}))`,\n\t\t\t\t\t\t\t\t...lines.map((l) => `\t${l}`),\n\t\t\t\t\t\t\t\tidx === branches.length - 1 ? `\t${opts.wrapError.format(`PipeError.rootFrom(${errorsVarname})`)}` : '',\n\t\t\t\t\t\t\t\t`}`,\n\t\t\t\t\t\t\t\t`else ${input} = ${validatedVarname}`,\n\t\t\t\t\t\t\t])\n\t\t\t\t\t\t\t.reduceRight<string[]>((acc, cur) => cur(acc), []),\n\t\t\t\t\t\topts.wrapError(`${input} instanceof PipeError`, input),\n\t\t\t\t\t],\n\t\t{\n\t\t\tschema: () => ({ oneOf: branches.map((branch) => schema(branch)) }),\n\t\t},\n\t)\n}\n\nexport const merge = <T1 extends Pipe<any, any>, T2 extends Pipe<any, any>>(branch1: T1, branch2: T2) => {\n\tconst inputVarname = `input_${getRandomValue()}`\n\treturn standard<PipeInput<T1> & PipeInput<T2>, PipeOutput<T1> & PipeOutput<T2>>(\n\t\t({ input, context }, opts) => [\n\t\t\t`let ${inputVarname}A = ${input}`,\n\t\t\t`let ${inputVarname}B = ${input}`,\n\t\t\t...compileNested({ opts, pipe: branch1, input: `${inputVarname}A` }),\n\t\t\topts.wrapError(`${inputVarname}A instanceof PipeError`, `${inputVarname}A`),\n\t\t\t...compileNested({ opts, pipe: branch2, input: `${inputVarname}B` }),\n\t\t\topts.wrapError(`${inputVarname}B instanceof PipeError`, `${inputVarname}B`),\n\t\t\t`${input} = ${context}.differMerge(${inputVarname}A, ${inputVarname}B)`,\n\t\t],\n\t\t{\n\t\t\tcontext: { differMerge },\n\t\t\tschema: () => ({ allOf: [schema(branch1), schema(branch2)] }),\n\t\t},\n\t)\n}\n\nexport const discriminate = <T extends Record<PropertyKey, Pipe<any, any>>>(\n\tdiscriminator: (val: PipeInput<T[keyof T]>) => PropertyKey,\n\tbranches: T,\n\terr = 'doesnt match any of the schema',\n) =>\n\tstandard<PipeInput<T[keyof T]>, PipeOutput<T[keyof T]>>(\n\t\t({ input, context, path }, opts) => [\n\t\t\t`switch (${context}.wrapInTryCatch(() => ${context}.discriminator(${input}))) {`,\n\t\t\t...Object.entries(branches).flatMap(([key, branch]) => [\n\t\t\t\t`\tcase ('${key}'): {`,\n\t\t\t\t...compileNested({ opts, pipe: branch, input }).map((l) => `\t\t${l}`),\n\t\t\t\t`\t\tbreak`,\n\t\t\t\t`\t}`,\n\t\t\t]),\n\t\t\t`\tdefault: ${opts.wrapError.format(`PipeError.root(\"${err}\", ${input}, ${path})`)}`,\n\t\t\t`}`,\n\t\t],\n\t\t{\n\t\t\tcontext: { wrapInTryCatch, discriminator },\n\t\t\tschema: () => ({ oneOf: Object.values(branches).map((s) => schema(s)) }),\n\t\t},\n\t)\n\nexport const fromJson = <T extends Pipe<any, any>>(branch: T) => {\n\tconst validatedVarname = `validated_${getRandomValue()}`\n\treturn standard<PipeInput<T>, PipeOutput<T>>(\n\t\t({ input, context }, opts) => [\n\t\t\t`let ${validatedVarname} = ${input}`,\n\t\t\t...compileNested({ opts, pipe: branch, input: validatedVarname, errorType: 'assign' }),\n\t\t\t`if (${validatedVarname} instanceof PipeError) {`,\n\t\t\topts.wrapError(`${input}?.constructor?.name !== 'String'`, validatedVarname),\n\t\t\t`\t${validatedVarname} = ${context}.wrapInTryCatch(() => JSON.parse(${input}), ${validatedVarname})`,\n\t\t\topts.wrapError(`${validatedVarname} instanceof PipeError`, validatedVarname),\n\t\t\t...compileNested({ opts, pipe: branch, input: validatedVarname, errorType: 'assign' }).map((l) => `\t${l}`),\n\t\t\topts.wrapError(`${validatedVarname} instanceof PipeError`, validatedVarname),\n\t\t\t`}`,\n\t\t\t`${input} = ${validatedVarname}`,\n\t\t],\n\t\t{\n\t\t\tcontext: { ...context(branch), wrapInTryCatch },\n\t\t\tschema: branch.schema,\n\t\t},\n\t)\n}\n\nexport const lazy = <T extends Pipe<any, any>>(pipeFn: () => T) =>\n\tdefine<PipeInput<T>, PipeOutput<T>>(\n\t\t(input) => {\n\t\t\tconst result = validate(pipeFn(), input)\n\t\t\treturn result.valid ? result.value : result.error\n\t\t},\n\t\t{\n\t\t\tschema: () => schema(pipeFn()),\n\t\t},\n\t)\n\nexport const recursive = <T extends Pipe<any, any>>(pipeFn: () => T, $refId: string) => {\n\tconst fnVarname = `fn_${getRandomValue()}`\n\tlet compiledBefore = false\n\tlet schemedBefore = false\n\treturn standard<PipeInput<T>, PipeOutput<T>>(\n\t\t({ input }, opts) => {\n\t\t\tconst common = [`${input} = ${fnVarname}(${input})`, opts.wrapError(`${input} instanceof PipeError`, input)]\n\t\t\
|
|
1
|
+
{"version":3,"sources":["../../../src/api/junctions.ts"],"sourcesContent":["import { Pipe, PipeInput, PipeOutput } from './base'\nimport { merge as differMerge } from '../utils/differ'\nimport { getRandomValue, wrapInTryCatch } from '../utils/functions'\nimport { compileNested, context, standard, schema, define, validate } from './base/pipes'\n\nexport const or = <T extends Pipe<any, any>[]>(branches: T) => {\n\tconst validatedVarname = `validated_${getRandomValue()}`\n\tconst errorsVarname = `errors_${getRandomValue()}`\n\treturn standard<PipeInput<T[number]>, PipeOutput<T[number]>>(\n\t\t({ input }, opts) =>\n\t\t\tbranches.length === 0\n\t\t\t\t? []\n\t\t\t\t: [\n\t\t\t\t\t\t`const ${errorsVarname} = []`,\n\t\t\t\t\t\t`let ${validatedVarname}`,\n\t\t\t\t\t\t...branches\n\t\t\t\t\t\t\t.map((branch, idx) => (lines: string[]) => [\n\t\t\t\t\t\t\t\t`${validatedVarname} = ${input}`,\n\t\t\t\t\t\t\t\t...compileNested({\n\t\t\t\t\t\t\t\t\topts: { ...opts, failEarly: true },\n\t\t\t\t\t\t\t\t\tpipe: branch,\n\t\t\t\t\t\t\t\t\tinput: validatedVarname,\n\t\t\t\t\t\t\t\t\terrorType: 'assign',\n\t\t\t\t\t\t\t\t\tkey: validatedVarname,\n\t\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t\t\t`if (${validatedVarname} instanceof PipeError) {`,\n\t\t\t\t\t\t\t\t`\t${errorsVarname}.push(PipeError.path(${idx}, '${validatedVarname}', ${validatedVarname}))`,\n\t\t\t\t\t\t\t\t...lines.map((l) => `\t${l}`),\n\t\t\t\t\t\t\t\tidx === branches.length - 1 ? `\t${opts.wrapError.format(`PipeError.rootFrom(${errorsVarname})`)}` : '',\n\t\t\t\t\t\t\t\t`}`,\n\t\t\t\t\t\t\t\t`else ${input} = ${validatedVarname}`,\n\t\t\t\t\t\t\t])\n\t\t\t\t\t\t\t.reduceRight<string[]>((acc, cur) => cur(acc), []),\n\t\t\t\t\t\topts.wrapError(`${input} instanceof PipeError`, input),\n\t\t\t\t\t],\n\t\t{\n\t\t\tschema: () => ({ oneOf: branches.map((branch) => schema(branch)) }),\n\t\t},\n\t)\n}\n\nexport const merge = <T1 extends Pipe<any, any>, T2 extends Pipe<any, any>>(branch1: T1, branch2: T2) => {\n\tconst inputVarname = `input_${getRandomValue()}`\n\treturn standard<PipeInput<T1> & PipeInput<T2>, PipeOutput<T1> & PipeOutput<T2>>(\n\t\t({ input, context }, opts) => [\n\t\t\t`let ${inputVarname}A = ${input}`,\n\t\t\t`let ${inputVarname}B = ${input}`,\n\t\t\t...compileNested({ opts, pipe: branch1, input: `${inputVarname}A` }),\n\t\t\topts.wrapError(`${inputVarname}A instanceof PipeError`, `${inputVarname}A`),\n\t\t\t...compileNested({ opts, pipe: branch2, input: `${inputVarname}B` }),\n\t\t\topts.wrapError(`${inputVarname}B instanceof PipeError`, `${inputVarname}B`),\n\t\t\t`${input} = ${context}.differMerge(${inputVarname}A, ${inputVarname}B)`,\n\t\t],\n\t\t{\n\t\t\tcontext: { differMerge },\n\t\t\tschema: () => ({ allOf: [schema(branch1), schema(branch2)] }),\n\t\t},\n\t)\n}\n\nexport const discriminate = <T extends Record<PropertyKey, Pipe<any, any>>>(\n\tdiscriminator: (val: PipeInput<T[keyof T]>) => PropertyKey,\n\tbranches: T,\n\terr = 'doesnt match any of the schema',\n) =>\n\tstandard<PipeInput<T[keyof T]>, PipeOutput<T[keyof T]>>(\n\t\t({ input, context, path }, opts) => [\n\t\t\t`switch (${context}.wrapInTryCatch(() => ${context}.discriminator(${input}))) {`,\n\t\t\t...Object.entries(branches).flatMap(([key, branch]) => [\n\t\t\t\t`\tcase ('${key}'): {`,\n\t\t\t\t...compileNested({ opts, pipe: branch, input }).map((l) => `\t\t${l}`),\n\t\t\t\t`\t\tbreak`,\n\t\t\t\t`\t}`,\n\t\t\t]),\n\t\t\t`\tdefault: ${opts.wrapError.format(`PipeError.root(\"${err}\", ${input}, ${path})`)}`,\n\t\t\t`}`,\n\t\t],\n\t\t{\n\t\t\tcontext: { wrapInTryCatch, discriminator },\n\t\t\tschema: () => ({ oneOf: Object.values(branches).map((s) => schema(s)) }),\n\t\t},\n\t)\n\nexport const fromJson = <T extends Pipe<any, any>>(branch: T) => {\n\tconst validatedVarname = `validated_${getRandomValue()}`\n\treturn standard<PipeInput<T>, PipeOutput<T>>(\n\t\t({ input, context }, opts) => [\n\t\t\t`let ${validatedVarname} = ${input}`,\n\t\t\t...compileNested({ opts, pipe: branch, input: validatedVarname, errorType: 'assign' }),\n\t\t\t`if (${validatedVarname} instanceof PipeError) {`,\n\t\t\topts.wrapError(`${input}?.constructor?.name !== 'String'`, validatedVarname),\n\t\t\t`\t${validatedVarname} = ${context}.wrapInTryCatch(() => JSON.parse(${input}), ${validatedVarname})`,\n\t\t\topts.wrapError(`${validatedVarname} instanceof PipeError`, validatedVarname),\n\t\t\t...compileNested({ opts, pipe: branch, input: validatedVarname, errorType: 'assign' }).map((l) => `\t${l}`),\n\t\t\topts.wrapError(`${validatedVarname} instanceof PipeError`, validatedVarname),\n\t\t\t`}`,\n\t\t\t`${input} = ${validatedVarname}`,\n\t\t],\n\t\t{\n\t\t\tcontext: { ...context(branch), wrapInTryCatch },\n\t\t\tschema: branch.schema,\n\t\t},\n\t)\n}\n\nexport const lazy = <T extends Pipe<any, any>>(pipeFn: () => T) =>\n\tdefine<PipeInput<T>, PipeOutput<T>>(\n\t\t(input) => {\n\t\t\tconst result = validate(pipeFn(), input)\n\t\t\treturn result.valid ? result.value : result.error\n\t\t},\n\t\t{\n\t\t\tschema: () => schema(pipeFn()),\n\t\t},\n\t)\n\nexport const recursive = <T extends Pipe<any, any>>(pipeFn: () => T, $refId: string) => {\n\tconst fnVarname = `fn_${getRandomValue()}`\n\tlet compiledBefore = false\n\tlet schemedBefore = false\n\treturn standard<PipeInput<T>, PipeOutput<T>>(\n\t\t({ input }, opts) => {\n\t\t\tconst common = [`${input} = ${fnVarname}(${input})`, opts.wrapError(`${input} instanceof PipeError`, input)]\n\t\t\tcompiledBefore = !compiledBefore\n\t\t\tif (!compiledBefore) return common\n\t\t\treturn [\n\t\t\t\t`function ${fnVarname}(node) {`,\n\t\t\t\t...compileNested({ opts: { ...opts, failEarly: true }, pipe: pipeFn(), input: 'node', errorType: 'return' }).map(\n\t\t\t\t\t(l) => `\t${l}`,\n\t\t\t\t),\n\t\t\t\t`\treturn node`,\n\t\t\t\t'}',\n\t\t\t\t...common,\n\t\t\t]\n\t\t},\n\t\t{\n\t\t\tschema: () => {\n\t\t\t\tschemedBefore = !schemedBefore\n\t\t\t\tif (!schemedBefore) return { $refId }\n\t\t\t\treturn schema(pipeFn(), { $refId })\n\t\t\t},\n\t\t},\n\t)\n}\n"],"mappings":"yaAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,kBAAAE,EAAA,aAAAC,EAAA,SAAAC,EAAA,UAAAC,EAAA,OAAAC,EAAA,cAAAC,IAAA,eAAAC,EAAAR,GACA,IAAAS,EAAqC,2BACrCC,EAA+C,8BAC/CC,EAA2E,wBAEpE,MAAML,EAAkCM,GAAgB,CAC9D,MAAMC,EAAmB,gBAAa,kBAAe,CAAC,GAChDC,E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,CAU7B,EACA,CACC,OAAQ,KACPD,EAAgB,CAACA,EACZA,KACE,UAAOL,EAAO,EAAG,CAAE,OAAAE,CAAO,CAAC,EADP,CAAE,OAAAA,CAAO,EAGtC,CACD,CACD","names":["junctions_exports","__export","discriminate","fromJson","lazy","merge","or","recursive","__toCommonJS","import_differ","import_functions","import_pipes","branches","validatedVarname","errorsVarname","input","opts","branch","idx","lines","l","acc","cur","branch1","branch2","inputVarname","context","differMerge","discriminator","err","path","key","s","pipeFn","result","$refId","fnVarname","compiledBefore","schemedBefore","common"]}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{merge as P}from "../utils/differ.min.mjs";import{getRandomValue as
|
|
1
|
+
import{merge as P}from "../utils/differ.min.mjs";import{getRandomValue as c,wrapInTryCatch as f}from "../utils/functions/index.min.mjs";import{compileNested as i,context as d,standard as u,schema as s,define as l,validate as y}from "./base/pipes.min.mjs";const v=a=>{const e=`validated_${c()}`,r=`errors_${c()}`;return u(({input:t},n)=>a.length===0?[]:[`const ${r} = []`,`let ${e}`,...a.map((o,p)=>$=>[`${e} = ${t}`,...i({opts:{...n,failEarly:!0},pipe:o,input:e,errorType:"assign",key:e}),`if (${e} instanceof PipeError) {`,` ${r}.push(PipeError.path(${p}, '${e}', ${e}))`,...$.map(m=>` ${m}`),p===a.length-1?` ${n.wrapError.format(`PipeError.rootFrom(${r})`)}`:"","}",`else ${t} = ${e}`]).reduceRight((o,p)=>p(o),[]),n.wrapError(`${t} instanceof PipeError`,t)],{schema:()=>({oneOf:a.map(t=>s(t))})})},B=(a,e)=>{const r=`input_${c()}`;return u(({input:t,context:n},o)=>[`let ${r}A = ${t}`,`let ${r}B = ${t}`,...i({opts:o,pipe:a,input:`${r}A`}),o.wrapError(`${r}A instanceof PipeError`,`${r}A`),...i({opts:o,pipe:e,input:`${r}B`}),o.wrapError(`${r}B instanceof PipeError`,`${r}B`),`${t} = ${n}.differMerge(${r}A, ${r}B)`],{context:{differMerge:P},schema:()=>({allOf:[s(a),s(e)]})})},V=(a,e,r="doesnt match any of the schema")=>u(({input:t,context:n,path:o},p)=>[`switch (${n}.wrapInTryCatch(() => ${n}.discriminator(${t}))) {`,...Object.entries(e).flatMap(([$,m])=>[` case ('${$}'): {`,...i({opts:p,pipe:m,input:t}).map(T=>` ${T}`)," break"," }"]),` default: ${p.wrapError.format(`PipeError.root("${r}", ${t}, ${o})`)}`,"}"],{context:{wrapInTryCatch:f,discriminator:a},schema:()=>({oneOf:Object.values(e).map(t=>s(t))})}),k=a=>{const e=`validated_${c()}`;return u(({input:r,context:t},n)=>[`let ${e} = ${r}`,...i({opts:n,pipe:a,input:e,errorType:"assign"}),`if (${e} instanceof PipeError) {`,n.wrapError(`${r}?.constructor?.name !== 'String'`,e),` ${e} = ${t}.wrapInTryCatch(() => JSON.parse(${r}), ${e})`,n.wrapError(`${e} instanceof PipeError`,e),...i({opts:n,pipe:a,input:e,errorType:"assign"}).map(o=>` ${o}`),n.wrapError(`${e} instanceof PipeError`,e),"}",`${r} = ${e}`],{context:{...d(a),wrapInTryCatch:f},schema:a.schema})},A=a=>l(e=>{const r=y(a(),e);return r.valid?r.value:r.error},{schema:()=>s(a())}),_=(a,e)=>{const r=`fn_${c()}`;let t=!1,n=!1;return u(({input:o},p)=>{const $=[`${o} = ${r}(${o})`,p.wrapError(`${o} instanceof PipeError`,o)];return t=!t,t?[`function ${r}(node) {`,...i({opts:{...p,failEarly:!0},pipe:a(),input:"node",errorType:"return"}).map(m=>` ${m}`)," return node","}",...$]:$},{schema:()=>(n=!n,n?s(a(),{$refId:e}):{$refId:e})})};export{V as discriminate,k as fromJson,A as lazy,B as merge,v as or,_ as recursive};
|
|
2
2
|
//# sourceMappingURL=junctions.min.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/api/junctions.ts"],"sourcesContent":["import { Pipe, PipeInput, PipeOutput } from './base'\nimport { merge as differMerge } from '../utils/differ'\nimport { getRandomValue, wrapInTryCatch } from '../utils/functions'\nimport { compileNested, context, standard, schema, define, validate } from './base/pipes'\n\nexport const or = <T extends Pipe<any, any>[]>(branches: T) => {\n\tconst validatedVarname = `validated_${getRandomValue()}`\n\tconst errorsVarname = `errors_${getRandomValue()}`\n\treturn standard<PipeInput<T[number]>, PipeOutput<T[number]>>(\n\t\t({ input }, opts) =>\n\t\t\tbranches.length === 0\n\t\t\t\t? []\n\t\t\t\t: [\n\t\t\t\t\t\t`const ${errorsVarname} = []`,\n\t\t\t\t\t\t`let ${validatedVarname}`,\n\t\t\t\t\t\t...branches\n\t\t\t\t\t\t\t.map((branch, idx) => (lines: string[]) => [\n\t\t\t\t\t\t\t\t`${validatedVarname} = ${input}`,\n\t\t\t\t\t\t\t\t...compileNested({\n\t\t\t\t\t\t\t\t\topts: { ...opts, failEarly: true },\n\t\t\t\t\t\t\t\t\tpipe: branch,\n\t\t\t\t\t\t\t\t\tinput: validatedVarname,\n\t\t\t\t\t\t\t\t\terrorType: 'assign',\n\t\t\t\t\t\t\t\t\tkey: validatedVarname,\n\t\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t\t\t`if (${validatedVarname} instanceof PipeError) {`,\n\t\t\t\t\t\t\t\t`\t${errorsVarname}.push(PipeError.path(${idx}, '${validatedVarname}', ${validatedVarname}))`,\n\t\t\t\t\t\t\t\t...lines.map((l) => `\t${l}`),\n\t\t\t\t\t\t\t\tidx === branches.length - 1 ? `\t${opts.wrapError.format(`PipeError.rootFrom(${errorsVarname})`)}` : '',\n\t\t\t\t\t\t\t\t`}`,\n\t\t\t\t\t\t\t\t`else ${input} = ${validatedVarname}`,\n\t\t\t\t\t\t\t])\n\t\t\t\t\t\t\t.reduceRight<string[]>((acc, cur) => cur(acc), []),\n\t\t\t\t\t\topts.wrapError(`${input} instanceof PipeError`, input),\n\t\t\t\t\t],\n\t\t{\n\t\t\tschema: () => ({ oneOf: branches.map((branch) => schema(branch)) }),\n\t\t},\n\t)\n}\n\nexport const merge = <T1 extends Pipe<any, any>, T2 extends Pipe<any, any>>(branch1: T1, branch2: T2) => {\n\tconst inputVarname = `input_${getRandomValue()}`\n\treturn standard<PipeInput<T1> & PipeInput<T2>, PipeOutput<T1> & PipeOutput<T2>>(\n\t\t({ input, context }, opts) => [\n\t\t\t`let ${inputVarname}A = ${input}`,\n\t\t\t`let ${inputVarname}B = ${input}`,\n\t\t\t...compileNested({ opts, pipe: branch1, input: `${inputVarname}A` }),\n\t\t\topts.wrapError(`${inputVarname}A instanceof PipeError`, `${inputVarname}A`),\n\t\t\t...compileNested({ opts, pipe: branch2, input: `${inputVarname}B` }),\n\t\t\topts.wrapError(`${inputVarname}B instanceof PipeError`, `${inputVarname}B`),\n\t\t\t`${input} = ${context}.differMerge(${inputVarname}A, ${inputVarname}B)`,\n\t\t],\n\t\t{\n\t\t\tcontext: { differMerge },\n\t\t\tschema: () => ({ allOf: [schema(branch1), schema(branch2)] }),\n\t\t},\n\t)\n}\n\nexport const discriminate = <T extends Record<PropertyKey, Pipe<any, any>>>(\n\tdiscriminator: (val: PipeInput<T[keyof T]>) => PropertyKey,\n\tbranches: T,\n\terr = 'doesnt match any of the schema',\n) =>\n\tstandard<PipeInput<T[keyof T]>, PipeOutput<T[keyof T]>>(\n\t\t({ input, context, path }, opts) => [\n\t\t\t`switch (${context}.wrapInTryCatch(() => ${context}.discriminator(${input}))) {`,\n\t\t\t...Object.entries(branches).flatMap(([key, branch]) => [\n\t\t\t\t`\tcase ('${key}'): {`,\n\t\t\t\t...compileNested({ opts, pipe: branch, input }).map((l) => `\t\t${l}`),\n\t\t\t\t`\t\tbreak`,\n\t\t\t\t`\t}`,\n\t\t\t]),\n\t\t\t`\tdefault: ${opts.wrapError.format(`PipeError.root(\"${err}\", ${input}, ${path})`)}`,\n\t\t\t`}`,\n\t\t],\n\t\t{\n\t\t\tcontext: { wrapInTryCatch, discriminator },\n\t\t\tschema: () => ({ oneOf: Object.values(branches).map((s) => schema(s)) }),\n\t\t},\n\t)\n\nexport const fromJson = <T extends Pipe<any, any>>(branch: T) => {\n\tconst validatedVarname = `validated_${getRandomValue()}`\n\treturn standard<PipeInput<T>, PipeOutput<T>>(\n\t\t({ input, context }, opts) => [\n\t\t\t`let ${validatedVarname} = ${input}`,\n\t\t\t...compileNested({ opts, pipe: branch, input: validatedVarname, errorType: 'assign' }),\n\t\t\t`if (${validatedVarname} instanceof PipeError) {`,\n\t\t\topts.wrapError(`${input}?.constructor?.name !== 'String'`, validatedVarname),\n\t\t\t`\t${validatedVarname} = ${context}.wrapInTryCatch(() => JSON.parse(${input}), ${validatedVarname})`,\n\t\t\topts.wrapError(`${validatedVarname} instanceof PipeError`, validatedVarname),\n\t\t\t...compileNested({ opts, pipe: branch, input: validatedVarname, errorType: 'assign' }).map((l) => `\t${l}`),\n\t\t\topts.wrapError(`${validatedVarname} instanceof PipeError`, validatedVarname),\n\t\t\t`}`,\n\t\t\t`${input} = ${validatedVarname}`,\n\t\t],\n\t\t{\n\t\t\tcontext: { ...context(branch), wrapInTryCatch },\n\t\t\tschema: branch.schema,\n\t\t},\n\t)\n}\n\nexport const lazy = <T extends Pipe<any, any>>(pipeFn: () => T) =>\n\tdefine<PipeInput<T>, PipeOutput<T>>(\n\t\t(input) => {\n\t\t\tconst result = validate(pipeFn(), input)\n\t\t\treturn result.valid ? result.value : result.error\n\t\t},\n\t\t{\n\t\t\tschema: () => schema(pipeFn()),\n\t\t},\n\t)\n\nexport const recursive = <T extends Pipe<any, any>>(pipeFn: () => T, $refId: string) => {\n\tconst fnVarname = `fn_${getRandomValue()}`\n\tlet compiledBefore = false\n\tlet schemedBefore = false\n\treturn standard<PipeInput<T>, PipeOutput<T>>(\n\t\t({ input }, opts) => {\n\t\t\tconst common = [`${input} = ${fnVarname}(${input})`, opts.wrapError(`${input} instanceof PipeError`, input)]\n\t\t\
|
|
1
|
+
{"version":3,"sources":["../../../src/api/junctions.ts"],"sourcesContent":["import { Pipe, PipeInput, PipeOutput } from './base'\nimport { merge as differMerge } from '../utils/differ'\nimport { getRandomValue, wrapInTryCatch } from '../utils/functions'\nimport { compileNested, context, standard, schema, define, validate } from './base/pipes'\n\nexport const or = <T extends Pipe<any, any>[]>(branches: T) => {\n\tconst validatedVarname = `validated_${getRandomValue()}`\n\tconst errorsVarname = `errors_${getRandomValue()}`\n\treturn standard<PipeInput<T[number]>, PipeOutput<T[number]>>(\n\t\t({ input }, opts) =>\n\t\t\tbranches.length === 0\n\t\t\t\t? []\n\t\t\t\t: [\n\t\t\t\t\t\t`const ${errorsVarname} = []`,\n\t\t\t\t\t\t`let ${validatedVarname}`,\n\t\t\t\t\t\t...branches\n\t\t\t\t\t\t\t.map((branch, idx) => (lines: string[]) => [\n\t\t\t\t\t\t\t\t`${validatedVarname} = ${input}`,\n\t\t\t\t\t\t\t\t...compileNested({\n\t\t\t\t\t\t\t\t\topts: { ...opts, failEarly: true },\n\t\t\t\t\t\t\t\t\tpipe: branch,\n\t\t\t\t\t\t\t\t\tinput: validatedVarname,\n\t\t\t\t\t\t\t\t\terrorType: 'assign',\n\t\t\t\t\t\t\t\t\tkey: validatedVarname,\n\t\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t\t\t`if (${validatedVarname} instanceof PipeError) {`,\n\t\t\t\t\t\t\t\t`\t${errorsVarname}.push(PipeError.path(${idx}, '${validatedVarname}', ${validatedVarname}))`,\n\t\t\t\t\t\t\t\t...lines.map((l) => `\t${l}`),\n\t\t\t\t\t\t\t\tidx === branches.length - 1 ? `\t${opts.wrapError.format(`PipeError.rootFrom(${errorsVarname})`)}` : '',\n\t\t\t\t\t\t\t\t`}`,\n\t\t\t\t\t\t\t\t`else ${input} = ${validatedVarname}`,\n\t\t\t\t\t\t\t])\n\t\t\t\t\t\t\t.reduceRight<string[]>((acc, cur) => cur(acc), []),\n\t\t\t\t\t\topts.wrapError(`${input} instanceof PipeError`, input),\n\t\t\t\t\t],\n\t\t{\n\t\t\tschema: () => ({ oneOf: branches.map((branch) => schema(branch)) }),\n\t\t},\n\t)\n}\n\nexport const merge = <T1 extends Pipe<any, any>, T2 extends Pipe<any, any>>(branch1: T1, branch2: T2) => {\n\tconst inputVarname = `input_${getRandomValue()}`\n\treturn standard<PipeInput<T1> & PipeInput<T2>, PipeOutput<T1> & PipeOutput<T2>>(\n\t\t({ input, context }, opts) => [\n\t\t\t`let ${inputVarname}A = ${input}`,\n\t\t\t`let ${inputVarname}B = ${input}`,\n\t\t\t...compileNested({ opts, pipe: branch1, input: `${inputVarname}A` }),\n\t\t\topts.wrapError(`${inputVarname}A instanceof PipeError`, `${inputVarname}A`),\n\t\t\t...compileNested({ opts, pipe: branch2, input: `${inputVarname}B` }),\n\t\t\topts.wrapError(`${inputVarname}B instanceof PipeError`, `${inputVarname}B`),\n\t\t\t`${input} = ${context}.differMerge(${inputVarname}A, ${inputVarname}B)`,\n\t\t],\n\t\t{\n\t\t\tcontext: { differMerge },\n\t\t\tschema: () => ({ allOf: [schema(branch1), schema(branch2)] }),\n\t\t},\n\t)\n}\n\nexport const discriminate = <T extends Record<PropertyKey, Pipe<any, any>>>(\n\tdiscriminator: (val: PipeInput<T[keyof T]>) => PropertyKey,\n\tbranches: T,\n\terr = 'doesnt match any of the schema',\n) =>\n\tstandard<PipeInput<T[keyof T]>, PipeOutput<T[keyof T]>>(\n\t\t({ input, context, path }, opts) => [\n\t\t\t`switch (${context}.wrapInTryCatch(() => ${context}.discriminator(${input}))) {`,\n\t\t\t...Object.entries(branches).flatMap(([key, branch]) => [\n\t\t\t\t`\tcase ('${key}'): {`,\n\t\t\t\t...compileNested({ opts, pipe: branch, input }).map((l) => `\t\t${l}`),\n\t\t\t\t`\t\tbreak`,\n\t\t\t\t`\t}`,\n\t\t\t]),\n\t\t\t`\tdefault: ${opts.wrapError.format(`PipeError.root(\"${err}\", ${input}, ${path})`)}`,\n\t\t\t`}`,\n\t\t],\n\t\t{\n\t\t\tcontext: { wrapInTryCatch, discriminator },\n\t\t\tschema: () => ({ oneOf: Object.values(branches).map((s) => schema(s)) }),\n\t\t},\n\t)\n\nexport const fromJson = <T extends Pipe<any, any>>(branch: T) => {\n\tconst validatedVarname = `validated_${getRandomValue()}`\n\treturn standard<PipeInput<T>, PipeOutput<T>>(\n\t\t({ input, context }, opts) => [\n\t\t\t`let ${validatedVarname} = ${input}`,\n\t\t\t...compileNested({ opts, pipe: branch, input: validatedVarname, errorType: 'assign' }),\n\t\t\t`if (${validatedVarname} instanceof PipeError) {`,\n\t\t\topts.wrapError(`${input}?.constructor?.name !== 'String'`, validatedVarname),\n\t\t\t`\t${validatedVarname} = ${context}.wrapInTryCatch(() => JSON.parse(${input}), ${validatedVarname})`,\n\t\t\topts.wrapError(`${validatedVarname} instanceof PipeError`, validatedVarname),\n\t\t\t...compileNested({ opts, pipe: branch, input: validatedVarname, errorType: 'assign' }).map((l) => `\t${l}`),\n\t\t\topts.wrapError(`${validatedVarname} instanceof PipeError`, validatedVarname),\n\t\t\t`}`,\n\t\t\t`${input} = ${validatedVarname}`,\n\t\t],\n\t\t{\n\t\t\tcontext: { ...context(branch), wrapInTryCatch },\n\t\t\tschema: branch.schema,\n\t\t},\n\t)\n}\n\nexport const lazy = <T extends Pipe<any, any>>(pipeFn: () => T) =>\n\tdefine<PipeInput<T>, PipeOutput<T>>(\n\t\t(input) => {\n\t\t\tconst result = validate(pipeFn(), input)\n\t\t\treturn result.valid ? result.value : result.error\n\t\t},\n\t\t{\n\t\t\tschema: () => schema(pipeFn()),\n\t\t},\n\t)\n\nexport const recursive = <T extends Pipe<any, any>>(pipeFn: () => T, $refId: string) => {\n\tconst fnVarname = `fn_${getRandomValue()}`\n\tlet compiledBefore = false\n\tlet schemedBefore = false\n\treturn standard<PipeInput<T>, PipeOutput<T>>(\n\t\t({ input }, opts) => {\n\t\t\tconst common = [`${input} = ${fnVarname}(${input})`, opts.wrapError(`${input} instanceof PipeError`, input)]\n\t\t\tcompiledBefore = !compiledBefore\n\t\t\tif (!compiledBefore) return common\n\t\t\treturn [\n\t\t\t\t`function ${fnVarname}(node) {`,\n\t\t\t\t...compileNested({ opts: { ...opts, failEarly: true }, pipe: pipeFn(), input: 'node', errorType: 'return' }).map(\n\t\t\t\t\t(l) => `\t${l}`,\n\t\t\t\t),\n\t\t\t\t`\treturn node`,\n\t\t\t\t'}',\n\t\t\t\t...common,\n\t\t\t]\n\t\t},\n\t\t{\n\t\t\tschema: () => {\n\t\t\t\tschemedBefore = !schemedBefore\n\t\t\t\tif (!schemedBefore) return { $refId }\n\t\t\t\treturn schema(pipeFn(), { $refId })\n\t\t\t},\n\t\t},\n\t)\n}\n"],"mappings":"AACA,OAAS,SAASA,MAAmB,kBACrC,OAAS,kBAAAC,EAAgB,kBAAAC,MAAsB,qBAC/C,OAAS,iBAAAC,EAAe,WAAAC,EAAS,YAAAC,EAAU,UAAAC,EAAQ,UAAAC,EAAQ,YAAAC,MAAgB,eAEpE,MAAMC,EAAkCC,GAAgB,CAC9D,MAAMC,EAAmB,aAAaV,EAAe,CAAC,GAChDW,EAAgB,UAAUX,EAAe,CAAC,GAChD,OAAOI,EACN,CAAC,CAAE,MAAAQ,CAAM,EAAGC,IACXJ,EAAS,SAAW,EACjB,CAAC,EACD,CACA,SAASE,CAAa,QACtB,OAAOD,CAAgB,GACvB,GAAGD,EACD,IAAI,CAACK,EAAQC,IAASC,GAAoB,CAC1C,GAAGN,CAAgB,MAAME,CAAK,GAC9B,GAAGV,EAAc,CAChB,KAAM,CAAE,GAAGW,EAAM,UAAW,EAAK,EACjC,KAAMC,EACN,MAAOJ,EACP,UAAW,SACX,IAAKA,CACN,CAAC,EACD,OAAOA,CAAgB,2BACvB,IAAIC,CAAa,wBAAwBI,CAAG,MAAML,CAAgB,MAAMA,CAAgB,KACxF,GAAGM,EAAM,IAAKC,GAAM,IAAIA,CAAC,EAAE,EAC3BF,IAAQN,EAAS,OAAS,EAAI,IAAII,EAAK,UAAU,OAAO,sBAAsBF,CAAa,GAAG,CAAC,GAAK,GACpG,IACA,QAAQC,CAAK,MAAMF,CAAgB,EACpC,CAAC,EACA,YAAsB,CAACQ,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AAQA,EAAO,KAC7C,EACA,CACC,OAAQ,IAAM5B,EAAO2B,EAAO,CAAC,CAC9B,CACD,EAEYE,EAAY,CAA2BF,EAAiBG,IAAmB,CACvF,MAAMC,EAAY,MAAMpC,EAAe,CAAC,GACxC,IAAIqC,EAAiB,GACjBC,EAAgB,GACpB,OAAOlC,EACN,CAAC,CAAE,MAAAQ,CAAM,EAAGC,IAAS,CACpB,MAAM0B,EAAS,CAAC,GAAG3B,CAAK,MAAMwB,CAAS,IAAIxB,CAAK,IAAKC,EAAK,UAAU,GAAGD,CAAK,wBAAyBA,CAAK,CAAC,EAE3G,OADAyB,EAAiB,CAACA,EACbA,EACE,CACN,YAAYD,CAAS,WACrB,GAAGlC,EAAc,CAAE,KAAM,CAAE,GAAGW,EAAM,UAAW,EAAK,EAAG,KAAMmB,EAAO,EAAG,MAAO,OAAQ,UAAW,QAAS,CAAC,EAAE,IAC3Gf,GAAM,IAAIA,CAAC,EACb,EACA,eACA,IACA,GAAGsB,CACJ,EAT4BA,CAU7B,EACA,CACC,OAAQ,KACPD,EAAgB,CAACA,EACZA,EACEjC,EAAO2B,EAAO,EAAG,CAAE,OAAAG,CAAO,CAAC,EADP,CAAE,OAAAA,CAAO,EAGtC,CACD,CACD","names":["differMerge","getRandomValue","wrapInTryCatch","compileNested","context","standard","schema","define","validate","or","branches","validatedVarname","errorsVarname","input","opts","branch","idx","lines","l","acc","cur","merge","branch1","branch2","inputVarname","discriminate","discriminator","err","path","key","s","fromJson","lazy","pipeFn","result","recursive","$refId","fnVarname","compiledBefore","schemedBefore","common"]}
|
|
@@ -103,8 +103,8 @@ const recursive = (pipeFn, $refId) => {
|
|
|
103
103
|
return standard(
|
|
104
104
|
({ input }, opts) => {
|
|
105
105
|
const common = [`${input} = ${fnVarname}(${input})`, opts.wrapError(`${input} instanceof PipeError`, input)];
|
|
106
|
-
|
|
107
|
-
compiledBefore
|
|
106
|
+
compiledBefore = !compiledBefore;
|
|
107
|
+
if (!compiledBefore) return common;
|
|
108
108
|
return [
|
|
109
109
|
`function ${fnVarname}(node) {`,
|
|
110
110
|
...compileNested({ opts: { ...opts, failEarly: true }, pipe: pipeFn(), input: "node", errorType: "return" }).map(
|
|
@@ -117,8 +117,8 @@ const recursive = (pipeFn, $refId) => {
|
|
|
117
117
|
},
|
|
118
118
|
{
|
|
119
119
|
schema: () => {
|
|
120
|
-
|
|
121
|
-
schemedBefore
|
|
120
|
+
schemedBefore = !schemedBefore;
|
|
121
|
+
if (!schemedBefore) return { $refId };
|
|
122
122
|
return schema(pipeFn(), { $refId });
|
|
123
123
|
}
|
|
124
124
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/api/junctions.ts"],"sourcesContent":["import { Pipe, PipeInput, PipeOutput } from './base'\nimport { merge as differMerge } from '../utils/differ'\nimport { getRandomValue, wrapInTryCatch } from '../utils/functions'\nimport { compileNested, context, standard, schema, define, validate } from './base/pipes'\n\nexport const or = <T extends Pipe<any, any>[]>(branches: T) => {\n\tconst validatedVarname = `validated_${getRandomValue()}`\n\tconst errorsVarname = `errors_${getRandomValue()}`\n\treturn standard<PipeInput<T[number]>, PipeOutput<T[number]>>(\n\t\t({ input }, opts) =>\n\t\t\tbranches.length === 0\n\t\t\t\t? []\n\t\t\t\t: [\n\t\t\t\t\t\t`const ${errorsVarname} = []`,\n\t\t\t\t\t\t`let ${validatedVarname}`,\n\t\t\t\t\t\t...branches\n\t\t\t\t\t\t\t.map((branch, idx) => (lines: string[]) => [\n\t\t\t\t\t\t\t\t`${validatedVarname} = ${input}`,\n\t\t\t\t\t\t\t\t...compileNested({\n\t\t\t\t\t\t\t\t\topts: { ...opts, failEarly: true },\n\t\t\t\t\t\t\t\t\tpipe: branch,\n\t\t\t\t\t\t\t\t\tinput: validatedVarname,\n\t\t\t\t\t\t\t\t\terrorType: 'assign',\n\t\t\t\t\t\t\t\t\tkey: validatedVarname,\n\t\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t\t\t`if (${validatedVarname} instanceof PipeError) {`,\n\t\t\t\t\t\t\t\t`\t${errorsVarname}.push(PipeError.path(${idx}, '${validatedVarname}', ${validatedVarname}))`,\n\t\t\t\t\t\t\t\t...lines.map((l) => `\t${l}`),\n\t\t\t\t\t\t\t\tidx === branches.length - 1 ? `\t${opts.wrapError.format(`PipeError.rootFrom(${errorsVarname})`)}` : '',\n\t\t\t\t\t\t\t\t`}`,\n\t\t\t\t\t\t\t\t`else ${input} = ${validatedVarname}`,\n\t\t\t\t\t\t\t])\n\t\t\t\t\t\t\t.reduceRight<string[]>((acc, cur) => cur(acc), []),\n\t\t\t\t\t\topts.wrapError(`${input} instanceof PipeError`, input),\n\t\t\t\t\t],\n\t\t{\n\t\t\tschema: () => ({ oneOf: branches.map((branch) => schema(branch)) }),\n\t\t},\n\t)\n}\n\nexport const merge = <T1 extends Pipe<any, any>, T2 extends Pipe<any, any>>(branch1: T1, branch2: T2) => {\n\tconst inputVarname = `input_${getRandomValue()}`\n\treturn standard<PipeInput<T1> & PipeInput<T2>, PipeOutput<T1> & PipeOutput<T2>>(\n\t\t({ input, context }, opts) => [\n\t\t\t`let ${inputVarname}A = ${input}`,\n\t\t\t`let ${inputVarname}B = ${input}`,\n\t\t\t...compileNested({ opts, pipe: branch1, input: `${inputVarname}A` }),\n\t\t\topts.wrapError(`${inputVarname}A instanceof PipeError`, `${inputVarname}A`),\n\t\t\t...compileNested({ opts, pipe: branch2, input: `${inputVarname}B` }),\n\t\t\topts.wrapError(`${inputVarname}B instanceof PipeError`, `${inputVarname}B`),\n\t\t\t`${input} = ${context}.differMerge(${inputVarname}A, ${inputVarname}B)`,\n\t\t],\n\t\t{\n\t\t\tcontext: { differMerge },\n\t\t\tschema: () => ({ allOf: [schema(branch1), schema(branch2)] }),\n\t\t},\n\t)\n}\n\nexport const discriminate = <T extends Record<PropertyKey, Pipe<any, any>>>(\n\tdiscriminator: (val: PipeInput<T[keyof T]>) => PropertyKey,\n\tbranches: T,\n\terr = 'doesnt match any of the schema',\n) =>\n\tstandard<PipeInput<T[keyof T]>, PipeOutput<T[keyof T]>>(\n\t\t({ input, context, path }, opts) => [\n\t\t\t`switch (${context}.wrapInTryCatch(() => ${context}.discriminator(${input}))) {`,\n\t\t\t...Object.entries(branches).flatMap(([key, branch]) => [\n\t\t\t\t`\tcase ('${key}'): {`,\n\t\t\t\t...compileNested({ opts, pipe: branch, input }).map((l) => `\t\t${l}`),\n\t\t\t\t`\t\tbreak`,\n\t\t\t\t`\t}`,\n\t\t\t]),\n\t\t\t`\tdefault: ${opts.wrapError.format(`PipeError.root(\"${err}\", ${input}, ${path})`)}`,\n\t\t\t`}`,\n\t\t],\n\t\t{\n\t\t\tcontext: { wrapInTryCatch, discriminator },\n\t\t\tschema: () => ({ oneOf: Object.values(branches).map((s) => schema(s)) }),\n\t\t},\n\t)\n\nexport const fromJson = <T extends Pipe<any, any>>(branch: T) => {\n\tconst validatedVarname = `validated_${getRandomValue()}`\n\treturn standard<PipeInput<T>, PipeOutput<T>>(\n\t\t({ input, context }, opts) => [\n\t\t\t`let ${validatedVarname} = ${input}`,\n\t\t\t...compileNested({ opts, pipe: branch, input: validatedVarname, errorType: 'assign' }),\n\t\t\t`if (${validatedVarname} instanceof PipeError) {`,\n\t\t\topts.wrapError(`${input}?.constructor?.name !== 'String'`, validatedVarname),\n\t\t\t`\t${validatedVarname} = ${context}.wrapInTryCatch(() => JSON.parse(${input}), ${validatedVarname})`,\n\t\t\topts.wrapError(`${validatedVarname} instanceof PipeError`, validatedVarname),\n\t\t\t...compileNested({ opts, pipe: branch, input: validatedVarname, errorType: 'assign' }).map((l) => `\t${l}`),\n\t\t\topts.wrapError(`${validatedVarname} instanceof PipeError`, validatedVarname),\n\t\t\t`}`,\n\t\t\t`${input} = ${validatedVarname}`,\n\t\t],\n\t\t{\n\t\t\tcontext: { ...context(branch), wrapInTryCatch },\n\t\t\tschema: branch.schema,\n\t\t},\n\t)\n}\n\nexport const lazy = <T extends Pipe<any, any>>(pipeFn: () => T) =>\n\tdefine<PipeInput<T>, PipeOutput<T>>(\n\t\t(input) => {\n\t\t\tconst result = validate(pipeFn(), input)\n\t\t\treturn result.valid ? result.value : result.error\n\t\t},\n\t\t{\n\t\t\tschema: () => schema(pipeFn()),\n\t\t},\n\t)\n\nexport const recursive = <T extends Pipe<any, any>>(pipeFn: () => T, $refId: string) => {\n\tconst fnVarname = `fn_${getRandomValue()}`\n\tlet compiledBefore = false\n\tlet schemedBefore = false\n\treturn standard<PipeInput<T>, PipeOutput<T>>(\n\t\t({ input }, opts) => {\n\t\t\tconst common = [`${input} = ${fnVarname}(${input})`, opts.wrapError(`${input} instanceof PipeError`, input)]\n\t\t\
|
|
1
|
+
{"version":3,"sources":["../../../src/api/junctions.ts"],"sourcesContent":["import { Pipe, PipeInput, PipeOutput } from './base'\nimport { merge as differMerge } from '../utils/differ'\nimport { getRandomValue, wrapInTryCatch } from '../utils/functions'\nimport { compileNested, context, standard, schema, define, validate } from './base/pipes'\n\nexport const or = <T extends Pipe<any, any>[]>(branches: T) => {\n\tconst validatedVarname = `validated_${getRandomValue()}`\n\tconst errorsVarname = `errors_${getRandomValue()}`\n\treturn standard<PipeInput<T[number]>, PipeOutput<T[number]>>(\n\t\t({ input }, opts) =>\n\t\t\tbranches.length === 0\n\t\t\t\t? []\n\t\t\t\t: [\n\t\t\t\t\t\t`const ${errorsVarname} = []`,\n\t\t\t\t\t\t`let ${validatedVarname}`,\n\t\t\t\t\t\t...branches\n\t\t\t\t\t\t\t.map((branch, idx) => (lines: string[]) => [\n\t\t\t\t\t\t\t\t`${validatedVarname} = ${input}`,\n\t\t\t\t\t\t\t\t...compileNested({\n\t\t\t\t\t\t\t\t\topts: { ...opts, failEarly: true },\n\t\t\t\t\t\t\t\t\tpipe: branch,\n\t\t\t\t\t\t\t\t\tinput: validatedVarname,\n\t\t\t\t\t\t\t\t\terrorType: 'assign',\n\t\t\t\t\t\t\t\t\tkey: validatedVarname,\n\t\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t\t\t`if (${validatedVarname} instanceof PipeError) {`,\n\t\t\t\t\t\t\t\t`\t${errorsVarname}.push(PipeError.path(${idx}, '${validatedVarname}', ${validatedVarname}))`,\n\t\t\t\t\t\t\t\t...lines.map((l) => `\t${l}`),\n\t\t\t\t\t\t\t\tidx === branches.length - 1 ? `\t${opts.wrapError.format(`PipeError.rootFrom(${errorsVarname})`)}` : '',\n\t\t\t\t\t\t\t\t`}`,\n\t\t\t\t\t\t\t\t`else ${input} = ${validatedVarname}`,\n\t\t\t\t\t\t\t])\n\t\t\t\t\t\t\t.reduceRight<string[]>((acc, cur) => cur(acc), []),\n\t\t\t\t\t\topts.wrapError(`${input} instanceof PipeError`, input),\n\t\t\t\t\t],\n\t\t{\n\t\t\tschema: () => ({ oneOf: branches.map((branch) => schema(branch)) }),\n\t\t},\n\t)\n}\n\nexport const merge = <T1 extends Pipe<any, any>, T2 extends Pipe<any, any>>(branch1: T1, branch2: T2) => {\n\tconst inputVarname = `input_${getRandomValue()}`\n\treturn standard<PipeInput<T1> & PipeInput<T2>, PipeOutput<T1> & PipeOutput<T2>>(\n\t\t({ input, context }, opts) => [\n\t\t\t`let ${inputVarname}A = ${input}`,\n\t\t\t`let ${inputVarname}B = ${input}`,\n\t\t\t...compileNested({ opts, pipe: branch1, input: `${inputVarname}A` }),\n\t\t\topts.wrapError(`${inputVarname}A instanceof PipeError`, `${inputVarname}A`),\n\t\t\t...compileNested({ opts, pipe: branch2, input: `${inputVarname}B` }),\n\t\t\topts.wrapError(`${inputVarname}B instanceof PipeError`, `${inputVarname}B`),\n\t\t\t`${input} = ${context}.differMerge(${inputVarname}A, ${inputVarname}B)`,\n\t\t],\n\t\t{\n\t\t\tcontext: { differMerge },\n\t\t\tschema: () => ({ allOf: [schema(branch1), schema(branch2)] }),\n\t\t},\n\t)\n}\n\nexport const discriminate = <T extends Record<PropertyKey, Pipe<any, any>>>(\n\tdiscriminator: (val: PipeInput<T[keyof T]>) => PropertyKey,\n\tbranches: T,\n\terr = 'doesnt match any of the schema',\n) =>\n\tstandard<PipeInput<T[keyof T]>, PipeOutput<T[keyof T]>>(\n\t\t({ input, context, path }, opts) => [\n\t\t\t`switch (${context}.wrapInTryCatch(() => ${context}.discriminator(${input}))) {`,\n\t\t\t...Object.entries(branches).flatMap(([key, branch]) => [\n\t\t\t\t`\tcase ('${key}'): {`,\n\t\t\t\t...compileNested({ opts, pipe: branch, input }).map((l) => `\t\t${l}`),\n\t\t\t\t`\t\tbreak`,\n\t\t\t\t`\t}`,\n\t\t\t]),\n\t\t\t`\tdefault: ${opts.wrapError.format(`PipeError.root(\"${err}\", ${input}, ${path})`)}`,\n\t\t\t`}`,\n\t\t],\n\t\t{\n\t\t\tcontext: { wrapInTryCatch, discriminator },\n\t\t\tschema: () => ({ oneOf: Object.values(branches).map((s) => schema(s)) }),\n\t\t},\n\t)\n\nexport const fromJson = <T extends Pipe<any, any>>(branch: T) => {\n\tconst validatedVarname = `validated_${getRandomValue()}`\n\treturn standard<PipeInput<T>, PipeOutput<T>>(\n\t\t({ input, context }, opts) => [\n\t\t\t`let ${validatedVarname} = ${input}`,\n\t\t\t...compileNested({ opts, pipe: branch, input: validatedVarname, errorType: 'assign' }),\n\t\t\t`if (${validatedVarname} instanceof PipeError) {`,\n\t\t\topts.wrapError(`${input}?.constructor?.name !== 'String'`, validatedVarname),\n\t\t\t`\t${validatedVarname} = ${context}.wrapInTryCatch(() => JSON.parse(${input}), ${validatedVarname})`,\n\t\t\topts.wrapError(`${validatedVarname} instanceof PipeError`, validatedVarname),\n\t\t\t...compileNested({ opts, pipe: branch, input: validatedVarname, errorType: 'assign' }).map((l) => `\t${l}`),\n\t\t\topts.wrapError(`${validatedVarname} instanceof PipeError`, validatedVarname),\n\t\t\t`}`,\n\t\t\t`${input} = ${validatedVarname}`,\n\t\t],\n\t\t{\n\t\t\tcontext: { ...context(branch), wrapInTryCatch },\n\t\t\tschema: branch.schema,\n\t\t},\n\t)\n}\n\nexport const lazy = <T extends Pipe<any, any>>(pipeFn: () => T) =>\n\tdefine<PipeInput<T>, PipeOutput<T>>(\n\t\t(input) => {\n\t\t\tconst result = validate(pipeFn(), input)\n\t\t\treturn result.valid ? result.value : result.error\n\t\t},\n\t\t{\n\t\t\tschema: () => schema(pipeFn()),\n\t\t},\n\t)\n\nexport const recursive = <T extends Pipe<any, any>>(pipeFn: () => T, $refId: string) => {\n\tconst fnVarname = `fn_${getRandomValue()}`\n\tlet compiledBefore = false\n\tlet schemedBefore = false\n\treturn standard<PipeInput<T>, PipeOutput<T>>(\n\t\t({ input }, opts) => {\n\t\t\tconst common = [`${input} = ${fnVarname}(${input})`, opts.wrapError(`${input} instanceof PipeError`, input)]\n\t\t\tcompiledBefore = !compiledBefore\n\t\t\tif (!compiledBefore) return common\n\t\t\treturn [\n\t\t\t\t`function ${fnVarname}(node) {`,\n\t\t\t\t...compileNested({ opts: { ...opts, failEarly: true }, pipe: pipeFn(), input: 'node', errorType: 'return' }).map(\n\t\t\t\t\t(l) => `\t${l}`,\n\t\t\t\t),\n\t\t\t\t`\treturn node`,\n\t\t\t\t'}',\n\t\t\t\t...common,\n\t\t\t]\n\t\t},\n\t\t{\n\t\t\tschema: () => {\n\t\t\t\tschemedBefore = !schemedBefore\n\t\t\t\tif (!schemedBefore) return { $refId }\n\t\t\t\treturn schema(pipeFn(), { $refId })\n\t\t\t},\n\t\t},\n\t)\n}\n"],"mappings":"AACA,SAAS,SAAS,mBAAmB;AACrC,SAAS,gBAAgB,sBAAsB;AAC/C,SAAS,eAAe,SAAS,UAAU,QAAQ,QAAQ,gBAAgB;AAEpE,MAAM,KAAK,CAA6B,aAAgB;AAC9D,QAAM,mBAAmB,aAAa,eAAe,CAAC;AACtD,QAAM,gBAAgB,UAAU,eAAe,CAAC;AAChD,SAAO;AAAA,IACN,CAAC,EAAE,MAAM,GAAG,SACX,SAAS,WAAW,IACjB,CAAC,IACD;AAAA,MACA,SAAS,aAAa;AAAA,MACtB,OAAO,gBAAgB;AAAA,MACvB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AE,GAAG,MAAM,WAAW,KAAK,GAAG,MAAM,OAAO,GAAG,OAAO,QAAQ,WAAW,SAAS,CAAC,EAAE;AAAA,UAC5G,CAAC,MAAM,IAAI,CAAC;AAAA,QACb;AAAA,QACA;AAAA,QACA;AAAA,QACA,GAAG;AAAA,MACJ;AAAA,IACD;AAAA,IACA;AAAA,MACC,QAAQ,MAAM;AACb,wBAAgB,CAAC;AACjB,YAAI,CAAC,cAAe,QAAO,EAAE,OAAO;AACpC,eAAO,OAAO,OAAO,GAAG,EAAE,OAAO,CAAC;AAAA,MACnC;AAAA,IACD;AAAA,EACD;AACD;","names":["context"]}
|
|
@@ -103,8 +103,8 @@ const recursive = (pipeFn, $refId) => {
|
|
|
103
103
|
return standard(
|
|
104
104
|
({ input }, opts) => {
|
|
105
105
|
const common = [`${input} = ${fnVarname}(${input})`, opts.wrapError(`${input} instanceof PipeError`, input)];
|
|
106
|
-
|
|
107
|
-
compiledBefore
|
|
106
|
+
compiledBefore = !compiledBefore;
|
|
107
|
+
if (!compiledBefore) return common;
|
|
108
108
|
return [
|
|
109
109
|
`function ${fnVarname}(node) {`,
|
|
110
110
|
...compileNested({ opts: { ...opts, failEarly: true }, pipe: pipeFn(), input: "node", errorType: "return" }).map(
|
|
@@ -117,8 +117,8 @@ const recursive = (pipeFn, $refId) => {
|
|
|
117
117
|
},
|
|
118
118
|
{
|
|
119
119
|
schema: () => {
|
|
120
|
-
|
|
121
|
-
schemedBefore
|
|
120
|
+
schemedBefore = !schemedBefore;
|
|
121
|
+
if (!schemedBefore) return { $refId };
|
|
122
122
|
return schema(pipeFn(), { $refId });
|
|
123
123
|
}
|
|
124
124
|
}
|
package/package.json
CHANGED