valleyed 4.5.11 → 4.5.12
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 +2 -0
- package/dist/cjs/api/base/errors.cjs +0 -3
- package/dist/cjs/api/base/errors.cjs.map +1 -1
- package/dist/cjs/api/base/errors.min.cjs +2 -2
- package/dist/cjs/api/base/errors.min.cjs.map +1 -1
- package/dist/cjs/api/base/pipes.cjs +7 -6
- package/dist/cjs/api/base/pipes.cjs.map +1 -1
- package/dist/cjs/api/base/pipes.min.cjs +2 -2
- package/dist/cjs/api/base/pipes.min.cjs.map +1 -1
- package/dist/cjs/api/base/types.cjs.map +1 -1
- package/dist/cjs/api/base/types.min.cjs +1 -1
- package/dist/cjs/api/base/types.min.cjs.map +1 -1
- package/dist/cjs/api/junctions.cjs +1 -1
- 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/base/errors.min.mjs +2 -2
- package/dist/esm/api/base/errors.min.mjs.map +1 -1
- package/dist/esm/api/base/errors.mjs +0 -3
- package/dist/esm/api/base/errors.mjs.map +1 -1
- package/dist/esm/api/base/pipes.min.mjs +2 -2
- package/dist/esm/api/base/pipes.min.mjs.map +1 -1
- package/dist/esm/api/base/pipes.mjs +7 -6
- package/dist/esm/api/base/pipes.mjs.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 +1 -1
- package/dist/esm/api/junctions.mjs.map +1 -1
- package/dist/types/api/arrays.d.ts +1 -1
- package/dist/types/api/base/errors.d.ts +1 -1
- package/dist/types/api/base/errors.js +0 -3
- package/dist/types/api/base/index.d.ts +1 -1
- package/dist/types/api/base/pipes.d.ts +8 -2
- package/dist/types/api/base/pipes.js +7 -6
- package/dist/types/api/base/types.d.ts +1 -1
- package/dist/types/api/coerce.d.ts +1 -1
- package/dist/types/api/core.d.ts +1 -1
- package/dist/types/api/externals.d.ts +1 -1
- package/dist/types/api/files.d.ts +1 -1
- package/dist/types/api/index.d.ts +1 -1
- package/dist/types/api/junctions.d.ts +1 -1
- package/dist/types/api/junctions.js +1 -1
- package/dist/types/api/numbers.d.ts +1 -1
- package/dist/types/api/optionals.d.ts +1 -1
- package/dist/types/api/records.d.ts +1 -1
- package/dist/types/api/strings.d.ts +1 -1
- package/dist/types/api/times.d.ts +1 -1
- package/dist/types/api/types.d.ts +1 -1
- package/dist/types/{errors-B4JoRN9o.d.ts → errors-C1ef9q7I.d.ts} +1 -5
- package/dist/types/index.d.ts +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
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.12](https://github.com/kevinand11/valleyed/compare/v4.5.11...v4.5.12) (2025-07-05)
|
|
6
|
+
|
|
5
7
|
### [4.5.11](https://github.com/kevinand11/valleyed/compare/v4.5.10...v4.5.11) (2025-07-05)
|
|
6
8
|
|
|
7
9
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../src/api/base/errors.ts"],"sourcesContent":["import { PipeErrorHandler } from './types'\n\ntype PipeErrorMessage = { message: string; path?: string; value: unknown }\n\nfunction formatError(message: PipeErrorMessage) {\n\treturn `${message.path ? `${message.path}: ` : ''}${message.message}`\n}\n\nexport class PipeError {\n\tconstructor(public messages: PipeErrorMessage[]) {}\n\n\
|
|
1
|
+
{"version":3,"sources":["../../../../src/api/base/errors.ts"],"sourcesContent":["import { PipeErrorHandler } from './types'\n\ntype PipeErrorMessage = { message: string; path?: string; value: unknown }\n\nfunction formatError(message: PipeErrorMessage) {\n\treturn `${message.path ? `${message.path}: ` : ''}${message.message}`\n}\n\nexport class PipeError {\n\tconstructor(public messages: PipeErrorMessage[]) {}\n\n\ttoString() {\n\t\treturn this.messages.map(formatError).join('\\n')\n\t}\n\n\tstatic root(message: string, value: unknown, path?: string) {\n\t\treturn new PipeError([{ message, path, value }])\n\t}\n\n\tstatic rootFrom(errors: PipeError[]) {\n\t\treturn new PipeError(errors.flatMap((error) => error.messages))\n\t}\n\n\tstatic path(path: PropertyKey, error: PipeError) {\n\t\tif (path === 0) path = '0'\n\t\tif (!path) return error\n\t\treturn new PipeError(\n\t\t\terror.messages.map((message) => ({ ...message, path: `${path.toString()}${message.path ? `.${message.path}` : ''}` })),\n\t\t)\n\t}\n}\n\nexport function createErrorHandler(input: string, type: 'return' | 'throw' | 'assign'): PipeErrorHandler {\n\tconst handler: PipeErrorHandler = Object.assign(\n\t\t(...args: Parameters<PipeErrorHandler>) =>\n\t\t\t(lines: string[]) => {\n\t\t\t\tswitch (type) {\n\t\t\t\t\tcase 'return':\n\t\t\t\t\tcase 'throw':\n\t\t\t\t\t\treturn [`if (${args[0]}) ${handler.format(args[1])}`, ...lines]\n\t\t\t\t\tcase 'assign':\n\t\t\t\t\t\treturn [\n\t\t\t\t\t\t\t`if (${args[0]}) ${handler.format(args[1])}`,\n\t\t\t\t\t\t\t...(lines.length ? [`else {`, ...lines.map((l) => `\t${l}`), `}`] : []),\n\t\t\t\t\t\t]\n\t\t\t\t\tdefault:\n\t\t\t\t\t\tthrow new Error(`Unknown error handling type: ${type satisfies never}`)\n\t\t\t\t}\n\t\t\t},\n\t\t{\n\t\t\ttype,\n\t\t\tformat: (error: string) => {\n\t\t\t\tswitch (type) {\n\t\t\t\t\tcase 'return':\n\t\t\t\t\t\treturn `return ${error}`\n\t\t\t\t\tcase 'throw':\n\t\t\t\t\t\treturn `throw ${error}`\n\t\t\t\t\tcase 'assign':\n\t\t\t\t\t\treturn `${input} = ${error}`\n\t\t\t\t\tdefault:\n\t\t\t\t\t\tthrow new Error(`Unknown error handling type: ${type satisfies never}`)\n\t\t\t\t}\n\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,OAAO,KAAK,CAAC,CAAC,CAAC;AAAA,YAC1C,GAAI,MAAM,SAAS,CAAC,UAAU,GAAG,MAAM,IAAI,CAAC,MAAM,IAAI,CAAC,EAAE,GAAG,GAAG,IAAI,CAAC;AAAA,UACrE;AAAA,QACD;AACC,gBAAM,IAAI,MAAM,gCAAgC,IAAoB,EAAE;AAAA,MACxE;AAAA,IACD;AAAA,IACD;AAAA,MACC;AAAA,MACA,QAAQ,CAAC,UAAkB;AAC1B,gBAAQ,MAAM;AAAA,UACb,KAAK;AACJ,mBAAO,UAAU,KAAK;AAAA,UACvB,KAAK;AACJ,mBAAO,SAAS,KAAK;AAAA,UACtB,KAAK;AACJ,mBAAO,GAAG,KAAK,MAAM,KAAK;AAAA,UAC3B;AACC,kBAAM,IAAI,MAAM,gCAAgC,IAAoB,EAAE;AAAA,QACxE;AAAA,MACD;AAAA,IACD;AAAA,EACD;AAEA,SAAO;AACR;","names":[]}
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
"use strict";var o=Object.defineProperty;var u=Object.getOwnPropertyDescriptor;var c=Object.getOwnPropertyNames;var
|
|
2
|
-
`)}static root(r,t
|
|
1
|
+
"use strict";var o=Object.defineProperty;var u=Object.getOwnPropertyDescriptor;var c=Object.getOwnPropertyNames;var p=Object.prototype.hasOwnProperty;var g=(n,r)=>{for(var e in r)o(n,e,{get:r[e],enumerable:!0})},h=(n,r,e,t)=>{if(r&&typeof r=="object"||typeof r=="function")for(let s of c(r))!p.call(n,s)&&s!==e&&o(n,s,{get:()=>r[s],enumerable:!(t=u(r,s))||t.enumerable});return n};var w=n=>h(o({},"__esModule",{value:!0}),n);var l={};g(l,{PipeError:()=>a,createErrorHandler:()=>f});module.exports=w(l);function $(n){return`${n.path?`${n.path}: `:""}${n.message}`}class a{constructor(r){this.messages=r}toString(){return this.messages.map($).join(`
|
|
2
|
+
`)}static root(r,e,t){return new a([{message:r,path:t,value:e}])}static rootFrom(r){return new a(r.flatMap(e=>e.messages))}static path(r,e){return r===0&&(r="0"),r?new a(e.messages.map(t=>({...t,path:`${r.toString()}${t.path?`.${t.path}`:""}`}))):e}}function f(n,r){const e=Object.assign((...t)=>s=>{switch(r){case"return":case"throw":return[`if (${t[0]}) ${e.format(t[1])}`,...s];case"assign":return[`if (${t[0]}) ${e.format(t[1])}`,...s.length?["else {",...s.map(i=>` ${i}`),"}"]:[]];default:throw new Error(`Unknown error handling type: ${r}`)}},{type:r,format:t=>{switch(r){case"return":return`return ${t}`;case"throw":return`throw ${t}`;case"assign":return`${n} = ${t}`;default:throw new Error(`Unknown error handling type: ${r}`)}}});return e}0&&(module.exports={PipeError,createErrorHandler});
|
|
3
3
|
//# sourceMappingURL=errors.min.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../src/api/base/errors.ts"],"sourcesContent":["import { PipeErrorHandler } from './types'\n\ntype PipeErrorMessage = { message: string; path?: string; value: unknown }\n\nfunction formatError(message: PipeErrorMessage) {\n\treturn `${message.path ? `${message.path}: ` : ''}${message.message}`\n}\n\nexport class PipeError {\n\tconstructor(public messages: PipeErrorMessage[]) {}\n\n\
|
|
1
|
+
{"version":3,"sources":["../../../../src/api/base/errors.ts"],"sourcesContent":["import { PipeErrorHandler } from './types'\n\ntype PipeErrorMessage = { message: string; path?: string; value: unknown }\n\nfunction formatError(message: PipeErrorMessage) {\n\treturn `${message.path ? `${message.path}: ` : ''}${message.message}`\n}\n\nexport class PipeError {\n\tconstructor(public messages: PipeErrorMessage[]) {}\n\n\ttoString() {\n\t\treturn this.messages.map(formatError).join('\\n')\n\t}\n\n\tstatic root(message: string, value: unknown, path?: string) {\n\t\treturn new PipeError([{ message, path, value }])\n\t}\n\n\tstatic rootFrom(errors: PipeError[]) {\n\t\treturn new PipeError(errors.flatMap((error) => error.messages))\n\t}\n\n\tstatic path(path: PropertyKey, error: PipeError) {\n\t\tif (path === 0) path = '0'\n\t\tif (!path) return error\n\t\treturn new PipeError(\n\t\t\terror.messages.map((message) => ({ ...message, path: `${path.toString()}${message.path ? `.${message.path}` : ''}` })),\n\t\t)\n\t}\n}\n\nexport function createErrorHandler(input: string, type: 'return' | 'throw' | 'assign'): PipeErrorHandler {\n\tconst handler: PipeErrorHandler = Object.assign(\n\t\t(...args: Parameters<PipeErrorHandler>) =>\n\t\t\t(lines: string[]) => {\n\t\t\t\tswitch (type) {\n\t\t\t\t\tcase 'return':\n\t\t\t\t\tcase 'throw':\n\t\t\t\t\t\treturn [`if (${args[0]}) ${handler.format(args[1])}`, ...lines]\n\t\t\t\t\tcase 'assign':\n\t\t\t\t\t\treturn [\n\t\t\t\t\t\t\t`if (${args[0]}) ${handler.format(args[1])}`,\n\t\t\t\t\t\t\t...(lines.length ? [`else {`, ...lines.map((l) => `\t${l}`), `}`] : []),\n\t\t\t\t\t\t]\n\t\t\t\t\tdefault:\n\t\t\t\t\t\tthrow new Error(`Unknown error handling type: ${type satisfies never}`)\n\t\t\t\t}\n\t\t\t},\n\t\t{\n\t\t\ttype,\n\t\t\tformat: (error: string) => {\n\t\t\t\tswitch (type) {\n\t\t\t\t\tcase 'return':\n\t\t\t\t\t\treturn `return ${error}`\n\t\t\t\t\tcase 'throw':\n\t\t\t\t\t\treturn `throw ${error}`\n\t\t\t\t\tcase 'assign':\n\t\t\t\t\t\treturn `${input} = ${error}`\n\t\t\t\t\tdefault:\n\t\t\t\t\t\tthrow new Error(`Unknown error handling type: ${type satisfies never}`)\n\t\t\t\t}\n\t\t\t},\n\t\t},\n\t)\n\n\treturn handler\n}\n"],"mappings":"yaAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,eAAAE,EAAA,uBAAAC,IAAA,eAAAC,EAAAJ,GAIA,SAASK,EAAYC,EAA2B,CAC/C,MAAO,GAAGA,EAAQ,KAAO,GAAGA,EAAQ,IAAI,KAAO,EAAE,GAAGA,EAAQ,OAAO,EACpE,CAEO,MAAMJ,CAAU,CACtB,YAAmBK,EAA8B,CAA9B,cAAAA,CAA+B,CAElD,UAAW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AGC,CAAK,MAAMD,CAAK,GAC3B,QACC,MAAM,IAAI,MAAM,gCAAgCE,CAAoB,EAAE,CACxE,CACD,CACD,CACD,EAEA,OAAOC,CACR","names":["errors_exports","__export","PipeError","createErrorHandler","__toCommonJS","formatError","message","messages","value","path","errors","error","input","type","handler","args","lines","l"]}
|
|
@@ -45,16 +45,17 @@ function context(pipe) {
|
|
|
45
45
|
}
|
|
46
46
|
function assert(pipe, input) {
|
|
47
47
|
const result = validate(pipe, input);
|
|
48
|
-
if (!result.valid) throw result;
|
|
48
|
+
if (!result.valid) throw result.error;
|
|
49
49
|
return result.value;
|
|
50
50
|
}
|
|
51
51
|
function validate(pipe, input) {
|
|
52
52
|
try {
|
|
53
53
|
const fn = pipe.__compiled ?? compile(pipe);
|
|
54
|
-
|
|
54
|
+
const res = fn(input);
|
|
55
|
+
return res instanceof import_errors.PipeError ? { error: res, valid: false } : { value: res, valid: true };
|
|
55
56
|
} catch (error) {
|
|
56
|
-
if (error instanceof import_errors.PipeError) return error;
|
|
57
|
-
return import_errors.PipeError.root(error instanceof Error ? error.message : `${error}`, input, void 0);
|
|
57
|
+
if (error instanceof import_errors.PipeError) return { error, valid: false };
|
|
58
|
+
return { error: import_errors.PipeError.root(error instanceof Error ? error.message : `${error}`, input, void 0), valid: false };
|
|
58
59
|
}
|
|
59
60
|
}
|
|
60
61
|
function schema(pipe, schema2 = {}) {
|
|
@@ -74,7 +75,7 @@ function compile(pipe, {
|
|
|
74
75
|
input: inputStr,
|
|
75
76
|
context: contextStr,
|
|
76
77
|
failEarly,
|
|
77
|
-
base: [`return
|
|
78
|
+
base: [`return ${inputStr}`]
|
|
78
79
|
});
|
|
79
80
|
const allLines = [
|
|
80
81
|
`return (${inputStr}) => {`,
|
|
@@ -107,7 +108,7 @@ function standard(compile2, config = {}) {
|
|
|
107
108
|
const validity = validate(piper, value);
|
|
108
109
|
if (validity.valid) return { value: validity.value };
|
|
109
110
|
return {
|
|
110
|
-
issues: validity.messages.map(({ message, path }) => ({
|
|
111
|
+
issues: validity.error.messages.map(({ message, path }) => ({
|
|
111
112
|
message,
|
|
112
113
|
path: path ? path.split(".") : void 0
|
|
113
114
|
}))
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../src/api/base/pipes.ts"],"sourcesContent":["import { createErrorHandler, PipeError } from './errors'\nimport { Context, JsonSchemaBuilder, PipeMeta, Pipe, Entry, PipeOutput, PipeFn, PipeCompiledFn, PipeErrorHandler } from './types'\nimport { getRandomValue } from '../../utils/functions'\nimport { JsonSchema } from '../../utils/types'\n\nexport function walk<T>(pipe: Pipe<any, any>, init: T, nodeFn: (cur: Pipe<any, any>, acc: T) => T) {\n\tlet acc: T = init\n\twhile (pipe) {\n\t\tacc = nodeFn(pipe, acc)\n\t\tpipe = pipe.next!\n\t}\n\treturn acc\n}\n\nexport function context<T extends Pipe<any, any>>(pipe: T): Context {\n\treturn walk(pipe, {} as Context, (p, acc) => ({ ...acc, ...p.context() }))\n}\n\nexport function assert<T extends Pipe<any, any>>(pipe: T, input: unknown): PipeOutput<T> {\n\tconst result = validate(pipe, input)\n\tif (!result.valid) throw result\n\treturn result.value\n}\n\nexport function validate<T extends Pipe<any, any>>(pipe: T, input: unknown): ReturnType<PipeCompiledFn<T>> {\n\ttry {\n\t\tconst fn = pipe.__compiled ?? compile(pipe)\n\t\treturn fn(input) as ReturnType<PipeCompiledFn<T>>\n\t} catch (error) {\n\t\tif (error instanceof PipeError) return error\n\t\treturn PipeError.root(error instanceof Error ? error.message : `${error}`, input, undefined)\n\t}\n}\n\nexport function schema<T extends Pipe<any, any>>(pipe: T, schema: JsonSchema = {}): JsonSchema {\n\tconst cont = context(pipe)\n\treturn walk(pipe, schema, (p, acc) => ({ ...acc, ...p.schema(cont) }))\n}\n\nexport function meta<T extends Pipe<any, any>>(p: T, meta: PipeMeta): T {\n\treturn p.pipe(standard(() => [], { schema: () => meta })) as T\n}\n\nexport function compile<T extends Pipe<any, any>>(\n\tpipe: T,\n\t{\n\t\tfailEarly = true,\n\t}: {\n\t\tfailEarly?: boolean\n\t} = {},\n): PipeCompiledFn<T> {\n\tconst inputStr = 'input'\n\tconst contextStr = 'context'\n\tconst { lines, context } = compilePipeToString({\n\t\tpipe,\n\t\tinput: inputStr,\n\t\tcontext: contextStr,\n\t\tfailEarly,\n\t\tbase: [`return { value: ${inputStr}, valid: true }`],\n\t})\n\tconst allLines = [\n\t\t`return (${inputStr}) => {`,\n\t\t...lines.filter((l) => l.trim() !== '').map((l) => `\\t${l}`),\n\t\t`\tthrow PipeError.root('unhandled root validation', ${inputStr})`,\n\t\t`}`,\n\t]\n\tpipe.__compiled = new Function(contextStr, 'PipeError', allLines.join('\\n'))(context, PipeError)\n\treturn pipe.__compiled!\n}\n\nexport function standard<I, O>(\n\tcompile: Pipe<I, O>['compile'],\n\tconfig: {\n\t\tcontext?: Context\n\t\tschema?: (context: Context) => JsonSchemaBuilder\n\t} = {},\n): Pipe<I, O> {\n\tconst piper: Pipe<I, O> = {\n\t\tcontext: () => config.context ?? ({} as any),\n\t\tschema: (context: Context) => config.schema?.(context) ?? ({} as any),\n\t\tpipe: (...entries: Entry<any, any>[]) => {\n\t\t\tdelete piper.__compiled\n\t\t\tfor (const cur of entries) {\n\t\t\t\tconst p = typeof cur === 'function' ? define(cur, config) : cur\n\t\t\t\tif (!piper.next) piper.next = p\n\t\t\t\tif (piper.last) piper.last.next = p\n\t\t\t\tpiper.last = p.last ?? p\n\t\t\t}\n\t\t\treturn piper\n\t\t},\n\t\tcompile,\n\t\t'~standard': {\n\t\t\tversion: 1,\n\t\t\tvendor: 'valleyed',\n\t\t\tvalidate(value) {\n\t\t\t\tconst validity = validate(piper, value)\n\t\t\t\tif (validity.valid) return { value: validity.value }\n\t\t\t\treturn {\n\t\t\t\t\tissues: validity.messages.map(({ message, path }) => ({\n\t\t\t\t\t\tmessage,\n\t\t\t\t\t\tpath: path ? path.split('.') : undefined,\n\t\t\t\t\t})),\n\t\t\t\t}\n\t\t\t},\n\t\t},\n\t}\n\treturn piper\n}\n\nexport function define<I, O>(\n\tfn: PipeFn<I, O>,\n\tconfig: {\n\t\tcontext?: Context\n\t\tschema?: (context: Context) => JsonSchemaBuilder\n\t} = {},\n): Pipe<I, O> {\n\tconst key = `define_${getRandomValue()}`\n\treturn standard<I, O>(\n\t\t({ input, context, path }) => [\n\t\t\t`${input} = ${context}['${key}'](${input})`,\n\t\t\t`if (${input} instanceof PipeError) return PipeError.path(${path}, ${input})`,\n\t\t],\n\t\t{\n\t\t\tcontext: { ...config?.context, [key]: fn },\n\t\t\tschema: config?.schema,\n\t\t},\n\t)\n}\n\nexport function compileNested(\n\tdata: {\n\t\tpipe: Pipe<any, any>\n\t\terrorType?: Parameters<typeof createErrorHandler>[1]\n\t\topts: Required<Pick<Parameters<Pipe<any, any>['compile']>[1], 'rootContext' | 'failEarly' | 'path' | 'wrapError'>>\n\t} & { input: string; key?: string },\n) {\n\tconst random = getRandomValue()\n\tconst { lines, context } = compilePipeToString({\n\t\t...data.opts,\n\t\twrapError: createErrorHandler(data.input, data.errorType ?? data.opts.wrapError.type),\n\t\tpipe: data.pipe,\n\t\tinput: data.input,\n\t\tcontext: `context[\\`${random}\\`]`,\n\t\tpath: ['key' in data ? data.key : '', data.opts.path].filter(Boolean).join('.') || undefined,\n\t})\n\tdata.opts.rootContext[random] = context\n\treturn lines\n}\n\nfunction compilePipeToString({\n\tpipe,\n\tinput,\n\tcontext: contextStr,\n\tfailEarly = false,\n\tpath = '',\n\trootContext,\n\twrapError = createErrorHandler(input, 'return'),\n\tbase = [],\n}: {\n\tpipe: Pipe<any, any>\n\tinput: string\n\tcontext: string\n\tfailEarly?: boolean\n\tpath?: string\n\trootContext?: Context\n\twrapError?: PipeErrorHandler\n\tbase?: string[]\n}) {\n\tconst ctx = context(pipe)\n\trootContext ??= ctx\n\tconst compiled = walk(pipe, <ReturnType<Pipe<any, any>['compile']>[]>[], (p, acc) => {\n\t\tacc.push(\n\t\t\tp.compile(\n\t\t\t\t{ input, context: contextStr, path: `${path ? `'${path}'` : undefined}` },\n\t\t\t\t{ rootContext, failEarly, path, wrapError },\n\t\t\t),\n\t\t)\n\t\treturn acc\n\t})\n\tconst lines = mergePipeLines(base, compiled.flat())\n\treturn { lines, context: ctx }\n}\n\nfunction mergePipeLines(base: string[], lines: ReturnType<Pipe<any, any>['compile']>) {\n\treturn (Array.isArray(lines) ? lines : [lines]).reduceRight<string[]>((acc, cur) => {\n\t\tif (typeof cur === 'string') acc.unshift(cur)\n\t\telse if (typeof cur === 'function') acc = cur(acc)\n\t\treturn acc\n\t}, base)\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,oBAA8C;AAE9C,uBAA+B;AAGxB,SAAS,KAAQ,MAAsB,MAAS,QAA4C;AAClG,MAAI,MAAS;AACb,SAAO,MAAM;AACZ,UAAM,OAAO,MAAM,GAAG;AACtB,WAAO,KAAK;AAAA,EACb;AACA,SAAO;AACR;AAEO,SAAS,QAAkC,MAAkB;AACnE,SAAO,KAAK,MAAM,CAAC,GAAc,CAAC,GAAG,SAAS,EAAE,GAAG,KAAK,GAAG,EAAE,QAAQ,EAAE,EAAE;AAC1E;AAEO,SAAS,OAAiC,MAAS,OAA+B;AACxF,QAAM,SAAS,SAAS,MAAM,KAAK;AACnC,MAAI,CAAC,OAAO,MAAO,OAAM;AACzB,SAAO,OAAO;AACf;AAEO,SAAS,SAAmC,MAAS,OAA+C;AAC1G,MAAI;AACH,UAAM,KAAK,KAAK,cAAc,QAAQ,IAAI;AAC1C,WAAO,GAAG,KAAK;AAAA,EAChB,SAAS,OAAO;AACf,QAAI,iBAAiB,wBAAW,QAAO;AACvC,WAAO,wBAAU,KAAK,iBAAiB,QAAQ,MAAM,UAAU,GAAG,KAAK,IAAI,OAAO,MAAS;AAAA,EAC5F;AACD;AAEO,SAAS,OAAiC,MAASA,UA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IAAI,EAAE,OAAO,OAAO,EAAE,KAAK,GAAG,KAAK;AAAA,EACpF,CAAC;AACD,OAAK,KAAK,YAAY,MAAM,IAAIA;AAChC,SAAO;AACR;AAEA,SAAS,oBAAoB;AAAA,EAC5B;AAAA,EACA;AAAA,EACA,SAAS;AAAA,EACT,YAAY;AAAA,EACZ,OAAO;AAAA,EACP;AAAA,EACA,gBAAY,kCAAmB,OAAO,QAAQ;AAAA,EAC9C,OAAO,CAAC;AACT,GASG;AACF,QAAM,MAAM,QAAQ,IAAI;AACxB,kBAAgB;AAChB,QAAM,WAAW,KAAK,MAA+C,CAAC,GAAG,CAAC,GAAG,QAAQ;AACpF,QAAI;AAAA,MACH,EAAE;AAAA,QACD,EAAE,OAAO,SAAS,YAAY,MAAM,GAAG,OAAO,IAAI,IAAI,MAAM,MAAS,GAAG;AAAA,QACxE,EAAE,aAAa,WAAW,MAAM,UAAU;AAAA,MAC3C;AAAA,IACD;AACA,WAAO;AAAA,EACR,CAAC;AACD,QAAM,QAAQ,eAAe,MAAM,SAAS,KAAK,CAAC;AAClD,SAAO,EAAE,OAAO,SAAS,IAAI;AAC9B;AAEA,SAAS,eAAe,MAAgB,OAA8C;AACrF,UAAQ,MAAM,QAAQ,KAAK,IAAI,QAAQ,CAAC,KAAK,GAAG,YAAsB,CAAC,KAAK,QAAQ;AACnF,QAAI,OAAO,QAAQ,SAAU,KAAI,QAAQ,GAAG;AAAA,aACnC,OAAO,QAAQ,WAAY,OAAM,IAAI,GAAG;AACjD,WAAO;AAAA,EACR,GAAG,IAAI;AACR;","names":["schema","meta","context","compile"]}
|
|
1
|
+
{"version":3,"sources":["../../../../src/api/base/pipes.ts"],"sourcesContent":["import { createErrorHandler, PipeError } from './errors'\nimport { Context, JsonSchemaBuilder, PipeMeta, Pipe, Entry, PipeOutput, PipeFn, PipeCompiledFn, PipeErrorHandler } from './types'\nimport { getRandomValue } from '../../utils/functions'\nimport { JsonSchema } from '../../utils/types'\n\nexport function walk<T>(pipe: Pipe<any, any>, init: T, nodeFn: (cur: Pipe<any, any>, acc: T) => T) {\n\tlet acc: T = init\n\twhile (pipe) {\n\t\tacc = nodeFn(pipe, acc)\n\t\tpipe = pipe.next!\n\t}\n\treturn acc\n}\n\nexport function context<T extends Pipe<any, any>>(pipe: T): Context {\n\treturn walk(pipe, {} as Context, (p, acc) => ({ ...acc, ...p.context() }))\n}\n\nexport function assert<T extends Pipe<any, any>>(pipe: T, input: unknown): PipeOutput<T> {\n\tconst result = validate(pipe, input)\n\tif (!result.valid) throw result.error\n\treturn result.value\n}\n\nexport function validate<T extends Pipe<any, any>>(\n\tpipe: T,\n\tinput: unknown,\n): { value: PipeOutput<T>; valid: true } | { error: PipeError; valid: false } {\n\ttry {\n\t\tconst fn = pipe.__compiled ?? compile(pipe)\n\t\tconst res = fn(input) as ReturnType<PipeCompiledFn<T>>\n\t\treturn res instanceof PipeError ? { error: res, valid: false } : { value: res, valid: true }\n\t} catch (error) {\n\t\tif (error instanceof PipeError) return { error, valid: false }\n\t\treturn { error: PipeError.root(error instanceof Error ? error.message : `${error}`, input, undefined), valid: false }\n\t}\n}\n\nexport function schema<T extends Pipe<any, any>>(pipe: T, schema: JsonSchema = {}): JsonSchema {\n\tconst cont = context(pipe)\n\treturn walk(pipe, schema, (p, acc) => ({ ...acc, ...p.schema(cont) }))\n}\n\nexport function meta<T extends Pipe<any, any>>(p: T, meta: PipeMeta): T {\n\treturn p.pipe(standard(() => [], { schema: () => meta })) as T\n}\n\nexport function compile<T extends Pipe<any, any>>(\n\tpipe: T,\n\t{\n\t\tfailEarly = true,\n\t}: {\n\t\tfailEarly?: boolean\n\t} = {},\n): PipeCompiledFn<T> {\n\tconst inputStr = 'input'\n\tconst contextStr = 'context'\n\tconst { lines, context } = compilePipeToString({\n\t\tpipe,\n\t\tinput: inputStr,\n\t\tcontext: contextStr,\n\t\tfailEarly,\n\t\tbase: [`return ${inputStr}`],\n\t})\n\tconst allLines = [\n\t\t`return (${inputStr}) => {`,\n\t\t...lines.filter((l) => l.trim() !== '').map((l) => `\\t${l}`),\n\t\t`\tthrow PipeError.root('unhandled root validation', ${inputStr})`,\n\t\t`}`,\n\t]\n\tpipe.__compiled = new Function(contextStr, 'PipeError', allLines.join('\\n'))(context, PipeError)\n\treturn pipe.__compiled!\n}\n\nexport function standard<I, O>(\n\tcompile: Pipe<I, O>['compile'],\n\tconfig: {\n\t\tcontext?: Context\n\t\tschema?: (context: Context) => JsonSchemaBuilder\n\t} = {},\n): Pipe<I, O> {\n\tconst piper: Pipe<I, O> = {\n\t\tcontext: () => config.context ?? ({} as any),\n\t\tschema: (context: Context) => config.schema?.(context) ?? ({} as any),\n\t\tpipe: (...entries: Entry<any, any>[]) => {\n\t\t\tdelete piper.__compiled\n\t\t\tfor (const cur of entries) {\n\t\t\t\tconst p = typeof cur === 'function' ? define(cur, config) : cur\n\t\t\t\tif (!piper.next) piper.next = p\n\t\t\t\tif (piper.last) piper.last.next = p\n\t\t\t\tpiper.last = p.last ?? p\n\t\t\t}\n\t\t\treturn piper\n\t\t},\n\t\tcompile,\n\t\t'~standard': {\n\t\t\tversion: 1,\n\t\t\tvendor: 'valleyed',\n\t\t\tvalidate(value) {\n\t\t\t\tconst validity = validate(piper, value)\n\t\t\t\tif (validity.valid) return { value: validity.value }\n\t\t\t\treturn {\n\t\t\t\t\tissues: validity.error.messages.map(({ message, path }) => ({\n\t\t\t\t\t\tmessage,\n\t\t\t\t\t\tpath: path ? path.split('.') : undefined,\n\t\t\t\t\t})),\n\t\t\t\t}\n\t\t\t},\n\t\t},\n\t}\n\treturn piper\n}\n\nexport function define<I, O>(\n\tfn: PipeFn<I, O>,\n\tconfig: {\n\t\tcontext?: Context\n\t\tschema?: (context: Context) => JsonSchemaBuilder\n\t} = {},\n): Pipe<I, O> {\n\tconst key = `define_${getRandomValue()}`\n\treturn standard<I, O>(\n\t\t({ input, context, path }) => [\n\t\t\t`${input} = ${context}['${key}'](${input})`,\n\t\t\t`if (${input} instanceof PipeError) return PipeError.path(${path}, ${input})`,\n\t\t],\n\t\t{\n\t\t\tcontext: { ...config?.context, [key]: fn },\n\t\t\tschema: config?.schema,\n\t\t},\n\t)\n}\n\nexport function compileNested(\n\tdata: {\n\t\tpipe: Pipe<any, any>\n\t\terrorType?: Parameters<typeof createErrorHandler>[1]\n\t\topts: Required<Pick<Parameters<Pipe<any, any>['compile']>[1], 'rootContext' | 'failEarly' | 'path' | 'wrapError'>>\n\t} & { input: string; key?: string },\n) {\n\tconst random = getRandomValue()\n\tconst { lines, context } = compilePipeToString({\n\t\t...data.opts,\n\t\twrapError: createErrorHandler(data.input, data.errorType ?? data.opts.wrapError.type),\n\t\tpipe: data.pipe,\n\t\tinput: data.input,\n\t\tcontext: `context[\\`${random}\\`]`,\n\t\tpath: ['key' in data ? data.key : '', data.opts.path].filter(Boolean).join('.') || undefined,\n\t})\n\tdata.opts.rootContext[random] = context\n\treturn lines\n}\n\nfunction compilePipeToString({\n\tpipe,\n\tinput,\n\tcontext: contextStr,\n\tfailEarly = false,\n\tpath = '',\n\trootContext,\n\twrapError = createErrorHandler(input, 'return'),\n\tbase = [],\n}: {\n\tpipe: Pipe<any, any>\n\tinput: string\n\tcontext: string\n\tfailEarly?: boolean\n\tpath?: string\n\trootContext?: Context\n\twrapError?: PipeErrorHandler\n\tbase?: string[]\n}) {\n\tconst ctx = context(pipe)\n\trootContext ??= ctx\n\tconst compiled = walk(pipe, <ReturnType<Pipe<any, any>['compile']>[]>[], (p, acc) => {\n\t\tacc.push(\n\t\t\tp.compile(\n\t\t\t\t{ input, context: contextStr, path: `${path ? `'${path}'` : undefined}` },\n\t\t\t\t{ rootContext, failEarly, path, wrapError },\n\t\t\t),\n\t\t)\n\t\treturn acc\n\t})\n\tconst lines = mergePipeLines(base, compiled.flat())\n\treturn { lines, context: ctx }\n}\n\nfunction mergePipeLines(base: string[], lines: ReturnType<Pipe<any, any>['compile']>) {\n\treturn (Array.isArray(lines) ? lines : [lines]).reduceRight<string[]>((acc, cur) => {\n\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,SAAS,oBAAoB;AAAA,EAC5B;AAAA,EACA;AAAA,EACA,SAAS;AAAA,EACT,YAAY;AAAA,EACZ,OAAO;AAAA,EACP;AAAA,EACA,gBAAY,kCAAmB,OAAO,QAAQ;AAAA,EAC9C,OAAO,CAAC;AACT,GASG;AACF,QAAM,MAAM,QAAQ,IAAI;AACxB,kBAAgB;AAChB,QAAM,WAAW,KAAK,MAA+C,CAAC,GAAG,CAAC,GAAG,QAAQ;AACpF,QAAI;AAAA,MACH,EAAE;AAAA,QACD,EAAE,OAAO,SAAS,YAAY,MAAM,GAAG,OAAO,IAAI,IAAI,MAAM,MAAS,GAAG;AAAA,QACxE,EAAE,aAAa,WAAW,MAAM,UAAU;AAAA,MAC3C;AAAA,IACD;AACA,WAAO;AAAA,EACR,CAAC;AACD,QAAM,QAAQ,eAAe,MAAM,SAAS,KAAK,CAAC;AAClD,SAAO,EAAE,OAAO,SAAS,IAAI;AAC9B;AAEA,SAAS,eAAe,MAAgB,OAA8C;AACrF,UAAQ,MAAM,QAAQ,KAAK,IAAI,QAAQ,CAAC,KAAK,GAAG,YAAsB,CAAC,KAAK,QAAQ;AACnF,QAAI,OAAO,QAAQ,SAAU,KAAI,QAAQ,GAAG;AAAA,aACnC,OAAO,QAAQ,WAAY,OAAM,IAAI,GAAG;AACjD,WAAO;AAAA,EACR,GAAG,IAAI;AACR;","names":["schema","meta","context","compile"]}
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
"use strict";var
|
|
2
|
-
`))(i,a.PipeError),e.__compiled}function
|
|
1
|
+
"use strict";var l=Object.defineProperty;var $=Object.getOwnPropertyDescriptor;var C=Object.getOwnPropertyNames;var g=Object.prototype.hasOwnProperty;var w=(e,n)=>{for(var t in n)l(e,t,{get:n[t],enumerable:!0})},O=(e,n,t,r)=>{if(n&&typeof n=="object"||typeof n=="function")for(let o of C(n))!g.call(e,o)&&o!==t&&l(e,o,{get:()=>n[o],enumerable:!(r=$(n,o))||r.enumerable});return e};var S=e=>O(l({},"__esModule",{value:!0}),e);var R={};w(R,{assert:()=>_,compile:()=>T,compileNested:()=>F,context:()=>y,define:()=>h,meta:()=>I,schema:()=>k,standard:()=>x,validate:()=>f,walk:()=>c});module.exports=S(R);var a=require('./errors.min.cjs'),u=require('../../utils/functions/index.min.cjs');function c(e,n,t){let r=n;for(;e;)r=t(e,r),e=e.next;return r}function y(e){return c(e,{},(n,t)=>({...t,...n.context()}))}function _(e,n){const t=f(e,n);if(!t.valid)throw t.error;return t.value}function f(e,n){try{const r=(e.__compiled??T(e))(n);return r instanceof a.PipeError?{error:r,valid:!1}:{value:r,valid:!0}}catch(t){return t instanceof a.PipeError?{error:t,valid:!1}:{error:a.PipeError.root(t instanceof Error?t.message:`${t}`,n,void 0),valid:!1}}}function k(e,n={}){const t=y(e);return c(e,n,(r,o)=>({...o,...r.schema(t)}))}function I(e,n){return e.pipe(x(()=>[],{schema:()=>n}))}function T(e,{failEarly:n=!0}={}){const t="input",r="context",{lines:o,context:i}=P({pipe:e,input:t,context:r,failEarly:n,base:[`return ${t}`]}),p=[`return (${t}) => {`,...o.filter(s=>s.trim()!=="").map(s=>` ${s}`),` throw PipeError.root('unhandled root validation', ${t})`,"}"];return e.__compiled=new Function(r,"PipeError",p.join(`
|
|
2
|
+
`))(i,a.PipeError),e.__compiled}function x(e,n={}){const t={context:()=>n.context??{},schema:r=>n.schema?.(r)??{},pipe:(...r)=>{delete t.__compiled;for(const o of r){const i=typeof o=="function"?h(o,n):o;t.next||(t.next=i),t.last&&(t.last.next=i),t.last=i.last??i}return t},compile:e,"~standard":{version:1,vendor:"valleyed",validate(r){const o=f(t,r);return o.valid?{value:o.value}:{issues:o.error.messages.map(({message:i,path:p})=>({message:i,path:p?p.split("."):void 0}))}}}};return t}function h(e,n={}){const t=`define_${(0,u.getRandomValue)()}`;return x(({input:r,context:o,path:i})=>[`${r} = ${o}['${t}'](${r})`,`if (${r} instanceof PipeError) return PipeError.path(${i}, ${r})`],{context:{...n?.context,[t]:e},schema:n?.schema})}function F(e){const n=(0,u.getRandomValue)(),{lines:t,context:r}=P({...e.opts,wrapError:(0,a.createErrorHandler)(e.input,e.errorType??e.opts.wrapError.type),pipe:e.pipe,input:e.input,context:`context[\`${n}\`]`,path:["key"in e?e.key:"",e.opts.path].filter(Boolean).join(".")||void 0});return e.opts.rootContext[n]=r,t}function P({pipe:e,input:n,context:t,failEarly:r=!1,path:o="",rootContext:i,wrapError:p=(0,a.createErrorHandler)(n,"return"),base:s=[]}){const m=y(e);i??=m;const v=c(e,[],(E,d)=>(d.push(E.compile({input:n,context:t,path:`${o?`'${o}'`:void 0}`},{rootContext:i,failEarly:r,path:o,wrapError:p})),d));return{lines:J(s,v.flat()),context:m}}function J(e,n){return(Array.isArray(n)?n:[n]).reduceRight((t,r)=>(typeof r=="string"?t.unshift(r):typeof r=="function"&&(t=r(t)),t),e)}0&&(module.exports={assert,compile,compileNested,context,define,meta,schema,standard,validate,walk});
|
|
3
3
|
//# sourceMappingURL=pipes.min.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../src/api/base/pipes.ts"],"sourcesContent":["import { createErrorHandler, PipeError } from './errors'\nimport { Context, JsonSchemaBuilder, PipeMeta, Pipe, Entry, PipeOutput, PipeFn, PipeCompiledFn, PipeErrorHandler } from './types'\nimport { getRandomValue } from '../../utils/functions'\nimport { JsonSchema } from '../../utils/types'\n\nexport function walk<T>(pipe: Pipe<any, any>, init: T, nodeFn: (cur: Pipe<any, any>, acc: T) => T) {\n\tlet acc: T = init\n\twhile (pipe) {\n\t\tacc = nodeFn(pipe, acc)\n\t\tpipe = pipe.next!\n\t}\n\treturn acc\n}\n\nexport function context<T extends Pipe<any, any>>(pipe: T): Context {\n\treturn walk(pipe, {} as Context, (p, acc) => ({ ...acc, ...p.context() }))\n}\n\nexport function assert<T extends Pipe<any, any>>(pipe: T, input: unknown): PipeOutput<T> {\n\tconst result = validate(pipe, input)\n\tif (!result.valid) throw result\n\treturn result.value\n}\n\nexport function validate<T extends Pipe<any, any>>(pipe: T, input: unknown): ReturnType<PipeCompiledFn<T>> {\n\ttry {\n\t\tconst fn = pipe.__compiled ?? compile(pipe)\n\t\treturn fn(input) as ReturnType<PipeCompiledFn<T>>\n\t} catch (error) {\n\t\tif (error instanceof PipeError) return error\n\t\treturn PipeError.root(error instanceof Error ? error.message : `${error}`, input, undefined)\n\t}\n}\n\nexport function schema<T extends Pipe<any, any>>(pipe: T, schema: JsonSchema = {}): JsonSchema {\n\tconst cont = context(pipe)\n\treturn walk(pipe, schema, (p, acc) => ({ ...acc, ...p.schema(cont) }))\n}\n\nexport function meta<T extends Pipe<any, any>>(p: T, meta: PipeMeta): T {\n\treturn p.pipe(standard(() => [], { schema: () => meta })) as T\n}\n\nexport function compile<T extends Pipe<any, any>>(\n\tpipe: T,\n\t{\n\t\tfailEarly = true,\n\t}: {\n\t\tfailEarly?: boolean\n\t} = {},\n): PipeCompiledFn<T> {\n\tconst inputStr = 'input'\n\tconst contextStr = 'context'\n\tconst { lines, context } = compilePipeToString({\n\t\tpipe,\n\t\tinput: inputStr,\n\t\tcontext: contextStr,\n\t\tfailEarly,\n\t\tbase: [`return { value: ${inputStr}, valid: true }`],\n\t})\n\tconst allLines = [\n\t\t`return (${inputStr}) => {`,\n\t\t...lines.filter((l) => l.trim() !== '').map((l) => `\\t${l}`),\n\t\t`\tthrow PipeError.root('unhandled root validation', ${inputStr})`,\n\t\t`}`,\n\t]\n\tpipe.__compiled = new Function(contextStr, 'PipeError', allLines.join('\\n'))(context, PipeError)\n\treturn pipe.__compiled!\n}\n\nexport function standard<I, O>(\n\tcompile: Pipe<I, O>['compile'],\n\tconfig: {\n\t\tcontext?: Context\n\t\tschema?: (context: Context) => JsonSchemaBuilder\n\t} = {},\n): Pipe<I, O> {\n\tconst piper: Pipe<I, O> = {\n\t\tcontext: () => config.context ?? ({} as any),\n\t\tschema: (context: Context) => config.schema?.(context) ?? ({} as any),\n\t\tpipe: (...entries: Entry<any, any>[]) => {\n\t\t\tdelete piper.__compiled\n\t\t\tfor (const cur of entries) {\n\t\t\t\tconst p = typeof cur === 'function' ? define(cur, config) : cur\n\t\t\t\tif (!piper.next) piper.next = p\n\t\t\t\tif (piper.last) piper.last.next = p\n\t\t\t\tpiper.last = p.last ?? p\n\t\t\t}\n\t\t\treturn piper\n\t\t},\n\t\tcompile,\n\t\t'~standard': {\n\t\t\tversion: 1,\n\t\t\tvendor: 'valleyed',\n\t\t\tvalidate(value) {\n\t\t\t\tconst validity = validate(piper, value)\n\t\t\t\tif (validity.valid) return { value: validity.value }\n\t\t\t\treturn {\n\t\t\t\t\tissues: validity.messages.map(({ message, path }) => ({\n\t\t\t\t\t\tmessage,\n\t\t\t\t\t\tpath: path ? path.split('.') : undefined,\n\t\t\t\t\t})),\n\t\t\t\t}\n\t\t\t},\n\t\t},\n\t}\n\treturn piper\n}\n\nexport function define<I, O>(\n\tfn: PipeFn<I, O>,\n\tconfig: {\n\t\tcontext?: Context\n\t\tschema?: (context: Context) => JsonSchemaBuilder\n\t} = {},\n): Pipe<I, O> {\n\tconst key = `define_${getRandomValue()}`\n\treturn standard<I, O>(\n\t\t({ input, context, path }) => [\n\t\t\t`${input} = ${context}['${key}'](${input})`,\n\t\t\t`if (${input} instanceof PipeError) return PipeError.path(${path}, ${input})`,\n\t\t],\n\t\t{\n\t\t\tcontext: { ...config?.context, [key]: fn },\n\t\t\tschema: config?.schema,\n\t\t},\n\t)\n}\n\nexport function compileNested(\n\tdata: {\n\t\tpipe: Pipe<any, any>\n\t\terrorType?: Parameters<typeof createErrorHandler>[1]\n\t\topts: Required<Pick<Parameters<Pipe<any, any>['compile']>[1], 'rootContext' | 'failEarly' | 'path' | 'wrapError'>>\n\t} & { input: string; key?: string },\n) {\n\tconst random = getRandomValue()\n\tconst { lines, context } = compilePipeToString({\n\t\t...data.opts,\n\t\twrapError: createErrorHandler(data.input, data.errorType ?? data.opts.wrapError.type),\n\t\tpipe: data.pipe,\n\t\tinput: data.input,\n\t\tcontext: `context[\\`${random}\\`]`,\n\t\tpath: ['key' in data ? data.key : '', data.opts.path].filter(Boolean).join('.') || undefined,\n\t})\n\tdata.opts.rootContext[random] = context\n\treturn lines\n}\n\nfunction compilePipeToString({\n\tpipe,\n\tinput,\n\tcontext: contextStr,\n\tfailEarly = false,\n\tpath = '',\n\trootContext,\n\twrapError = createErrorHandler(input, 'return'),\n\tbase = [],\n}: {\n\tpipe: Pipe<any, any>\n\tinput: string\n\tcontext: string\n\tfailEarly?: boolean\n\tpath?: string\n\trootContext?: Context\n\twrapError?: PipeErrorHandler\n\tbase?: string[]\n}) {\n\tconst ctx = context(pipe)\n\trootContext ??= ctx\n\tconst compiled = walk(pipe, <ReturnType<Pipe<any, any>['compile']>[]>[], (p, acc) => {\n\t\tacc.push(\n\t\t\tp.compile(\n\t\t\t\t{ input, context: contextStr, path: `${path ? `'${path}'` : undefined}` },\n\t\t\t\t{ rootContext, failEarly, path, wrapError },\n\t\t\t),\n\t\t)\n\t\treturn acc\n\t})\n\tconst lines = mergePipeLines(base, compiled.flat())\n\treturn { lines, context: ctx }\n}\n\nfunction mergePipeLines(base: string[], lines: ReturnType<Pipe<any, any>['compile']>) {\n\treturn (Array.isArray(lines) ? lines : [lines]).reduceRight<string[]>((acc, cur) => {\n\t\tif (typeof cur === 'string') acc.unshift(cur)\n\t\telse if (typeof cur === 'function') acc = cur(acc)\n\t\treturn acc\n\t}, base)\n}\n"],"mappings":"yaAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,YAAAE,EAAA,YAAAC,EAAA,kBAAAC,EAAA,YAAAC,EAAA,WAAAC,EAAA,SAAAC,EAAA,WAAAC,EAAA,aAAAC,EAAA,aAAAC,EAAA,SAAAC,IAAA,eAAAC,EAAAZ,GAAA,IAAAa,EAA8C,oBAE9CC,EAA+B,iCAGxB,SAASH,EAAQI,EAAsBC,EAASC,EAA4C,CAClG,IAAIC,EAASF,EACb,KAAOD,GACNG,EAAMD,EAAOF,EAAMG,CAAG,E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,CAACA,CAAK,GAAG,YAAsB,CAACT,EAAKgB,KACvE,OAAOA,GAAQ,SAAUhB,EAAI,QAAQgB,CAAG,EACnC,OAAOA,GAAQ,aAAYhB,EAAMgB,EAAIhB,CAAG,GAC1CA,GACL2B,CAAI,CACR","names":["pipes_exports","__export","assert","compile","compileNested","context","define","meta","schema","standard","validate","walk","__toCommonJS","import_errors","import_functions","pipe","init","nodeFn","acc","p","input","result","error","cont","failEarly","inputStr","contextStr","lines","compilePipeToString","allLines","l","config","piper","entries","cur","value","validity","message","path","fn","key","data","random","rootContext","wrapError","base","ctx","compiled","mergePipeLines"]}
|
|
1
|
+
{"version":3,"sources":["../../../../src/api/base/pipes.ts"],"sourcesContent":["import { createErrorHandler, PipeError } from './errors'\nimport { Context, JsonSchemaBuilder, PipeMeta, Pipe, Entry, PipeOutput, PipeFn, PipeCompiledFn, PipeErrorHandler } from './types'\nimport { getRandomValue } from '../../utils/functions'\nimport { JsonSchema } from '../../utils/types'\n\nexport function walk<T>(pipe: Pipe<any, any>, init: T, nodeFn: (cur: Pipe<any, any>, acc: T) => T) {\n\tlet acc: T = init\n\twhile (pipe) {\n\t\tacc = nodeFn(pipe, acc)\n\t\tpipe = pipe.next!\n\t}\n\treturn acc\n}\n\nexport function context<T extends Pipe<any, any>>(pipe: T): Context {\n\treturn walk(pipe, {} as Context, (p, acc) => ({ ...acc, ...p.context() }))\n}\n\nexport function assert<T extends Pipe<any, any>>(pipe: T, input: unknown): PipeOutput<T> {\n\tconst result = validate(pipe, input)\n\tif (!result.valid) throw result.error\n\treturn result.value\n}\n\nexport function validate<T extends Pipe<any, any>>(\n\tpipe: T,\n\tinput: unknown,\n): { value: PipeOutput<T>; valid: true } | { error: PipeError; valid: false } {\n\ttry {\n\t\tconst fn = pipe.__compiled ?? compile(pipe)\n\t\tconst res = fn(input) as ReturnType<PipeCompiledFn<T>>\n\t\treturn res instanceof PipeError ? { error: res, valid: false } : { value: res, valid: true }\n\t} catch (error) {\n\t\tif (error instanceof PipeError) return { error, valid: false }\n\t\treturn { error: PipeError.root(error instanceof Error ? error.message : `${error}`, input, undefined), valid: false }\n\t}\n}\n\nexport function schema<T extends Pipe<any, any>>(pipe: T, schema: JsonSchema = {}): JsonSchema {\n\tconst cont = context(pipe)\n\treturn walk(pipe, schema, (p, acc) => ({ ...acc, ...p.schema(cont) }))\n}\n\nexport function meta<T extends Pipe<any, any>>(p: T, meta: PipeMeta): T {\n\treturn p.pipe(standard(() => [], { schema: () => meta })) as T\n}\n\nexport function compile<T extends Pipe<any, any>>(\n\tpipe: T,\n\t{\n\t\tfailEarly = true,\n\t}: {\n\t\tfailEarly?: boolean\n\t} = {},\n): PipeCompiledFn<T> {\n\tconst inputStr = 'input'\n\tconst contextStr = 'context'\n\tconst { lines, context } = compilePipeToString({\n\t\tpipe,\n\t\tinput: inputStr,\n\t\tcontext: contextStr,\n\t\tfailEarly,\n\t\tbase: [`return ${inputStr}`],\n\t})\n\tconst allLines = [\n\t\t`return (${inputStr}) => {`,\n\t\t...lines.filter((l) => l.trim() !== '').map((l) => `\\t${l}`),\n\t\t`\tthrow PipeError.root('unhandled root validation', ${inputStr})`,\n\t\t`}`,\n\t]\n\tpipe.__compiled = new Function(contextStr, 'PipeError', allLines.join('\\n'))(context, PipeError)\n\treturn pipe.__compiled!\n}\n\nexport function standard<I, O>(\n\tcompile: Pipe<I, O>['compile'],\n\tconfig: {\n\t\tcontext?: Context\n\t\tschema?: (context: Context) => JsonSchemaBuilder\n\t} = {},\n): Pipe<I, O> {\n\tconst piper: Pipe<I, O> = {\n\t\tcontext: () => config.context ?? ({} as any),\n\t\tschema: (context: Context) => config.schema?.(context) ?? ({} as any),\n\t\tpipe: (...entries: Entry<any, any>[]) => {\n\t\t\tdelete piper.__compiled\n\t\t\tfor (const cur of entries) {\n\t\t\t\tconst p = typeof cur === 'function' ? define(cur, config) : cur\n\t\t\t\tif (!piper.next) piper.next = p\n\t\t\t\tif (piper.last) piper.last.next = p\n\t\t\t\tpiper.last = p.last ?? p\n\t\t\t}\n\t\t\treturn piper\n\t\t},\n\t\tcompile,\n\t\t'~standard': {\n\t\t\tversion: 1,\n\t\t\tvendor: 'valleyed',\n\t\t\tvalidate(value) {\n\t\t\t\tconst validity = validate(piper, value)\n\t\t\t\tif (validity.valid) return { value: validity.value }\n\t\t\t\treturn {\n\t\t\t\t\tissues: validity.error.messages.map(({ message, path }) => ({\n\t\t\t\t\t\tmessage,\n\t\t\t\t\t\tpath: path ? path.split('.') : undefined,\n\t\t\t\t\t})),\n\t\t\t\t}\n\t\t\t},\n\t\t},\n\t}\n\treturn piper\n}\n\nexport function define<I, O>(\n\tfn: PipeFn<I, O>,\n\tconfig: {\n\t\tcontext?: Context\n\t\tschema?: (context: Context) => JsonSchemaBuilder\n\t} = {},\n): Pipe<I, O> {\n\tconst key = `define_${getRandomValue()}`\n\treturn standard<I, O>(\n\t\t({ input, context, path }) => [\n\t\t\t`${input} = ${context}['${key}'](${input})`,\n\t\t\t`if (${input} instanceof PipeError) return PipeError.path(${path}, ${input})`,\n\t\t],\n\t\t{\n\t\t\tcontext: { ...config?.context, [key]: fn },\n\t\t\tschema: config?.schema,\n\t\t},\n\t)\n}\n\nexport function compileNested(\n\tdata: {\n\t\tpipe: Pipe<any, any>\n\t\terrorType?: Parameters<typeof createErrorHandler>[1]\n\t\topts: Required<Pick<Parameters<Pipe<any, any>['compile']>[1], 'rootContext' | 'failEarly' | 'path' | 'wrapError'>>\n\t} & { input: string; key?: string },\n) {\n\tconst random = getRandomValue()\n\tconst { lines, context } = compilePipeToString({\n\t\t...data.opts,\n\t\twrapError: createErrorHandler(data.input, data.errorType ?? data.opts.wrapError.type),\n\t\tpipe: data.pipe,\n\t\tinput: data.input,\n\t\tcontext: `context[\\`${random}\\`]`,\n\t\tpath: ['key' in data ? data.key : '', data.opts.path].filter(Boolean).join('.') || undefined,\n\t})\n\tdata.opts.rootContext[random] = context\n\treturn lines\n}\n\nfunction compilePipeToString({\n\tpipe,\n\tinput,\n\tcontext: contextStr,\n\tfailEarly = false,\n\tpath = '',\n\trootContext,\n\twrapError = createErrorHandler(input, 'return'),\n\tbase = [],\n}: {\n\tpipe: Pipe<any, any>\n\tinput: string\n\tcontext: string\n\tfailEarly?: boolean\n\tpath?: string\n\trootContext?: Context\n\twrapError?: PipeErrorHandler\n\tbase?: string[]\n}) {\n\tconst ctx = context(pipe)\n\trootContext ??= ctx\n\tconst compiled = walk(pipe, <ReturnType<Pipe<any, any>['compile']>[]>[], (p, acc) => {\n\t\tacc.push(\n\t\t\tp.compile(\n\t\t\t\t{ input, context: contextStr, path: `${path ? `'${path}'` : undefined}` },\n\t\t\t\t{ rootContext, failEarly, path, wrapError },\n\t\t\t),\n\t\t)\n\t\treturn acc\n\t})\n\tconst lines = mergePipeLines(base, compiled.flat())\n\treturn { lines, context: ctx }\n}\n\nfunction mergePipeLines(base: string[], lines: ReturnType<Pipe<any, any>['compile']>) {\n\treturn (Array.isArray(lines) ? lines : [lines]).reduceRight<string[]>((acc, cur) => {\n\t\tif (typeof cur === 'string') acc.unshift(cur)\n\t\telse if (typeof cur === 'function') acc = cur(acc)\n\t\treturn acc\n\t}, base)\n}\n"],"mappings":"yaAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,YAAAE,EAAA,YAAAC,EAAA,kBAAAC,EAAA,YAAAC,EAAA,WAAAC,EAAA,SAAAC,EAAA,WAAAC,EAAA,aAAAC,EAAA,aAAAC,EAAA,SAAAC,IAAA,eAAAC,EAAAZ,GAAA,IAAAa,EAA8C,oBAE9CC,EAA+B,iCAGxB,SAASH,EAAQI,EAAsBC,EAASC,EAA4C,CAClG,IAAIC,EAASF,EACb,KAAOD,GACNG,EAAMD,EAAOF,EAAMG,CAAG,EACtBH,EAAOA,EAAK,KAEb,OAAOG,CACR,CAEO,SAASb,EAAkCU,EAAkB,CACnE,OAAOJ,EAAKI,EAAM,CAAC,EAAc,CAACI,EAAGD,KAAS,CAAE,GAAGA,EAAK,GAAGC,EAAE,QAAQ,CA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EAAQ,CAACA,CAAK,GAAG,YAAsB,CAACV,EAAKiB,KACvE,OAAOA,GAAQ,SAAUjB,EAAI,QAAQiB,CAAG,EACnC,OAAOA,GAAQ,aAAYjB,EAAMiB,EAAIjB,CAAG,GAC1CA,GACL4B,CAAI,CACR","names":["pipes_exports","__export","assert","compile","compileNested","context","define","meta","schema","standard","validate","walk","__toCommonJS","import_errors","import_functions","pipe","init","nodeFn","acc","p","input","result","res","error","cont","failEarly","inputStr","contextStr","lines","compilePipeToString","allLines","l","config","piper","entries","cur","value","validity","message","path","fn","key","data","random","rootContext","wrapError","base","ctx","compiled","mergePipeLines"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../src/api/base/types.ts"],"sourcesContent":["import { StandardSchemaV1 } from '@standard-schema/spec'\n\nimport { PipeError } from './errors'\nimport { JsonSchema } from '../../utils/types'\n\nexport type PipeFn<I, O> = (input: I) => O | PipeError\nexport type PipeCompiledFn<T extends Pipe<any, any>> = (input: unknown) =>
|
|
1
|
+
{"version":3,"sources":["../../../../src/api/base/types.ts"],"sourcesContent":["import { StandardSchemaV1 } from '@standard-schema/spec'\n\nimport { PipeError } from './errors'\nimport { JsonSchema } from '../../utils/types'\n\nexport type PipeFn<I, O> = (input: I) => O | PipeError\nexport type PipeCompiledFn<T extends Pipe<any, any>> = (input: unknown) => PipeOutput<T> | PipeError\nexport type PipeErrorHandlerType = 'return' | 'throw' | 'assign'\nexport type PipeErrorHandler = ((errorCondition: string, error: string) => (lines: string[]) => string[]) & {\n\ttype: PipeErrorHandlerType\n\tformat: (error: string) => string\n}\n\nexport type PipeInput<T> = T extends Pipe<infer I, any> ? I : never\nexport type PipeOutput<T> = T extends Pipe<any, infer O> ? O : never\nexport type PipeMeta = Pick<JsonSchema, '$refId' | 'title' | 'description' | 'examples' | 'default'>\nexport type Context = Record<string, any>\nexport type JsonSchemaBuilder = JsonSchema\n\nexport type Entry<I, O> = Pipe<I, O> | PipeFn<I, O>\ntype PipeChain<I, O> = {\n\t<T1>(fn1: Entry<O, T1>): Pipe<I, T1>\n\t<T1, T2>(fn1: Entry<O, T1>, fn2: Entry<T1, T2>): Pipe<I, T2>\n\t<T1, T2, T3>(fn1: Entry<O, T1>, fn2: Entry<T1, T2>, f3: Entry<T2, T3>): Pipe<I, T3>\n\t<T1, T2, T3, T4>(fn1: Entry<O, T1>, fn2: Entry<T1, T2>, f3: Entry<T2, T3>, f4: Entry<T3, T4>): Pipe<I, T4>\n\t<T1, T2, T3, T4, T5>(fn1: Entry<O, T1>, fn2: Entry<T1, T2>, f3: Entry<T2, T3>, f4: Entry<T3, T4>, f5: Entry<T4, T5>): Pipe<I, T5>\n\t<T1, T2, T3, T4, T5, T6>(\n\t\tfn1: Entry<O, T1>,\n\t\tfn2: Entry<T1, T2>,\n\t\tf3: Entry<T2, T3>,\n\t\tf4: Entry<T3, T4>,\n\t\tf5: Entry<T4, T5>,\n\t\tf6: Entry<T5, T6>,\n\t): Pipe<I, T6>\n\t<T1, T2, T3, T4, T5, T6, T7>(\n\t\tfn1: Entry<O, T1>,\n\t\tfn2: Entry<T1, T2>,\n\t\tf3: Entry<T2, T3>,\n\t\tf4: Entry<T3, T4>,\n\t\tf5: Entry<T4, T5>,\n\t\tf6: Entry<T5, T6>,\n\t\tf7: Entry<T6, T7>,\n\t): Pipe<I, T7>\n\t<T1, T2, T3, T4, T5, T6, T7, T8>(\n\t\tfn1: Entry<O, T1>,\n\t\tfn2: Entry<T1, T2>,\n\t\tf3: Entry<T2, T3>,\n\t\tf4: Entry<T3, T4>,\n\t\tf5: Entry<T4, T5>,\n\t\tf6: Entry<T5, T6>,\n\t\tf7: Entry<T6, T7>,\n\t\tf8: Entry<T7, T8>,\n\t): Pipe<I, T8>\n\t<T1, T2, T3, T4, T5, T6, T7, T8, T9>(\n\t\tfn1: Entry<O, T1>,\n\t\tfn2: Entry<T1, T2>,\n\t\tf3: Entry<T2, T3>,\n\t\tf4: Entry<T3, T4>,\n\t\tf5: Entry<T4, T5>,\n\t\tf6: Entry<T5, T6>,\n\t\tf7: Entry<T6, T7>,\n\t\tf8: Entry<T7, T8>,\n\t\tf9: Entry<T8, T9>,\n\t): Pipe<I, T9>\n\t<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>(\n\t\tfn1: Entry<O, T1>,\n\t\tfn2: Entry<T1, T2>,\n\t\tf3: Entry<T2, T3>,\n\t\tf4: Entry<T3, T4>,\n\t\tf5: Entry<T4, T5>,\n\t\tf6: Entry<T5, T6>,\n\t\tf7: Entry<T6, T7>,\n\t\tf8: Entry<T7, T8>,\n\t\tf9: Entry<T8, T9>,\n\t\tf10: Entry<T9, T10>,\n\t): Pipe<I, T10>\n}\n\ntype Arrayable<T> = T | T[]\n\nexport interface Pipe<I, O> extends StandardSchemaV1<I, O> {\n\treadonly context: () => Context\n\treadonly schema: (context: Context) => JsonSchema\n\treadonly pipe: PipeChain<I, O>\n\treadonly compile: (\n\t\tnames: { input: string; context: string; path: string },\n\t\topts: { rootContext: Context; failEarly: boolean; path: string; wrapError: PipeErrorHandler },\n\t) => Arrayable<string | ReturnType<PipeErrorHandler>>\n\tnext?: Pipe<any, any>\n\tlast?: Pipe<any, any>\n\t__compiled?: PipeCompiledFn<any>\n}\n"],"mappings":";;;;;;;;;;;;;;AAAA;AAAA;","names":[]}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";var e=Object.defineProperty;var p=Object.getOwnPropertyDescriptor;var i=Object.getOwnPropertyNames;var E=Object.prototype.hasOwnProperty;var f=(
|
|
1
|
+
"use strict";var e=Object.defineProperty;var p=Object.getOwnPropertyDescriptor;var i=Object.getOwnPropertyNames;var E=Object.prototype.hasOwnProperty;var f=(n,T,y,t)=>{if(T&&typeof T=="object"||typeof T=="function")for(let r of i(T))!E.call(n,r)&&r!==y&&e(n,r,{get:()=>T[r],enumerable:!(t=p(T,r))||t.enumerable});return n};var o=n=>f(e({},"__esModule",{value:!0}),n);var a={};module.exports=o(a);
|
|
2
2
|
//# sourceMappingURL=types.min.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../src/api/base/types.ts"],"sourcesContent":["import { StandardSchemaV1 } from '@standard-schema/spec'\n\nimport { PipeError } from './errors'\nimport { JsonSchema } from '../../utils/types'\n\nexport type PipeFn<I, O> = (input: I) => O | PipeError\nexport type PipeCompiledFn<T extends Pipe<any, any>> = (input: unknown) =>
|
|
1
|
+
{"version":3,"sources":["../../../../src/api/base/types.ts"],"sourcesContent":["import { StandardSchemaV1 } from '@standard-schema/spec'\n\nimport { PipeError } from './errors'\nimport { JsonSchema } from '../../utils/types'\n\nexport type PipeFn<I, O> = (input: I) => O | PipeError\nexport type PipeCompiledFn<T extends Pipe<any, any>> = (input: unknown) => PipeOutput<T> | PipeError\nexport type PipeErrorHandlerType = 'return' | 'throw' | 'assign'\nexport type PipeErrorHandler = ((errorCondition: string, error: string) => (lines: string[]) => string[]) & {\n\ttype: PipeErrorHandlerType\n\tformat: (error: string) => string\n}\n\nexport type PipeInput<T> = T extends Pipe<infer I, any> ? I : never\nexport type PipeOutput<T> = T extends Pipe<any, infer O> ? O : never\nexport type PipeMeta = Pick<JsonSchema, '$refId' | 'title' | 'description' | 'examples' | 'default'>\nexport type Context = Record<string, any>\nexport type JsonSchemaBuilder = JsonSchema\n\nexport type Entry<I, O> = Pipe<I, O> | PipeFn<I, O>\ntype PipeChain<I, O> = {\n\t<T1>(fn1: Entry<O, T1>): Pipe<I, T1>\n\t<T1, T2>(fn1: Entry<O, T1>, fn2: Entry<T1, T2>): Pipe<I, T2>\n\t<T1, T2, T3>(fn1: Entry<O, T1>, fn2: Entry<T1, T2>, f3: Entry<T2, T3>): Pipe<I, T3>\n\t<T1, T2, T3, T4>(fn1: Entry<O, T1>, fn2: Entry<T1, T2>, f3: Entry<T2, T3>, f4: Entry<T3, T4>): Pipe<I, T4>\n\t<T1, T2, T3, T4, T5>(fn1: Entry<O, T1>, fn2: Entry<T1, T2>, f3: Entry<T2, T3>, f4: Entry<T3, T4>, f5: Entry<T4, T5>): Pipe<I, T5>\n\t<T1, T2, T3, T4, T5, T6>(\n\t\tfn1: Entry<O, T1>,\n\t\tfn2: Entry<T1, T2>,\n\t\tf3: Entry<T2, T3>,\n\t\tf4: Entry<T3, T4>,\n\t\tf5: Entry<T4, T5>,\n\t\tf6: Entry<T5, T6>,\n\t): Pipe<I, T6>\n\t<T1, T2, T3, T4, T5, T6, T7>(\n\t\tfn1: Entry<O, T1>,\n\t\tfn2: Entry<T1, T2>,\n\t\tf3: Entry<T2, T3>,\n\t\tf4: Entry<T3, T4>,\n\t\tf5: Entry<T4, T5>,\n\t\tf6: Entry<T5, T6>,\n\t\tf7: Entry<T6, T7>,\n\t): Pipe<I, T7>\n\t<T1, T2, T3, T4, T5, T6, T7, T8>(\n\t\tfn1: Entry<O, T1>,\n\t\tfn2: Entry<T1, T2>,\n\t\tf3: Entry<T2, T3>,\n\t\tf4: Entry<T3, T4>,\n\t\tf5: Entry<T4, T5>,\n\t\tf6: Entry<T5, T6>,\n\t\tf7: Entry<T6, T7>,\n\t\tf8: Entry<T7, T8>,\n\t): Pipe<I, T8>\n\t<T1, T2, T3, T4, T5, T6, T7, T8, T9>(\n\t\tfn1: Entry<O, T1>,\n\t\tfn2: Entry<T1, T2>,\n\t\tf3: Entry<T2, T3>,\n\t\tf4: Entry<T3, T4>,\n\t\tf5: Entry<T4, T5>,\n\t\tf6: Entry<T5, T6>,\n\t\tf7: Entry<T6, T7>,\n\t\tf8: Entry<T7, T8>,\n\t\tf9: Entry<T8, T9>,\n\t): Pipe<I, T9>\n\t<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>(\n\t\tfn1: Entry<O, T1>,\n\t\tfn2: Entry<T1, T2>,\n\t\tf3: Entry<T2, T3>,\n\t\tf4: Entry<T3, T4>,\n\t\tf5: Entry<T4, T5>,\n\t\tf6: Entry<T5, T6>,\n\t\tf7: Entry<T6, T7>,\n\t\tf8: Entry<T7, T8>,\n\t\tf9: Entry<T8, T9>,\n\t\tf10: Entry<T9, T10>,\n\t): Pipe<I, T10>\n}\n\ntype Arrayable<T> = T | T[]\n\nexport interface Pipe<I, O> extends StandardSchemaV1<I, O> {\n\treadonly context: () => Context\n\treadonly schema: (context: Context) => JsonSchema\n\treadonly pipe: PipeChain<I, O>\n\treadonly compile: (\n\t\tnames: { input: string; context: string; path: string },\n\t\topts: { rootContext: Context; failEarly: boolean; path: string; wrapError: PipeErrorHandler },\n\t) => Arrayable<string | ReturnType<PipeErrorHandler>>\n\tnext?: Pipe<any, any>\n\tlast?: Pipe<any, any>\n\t__compiled?: PipeCompiledFn<any>\n}\n"],"mappings":"+WAAA,IAAAA,EAAA,kBAAAC,EAAAD","names":["types_exports","__toCommonJS"]}
|
|
@@ -117,7 +117,7 @@ const fromJson = (branch) => {
|
|
|
117
117
|
const lazy = (pipeFn) => (0, import_pipes.define)(
|
|
118
118
|
(input) => {
|
|
119
119
|
const result = (0, import_pipes.validate)(pipeFn(), input);
|
|
120
|
-
return result.valid ? result.value : result;
|
|
120
|
+
return result.valid ? result.value : result.error;
|
|
121
121
|
},
|
|
122
122
|
{
|
|
123
123
|
schema: () => (0, import_pipes.schema)(pipeFn())
|
|
@@ -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}),\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}))`,\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(\n\t\t(input) => {\n\t\t\tconst result = validate(pipeFn(), input)\n\t\t\treturn result.valid ? result.value : result\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\tif (compiledBefore) return common\n\t\t\tcompiledBefore = true\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'}',\n\t\t\t\t...common,\n\t\t\t]\n\t\t},\n\t\t{\n\t\t\tschema: () => {\n\t\t\t\tif (schemedBefore) return { $refId }\n\t\t\t\tschemedBefore = true\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,mBA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EAAE;AAAA,EACvE;AACD;AAEM,MAAM,WAAW,CAA2B,WAAc;AAChE,QAAM,mBAAmB,iBAAa,iCAAe,CAAC;AACtD,aAAO;AAAA,IACN,CAAC,EAAE,OAAO,SAAAA,SAAQ,GAAG,SAAS;AAAA,MAC7B,OAAO,gBAAgB,MAAM,KAAK;AAAA,MAClC,OAAG,4BAAc,EAAE,MAAM,MAAM,QAAQ,OAAO,kBAAkB,WAAW,SAAS,CAAC;AAAA,MACrF,OAAO,gBAAgB;AAAA,MACvB,KAAK,UAAU,GAAG,KAAK,oCAAoC,gBAAgB;AAAA,MAC3E,IAAI,gBAAgB,MAAMA,QAAO,oCAAoC,KAAK,MAAM,gBAAgB;AAAA,MAChG,KAAK,UAAU,GAAG,gBAAgB,yBAAyB,gBAAgB;AAAA,MAC3E,OAAG,4BAAc,EAAE,MAAM,MAAM,QAAQ,OAAO,kBAAkB,WAAW,SAAS,CAAC,EAAE,IAAI,CAAC,MAAM,IAAI,CAAC,EAAE;AAAA,MACzG,KAAK,UAAU,GAAG,gBAAgB,yBAAyB,gBAAgB;AAAA,MAC3E;AAAA,MACA,GAAG,KAAK,MAAM,gBAAgB;AAAA,IAC/B;AAAA,IACA;AAAA,MACC,SAAS,EAAE,OAAG,sBAAQ,MAAM,GAAG,gDAAe;AAAA,MAC9C,QAAQ,OAAO;AAAA,IAChB;AAAA,EACD;AACD;AAEO,MAAM,OAAO,CAA2B,eAC9C;AAAA,EACC,CAAC,UAAU;AACV,UAAM,aAAS,uBAAS,OAAO,GAAG,KAAK;AACvC,WAAO,OAAO,QAAQ,OAAO,QAAQ;AAAA,
|
|
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}),\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}))`,\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(\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\tif (compiledBefore) return common\n\t\t\tcompiledBefore = true\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'}',\n\t\t\t\t...common,\n\t\t\t]\n\t\t},\n\t\t{\n\t\t\tschema: () => {\n\t\t\t\tif (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A,EAC7C;AAAA,EACA;AAAA,IACC,QAAQ,UAAM,qBAAO,OAAO,CAAC;AAAA,EAC9B;AACD;AAEM,MAAM,YAAY,CAA2B,QAAiB,WAAmB;AACvF,QAAM,YAAY,UAAM,iCAAe,CAAC;AACxC,MAAI,iBAAiB;AACrB,MAAI,gBAAgB;AACpB,aAAO;AAAA,IACN,CAAC,EAAE,MAAM,GAAG,SAAS;AACpB,YAAM,SAAS,CAAC,GAAG,KAAK,MAAM,SAAS,IAAI,KAAK,KAAK,KAAK,UAAU,GAAG,KAAK,yBAAyB,KAAK,CAAC;AAC3G,UAAI,eAAgB,QAAO;AAC3B,uBAAiB;AACjB,aAAO;AAAA,QACN,YAAY,SAAS;AAAA,QACrB,OAAG,4BAAc,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,GAAG;AAAA,MACJ;AAAA,IACD;AAAA,IACA;AAAA,MACC,QAAQ,MAAM;AACb,YAAI,cAAe,QAAO,EAAE,OAAO;AACnC,wBAAgB;AAChB,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 c=Object.defineProperty;var T=Object.getOwnPropertyDescriptor;var P=Object.getOwnPropertyNames;var l=Object.prototype.hasOwnProperty;var d=(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))!l.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 I={};d(I,{discriminate:()=>w,fromJson:()=>O,lazy:()=>h,merge:()=>x,or:()=>g,recursive:()=>v});module.exports=E(I);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"}),`if (${e} instanceof PipeError) {`,` ${r}.push(PipeError.path(${i}, ${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)]})})},w=(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))})}),O=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},{schema:()=>(0,a.schema)(t())}),v=(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?s:(n=!0,[`function ${r}(node) {`,...(0,a.compileNested)({opts:{...i,failEarly:!0},pipe:t(),input:"node",errorType:"return"}).map(m=>` ${m}`),"}",...s])},{schema:()=>o?{$refId:e}:(o=!0,(0,a.schema)(t(),{$refId:e}))})};0&&(module.exports={discriminate,fromJson,lazy,merge,or,recursive});
|
|
1
|
+
"use strict";var c=Object.defineProperty;var T=Object.getOwnPropertyDescriptor;var P=Object.getOwnPropertyNames;var l=Object.prototype.hasOwnProperty;var d=(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))!l.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 I={};d(I,{discriminate:()=>w,fromJson:()=>O,lazy:()=>h,merge:()=>x,or:()=>g,recursive:()=>v});module.exports=E(I);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"}),`if (${e} instanceof PipeError) {`,` ${r}.push(PipeError.path(${i}, ${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)]})})},w=(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))})}),O=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())}),v=(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?s:(n=!0,[`function ${r}(node) {`,...(0,a.compileNested)({opts:{...i,failEarly:!0},pipe:t(),input:"node",errorType:"return"}).map(m=>` ${m}`),"}",...s])},{schema:()=>o?{$refId:e}:(o=!0,(0,a.schema)(t(),{$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}),\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}))`,\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(\n\t\t(input) => {\n\t\t\tconst result = validate(pipeFn(), input)\n\t\t\treturn result.valid ? result.value : result\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\tif (compiledBefore) return common\n\t\t\tcompiledBefore = true\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'}',\n\t\t\t\t...common,\n\t\t\t]\n\t\t},\n\t\t{\n\t\t\tschema: () => {\n\t\t\t\tif (schemedBefore) return { $refId }\n\t\t\t\tschemedBefore = true\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,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kBAAe,CAAC,GACtD,SAAO,YACN,CAAC,CAAE,MAAAE,EAAO,QAAAW,CAAQ,EAAGV,IAAS,CAC7B,OAAOH,CAAgB,MAAME,CAAK,GAClC,MAAG,iBAAc,CAAE,KAAAC,EAAM,KAAMC,EAAQ,MAAOJ,EAAkB,UAAW,QAAS,CAAC,EACrF,OAAOA,CAAgB,2BACvBG,EAAK,UAAU,GAAGD,CAAK,mCAAoCF,CAAgB,EAC3E,IAAIA,CAAgB,MAAMa,CAAO,oCAAoCX,CAAK,MAAMF,CAAgB,IAChGG,EAAK,UAAU,GAAGH,CAAgB,wBAAyBA,CAAgB,EAC3E,MAAG,iBAAc,CAAE,KAAAG,EAAM,KAAMC,EAAQ,MAAOJ,EAAkB,UAAW,QAAS,CAAC,EAAE,IAAKO,GAAM,IAAIA,CAAC,EAAE,EACzGJ,EAAK,UAAU,GAAGH,CAAgB,wBAAyBA,CAAgB,EAC3E,IACA,GAAGE,CAAK,MAAMF,CAAgB,EAC/B,EACA,CACC,QAAS,CAAE,MAAG,WAAQI,CAAM,EAAG,+BAAe,EAC9C,OAAQA,EAAO,MAChB,CACD,CACD,EAEab,EAAkC6B,MAC9C,UACElB,GAAU,CACV,MAAMmB,KAAS,YAASD,EAAO,EAAGlB,CAAK,EACvC,OAAOmB,EAAO,MAAQA,EAAO,MAAQA,
|
|
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}),\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}))`,\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(\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\tif (compiledBefore) return common\n\t\t\tcompiledBefore = true\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'}',\n\t\t\t\t...common,\n\t\t\t]\n\t\t},\n\t\t{\n\t\t\tschema: () => {\n\t\t\t\tif (schemedBefore) return { $refId }\n\t\t\t\tschemedBefore = true\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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AG,iBAAc,CAAE,KAAM,CAAE,GAAGpB,EAAM,UAAW,EAAK,EAAG,KAAMiB,EAAO,EAAG,MAAO,OAAQ,UAAW,QAAS,CAAC,EAAE,IAC3Gb,GAAM,IAAIA,CAAC,EACb,EACA,IACA,GAAGmB,CACJ,EACD,EACA,CACC,OAAQ,IACHD,EAAsB,CAAE,OAAAH,CAAO,GACnCG,EAAgB,MACT,UAAOL,EAAO,EAAG,CAAE,OAAAE,CAAO,CAAC,EAEpC,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,3 +1,3 @@
|
|
|
1
|
-
function i(n){return`${n.path?`${n.path}: `:""}${n.message}`}class s{constructor(r){this.messages=r}
|
|
2
|
-
`)}static root(r,t
|
|
1
|
+
function i(n){return`${n.path?`${n.path}: `:""}${n.message}`}class s{constructor(r){this.messages=r}toString(){return this.messages.map(i).join(`
|
|
2
|
+
`)}static root(r,e,t){return new s([{message:r,path:t,value:e}])}static rootFrom(r){return new s(r.flatMap(e=>e.messages))}static path(r,e){return r===0&&(r="0"),r?new s(e.messages.map(t=>({...t,path:`${r.toString()}${t.path?`.${t.path}`:""}`}))):e}}function p(n,r){const e=Object.assign((...t)=>a=>{switch(r){case"return":case"throw":return[`if (${t[0]}) ${e.format(t[1])}`,...a];case"assign":return[`if (${t[0]}) ${e.format(t[1])}`,...a.length?["else {",...a.map(o=>` ${o}`),"}"]:[]];default:throw new Error(`Unknown error handling type: ${r}`)}},{type:r,format:t=>{switch(r){case"return":return`return ${t}`;case"throw":return`throw ${t}`;case"assign":return`${n} = ${t}`;default:throw new Error(`Unknown error handling type: ${r}`)}}});return e}export{s as PipeError,p as createErrorHandler};
|
|
3
3
|
//# sourceMappingURL=errors.min.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../src/api/base/errors.ts"],"sourcesContent":["import { PipeErrorHandler } from './types'\n\ntype PipeErrorMessage = { message: string; path?: string; value: unknown }\n\nfunction formatError(message: PipeErrorMessage) {\n\treturn `${message.path ? `${message.path}: ` : ''}${message.message}`\n}\n\nexport class PipeError {\n\tconstructor(public messages: PipeErrorMessage[]) {}\n\n\
|
|
1
|
+
{"version":3,"sources":["../../../../src/api/base/errors.ts"],"sourcesContent":["import { PipeErrorHandler } from './types'\n\ntype PipeErrorMessage = { message: string; path?: string; value: unknown }\n\nfunction formatError(message: PipeErrorMessage) {\n\treturn `${message.path ? `${message.path}: ` : ''}${message.message}`\n}\n\nexport class PipeError {\n\tconstructor(public messages: PipeErrorMessage[]) {}\n\n\ttoString() {\n\t\treturn this.messages.map(formatError).join('\\n')\n\t}\n\n\tstatic root(message: string, value: unknown, path?: string) {\n\t\treturn new PipeError([{ message, path, value }])\n\t}\n\n\tstatic rootFrom(errors: PipeError[]) {\n\t\treturn new PipeError(errors.flatMap((error) => error.messages))\n\t}\n\n\tstatic path(path: PropertyKey, error: PipeError) {\n\t\tif (path === 0) path = '0'\n\t\tif (!path) return error\n\t\treturn new PipeError(\n\t\t\terror.messages.map((message) => ({ ...message, path: `${path.toString()}${message.path ? `.${message.path}` : ''}` })),\n\t\t)\n\t}\n}\n\nexport function createErrorHandler(input: string, type: 'return' | 'throw' | 'assign'): PipeErrorHandler {\n\tconst handler: PipeErrorHandler = Object.assign(\n\t\t(...args: Parameters<PipeErrorHandler>) =>\n\t\t\t(lines: string[]) => {\n\t\t\t\tswitch (type) {\n\t\t\t\t\tcase 'return':\n\t\t\t\t\tcase 'throw':\n\t\t\t\t\t\treturn [`if (${args[0]}) ${handler.format(args[1])}`, ...lines]\n\t\t\t\t\tcase 'assign':\n\t\t\t\t\t\treturn [\n\t\t\t\t\t\t\t`if (${args[0]}) ${handler.format(args[1])}`,\n\t\t\t\t\t\t\t...(lines.length ? [`else {`, ...lines.map((l) => `\t${l}`), `}`] : []),\n\t\t\t\t\t\t]\n\t\t\t\t\tdefault:\n\t\t\t\t\t\tthrow new Error(`Unknown error handling type: ${type satisfies never}`)\n\t\t\t\t}\n\t\t\t},\n\t\t{\n\t\t\ttype,\n\t\t\tformat: (error: string) => {\n\t\t\t\tswitch (type) {\n\t\t\t\t\tcase 'return':\n\t\t\t\t\t\treturn `return ${error}`\n\t\t\t\t\tcase 'throw':\n\t\t\t\t\t\treturn `throw ${error}`\n\t\t\t\t\tcase 'assign':\n\t\t\t\t\t\treturn `${input} = ${error}`\n\t\t\t\t\tdefault:\n\t\t\t\t\t\tthrow new Error(`Unknown error handling type: ${type satisfies never}`)\n\t\t\t\t}\n\t\t\t},\n\t\t},\n\t)\n\n\treturn handler\n}\n"],"mappings":"AAIA,SAASA,EAAYC,EAA2B,CAC/C,MAAO,GAAGA,EAAQ,KAAO,GAAGA,EAAQ,IAAI,KAAO,EAAE,GAAGA,EAAQ,OAAO,EACpE,CAEO,MAAMC,CAAU,CACtB,YAAmBC,EAA8B,CAA9B,cAAAA,CAA+B,CAElD,UAAW,CACV,OAAO,KAAK,SAAS,IAAIH,CAAW,EAAE,KAAK;AAAA,CAAI,CAChD,CAEA,OAAO,KAAKC,EAAiBG,EAAgBC,EAAe,CAC3D,OAAO,IAAIH,EAAU,CAAC,CAAE,QAAAD,EAAS,KAAAI,EAAM,MAAAD,CAAM,CAAC,CAAC,CAChD,CAEA,OAAO,SAASE,EAAqB,CACpC,OAAO,IAAIJ,EAAUI,EAAO,QAASC,GAAUA,EAAM,QAAQ,CAAC,CAC/D,CAEA,OAAO,KAAKF,EAAmBE,EAAkB,CAEhD,OADIF,IAAS,IAAGA,EAAO,KAClBA,EACE,IAAIH,EACVK,EAAM,SAAS,IAAKN,IAAa,CAAE,GAAGA,EAAS,KAAM,GAAGI,EAAK,SAAS,CAAC,GAAGJ,EAAQ,KAAO,IAAIA,EAAQ,IAAI,GAAK,EAAE,EAAG,EAAE,CACtH,EAHkBM,CAInB,CACD,CAEO,SAASC,EAAmBC,EAAeC,EAAuD,CACxG,MAAMC,EAA4B,OAAO,OACxC,IAAIC,IACFC,GAAoB,CACpB,OAAQH,EAAM,CACb,IAAK,SACL,IAAK,QACJ,MAAO,CAAC,OAAOE,EAAK,CAAC,CAAC,KAAKD,EAAQ,OAAOC,EAAK,CAAC,CAAC,CAAC,GAAI,GAAGC,CAAK,EAC/D,IAAK,SACJ,MAAO,CACN,OAAOD,EAAK,CAAC,CAAC,KAAKD,EAAQ,OAAOC,EAAK,CAAC,CAAC,CAAC,GAC1C,GAAIC,EAAM,OAAS,CAAC,SAAU,GAAGA,EAAM,IAAKC,GAAM,IAAIA,CAAC,EAAE,EAAG,GAAG,EAAI,CAAC,CACrE,EACD,QACC,MAAM,IAAI,MAAM,gCAAgCJ,CAAoB,EAAE,CACxE,CACD,EACD,CACC,KAAAA,EACA,OAASH,GAAkB,CAC1B,OAAQG,EAAM,CACb,IAAK,SACJ,MAAO,UAAUH,CAAK,GACvB,IAAK,QACJ,MAAO,SAASA,CAAK,GACtB,IAAK,SACJ,MAAO,GAAGE,CAAK,MAAMF,CAAK,GAC3B,QACC,MAAM,IAAI,MAAM,gCAAgCG,CAAoB,EAAE,CACxE,CACD,CACD,CACD,EAEA,OAAOC,CACR","names":["formatError","message","PipeError","messages","value","path","errors","error","createErrorHandler","input","type","handler","args","lines","l"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../src/api/base/errors.ts"],"sourcesContent":["import { PipeErrorHandler } from './types'\n\ntype PipeErrorMessage = { message: string; path?: string; value: unknown }\n\nfunction formatError(message: PipeErrorMessage) {\n\treturn `${message.path ? `${message.path}: ` : ''}${message.message}`\n}\n\nexport class PipeError {\n\tconstructor(public messages: PipeErrorMessage[]) {}\n\n\
|
|
1
|
+
{"version":3,"sources":["../../../../src/api/base/errors.ts"],"sourcesContent":["import { PipeErrorHandler } from './types'\n\ntype PipeErrorMessage = { message: string; path?: string; value: unknown }\n\nfunction formatError(message: PipeErrorMessage) {\n\treturn `${message.path ? `${message.path}: ` : ''}${message.message}`\n}\n\nexport class PipeError {\n\tconstructor(public messages: PipeErrorMessage[]) {}\n\n\ttoString() {\n\t\treturn this.messages.map(formatError).join('\\n')\n\t}\n\n\tstatic root(message: string, value: unknown, path?: string) {\n\t\treturn new PipeError([{ message, path, value }])\n\t}\n\n\tstatic rootFrom(errors: PipeError[]) {\n\t\treturn new PipeError(errors.flatMap((error) => error.messages))\n\t}\n\n\tstatic path(path: PropertyKey, error: PipeError) {\n\t\tif (path === 0) path = '0'\n\t\tif (!path) return error\n\t\treturn new PipeError(\n\t\t\terror.messages.map((message) => ({ ...message, path: `${path.toString()}${message.path ? `.${message.path}` : ''}` })),\n\t\t)\n\t}\n}\n\nexport function createErrorHandler(input: string, type: 'return' | 'throw' | 'assign'): PipeErrorHandler {\n\tconst handler: PipeErrorHandler = Object.assign(\n\t\t(...args: Parameters<PipeErrorHandler>) =>\n\t\t\t(lines: string[]) => {\n\t\t\t\tswitch (type) {\n\t\t\t\t\tcase 'return':\n\t\t\t\t\tcase 'throw':\n\t\t\t\t\t\treturn [`if (${args[0]}) ${handler.format(args[1])}`, ...lines]\n\t\t\t\t\tcase 'assign':\n\t\t\t\t\t\treturn [\n\t\t\t\t\t\t\t`if (${args[0]}) ${handler.format(args[1])}`,\n\t\t\t\t\t\t\t...(lines.length ? [`else {`, ...lines.map((l) => `\t${l}`), `}`] : []),\n\t\t\t\t\t\t]\n\t\t\t\t\tdefault:\n\t\t\t\t\t\tthrow new Error(`Unknown error handling type: ${type satisfies never}`)\n\t\t\t\t}\n\t\t\t},\n\t\t{\n\t\t\ttype,\n\t\t\tformat: (error: string) => {\n\t\t\t\tswitch (type) {\n\t\t\t\t\tcase 'return':\n\t\t\t\t\t\treturn `return ${error}`\n\t\t\t\t\tcase 'throw':\n\t\t\t\t\t\treturn `throw ${error}`\n\t\t\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AA,QAC/D,KAAK;AACJ,iBAAO;AAAA,YACN,OAAO,KAAK,CAAC,CAAC,KAAK,QAAQ,OAAO,KAAK,CAAC,CAAC,CAAC;AAAA,YAC1C,GAAI,MAAM,SAAS,CAAC,UAAU,GAAG,MAAM,IAAI,CAAC,MAAM,IAAI,CAAC,EAAE,GAAG,GAAG,IAAI,CAAC;AAAA,UACrE;AAAA,QACD;AACC,gBAAM,IAAI,MAAM,gCAAgC,IAAoB,EAAE;AAAA,MACxE;AAAA,IACD;AAAA,IACD;AAAA,MACC;AAAA,MACA,QAAQ,CAAC,UAAkB;AAC1B,gBAAQ,MAAM;AAAA,UACb,KAAK;AACJ,mBAAO,UAAU,KAAK;AAAA,UACvB,KAAK;AACJ,mBAAO,SAAS,KAAK;AAAA,UACtB,KAAK;AACJ,mBAAO,GAAG,KAAK,MAAM,KAAK;AAAA,UAC3B;AACC,kBAAM,IAAI,MAAM,gCAAgC,IAAoB,EAAE;AAAA,QACxE;AAAA,MACD;AAAA,IACD;AAAA,EACD;AAEA,SAAO;AACR;","names":[]}
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import{createErrorHandler as y,PipeError as s}from "./errors.min.mjs";import{getRandomValue as
|
|
2
|
-
`))(i,s),
|
|
1
|
+
import{createErrorHandler as y,PipeError as s}from "./errors.min.mjs";import{getRandomValue as f}from "../../utils/functions/index.min.mjs";function c(t,r,e){let n=r;for(;t;)n=e(t,n),t=t.next;return n}function x(t){return c(t,{},(r,e)=>({...e,...r.context()}))}function A(t,r){const e=m(t,r);if(!e.valid)throw e.error;return e.value}function m(t,r){try{const n=(t.__compiled??v(t))(r);return n instanceof s?{error:n,valid:!1}:{value:n,valid:!0}}catch(e){return e instanceof s?{error:e,valid:!1}:{error:s.root(e instanceof Error?e.message:`${e}`,r,void 0),valid:!1}}}function L(t,r={}){const e=x(t);return c(t,r,(n,o)=>({...o,...n.schema(e)}))}function M(t,r){return t.pipe(d(()=>[],{schema:()=>r}))}function v(t,{failEarly:r=!0}={}){const e="input",n="context",{lines:o,context:i}=P({pipe:t,input:e,context:n,failEarly:r,base:[`return ${e}`]}),a=[`return (${e}) => {`,...o.filter(p=>p.trim()!=="").map(p=>` ${p}`),` throw PipeError.root('unhandled root validation', ${e})`,"}"];return t.__compiled=new Function(n,"PipeError",a.join(`
|
|
2
|
+
`))(i,s),t.__compiled}function d(t,r={}){const e={context:()=>r.context??{},schema:n=>r.schema?.(n)??{},pipe:(...n)=>{delete e.__compiled;for(const o of n){const i=typeof o=="function"?E(o,r):o;e.next||(e.next=i),e.last&&(e.last.next=i),e.last=i.last??i}return e},compile:t,"~standard":{version:1,vendor:"valleyed",validate(n){const o=m(e,n);return o.valid?{value:o.value}:{issues:o.error.messages.map(({message:i,path:a})=>({message:i,path:a?a.split("."):void 0}))}}}};return e}function E(t,r={}){const e=`define_${f()}`;return d(({input:n,context:o,path:i})=>[`${n} = ${o}['${e}'](${n})`,`if (${n} instanceof PipeError) return PipeError.path(${i}, ${n})`],{context:{...r?.context,[e]:t},schema:r?.schema})}function q(t){const r=f(),{lines:e,context:n}=P({...t.opts,wrapError:y(t.input,t.errorType??t.opts.wrapError.type),pipe:t.pipe,input:t.input,context:`context[\`${r}\`]`,path:["key"in t?t.key:"",t.opts.path].filter(Boolean).join(".")||void 0});return t.opts.rootContext[r]=n,e}function P({pipe:t,input:r,context:e,failEarly:n=!1,path:o="",rootContext:i,wrapError:a=y(r,"return"),base:p=[]}){const l=x(t);i??=l;const T=c(t,[],(h,u)=>(u.push(h.compile({input:r,context:e,path:`${o?`'${o}'`:void 0}`},{rootContext:i,failEarly:n,path:o,wrapError:a})),u));return{lines:$(p,T.flat()),context:l}}function $(t,r){return(Array.isArray(r)?r:[r]).reduceRight((e,n)=>(typeof n=="string"?e.unshift(n):typeof n=="function"&&(e=n(e)),e),t)}export{A as assert,v as compile,q as compileNested,x as context,E as define,M as meta,L as schema,d as standard,m as validate,c as walk};
|
|
3
3
|
//# sourceMappingURL=pipes.min.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../src/api/base/pipes.ts"],"sourcesContent":["import { createErrorHandler, PipeError } from './errors'\nimport { Context, JsonSchemaBuilder, PipeMeta, Pipe, Entry, PipeOutput, PipeFn, PipeCompiledFn, PipeErrorHandler } from './types'\nimport { getRandomValue } from '../../utils/functions'\nimport { JsonSchema } from '../../utils/types'\n\nexport function walk<T>(pipe: Pipe<any, any>, init: T, nodeFn: (cur: Pipe<any, any>, acc: T) => T) {\n\tlet acc: T = init\n\twhile (pipe) {\n\t\tacc = nodeFn(pipe, acc)\n\t\tpipe = pipe.next!\n\t}\n\treturn acc\n}\n\nexport function context<T extends Pipe<any, any>>(pipe: T): Context {\n\treturn walk(pipe, {} as Context, (p, acc) => ({ ...acc, ...p.context() }))\n}\n\nexport function assert<T extends Pipe<any, any>>(pipe: T, input: unknown): PipeOutput<T> {\n\tconst result = validate(pipe, input)\n\tif (!result.valid) throw result\n\treturn result.value\n}\n\nexport function validate<T extends Pipe<any, any>>(pipe: T, input: unknown): ReturnType<PipeCompiledFn<T>> {\n\ttry {\n\t\tconst fn = pipe.__compiled ?? compile(pipe)\n\t\treturn fn(input) as ReturnType<PipeCompiledFn<T>>\n\t} catch (error) {\n\t\tif (error instanceof PipeError) return error\n\t\treturn PipeError.root(error instanceof Error ? error.message : `${error}`, input, undefined)\n\t}\n}\n\nexport function schema<T extends Pipe<any, any>>(pipe: T, schema: JsonSchema = {}): JsonSchema {\n\tconst cont = context(pipe)\n\treturn walk(pipe, schema, (p, acc) => ({ ...acc, ...p.schema(cont) }))\n}\n\nexport function meta<T extends Pipe<any, any>>(p: T, meta: PipeMeta): T {\n\treturn p.pipe(standard(() => [], { schema: () => meta })) as T\n}\n\nexport function compile<T extends Pipe<any, any>>(\n\tpipe: T,\n\t{\n\t\tfailEarly = true,\n\t}: {\n\t\tfailEarly?: boolean\n\t} = {},\n): PipeCompiledFn<T> {\n\tconst inputStr = 'input'\n\tconst contextStr = 'context'\n\tconst { lines, context } = compilePipeToString({\n\t\tpipe,\n\t\tinput: inputStr,\n\t\tcontext: contextStr,\n\t\tfailEarly,\n\t\tbase: [`return { value: ${inputStr}, valid: true }`],\n\t})\n\tconst allLines = [\n\t\t`return (${inputStr}) => {`,\n\t\t...lines.filter((l) => l.trim() !== '').map((l) => `\\t${l}`),\n\t\t`\tthrow PipeError.root('unhandled root validation', ${inputStr})`,\n\t\t`}`,\n\t]\n\tpipe.__compiled = new Function(contextStr, 'PipeError', allLines.join('\\n'))(context, PipeError)\n\treturn pipe.__compiled!\n}\n\nexport function standard<I, O>(\n\tcompile: Pipe<I, O>['compile'],\n\tconfig: {\n\t\tcontext?: Context\n\t\tschema?: (context: Context) => JsonSchemaBuilder\n\t} = {},\n): Pipe<I, O> {\n\tconst piper: Pipe<I, O> = {\n\t\tcontext: () => config.context ?? ({} as any),\n\t\tschema: (context: Context) => config.schema?.(context) ?? ({} as any),\n\t\tpipe: (...entries: Entry<any, any>[]) => {\n\t\t\tdelete piper.__compiled\n\t\t\tfor (const cur of entries) {\n\t\t\t\tconst p = typeof cur === 'function' ? define(cur, config) : cur\n\t\t\t\tif (!piper.next) piper.next = p\n\t\t\t\tif (piper.last) piper.last.next = p\n\t\t\t\tpiper.last = p.last ?? p\n\t\t\t}\n\t\t\treturn piper\n\t\t},\n\t\tcompile,\n\t\t'~standard': {\n\t\t\tversion: 1,\n\t\t\tvendor: 'valleyed',\n\t\t\tvalidate(value) {\n\t\t\t\tconst validity = validate(piper, value)\n\t\t\t\tif (validity.valid) return { value: validity.value }\n\t\t\t\treturn {\n\t\t\t\t\tissues: validity.messages.map(({ message, path }) => ({\n\t\t\t\t\t\tmessage,\n\t\t\t\t\t\tpath: path ? path.split('.') : undefined,\n\t\t\t\t\t})),\n\t\t\t\t}\n\t\t\t},\n\t\t},\n\t}\n\treturn piper\n}\n\nexport function define<I, O>(\n\tfn: PipeFn<I, O>,\n\tconfig: {\n\t\tcontext?: Context\n\t\tschema?: (context: Context) => JsonSchemaBuilder\n\t} = {},\n): Pipe<I, O> {\n\tconst key = `define_${getRandomValue()}`\n\treturn standard<I, O>(\n\t\t({ input, context, path }) => [\n\t\t\t`${input} = ${context}['${key}'](${input})`,\n\t\t\t`if (${input} instanceof PipeError) return PipeError.path(${path}, ${input})`,\n\t\t],\n\t\t{\n\t\t\tcontext: { ...config?.context, [key]: fn },\n\t\t\tschema: config?.schema,\n\t\t},\n\t)\n}\n\nexport function compileNested(\n\tdata: {\n\t\tpipe: Pipe<any, any>\n\t\terrorType?: Parameters<typeof createErrorHandler>[1]\n\t\topts: Required<Pick<Parameters<Pipe<any, any>['compile']>[1], 'rootContext' | 'failEarly' | 'path' | 'wrapError'>>\n\t} & { input: string; key?: string },\n) {\n\tconst random = getRandomValue()\n\tconst { lines, context } = compilePipeToString({\n\t\t...data.opts,\n\t\twrapError: createErrorHandler(data.input, data.errorType ?? data.opts.wrapError.type),\n\t\tpipe: data.pipe,\n\t\tinput: data.input,\n\t\tcontext: `context[\\`${random}\\`]`,\n\t\tpath: ['key' in data ? data.key : '', data.opts.path].filter(Boolean).join('.') || undefined,\n\t})\n\tdata.opts.rootContext[random] = context\n\treturn lines\n}\n\nfunction compilePipeToString({\n\tpipe,\n\tinput,\n\tcontext: contextStr,\n\tfailEarly = false,\n\tpath = '',\n\trootContext,\n\twrapError = createErrorHandler(input, 'return'),\n\tbase = [],\n}: {\n\tpipe: Pipe<any, any>\n\tinput: string\n\tcontext: string\n\tfailEarly?: boolean\n\tpath?: string\n\trootContext?: Context\n\twrapError?: PipeErrorHandler\n\tbase?: string[]\n}) {\n\tconst ctx = context(pipe)\n\trootContext ??= ctx\n\tconst compiled = walk(pipe, <ReturnType<Pipe<any, any>['compile']>[]>[], (p, acc) => {\n\t\tacc.push(\n\t\t\tp.compile(\n\t\t\t\t{ input, context: contextStr, path: `${path ? `'${path}'` : undefined}` },\n\t\t\t\t{ rootContext, failEarly, path, wrapError },\n\t\t\t),\n\t\t)\n\t\treturn acc\n\t})\n\tconst lines = mergePipeLines(base, compiled.flat())\n\treturn { lines, context: ctx }\n}\n\nfunction mergePipeLines(base: string[], lines: ReturnType<Pipe<any, any>['compile']>) {\n\treturn (Array.isArray(lines) ? lines : [lines]).reduceRight<string[]>((acc, cur) => {\n\t\tif (typeof cur === 'string') acc.unshift(cur)\n\t\telse if (typeof cur === 'function') acc = cur(acc)\n\t\treturn acc\n\t}, base)\n}\n"],"mappings":"AAAA,OAAS,sBAAAA,EAAoB,aAAAC,MAAiB,WAE9C,OAAS,kBAAAC,MAAsB,wBAGxB,SAASC,EAAQC,EAAsBC,EAASC,EAA4C,CAClG,IAAIC,EAASF,EACb,KAAOD,GACNG,EAAMD,EAAOF,EAAMG,CAAG,EACtBH,EAAOA,EAAK,KAEb,OAAOG,CACR,CAEO,SAASC,EAAkCJ,EAAkB,CACnE,OAAOD,EAAKC,EAAM,CAAC,EAAc,CAACK,EAAGF,KAAS,CAAE,GAAGA,EAAK,GAAGE,EAAE,QAAQ,CAAE,EAAE,CAC1E,CAEO,SAASC,EAAiCN,EAASO,EAA+B,CACxF,MAAMC,EAASC,EAAST,EAAMO,CAAK,EACnC,GAAI,CAACC,EAAO,MAAO,MAAMA,EACzB,OAAOA,EAAO,KACf,CAEO,SAASC,EAAmCT,EAASO,EAA+C,CAC1G,GAAI,CAEH,OADWP,EAAK,YAAcU,EAAQV,CAAI,GAChCO,CAAK,CAChB,OAASI,EAAO,CACf,OAAIA,aAAiBd,EAAkBc,EAChCd,EAAU,KAAKc,aAAiB,MAAQA,EAAM,QAAU,GAAGA,CAAK,GAAIJ,EAAO,MAAS,CAC5F,CACD,CAEO,SAASK,EAAiCZ,EAASY,EAAqB,CAAC,EAAe,CAC9F,MAAMC,EAAOT,EAAQJ,CAAI,EACzB,OAAOD,EAAKC,EAAMY,EAAQ,CAACP,EAAGF,KAAS,CAAE,GAAGA,EAAK,GAAGE,EAAE,OAAOQ,CAAI,CAAE,EAAE,CACtE,CAEO,SAASC,EAA+BT,EAAM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J,CAAI,EACxBqC,IAAgBG,EAChB,MAAMC,EAAW1C,EAAKC,EAA+C,CAAC,EAAG,CAACK,EAAGF,KAC5EA,EAAI,KACHE,EAAE,QACD,CAAE,MAAAE,EAAO,QAASW,EAAY,KAAM,GAAGa,EAAO,IAAIA,CAAI,IAAM,MAAS,EAAG,EACxE,CAAE,YAAAM,EAAa,UAAArB,EAAW,KAAAe,EAAM,UAAAO,CAAU,CAC3C,CACD,EACOnC,EACP,EAED,MAAO,CAAE,MADKuC,EAAeH,EAAME,EAAS,KAAK,CAAC,EAClC,QAASD,CAAI,CAC9B,CAEA,SAASE,EAAeH,EAAgBpB,EAA8C,CACrF,OAAQ,MAAM,QAAQA,CAAK,EAAIA,EAAQ,CAACA,CAAK,GAAG,YAAsB,CAAChB,EAAKuB,KACvE,OAAOA,GAAQ,SAAUvB,EAAI,QAAQuB,CAAG,EACnC,OAAOA,GAAQ,aAAYvB,EAAMuB,EAAIvB,CAAG,GAC1CA,GACLoC,CAAI,CACR","names":["createErrorHandler","PipeError","getRandomValue","walk","pipe","init","nodeFn","acc","context","p","assert","input","result","validate","compile","error","schema","cont","meta","standard","failEarly","inputStr","contextStr","lines","compilePipeToString","allLines","l","config","piper","entries","cur","define","value","validity","message","path","fn","key","compileNested","data","random","rootContext","wrapError","base","ctx","compiled","mergePipeLines"]}
|
|
1
|
+
{"version":3,"sources":["../../../../src/api/base/pipes.ts"],"sourcesContent":["import { createErrorHandler, PipeError } from './errors'\nimport { Context, JsonSchemaBuilder, PipeMeta, Pipe, Entry, PipeOutput, PipeFn, PipeCompiledFn, PipeErrorHandler } from './types'\nimport { getRandomValue } from '../../utils/functions'\nimport { JsonSchema } from '../../utils/types'\n\nexport function walk<T>(pipe: Pipe<any, any>, init: T, nodeFn: (cur: Pipe<any, any>, acc: T) => T) {\n\tlet acc: T = init\n\twhile (pipe) {\n\t\tacc = nodeFn(pipe, acc)\n\t\tpipe = pipe.next!\n\t}\n\treturn acc\n}\n\nexport function context<T extends Pipe<any, any>>(pipe: T): Context {\n\treturn walk(pipe, {} as Context, (p, acc) => ({ ...acc, ...p.context() }))\n}\n\nexport function assert<T extends Pipe<any, any>>(pipe: T, input: unknown): PipeOutput<T> {\n\tconst result = validate(pipe, input)\n\tif (!result.valid) throw result.error\n\treturn result.value\n}\n\nexport function validate<T extends Pipe<any, any>>(\n\tpipe: T,\n\tinput: unknown,\n): { value: PipeOutput<T>; valid: true } | { error: PipeError; valid: false } {\n\ttry {\n\t\tconst fn = pipe.__compiled ?? compile(pipe)\n\t\tconst res = fn(input) as ReturnType<PipeCompiledFn<T>>\n\t\treturn res instanceof PipeError ? { error: res, valid: false } : { value: res, valid: true }\n\t} catch (error) {\n\t\tif (error instanceof PipeError) return { error, valid: false }\n\t\treturn { error: PipeError.root(error instanceof Error ? error.message : `${error}`, input, undefined), valid: false }\n\t}\n}\n\nexport function schema<T extends Pipe<any, any>>(pipe: T, schema: JsonSchema = {}): JsonSchema {\n\tconst cont = context(pipe)\n\treturn walk(pipe, schema, (p, acc) => ({ ...acc, ...p.schema(cont) }))\n}\n\nexport function meta<T extends Pipe<any, any>>(p: T, meta: PipeMeta): T {\n\treturn p.pipe(standard(() => [], { schema: () => meta })) as T\n}\n\nexport function compile<T extends Pipe<any, any>>(\n\tpipe: T,\n\t{\n\t\tfailEarly = true,\n\t}: {\n\t\tfailEarly?: boolean\n\t} = {},\n): PipeCompiledFn<T> {\n\tconst inputStr = 'input'\n\tconst contextStr = 'context'\n\tconst { lines, context } = compilePipeToString({\n\t\tpipe,\n\t\tinput: inputStr,\n\t\tcontext: contextStr,\n\t\tfailEarly,\n\t\tbase: [`return ${inputStr}`],\n\t})\n\tconst allLines = [\n\t\t`return (${inputStr}) => {`,\n\t\t...lines.filter((l) => l.trim() !== '').map((l) => `\\t${l}`),\n\t\t`\tthrow PipeError.root('unhandled root validation', ${inputStr})`,\n\t\t`}`,\n\t]\n\tpipe.__compiled = new Function(contextStr, 'PipeError', allLines.join('\\n'))(context, PipeError)\n\treturn pipe.__compiled!\n}\n\nexport function standard<I, O>(\n\tcompile: Pipe<I, O>['compile'],\n\tconfig: {\n\t\tcontext?: Context\n\t\tschema?: (context: Context) => JsonSchemaBuilder\n\t} = {},\n): Pipe<I, O> {\n\tconst piper: Pipe<I, O> = {\n\t\tcontext: () => config.context ?? ({} as any),\n\t\tschema: (context: Context) => config.schema?.(context) ?? ({} as any),\n\t\tpipe: (...entries: Entry<any, any>[]) => {\n\t\t\tdelete piper.__compiled\n\t\t\tfor (const cur of entries) {\n\t\t\t\tconst p = typeof cur === 'function' ? define(cur, config) : cur\n\t\t\t\tif (!piper.next) piper.next = p\n\t\t\t\tif (piper.last) piper.last.next = p\n\t\t\t\tpiper.last = p.last ?? p\n\t\t\t}\n\t\t\treturn piper\n\t\t},\n\t\tcompile,\n\t\t'~standard': {\n\t\t\tversion: 1,\n\t\t\tvendor: 'valleyed',\n\t\t\tvalidate(value) {\n\t\t\t\tconst validity = validate(piper, value)\n\t\t\t\tif (validity.valid) return { value: validity.value }\n\t\t\t\treturn {\n\t\t\t\t\tissues: validity.error.messages.map(({ message, path }) => ({\n\t\t\t\t\t\tmessage,\n\t\t\t\t\t\tpath: path ? path.split('.') : undefined,\n\t\t\t\t\t})),\n\t\t\t\t}\n\t\t\t},\n\t\t},\n\t}\n\treturn piper\n}\n\nexport function define<I, O>(\n\tfn: PipeFn<I, O>,\n\tconfig: {\n\t\tcontext?: Context\n\t\tschema?: (context: Context) => JsonSchemaBuilder\n\t} = {},\n): Pipe<I, O> {\n\tconst key = `define_${getRandomValue()}`\n\treturn standard<I, O>(\n\t\t({ input, context, path }) => [\n\t\t\t`${input} = ${context}['${key}'](${input})`,\n\t\t\t`if (${input} instanceof PipeError) return PipeError.path(${path}, ${input})`,\n\t\t],\n\t\t{\n\t\t\tcontext: { ...config?.context, [key]: fn },\n\t\t\tschema: config?.schema,\n\t\t},\n\t)\n}\n\nexport function compileNested(\n\tdata: {\n\t\tpipe: Pipe<any, any>\n\t\terrorType?: Parameters<typeof createErrorHandler>[1]\n\t\topts: Required<Pick<Parameters<Pipe<any, any>['compile']>[1], 'rootContext' | 'failEarly' | 'path' | 'wrapError'>>\n\t} & { input: string; key?: string },\n) {\n\tconst random = getRandomValue()\n\tconst { lines, context } = compilePipeToString({\n\t\t...data.opts,\n\t\twrapError: createErrorHandler(data.input, data.errorType ?? data.opts.wrapError.type),\n\t\tpipe: data.pipe,\n\t\tinput: data.input,\n\t\tcontext: `context[\\`${random}\\`]`,\n\t\tpath: ['key' in data ? data.key : '', data.opts.path].filter(Boolean).join('.') || undefined,\n\t})\n\tdata.opts.rootContext[random] = context\n\treturn lines\n}\n\nfunction compilePipeToString({\n\tpipe,\n\tinput,\n\tcontext: contextStr,\n\tfailEarly = false,\n\tpath = '',\n\trootContext,\n\twrapError = createErrorHandler(input, 'return'),\n\tbase = [],\n}: {\n\tpipe: Pipe<any, any>\n\tinput: string\n\tcontext: string\n\tfailEarly?: boolean\n\tpath?: string\n\trootContext?: Context\n\twrapError?: PipeErrorHandler\n\tbase?: string[]\n}) {\n\tconst ctx = context(pipe)\n\trootContext ??= ctx\n\tconst compiled = walk(pipe, <ReturnType<Pipe<any, any>['compile']>[]>[], (p, acc) => {\n\t\tacc.push(\n\t\t\tp.compile(\n\t\t\t\t{ input, context: contextStr, path: `${path ? `'${path}'` : undefined}` },\n\t\t\t\t{ rootContext, failEarly, path, wrapError },\n\t\t\t),\n\t\t)\n\t\treturn acc\n\t})\n\tconst lines = mergePipeLines(base, compiled.flat())\n\treturn { lines, context: ctx }\n}\n\nfunction mergePipeLines(base: string[], lines: ReturnType<Pipe<any, any>['compile']>) {\n\treturn (Array.isArray(lines) ? lines : [lines]).reduceRight<string[]>((acc, cur) => {\n\t\tif (typeof cur === 'string') acc.unshift(cur)\n\t\telse if (typeof cur === 'function') acc = cur(acc)\n\t\treturn acc\n\t}, base)\n}\n"],"mappings":"AAAA,OAAS,sBAAAA,EAAoB,aAAAC,MAAiB,WAE9C,OAAS,kBAA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AAGa,EAAO,IAAIA,CAAI,IAAM,MAAS,EAAG,EACxE,CAAE,YAAAM,EAAa,UAAArB,EAAW,KAAAe,EAAM,UAAAO,CAAU,CAC3C,CACD,EACOpC,EACP,EAED,MAAO,CAAE,MADKwC,EAAeH,EAAME,EAAS,KAAK,CAAC,EAClC,QAASD,CAAI,CAC9B,CAEA,SAASE,EAAeH,EAAgBpB,EAA8C,CACrF,OAAQ,MAAM,QAAQA,CAAK,EAAIA,EAAQ,CAACA,CAAK,GAAG,YAAsB,CAACjB,EAAKwB,KACvE,OAAOA,GAAQ,SAAUxB,EAAI,QAAQwB,CAAG,EACnC,OAAOA,GAAQ,aAAYxB,EAAMwB,EAAIxB,CAAG,GAC1CA,GACLqC,CAAI,CACR","names":["createErrorHandler","PipeError","getRandomValue","walk","pipe","init","nodeFn","acc","context","p","assert","input","result","validate","res","compile","error","schema","cont","meta","standard","failEarly","inputStr","contextStr","lines","compilePipeToString","allLines","l","config","piper","entries","cur","define","value","validity","message","path","fn","key","compileNested","data","random","rootContext","wrapError","base","ctx","compiled","mergePipeLines"]}
|
|
@@ -13,16 +13,17 @@ function context(pipe) {
|
|
|
13
13
|
}
|
|
14
14
|
function assert(pipe, input) {
|
|
15
15
|
const result = validate(pipe, input);
|
|
16
|
-
if (!result.valid) throw result;
|
|
16
|
+
if (!result.valid) throw result.error;
|
|
17
17
|
return result.value;
|
|
18
18
|
}
|
|
19
19
|
function validate(pipe, input) {
|
|
20
20
|
try {
|
|
21
21
|
const fn = pipe.__compiled ?? compile(pipe);
|
|
22
|
-
|
|
22
|
+
const res = fn(input);
|
|
23
|
+
return res instanceof PipeError ? { error: res, valid: false } : { value: res, valid: true };
|
|
23
24
|
} catch (error) {
|
|
24
|
-
if (error instanceof PipeError) return error;
|
|
25
|
-
return PipeError.root(error instanceof Error ? error.message : `${error}`, input, void 0);
|
|
25
|
+
if (error instanceof PipeError) return { error, valid: false };
|
|
26
|
+
return { error: PipeError.root(error instanceof Error ? error.message : `${error}`, input, void 0), valid: false };
|
|
26
27
|
}
|
|
27
28
|
}
|
|
28
29
|
function schema(pipe, schema2 = {}) {
|
|
@@ -42,7 +43,7 @@ function compile(pipe, {
|
|
|
42
43
|
input: inputStr,
|
|
43
44
|
context: contextStr,
|
|
44
45
|
failEarly,
|
|
45
|
-
base: [`return
|
|
46
|
+
base: [`return ${inputStr}`]
|
|
46
47
|
});
|
|
47
48
|
const allLines = [
|
|
48
49
|
`return (${inputStr}) => {`,
|
|
@@ -75,7 +76,7 @@ function standard(compile2, config = {}) {
|
|
|
75
76
|
const validity = validate(piper, value);
|
|
76
77
|
if (validity.valid) return { value: validity.value };
|
|
77
78
|
return {
|
|
78
|
-
issues: validity.messages.map(({ message, path }) => ({
|
|
79
|
+
issues: validity.error.messages.map(({ message, path }) => ({
|
|
79
80
|
message,
|
|
80
81
|
path: path ? path.split(".") : void 0
|
|
81
82
|
}))
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../src/api/base/pipes.ts"],"sourcesContent":["import { createErrorHandler, PipeError } from './errors'\nimport { Context, JsonSchemaBuilder, PipeMeta, Pipe, Entry, PipeOutput, PipeFn, PipeCompiledFn, PipeErrorHandler } from './types'\nimport { getRandomValue } from '../../utils/functions'\nimport { JsonSchema } from '../../utils/types'\n\nexport function walk<T>(pipe: Pipe<any, any>, init: T, nodeFn: (cur: Pipe<any, any>, acc: T) => T) {\n\tlet acc: T = init\n\twhile (pipe) {\n\t\tacc = nodeFn(pipe, acc)\n\t\tpipe = pipe.next!\n\t}\n\treturn acc\n}\n\nexport function context<T extends Pipe<any, any>>(pipe: T): Context {\n\treturn walk(pipe, {} as Context, (p, acc) => ({ ...acc, ...p.context() }))\n}\n\nexport function assert<T extends Pipe<any, any>>(pipe: T, input: unknown): PipeOutput<T> {\n\tconst result = validate(pipe, input)\n\tif (!result.valid) throw result\n\treturn result.value\n}\n\nexport function validate<T extends Pipe<any, any>>(
|
|
1
|
+
{"version":3,"sources":["../../../../src/api/base/pipes.ts"],"sourcesContent":["import { createErrorHandler, PipeError } from './errors'\nimport { Context, JsonSchemaBuilder, PipeMeta, Pipe, Entry, PipeOutput, PipeFn, PipeCompiledFn, PipeErrorHandler } from './types'\nimport { getRandomValue } from '../../utils/functions'\nimport { JsonSchema } from '../../utils/types'\n\nexport function walk<T>(pipe: Pipe<any, any>, init: T, nodeFn: (cur: Pipe<any, any>, acc: T) => T) {\n\tlet acc: T = init\n\twhile (pipe) {\n\t\tacc = nodeFn(pipe, acc)\n\t\tpipe = pipe.next!\n\t}\n\treturn acc\n}\n\nexport function context<T extends Pipe<any, any>>(pipe: T): Context {\n\treturn walk(pipe, {} as Context, (p, acc) => ({ ...acc, ...p.context() }))\n}\n\nexport function assert<T extends Pipe<any, any>>(pipe: T, input: unknown): PipeOutput<T> {\n\tconst result = validate(pipe, input)\n\tif (!result.valid) throw result.error\n\treturn result.value\n}\n\nexport function validate<T extends Pipe<any, any>>(\n\tpipe: T,\n\tinput: unknown,\n): { value: PipeOutput<T>; valid: true } | { error: PipeError; valid: false } {\n\ttry {\n\t\tconst fn = pipe.__compiled ?? compile(pipe)\n\t\tconst res = fn(input) as ReturnType<PipeCompiledFn<T>>\n\t\treturn res instanceof PipeError ? { error: res, valid: false } : { value: res, valid: true }\n\t} catch (error) {\n\t\tif (error instanceof PipeError) return { error, valid: false }\n\t\treturn { error: PipeError.root(error instanceof Error ? error.message : `${error}`, input, undefined), valid: false }\n\t}\n}\n\nexport function schema<T extends Pipe<any, any>>(pipe: T, schema: JsonSchema = {}): JsonSchema {\n\tconst cont = context(pipe)\n\treturn walk(pipe, schema, (p, acc) => ({ ...acc, ...p.schema(cont) }))\n}\n\nexport function meta<T extends Pipe<any, any>>(p: T, meta: PipeMeta): T {\n\treturn p.pipe(standard(() => [], { schema: () => meta })) as T\n}\n\nexport function compile<T extends Pipe<any, any>>(\n\tpipe: T,\n\t{\n\t\tfailEarly = true,\n\t}: {\n\t\tfailEarly?: boolean\n\t} = {},\n): PipeCompiledFn<T> {\n\tconst inputStr = 'input'\n\tconst contextStr = 'context'\n\tconst { lines, context } = compilePipeToString({\n\t\tpipe,\n\t\tinput: inputStr,\n\t\tcontext: contextStr,\n\t\tfailEarly,\n\t\tbase: [`return ${inputStr}`],\n\t})\n\tconst allLines = [\n\t\t`return (${inputStr}) => {`,\n\t\t...lines.filter((l) => l.trim() !== '').map((l) => `\\t${l}`),\n\t\t`\tthrow PipeError.root('unhandled root validation', ${inputStr})`,\n\t\t`}`,\n\t]\n\tpipe.__compiled = new Function(contextStr, 'PipeError', allLines.join('\\n'))(context, PipeError)\n\treturn pipe.__compiled!\n}\n\nexport function standard<I, O>(\n\tcompile: Pipe<I, O>['compile'],\n\tconfig: {\n\t\tcontext?: Context\n\t\tschema?: (context: Context) => JsonSchemaBuilder\n\t} = {},\n): Pipe<I, O> {\n\tconst piper: Pipe<I, O> = {\n\t\tcontext: () => config.context ?? ({} as any),\n\t\tschema: (context: Context) => config.schema?.(context) ?? ({} as any),\n\t\tpipe: (...entries: Entry<any, any>[]) => {\n\t\t\tdelete piper.__compiled\n\t\t\tfor (const cur of entries) {\n\t\t\t\tconst p = typeof cur === 'function' ? define(cur, config) : cur\n\t\t\t\tif (!piper.next) piper.next = p\n\t\t\t\tif (piper.last) piper.last.next = p\n\t\t\t\tpiper.last = p.last ?? p\n\t\t\t}\n\t\t\treturn piper\n\t\t},\n\t\tcompile,\n\t\t'~standard': {\n\t\t\tversion: 1,\n\t\t\tvendor: 'valleyed',\n\t\t\tvalidate(value) {\n\t\t\t\tconst validity = validate(piper, value)\n\t\t\t\tif (validity.valid) return { value: validity.value }\n\t\t\t\treturn {\n\t\t\t\t\tissues: validity.error.messages.map(({ message, path }) => ({\n\t\t\t\t\t\tmessage,\n\t\t\t\t\t\tpath: path ? path.split('.') : undefined,\n\t\t\t\t\t})),\n\t\t\t\t}\n\t\t\t},\n\t\t},\n\t}\n\treturn piper\n}\n\nexport function define<I, O>(\n\tfn: PipeFn<I, O>,\n\tconfig: {\n\t\tcontext?: Context\n\t\tschema?: (context: Context) => JsonSchemaBuilder\n\t} = {},\n): Pipe<I, O> {\n\tconst key = `define_${getRandomValue()}`\n\treturn standard<I, O>(\n\t\t({ input, context, path }) => [\n\t\t\t`${input} = ${context}['${key}'](${input})`,\n\t\t\t`if (${input} instanceof PipeError) return PipeError.path(${path}, ${input})`,\n\t\t],\n\t\t{\n\t\t\tcontext: { ...config?.context, [key]: fn },\n\t\t\tschema: config?.schema,\n\t\t},\n\t)\n}\n\nexport function compileNested(\n\tdata: {\n\t\tpipe: Pipe<any, any>\n\t\terrorType?: Parameters<typeof createErrorHandler>[1]\n\t\topts: Required<Pick<Parameters<Pipe<any, any>['compile']>[1], 'rootContext' | 'failEarly' | 'path' | 'wrapError'>>\n\t} & { input: string; key?: string },\n) {\n\tconst random = getRandomValue()\n\tconst { lines, context } = compilePipeToString({\n\t\t...data.opts,\n\t\twrapError: createErrorHandler(data.input, data.errorType ?? data.opts.wrapError.type),\n\t\tpipe: data.pipe,\n\t\tinput: data.input,\n\t\tcontext: `context[\\`${random}\\`]`,\n\t\tpath: ['key' in data ? data.key : '', data.opts.path].filter(Boolean).join('.') || undefined,\n\t})\n\tdata.opts.rootContext[random] = context\n\treturn lines\n}\n\nfunction compilePipeToString({\n\tpipe,\n\tinput,\n\tcontext: contextStr,\n\tfailEarly = false,\n\tpath = '',\n\trootContext,\n\twrapError = createErrorHandler(input, 'return'),\n\tbase = [],\n}: {\n\tpipe: Pipe<any, any>\n\tinput: string\n\tcontext: string\n\tfailEarly?: boolean\n\tpath?: string\n\trootContext?: Context\n\twrapError?: PipeErrorHandler\n\tbase?: string[]\n}) {\n\tconst ctx = context(pipe)\n\trootContext ??= ctx\n\tconst compiled = walk(pipe, <ReturnType<Pipe<any, any>['compile']>[]>[], (p, acc) => {\n\t\tacc.push(\n\t\t\tp.compile(\n\t\t\t\t{ input, context: contextStr, path: `${path ? `'${path}'` : undefined}` },\n\t\t\t\t{ rootContext, failEarly, path, wrapError },\n\t\t\t),\n\t\t)\n\t\treturn acc\n\t})\n\tconst lines = mergePipeLines(base, compiled.flat())\n\treturn { lines, context: ctx }\n}\n\nfunction mergePipeLines(base: string[], lines: ReturnType<Pipe<any, any>['compile']>) {\n\treturn (Array.isArray(lines) ? lines : [lines]).reduceRight<string[]>((acc, cur) => {\n\t\tif (typeof cur === 'string') acc.unshift(cur)\n\t\telse if (typeof cur === 'function') acc = cur(acc)\n\t\treturn acc\n\t}, base)\n}\n"],"mappings":"AAAA,SAAS,oBAAoB,iBAAiB;AAE9C,SAAS,sBAAsB;AAGxB,SAAS,KAAQ,MAAsB,MAAS,QAA4C;AAClG,MAAI,MAAS;AACb,SAAO,MAAM;AACZ,UAAM,OAAO,MAAM,GAAG;AACtB,WAAO,KAAK;AAAA,EACb;AACA,SAAO;AACR;AAEO,SAAS,QAAkC,MAAkB;AACnE,SAAO,KAAK,MAAM,CAAC,GAAc,CAAC,GAAG,SAAS,EAAE,GAAG,KAAK,GAAG,EAAE,QAAQ,EAAE,EAAE;AAC1E;AAEO,SAAS,OAAiC,MAAS,OAA+B;AACxF,QAAM,SAAS,SAAS,MAAM,KAAK;AACnC,MAAI,CAAC,OAAO,MAAO,OAAM,OAAO;AAChC,SAAO,OAAO;AACf;AAEO,SAAS,SACf,MACA,OAC6E;AAC7E,MAAI;AACH,UAAM,KAAK,KAAK,cAAc,QAAQ,IAAI;AAC1C,UAAM,MAAM,GAAG,KAAK;AACpB,WAAO,eAAe,YAAY,EAAE,OAAO,KAAK,OAAO,MAAM,IAAI,EAAE,OAAO,KAAK,OAAO,KAAK;AAAA,EAC5F,SAAS,OAAO;AACf,QAAI,iBAAiB,UAAW,QAAO,EAAE,OAAO,OAAO,MAAM;AAC7D,WAAO,EAAE,OAAO,UAAU,KAAK,iBAAiB,QAAQ,MAAM,UAAU,GAAG,KAAK,IAAI,OAAO,MAAS,GAAG,OAAO,MAAM;AAAA,EACrH;AACD;AAEO,SAAS,OAAiC,MAASA,UAAqB,CAA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AE,OAAO,OAAO,EAAE,KAAK,GAAG,KAAK;AAAA,EACpF,CAAC;AACD,OAAK,KAAK,YAAY,MAAM,IAAIA;AAChC,SAAO;AACR;AAEA,SAAS,oBAAoB;AAAA,EAC5B;AAAA,EACA;AAAA,EACA,SAAS;AAAA,EACT,YAAY;AAAA,EACZ,OAAO;AAAA,EACP;AAAA,EACA,YAAY,mBAAmB,OAAO,QAAQ;AAAA,EAC9C,OAAO,CAAC;AACT,GASG;AACF,QAAM,MAAM,QAAQ,IAAI;AACxB,kBAAgB;AAChB,QAAM,WAAW,KAAK,MAA+C,CAAC,GAAG,CAAC,GAAG,QAAQ;AACpF,QAAI;AAAA,MACH,EAAE;AAAA,QACD,EAAE,OAAO,SAAS,YAAY,MAAM,GAAG,OAAO,IAAI,IAAI,MAAM,MAAS,GAAG;AAAA,QACxE,EAAE,aAAa,WAAW,MAAM,UAAU;AAAA,MAC3C;AAAA,IACD;AACA,WAAO;AAAA,EACR,CAAC;AACD,QAAM,QAAQ,eAAe,MAAM,SAAS,KAAK,CAAC;AAClD,SAAO,EAAE,OAAO,SAAS,IAAI;AAC9B;AAEA,SAAS,eAAe,MAAgB,OAA8C;AACrF,UAAQ,MAAM,QAAQ,KAAK,IAAI,QAAQ,CAAC,KAAK,GAAG,YAAsB,CAAC,KAAK,QAAQ;AACnF,QAAI,OAAO,QAAQ,SAAU,KAAI,QAAQ,GAAG;AAAA,aACnC,OAAO,QAAQ,WAAY,OAAM,IAAI,GAAG;AACjD,WAAO;AAAA,EACR,GAAG,IAAI;AACR;","names":["schema","meta","context","compile"]}
|
|
@@ -1,2 +1,2 @@
|
|
|
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 l,standard as u,schema as s,define as d,validate as y}from "./base/pipes.min.mjs";const I=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"}),`if (${e} instanceof PipeError) {`,` ${r}.push(PipeError.path(${p}, ${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))})}),A=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:{...l(a),wrapInTryCatch:f},schema:a.schema})},_=a=>d(e=>{const r=y(a(),e);return r.valid?r.value:r},{schema:()=>s(a())}),k=(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=!0,[`function ${r}(node) {`,...i({opts:{...p,failEarly:!0},pipe:a(),input:"node",errorType:"return"}).map(m=>` ${m}`),"}",...$])},{schema:()=>n?{$refId:e}:(n=!0,s(a(),{$refId:e}))})};export{V as discriminate,A as fromJson,_ as lazy,B as merge,I as or,k as recursive};
|
|
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 l,standard as u,schema as s,define as d,validate as y}from "./base/pipes.min.mjs";const I=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"}),`if (${e} instanceof PipeError) {`,` ${r}.push(PipeError.path(${p}, ${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))})}),A=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:{...l(a),wrapInTryCatch:f},schema:a.schema})},_=a=>d(e=>{const r=y(a(),e);return r.valid?r.value:r.error},{schema:()=>s(a())}),k=(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=!0,[`function ${r}(node) {`,...i({opts:{...p,failEarly:!0},pipe:a(),input:"node",errorType:"return"}).map(m=>` ${m}`),"}",...$])},{schema:()=>n?{$refId:e}:(n=!0,s(a(),{$refId:e}))})};export{V as discriminate,A as fromJson,_ as lazy,B as merge,I as or,k 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}),\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}))`,\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(\n\t\t(input) => {\n\t\t\tconst result = validate(pipeFn(), input)\n\t\t\treturn result.valid ? result.value : result\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\tif (compiledBefore) return common\n\t\t\tcompiledBefore = true\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'}',\n\t\t\t\t...common,\n\t\t\t]\n\t\t},\n\t\t{\n\t\t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AAwB,CAAc,EACzC,OAAQ,KAAO,CAAE,MAAO,OAAO,OAAOhB,CAAQ,EAAE,IAAKoB,GAAMxB,EAAOwB,CAAC,CAAC,CAAE,EACvE,CACD,EAEYC,EAAsChB,GAAc,CAChE,MAAMJ,EAAmB,aAAaV,EAAe,CAAC,GACtD,OAAOI,EACN,CAAC,CAAE,MAAAQ,EAAO,QAAAT,CAAQ,EAAGU,IAAS,CAC7B,OAAOH,CAAgB,MAAME,CAAK,GAClC,GAAGV,EAAc,CAAE,KAAAW,EAAM,KAAMC,EAAQ,MAAOJ,EAAkB,UAAW,QAAS,CAAC,EACrF,OAAOA,CAAgB,2BACvBG,EAAK,UAAU,GAAGD,CAAK,mCAAoCF,CAAgB,EAC3E,IAAIA,CAAgB,MAAMP,CAAO,oCAAoCS,CAAK,MAAMF,CAAgB,IAChGG,EAAK,UAAU,GAAGH,CAAgB,wBAAyBA,CAAgB,EAC3E,GAAGR,EAAc,CAAE,KAAAW,EAAM,KAAMC,EAAQ,MAAOJ,EAAkB,UAAW,QAAS,CAAC,EAAE,IAAKO,GAAM,IAAIA,CAAC,EAAE,EACzGJ,EAAK,UAAU,GAAGH,CAAgB,wBAAyBA,CAAgB,EAC3E,IACA,GAAGE,CAAK,MAAMF,CAAgB,EAC/B,EACA,CACC,QAAS,CAAE,GAAGP,EAAQW,CAAM,EAAG,eAAAb,CAAe,EAC9C,OAAQa,EAAO,MAChB,CACD,CACD,EAEaiB,EAAkCC,GAC9C1B,EACEM,GAAU,CACV,MAAMqB,EAAS1B,EAASyB,EAAO,EAAGpB,CAAK,EACvC,OAAOqB,EAAO,MAAQA,EAAO,MAAQA,
|
|
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}),\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}))`,\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(\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\tif (compiledBefore) return common\n\t\t\tcompiledBefore = true\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'}',\n\t\t\t\t...common,\n\t\t\t]\n\t\t},\n\t\t{\n\t\t\tschema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AQ,CAAM,EAAGC,IAAS,CACpB,MAAM0B,EAAS,CAAC,GAAG3B,CAAK,MAAMwB,CAAS,IAAIxB,CAAK,IAAKC,EAAK,UAAU,GAAGD,CAAK,wBAAyBA,CAAK,CAAC,EAC3G,OAAIyB,EAAuBE,GAC3BF,EAAiB,GACV,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,IACA,GAAGsB,CACJ,EACD,EACA,CACC,OAAQ,IACHD,EAAsB,CAAE,OAAAH,CAAO,GACnCG,EAAgB,GACTjC,EAAO2B,EAAO,EAAG,CAAE,OAAAG,CAAO,CAAC,EAEpC,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"]}
|
|
@@ -89,7 +89,7 @@ const fromJson = (branch) => {
|
|
|
89
89
|
const lazy = (pipeFn) => define(
|
|
90
90
|
(input) => {
|
|
91
91
|
const result = validate(pipeFn(), input);
|
|
92
|
-
return result.valid ? result.value : result;
|
|
92
|
+
return result.valid ? result.value : result.error;
|
|
93
93
|
},
|
|
94
94
|
{
|
|
95
95
|
schema: () => schema(pipeFn())
|
|
@@ -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}),\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}))`,\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(\n\t\t(input) => {\n\t\t\tconst result = validate(pipeFn(), input)\n\t\t\treturn result.valid ? result.value : result\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\tif (compiledBefore) return common\n\t\t\tcompiledBefore = true\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'}',\n\t\t\t\t...common,\n\t\t\t]\n\t\t},\n\t\t{\n\t\t\tschema: () => {\n\t\t\t\tif (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Content":["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}),\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}))`,\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(\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\tif (compiledBefore) return common\n\t\t\tcompiledBefore = true\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'}',\n\t\t\t\t...common,\n\t\t\t]\n\t\t},\n\t\t{\n\t\t\tschema: () => {\n\t\t\t\tif (schemedBefore) return { $refId }\n\t\t\t\tschemedBefore = true\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,GAAG,SACD,IAAI,CAAC,QAAQ,QAAQ,CAAC,UAAoB;AAAA,QAC1C,GAAG,gBAAgB,MAAM,KAAK;AAAA,QAC9B,GAAG,cAAc;AAAA,UAChB,MAAM,EAAE,GAAG,MAAM,WAAW,KAAK;AAAA,UACjC,MAAM;AAAA,UACN,OAAO;AAAA,UACP,WAAW;AAAA,QACZ,CAAC;AAAA,QACD,OAAO,gBAAgB;AAAA,QACvB,IAAI,aAAa,wBAAwB,GAAG,KAAK,gBAAgB;AAAA,QACjE,GAAG,MAAM,IAAI,CAAC,MAAM,IAAI,CAAC,EAAE;AAAA,QAC3B,QAAQ,SAAS,SAAS,IAAI,IAAI,KAAK,UAAU,OAAO,sBAAsB,aAAa,GAAG,CAAC,KAAK;AAAA,QACpG;AAAA,QACA,QAAQ,KAAK,MAAM,gBAAgB;AAAA,MACpC,CAAC,EACA,YAAsB,CAAC,KAAK,QAAQ,IAAI,GAA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AM,OAAO,CAA2B,WAC9C;AAAA,EACC,CAAC,UAAU;AACV,UAAM,SAAS,SAAS,OAAO,GAAG,KAAK;AACvC,WAAO,OAAO,QAAQ,OAAO,QAAQ,OAAO;AAAA,EAC7C;AAAA,EACA;AAAA,IACC,QAAQ,MAAM,OAAO,OAAO,CAAC;AAAA,EAC9B;AACD;AAEM,MAAM,YAAY,CAA2B,QAAiB,WAAmB;AACvF,QAAM,YAAY,MAAM,eAAe,CAAC;AACxC,MAAI,iBAAiB;AACrB,MAAI,gBAAgB;AACpB,SAAO;AAAA,IACN,CAAC,EAAE,MAAM,GAAG,SAAS;AACpB,YAAM,SAAS,CAAC,GAAG,KAAK,MAAM,SAAS,IAAI,KAAK,KAAK,KAAK,UAAU,GAAG,KAAK,yBAAyB,KAAK,CAAC;AAC3G,UAAI,eAAgB,QAAO;AAC3B,uBAAiB;AACjB,aAAO;AAAA,QACN,YAAY,SAAS;AAAA,QACrB,GAAG,cAAc,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,GAAG;AAAA,MACJ;AAAA,IACD;AAAA,IACA;AAAA,MACC,QAAQ,MAAM;AACb,YAAI,cAAe,QAAO,EAAE,OAAO;AACnC,wBAAgB;AAChB,eAAO,OAAO,OAAO,GAAG,EAAE,OAAO,CAAC;AAAA,MACnC;AAAA,IACD;AAAA,EACD;AACD;","names":["context"]}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { C as Context, E as Entry, J as JsonSchemaBuilder, i as Pipe, b as PipeCompiledFn, P as PipeError, e as PipeErrorHandler, d as PipeErrorHandlerType, a as PipeFn, f as PipeInput, h as PipeMeta, g as PipeOutput, c as createErrorHandler } from '../../errors-
|
|
1
|
+
export { C as Context, E as Entry, J as JsonSchemaBuilder, i as Pipe, b as PipeCompiledFn, P as PipeError, e as PipeErrorHandler, d as PipeErrorHandlerType, a as PipeFn, f as PipeInput, h as PipeMeta, g as PipeOutput, c as createErrorHandler } from '../../errors-C1ef9q7I.js';
|
|
2
2
|
export { compileNested } from './pipes.js';
|
|
3
3
|
import '@standard-schema/spec';
|
|
4
4
|
import '../../utils/types.js';
|
|
@@ -1,11 +1,17 @@
|
|
|
1
|
-
import { i as Pipe, c as createErrorHandler, g as PipeOutput, C as Context, b as PipeCompiledFn, a as PipeFn, J as JsonSchemaBuilder, h as PipeMeta } from '../../errors-
|
|
1
|
+
import { i as Pipe, c as createErrorHandler, g as PipeOutput, C as Context, P as PipeError, b as PipeCompiledFn, a as PipeFn, J as JsonSchemaBuilder, h as PipeMeta } from '../../errors-C1ef9q7I.js';
|
|
2
2
|
import { JsonSchema } from '../../utils/types.js';
|
|
3
3
|
import '@standard-schema/spec';
|
|
4
4
|
|
|
5
5
|
declare function walk<T>(pipe: Pipe<any, any>, init: T, nodeFn: (cur: Pipe<any, any>, acc: T) => T): T;
|
|
6
6
|
declare function context<T extends Pipe<any, any>>(pipe: T): Context;
|
|
7
7
|
declare function assert<T extends Pipe<any, any>>(pipe: T, input: unknown): PipeOutput<T>;
|
|
8
|
-
declare function validate<T extends Pipe<any, any>>(pipe: T, input: unknown):
|
|
8
|
+
declare function validate<T extends Pipe<any, any>>(pipe: T, input: unknown): {
|
|
9
|
+
value: PipeOutput<T>;
|
|
10
|
+
valid: true;
|
|
11
|
+
} | {
|
|
12
|
+
error: PipeError;
|
|
13
|
+
valid: false;
|
|
14
|
+
};
|
|
9
15
|
declare function schema<T extends Pipe<any, any>>(pipe: T, schema?: JsonSchema): JsonSchema;
|
|
10
16
|
declare function meta<T extends Pipe<any, any>>(p: T, meta: PipeMeta): T;
|
|
11
17
|
declare function compile<T extends Pipe<any, any>>(pipe: T, { failEarly, }?: {
|
|
@@ -13,16 +13,17 @@ function context(pipe) {
|
|
|
13
13
|
}
|
|
14
14
|
function assert(pipe, input) {
|
|
15
15
|
const result = validate(pipe, input);
|
|
16
|
-
if (!result.valid) throw result;
|
|
16
|
+
if (!result.valid) throw result.error;
|
|
17
17
|
return result.value;
|
|
18
18
|
}
|
|
19
19
|
function validate(pipe, input) {
|
|
20
20
|
try {
|
|
21
21
|
const fn = pipe.__compiled ?? compile(pipe);
|
|
22
|
-
|
|
22
|
+
const res = fn(input);
|
|
23
|
+
return res instanceof PipeError ? { error: res, valid: false } : { value: res, valid: true };
|
|
23
24
|
} catch (error) {
|
|
24
|
-
if (error instanceof PipeError) return error;
|
|
25
|
-
return PipeError.root(error instanceof Error ? error.message : `${error}`, input, void 0);
|
|
25
|
+
if (error instanceof PipeError) return { error, valid: false };
|
|
26
|
+
return { error: PipeError.root(error instanceof Error ? error.message : `${error}`, input, void 0), valid: false };
|
|
26
27
|
}
|
|
27
28
|
}
|
|
28
29
|
function schema(pipe, schema2 = {}) {
|
|
@@ -42,7 +43,7 @@ function compile(pipe, {
|
|
|
42
43
|
input: inputStr,
|
|
43
44
|
context: contextStr,
|
|
44
45
|
failEarly,
|
|
45
|
-
base: [`return
|
|
46
|
+
base: [`return ${inputStr}`]
|
|
46
47
|
});
|
|
47
48
|
const allLines = [
|
|
48
49
|
`return (${inputStr}) => {`,
|
|
@@ -75,7 +76,7 @@ function standard(compile2, config = {}) {
|
|
|
75
76
|
const validity = validate(piper, value);
|
|
76
77
|
if (validity.valid) return { value: validity.value };
|
|
77
78
|
return {
|
|
78
|
-
issues: validity.messages.map(({ message, path }) => ({
|
|
79
|
+
issues: validity.error.messages.map(({ message, path }) => ({
|
|
79
80
|
message,
|
|
80
81
|
path: path ? path.split(".") : void 0
|
|
81
82
|
}))
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import '@standard-schema/spec';
|
|
2
|
-
export { C as Context, E as Entry, J as JsonSchemaBuilder, i as Pipe, b as PipeCompiledFn, e as PipeErrorHandler, d as PipeErrorHandlerType, a as PipeFn, f as PipeInput, h as PipeMeta, g as PipeOutput } from '../../errors-
|
|
2
|
+
export { C as Context, E as Entry, J as JsonSchemaBuilder, i as Pipe, b as PipeCompiledFn, e as PipeErrorHandler, d as PipeErrorHandlerType, a as PipeFn, f as PipeInput, h as PipeMeta, g as PipeOutput } from '../../errors-C1ef9q7I.js';
|
|
3
3
|
import '../../utils/types.js';
|
package/dist/types/api/core.d.ts
CHANGED
|
@@ -10,6 +10,6 @@ export { any, boolean, instanceOf, null, number, string, undefined } from './typ
|
|
|
10
10
|
export { asMap, object, objectOmit, objectPick, record } from './records.js';
|
|
11
11
|
export { asCapitalized, asLowercased, asSliced, asStrippedHtml, asTrimmed, asUppercased, email, url, withStrippedHtml } from './strings.js';
|
|
12
12
|
export { Timeable, after, asISOString, asStamp, before, time } from './times.js';
|
|
13
|
-
import '../errors-
|
|
13
|
+
import '../errors-C1ef9q7I.js';
|
|
14
14
|
import '@standard-schema/spec';
|
|
15
15
|
import '../utils/types.js';
|
|
@@ -10,7 +10,7 @@ import { any as isAny, boolean as isBoolean, instanceOf as isInstanceOf, null as
|
|
|
10
10
|
import { asMap, object, objectOmit, objectPick, record } from './records.js';
|
|
11
11
|
import { asCapitalized, asLowercased, asSliced, asStrippedHtml, asTrimmed, asUppercased, email, url, withStrippedHtml } from './strings.js';
|
|
12
12
|
import { Timeable, after, asISOString, asStamp, before, time } from './times.js';
|
|
13
|
-
import '../errors-
|
|
13
|
+
import '../errors-C1ef9q7I.js';
|
|
14
14
|
import '@standard-schema/spec';
|
|
15
15
|
import '../utils/types.js';
|
|
16
16
|
|
|
@@ -89,7 +89,7 @@ const fromJson = (branch) => {
|
|
|
89
89
|
const lazy = (pipeFn) => define(
|
|
90
90
|
(input) => {
|
|
91
91
|
const result = validate(pipeFn(), input);
|
|
92
|
-
return result.valid ? result.value : result;
|
|
92
|
+
return result.valid ? result.value : result.error;
|
|
93
93
|
},
|
|
94
94
|
{
|
|
95
95
|
schema: () => schema(pipeFn())
|
|
@@ -2,10 +2,7 @@ import { StandardSchemaV1 } from '@standard-schema/spec';
|
|
|
2
2
|
import { JsonSchema } from './utils/types.js';
|
|
3
3
|
|
|
4
4
|
type PipeFn<I, O> = (input: I) => O | PipeError;
|
|
5
|
-
type PipeCompiledFn<T extends Pipe<any, any>> = (input: unknown) =>
|
|
6
|
-
value: PipeOutput<T>;
|
|
7
|
-
valid: true;
|
|
8
|
-
} | PipeError;
|
|
5
|
+
type PipeCompiledFn<T extends Pipe<any, any>> = (input: unknown) => PipeOutput<T> | PipeError;
|
|
9
6
|
type PipeErrorHandlerType = 'return' | 'throw' | 'assign';
|
|
10
7
|
type PipeErrorHandler = ((errorCondition: string, error: string) => (lines: string[]) => string[]) & {
|
|
11
8
|
type: PipeErrorHandlerType;
|
|
@@ -57,7 +54,6 @@ type PipeErrorMessage = {
|
|
|
57
54
|
declare class PipeError {
|
|
58
55
|
messages: PipeErrorMessage[];
|
|
59
56
|
constructor(messages: PipeErrorMessage[]);
|
|
60
|
-
get valid(): false;
|
|
61
57
|
toString(): string;
|
|
62
58
|
static root(message: string, value: unknown, path?: string): PipeError;
|
|
63
59
|
static rootFrom(errors: PipeError[]): PipeError;
|
package/dist/types/index.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ export { d as differ } from './differ-oxbwYLvl.js';
|
|
|
3
3
|
export { addToArray, capitalize, chunkArray, compareTwoStrings, divideByZero, extractUrls, formatNumber, getAlphabet, getPercentage, getRandomSample, getRandomValue, groupBy, pluralize, shuffleArray, stripHTML, trimToLength, wrapInTryCatch } from './utils/functions/index.js';
|
|
4
4
|
export { g as geohash } from './geohash-BCD6jCDO.js';
|
|
5
5
|
export { ConditionalObjectKeys, DeepOmit, DeepPartial, DistributiveOmit, EnumToStringUnion, IsClassInstance, IsInTypeList, IsPlainObject, IsType, JSONPrimitives, JSONValue, JSONValueOf, JsonSchema, Paths, Prettify } from './utils/types.js';
|
|
6
|
-
export { C as Context, E as Entry, J as JsonSchemaBuilder, i as Pipe, b as PipeCompiledFn, P as PipeError, e as PipeErrorHandler, d as PipeErrorHandlerType, a as PipeFn, f as PipeInput, h as PipeMeta, g as PipeOutput, c as createErrorHandler } from './errors-
|
|
6
|
+
export { C as Context, E as Entry, J as JsonSchemaBuilder, i as Pipe, b as PipeCompiledFn, P as PipeError, e as PipeErrorHandler, d as PipeErrorHandlerType, a as PipeFn, f as PipeInput, h as PipeMeta, g as PipeOutput, c as createErrorHandler } from './errors-C1ef9q7I.js';
|
|
7
7
|
export { compileNested } from './api/base/pipes.js';
|
|
8
8
|
export { v } from './api/index.js';
|
|
9
9
|
import '@standard-schema/spec';
|
package/package.json
CHANGED