elysia 2.0.0-exp.2 → 2.0.0-exp.21
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/dist/_virtual/_rolldown/runtime.js +2 -0
- package/dist/adapter/bun/index.d.ts +2 -25
- package/dist/adapter/bun/index.js +18 -12
- package/dist/adapter/bun/index.mjs +19 -13
- package/dist/adapter/bun/router.js +1 -1
- package/dist/adapter/bun/router.mjs +1 -1
- package/dist/adapter/constants.d.ts +3 -26
- package/dist/adapter/index.d.ts +1 -26
- package/dist/adapter/index.js +1 -17
- package/dist/adapter/index.mjs +1 -17
- package/dist/adapter/types.d.ts +1 -0
- package/dist/adapter/utils.d.ts +3 -4
- package/dist/adapter/utils.js +83 -81
- package/dist/adapter/utils.mjs +82 -80
- package/dist/adapter/web-standard/handler.js +6 -3
- package/dist/adapter/web-standard/handler.mjs +6 -3
- package/dist/adapter/web-standard/index.d.ts +2 -26
- package/dist/adapter/web-standard/index.js +12 -1
- package/dist/adapter/web-standard/index.mjs +12 -1
- package/dist/base.d.ts +38 -68
- package/dist/base.js +133 -97
- package/dist/base.mjs +133 -97
- package/dist/compile/aot.d.ts +22 -1
- package/dist/compile/aot.js +22 -0
- package/dist/compile/aot.mjs +22 -0
- package/dist/compile/handler/index.d.ts +2 -2
- package/dist/compile/handler/index.js +85 -456
- package/dist/compile/handler/index.mjs +86 -457
- package/dist/compile/handler/jit.d.ts +35 -0
- package/dist/compile/handler/jit.js +445 -0
- package/dist/compile/handler/jit.mjs +443 -0
- package/dist/compile/handler/params.js +3 -2
- package/dist/compile/handler/params.mjs +4 -3
- package/dist/compile/handler/reconstruct.d.ts +13 -0
- package/dist/compile/handler/reconstruct.js +29 -0
- package/dist/compile/handler/reconstruct.mjs +28 -0
- package/dist/compile/handler/utils.d.ts +2 -1
- package/dist/compile/handler/utils.js +9 -3
- package/dist/compile/handler/utils.mjs +9 -4
- package/dist/compile/jit-probe.d.ts +19 -0
- package/dist/compile/jit-probe.js +38 -0
- package/dist/compile/jit-probe.mjs +36 -0
- package/dist/cookie/index.d.ts +4 -3
- package/dist/cookie/index.js +2 -1
- package/dist/cookie/index.mjs +2 -1
- package/dist/cookie/serialize.d.ts +6 -0
- package/dist/cookie/serialize.js +28 -0
- package/dist/cookie/serialize.mjs +27 -0
- package/dist/cookie/utils.d.ts +1 -2
- package/dist/cookie/utils.js +2 -23
- package/dist/cookie/utils.mjs +5 -25
- package/dist/handler/error.js +2 -2
- package/dist/handler/error.mjs +3 -3
- package/dist/handler/fetch.js +49 -42
- package/dist/handler/fetch.mjs +50 -43
- package/dist/index.d.ts +13 -8
- package/dist/index.js +11 -4
- package/dist/index.mjs +7 -6
- package/dist/parse-query.js +1 -1
- package/dist/parse-query.mjs +1 -1
- package/dist/plugin/bun.d.ts +10 -0
- package/dist/plugin/bun.js +18 -1
- package/dist/plugin/bun.mjs +19 -2
- package/dist/plugin/core.d.ts +61 -1
- package/dist/plugin/core.js +90 -10
- package/dist/plugin/core.mjs +90 -12
- package/dist/plugin/esbuild.d.ts +10 -0
- package/dist/plugin/esbuild.js +18 -1
- package/dist/plugin/esbuild.mjs +19 -2
- package/dist/plugin/source.d.ts +38 -1
- package/dist/plugin/source.js +104 -13
- package/dist/plugin/source.mjs +102 -14
- package/dist/plugin/vite.d.ts +7 -0
- package/dist/plugin/vite.js +22 -1
- package/dist/plugin/vite.mjs +23 -2
- package/dist/sucrose.js +23 -10
- package/dist/sucrose.mjs +23 -10
- package/dist/trace.d.ts +1 -1
- package/dist/trace.js +4 -6
- package/dist/trace.mjs +4 -6
- package/dist/type/bridge.d.ts +9 -8
- package/dist/type/coerce.d.ts +20 -2
- package/dist/type/coerce.js +110 -0
- package/dist/type/coerce.mjs +109 -2
- package/dist/type/compat.js +4 -3
- package/dist/type/compat.mjs +2 -1
- package/dist/type/constants.d.ts +1 -1
- package/dist/type/elysia/boolean-string.js +1 -1
- package/dist/type/elysia/boolean-string.mjs +1 -1
- package/dist/type/elysia/file-type.js +1 -1
- package/dist/type/elysia/file-type.mjs +1 -1
- package/dist/type/elysia/integer-string.js +1 -1
- package/dist/type/elysia/integer-string.mjs +1 -1
- package/dist/type/exports.js +29 -2059
- package/dist/type/exports.mjs +7 -295
- package/dist/type/index.d.ts +6 -2
- package/dist/type/index.js +4 -0
- package/dist/type/index.mjs +3 -1
- package/dist/type/types.d.ts +1 -1
- package/dist/type/utils.js +4 -4
- package/dist/type/utils.mjs +4 -4
- package/dist/type/validator/custom-error.d.ts +10 -0
- package/dist/type/validator/custom-error.js +113 -0
- package/dist/type/validator/custom-error.mjs +110 -0
- package/dist/type/validator/default-precompute.d.ts +22 -0
- package/dist/type/validator/default-precompute.js +476 -0
- package/dist/type/validator/default-precompute.mjs +465 -0
- package/dist/type/validator/frozen-check.d.ts +11 -0
- package/dist/type/validator/frozen-check.js +19 -0
- package/dist/type/validator/frozen-check.mjs +18 -0
- package/dist/type/{validator.d.ts → validator/index.d.ts} +7 -20
- package/dist/type/validator/index.js +572 -0
- package/dist/type/validator/index.mjs +567 -0
- package/dist/type/validator/string-codec-aot.d.ts +9 -0
- package/dist/type/validator/string-codec-aot.js +106 -0
- package/dist/type/validator/string-codec-aot.mjs +102 -0
- package/dist/type/validator/validator-cache.d.ts +21 -0
- package/dist/type/validator/validator-cache.js +163 -0
- package/dist/type/validator/validator-cache.mjs +161 -0
- package/dist/types.d.ts +22 -14
- package/dist/universal/file.d.ts +1 -1
- package/dist/universal/file.js +35 -29
- package/dist/universal/file.mjs +35 -29
- package/dist/utils.d.ts +12 -1
- package/dist/utils.js +89 -45
- package/dist/utils.mjs +88 -46
- package/dist/validator/index.d.ts +1 -1
- package/dist/validator/index.js +15 -2
- package/dist/validator/index.mjs +15 -2
- package/dist/validator/route.d.ts +0 -1
- package/dist/ws/route.js +47 -24
- package/dist/ws/route.mjs +48 -25
- package/package.json +10 -4
- package/dist/type/validator.js +0 -1033
- package/dist/type/validator.mjs +0 -1029
package/dist/type/validator.mjs
DELETED
|
@@ -1,1029 +0,0 @@
|
|
|
1
|
-
import { isCloudflareWorker } from "../universal/constants.mjs";
|
|
2
|
-
import { nullObject } from "../utils.mjs";
|
|
3
|
-
import { ValidationError } from "../error.mjs";
|
|
4
|
-
import { Capture, Compiled, EMPTY_EXTERNALS, collectExternals, externalsMatch, instantiateFrozenBoth, instantiateFrozenDecodeMirror, instantiateFrozenMirror, reconstructCheck } from "../compile/aot.mjs";
|
|
5
|
-
import { Validator as Validator$1 } from "../validator/index.mjs";
|
|
6
|
-
import { ELYSIA_TYPES } from "./constants.mjs";
|
|
7
|
-
import { isAsyncFunction } from "../compile/utils.mjs";
|
|
8
|
-
import { applyCoercions, deferCoercions, nonAdditionalProperties } from "./coerce.mjs";
|
|
9
|
-
import { hasProperty } from "./utils.mjs";
|
|
10
|
-
import { ASYNC_REFINE, collectFileTypeChecks, takeFileTypeChecks } from "./elysia/file-type.mjs";
|
|
11
|
-
import { Evaluate, Intersect, Module } from "typebox/type";
|
|
12
|
-
import { Clean, Decode as Decode$1, DecodeUnsafe, Default, Encode, EncodeUnsafe, Errors, HasCodec } from "typebox/value";
|
|
13
|
-
import { Build, Compile } from "typebox/schema";
|
|
14
|
-
import createMirror from "exact-mirror";
|
|
15
|
-
|
|
16
|
-
//#region src/type/validator.ts
|
|
17
|
-
const moduleCache = /* @__PURE__ */ new WeakMap();
|
|
18
|
-
function schemaContainsRef(node, seen = /* @__PURE__ */ new WeakSet()) {
|
|
19
|
-
if (!node || typeof node !== "object" || seen.has(node)) return false;
|
|
20
|
-
seen.add(node);
|
|
21
|
-
if (node.$ref) return true;
|
|
22
|
-
const props = node.properties;
|
|
23
|
-
if (props) {
|
|
24
|
-
for (const k in props) if (schemaContainsRef(props[k], seen)) return true;
|
|
25
|
-
}
|
|
26
|
-
const items = node.items;
|
|
27
|
-
if (Array.isArray(items)) {
|
|
28
|
-
for (const it of items) if (schemaContainsRef(it, seen)) return true;
|
|
29
|
-
} else if (items && schemaContainsRef(items, seen)) return true;
|
|
30
|
-
for (const key of [
|
|
31
|
-
"anyOf",
|
|
32
|
-
"allOf",
|
|
33
|
-
"oneOf"
|
|
34
|
-
]) {
|
|
35
|
-
const arr = node[key];
|
|
36
|
-
if (Array.isArray(arr)) {
|
|
37
|
-
for (const x of arr) if (schemaContainsRef(x, seen)) return true;
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
if (node.additionalProperties && typeof node.additionalProperties === "object" && schemaContainsRef(node.additionalProperties, seen)) return true;
|
|
41
|
-
if (node.not && schemaContainsRef(node.not, seen)) return true;
|
|
42
|
-
const pp = node.patternProperties;
|
|
43
|
-
if (pp) {
|
|
44
|
-
for (const k in pp) if (schemaContainsRef(pp[k], seen)) return true;
|
|
45
|
-
}
|
|
46
|
-
return false;
|
|
47
|
-
}
|
|
48
|
-
let inlineRefId = 0;
|
|
49
|
-
const isAsyncPredicate = (v) => Array.isArray(v) ? v.some((x) => typeof x.check === "function" ? isAsyncFunction(x.check) || x.check["~elyAsyncRefine"] === true : false) : false;
|
|
50
|
-
async function enforceFileTypeChecks(pending, type, value, schema) {
|
|
51
|
-
const results = await Promise.all(pending.map((x) => x.check));
|
|
52
|
-
for (let i = 0; i < results.length; i++) if (results[i] !== true) throw new ValidationError(type, value, [{
|
|
53
|
-
instancePath: findInstancePath(value, pending[i].file) ?? "",
|
|
54
|
-
message: results[i]
|
|
55
|
-
}], schema);
|
|
56
|
-
}
|
|
57
|
-
function findInstancePath(value, target, path = "") {
|
|
58
|
-
if (value === target) return path;
|
|
59
|
-
if (!value || typeof value !== "object") return;
|
|
60
|
-
if (Array.isArray(value)) {
|
|
61
|
-
for (let i = 0; i < value.length; i++) {
|
|
62
|
-
const found = findInstancePath(value[i], target, `${path}/${i}`);
|
|
63
|
-
if (found !== void 0) return found;
|
|
64
|
-
}
|
|
65
|
-
return;
|
|
66
|
-
}
|
|
67
|
-
for (const key in value) {
|
|
68
|
-
const found = findInstancePath(value[key], target, `${path}/${key}`);
|
|
69
|
-
if (found !== void 0) return found;
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
function isPrecomputeSafe(schema, depth = 0) {
|
|
73
|
-
if (!schema || typeof schema !== "object") return true;
|
|
74
|
-
const kind = schema["~kind"];
|
|
75
|
-
if (kind === "Union" || kind === "Intersect" || kind === "Ref" || kind === "This" || kind === "Cyclic") return false;
|
|
76
|
-
if (schema["~codec"] || schema["~refine"]) return false;
|
|
77
|
-
if (depth > 0 && (kind === "Object" || schema.type === "object")) {
|
|
78
|
-
if (schema.default === void 0 || nestedOwnDefaultDiverges(schema)) return false;
|
|
79
|
-
}
|
|
80
|
-
if (schema.properties) {
|
|
81
|
-
for (const v of Object.values(schema.properties)) if (!isPrecomputeSafe(v, depth + 1)) return false;
|
|
82
|
-
}
|
|
83
|
-
if (schema.items) {
|
|
84
|
-
if (Array.isArray(schema.items)) {
|
|
85
|
-
for (const v of schema.items) if (!isPrecomputeSafe(v, depth + 1)) return false;
|
|
86
|
-
} else if (!isPrecomputeSafe(schema.items, depth + 1)) return false;
|
|
87
|
-
}
|
|
88
|
-
if (typeof schema.additionalProperties === "object" && !isPrecomputeSafe(schema.additionalProperties, depth + 1)) return false;
|
|
89
|
-
if (schema.patternProperties) {
|
|
90
|
-
for (const v of Object.values(schema.patternProperties)) if (!isPrecomputeSafe(v, depth + 1)) return false;
|
|
91
|
-
}
|
|
92
|
-
return true;
|
|
93
|
-
}
|
|
94
|
-
function nestedOwnDefaultDiverges(objectSchema) {
|
|
95
|
-
const own = objectSchema.default;
|
|
96
|
-
if (own === null || typeof own !== "object" || Array.isArray(own)) return false;
|
|
97
|
-
const props = objectSchema.properties;
|
|
98
|
-
if (!props) return false;
|
|
99
|
-
for (const key in props) {
|
|
100
|
-
const child = props[key];
|
|
101
|
-
if (!child || typeof child !== "object") continue;
|
|
102
|
-
if ("default" in child) {
|
|
103
|
-
if (!(key in own) || canonical(own[key]) !== canonical(child.default)) return true;
|
|
104
|
-
} else if (child["~kind"] === "Object" || child.type === "object") {
|
|
105
|
-
if (key in own) {
|
|
106
|
-
if (nestedOwnDefaultDiverges({
|
|
107
|
-
...child,
|
|
108
|
-
default: own[key]
|
|
109
|
-
})) return true;
|
|
110
|
-
} else if (hasDefaultBelow(child)) return true;
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
return false;
|
|
114
|
-
}
|
|
115
|
-
function applyPrecomputed(defaults, value) {
|
|
116
|
-
const out = nullObject();
|
|
117
|
-
for (const k in defaults) {
|
|
118
|
-
const d = defaults[k];
|
|
119
|
-
out[k] = value[k] === void 0 && d !== null && typeof d === "object" ? structuredClone(d) : d;
|
|
120
|
-
}
|
|
121
|
-
for (const k in value) {
|
|
122
|
-
const v = value[k];
|
|
123
|
-
if (v === void 0) continue;
|
|
124
|
-
const d = out[k];
|
|
125
|
-
if (v && typeof v === "object" && !Array.isArray(v) && d && typeof d === "object" && !Array.isArray(d)) out[k] = applyPrecomputed(d, v);
|
|
126
|
-
else out[k] = v;
|
|
127
|
-
}
|
|
128
|
-
return out;
|
|
129
|
-
}
|
|
130
|
-
const subtreeHasDefault = (n) => !!n && typeof n === "object" && ("default" in n || childSchemaSome(n, subtreeHasDefault));
|
|
131
|
-
function childSchemaSome(n, f) {
|
|
132
|
-
if (!n || typeof n !== "object") return false;
|
|
133
|
-
if (n.properties) {
|
|
134
|
-
for (const k in n.properties) if (f(n.properties[k])) return true;
|
|
135
|
-
}
|
|
136
|
-
const items = n.items;
|
|
137
|
-
if (Array.isArray(items)) {
|
|
138
|
-
for (const it of items) if (f(it)) return true;
|
|
139
|
-
} else if (items && f(items)) return true;
|
|
140
|
-
for (const key of [
|
|
141
|
-
"anyOf",
|
|
142
|
-
"allOf",
|
|
143
|
-
"oneOf"
|
|
144
|
-
]) {
|
|
145
|
-
const arr = n[key];
|
|
146
|
-
if (Array.isArray(arr)) {
|
|
147
|
-
for (const s of arr) if (f(s)) return true;
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
if (typeof n.additionalProperties === "object" && f(n.additionalProperties)) return true;
|
|
151
|
-
if (n.patternProperties) {
|
|
152
|
-
for (const k in n.patternProperties) if (f(n.patternProperties[k])) return true;
|
|
153
|
-
}
|
|
154
|
-
for (const key of [
|
|
155
|
-
"not",
|
|
156
|
-
"if",
|
|
157
|
-
"then",
|
|
158
|
-
"else"
|
|
159
|
-
]) if (n[key] && f(n[key])) return true;
|
|
160
|
-
return false;
|
|
161
|
-
}
|
|
162
|
-
const hasDefaultBelow = (node) => childSchemaSome(node, subtreeHasDefault);
|
|
163
|
-
function hasDivergentDefaultBelow(node) {
|
|
164
|
-
const ownKey = "default" in node ? canonical(node.default) : void 0;
|
|
165
|
-
let bad = false;
|
|
166
|
-
const visit = (n) => {
|
|
167
|
-
if (!n || typeof n !== "object") return false;
|
|
168
|
-
if ("default" in n && (ownKey === void 0 || canonical(n.default) !== ownKey)) {
|
|
169
|
-
bad = true;
|
|
170
|
-
return true;
|
|
171
|
-
}
|
|
172
|
-
return childSchemaSome(n, visit);
|
|
173
|
-
};
|
|
174
|
-
childSchemaSome(node, visit);
|
|
175
|
-
return bad;
|
|
176
|
-
}
|
|
177
|
-
function structuralPreallocatable(schema, depth = 0) {
|
|
178
|
-
if (!schema || typeof schema !== "object") return true;
|
|
179
|
-
if (!((schema["~kind"] === "Object" || schema.type === "object") && !schema["~codec"] && !schema["~refine"] && !Array.isArray(schema.anyOf) && !Array.isArray(schema.allOf) && !Array.isArray(schema.oneOf) && schema.$ref === void 0 && schema.if === void 0 && schema.not === void 0)) return !hasDivergentDefaultBelow(schema);
|
|
180
|
-
if (depth > 0 && schema.default === void 0 && hasDefaultBelow(schema)) return false;
|
|
181
|
-
if (typeof schema.additionalProperties === "object" && subtreeHasDefault(schema.additionalProperties)) return false;
|
|
182
|
-
if (schema.patternProperties) {
|
|
183
|
-
for (const k in schema.patternProperties) if (subtreeHasDefault(schema.patternProperties[k])) return false;
|
|
184
|
-
}
|
|
185
|
-
if (schema.properties) {
|
|
186
|
-
for (const k in schema.properties) if (!structuralPreallocatable(schema.properties[k], depth + 1)) return false;
|
|
187
|
-
}
|
|
188
|
-
return true;
|
|
189
|
-
}
|
|
190
|
-
function isEmittable(v, seen, top = false) {
|
|
191
|
-
if (v === void 0) return top;
|
|
192
|
-
if (v === null) return true;
|
|
193
|
-
const t = typeof v;
|
|
194
|
-
if (t === "number") return Number.isFinite(v) && !Object.is(v, -0);
|
|
195
|
-
if (t === "string" || t === "boolean") return true;
|
|
196
|
-
if (t !== "object") return false;
|
|
197
|
-
if (seen.has(v)) return false;
|
|
198
|
-
seen.add(v);
|
|
199
|
-
let ok = true;
|
|
200
|
-
if (Array.isArray(v)) {
|
|
201
|
-
for (const x of v) if (!isEmittable(x, seen)) {
|
|
202
|
-
ok = false;
|
|
203
|
-
break;
|
|
204
|
-
}
|
|
205
|
-
} else {
|
|
206
|
-
const proto = Object.getPrototypeOf(v);
|
|
207
|
-
if (proto !== Object.prototype && proto !== null) ok = false;
|
|
208
|
-
else for (const k in v) {
|
|
209
|
-
if (k === "__proto__") {
|
|
210
|
-
ok = false;
|
|
211
|
-
break;
|
|
212
|
-
}
|
|
213
|
-
if (!Object.prototype.hasOwnProperty.call(v, k)) continue;
|
|
214
|
-
if (!isEmittable(v[k], seen)) {
|
|
215
|
-
ok = false;
|
|
216
|
-
break;
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
}
|
|
220
|
-
seen.delete(v);
|
|
221
|
-
return ok;
|
|
222
|
-
}
|
|
223
|
-
const PROBE_SENTINEL = "__elysia_default_probe__";
|
|
224
|
-
function* defaultProbes(pod) {
|
|
225
|
-
if (!pod) return;
|
|
226
|
-
yield {};
|
|
227
|
-
for (const k in pod) {
|
|
228
|
-
yield { [k]: PROBE_SENTINEL };
|
|
229
|
-
const d = pod[k];
|
|
230
|
-
if (d && typeof d === "object" && !Array.isArray(d)) {
|
|
231
|
-
yield { [k]: {} };
|
|
232
|
-
for (const k2 in d) {
|
|
233
|
-
yield { [k]: { [k2]: PROBE_SENTINEL } };
|
|
234
|
-
break;
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
function canonical(v) {
|
|
240
|
-
if (v === void 0) return "\0u";
|
|
241
|
-
if (Object.is(v, -0)) return "-0";
|
|
242
|
-
if (v === null || typeof v !== "object") return JSON.stringify(v) ?? "\0u";
|
|
243
|
-
if (Array.isArray(v)) return "[" + v.map(canonical).join(",") + "]";
|
|
244
|
-
return "{" + Object.keys(v).sort().map((k) => JSON.stringify(k) + ":" + canonical(v[k])).join(",") + "}";
|
|
245
|
-
}
|
|
246
|
-
function verifyPreallocatableDefault(schema) {
|
|
247
|
-
let pd;
|
|
248
|
-
let podRaw;
|
|
249
|
-
try {
|
|
250
|
-
pd = Default(schema, void 0);
|
|
251
|
-
podRaw = Default(schema, nullObject());
|
|
252
|
-
} catch {
|
|
253
|
-
return;
|
|
254
|
-
}
|
|
255
|
-
const pod = podRaw && typeof podRaw === "object" && !Array.isArray(podRaw) ? podRaw : void 0;
|
|
256
|
-
if (!isEmittable(pd, /* @__PURE__ */ new Set(), true) || pod !== void 0 && !isEmittable(pod, /* @__PURE__ */ new Set(), true)) return;
|
|
257
|
-
if (!isPrecomputeSafe(schema)) {
|
|
258
|
-
const s = schema;
|
|
259
|
-
if (s.type !== "object" && s["~kind"] !== "Object") return;
|
|
260
|
-
if (!structuralPreallocatable(schema)) return;
|
|
261
|
-
for (const probe of defaultProbes(pod)) {
|
|
262
|
-
let expected;
|
|
263
|
-
try {
|
|
264
|
-
expected = Default(schema, structuredClone(probe));
|
|
265
|
-
} catch {
|
|
266
|
-
return;
|
|
267
|
-
}
|
|
268
|
-
const actual = applyPrecomputed(pod, structuredClone(probe));
|
|
269
|
-
if (canonical(expected) !== canonical(actual)) return;
|
|
270
|
-
}
|
|
271
|
-
}
|
|
272
|
-
return {
|
|
273
|
-
pd,
|
|
274
|
-
pod
|
|
275
|
-
};
|
|
276
|
-
}
|
|
277
|
-
function collectCustomErrorNodes(schema, path, out, union) {
|
|
278
|
-
if (!schema || typeof schema !== "object") return out;
|
|
279
|
-
if (schema.error !== void 0) out.push({
|
|
280
|
-
path,
|
|
281
|
-
node: schema,
|
|
282
|
-
union
|
|
283
|
-
});
|
|
284
|
-
if (schema.properties) for (const k in schema.properties) collectCustomErrorNodes(schema.properties[k], path + "/" + k, out, union);
|
|
285
|
-
const items = schema.items;
|
|
286
|
-
if (Array.isArray(items)) for (let i = 0; i < items.length; i++) collectCustomErrorNodes(items[i], path + "/" + i, out, union);
|
|
287
|
-
else if (items && items.error !== void 0) out.push({
|
|
288
|
-
path,
|
|
289
|
-
node: items,
|
|
290
|
-
each: true,
|
|
291
|
-
union
|
|
292
|
-
});
|
|
293
|
-
const branches = schema.anyOf ?? schema.oneOf;
|
|
294
|
-
if (Array.isArray(branches)) {
|
|
295
|
-
const gate = {
|
|
296
|
-
node: schema,
|
|
297
|
-
path
|
|
298
|
-
};
|
|
299
|
-
for (const branch of branches) collectCustomErrorNodes(branch, path, out, gate);
|
|
300
|
-
}
|
|
301
|
-
return out;
|
|
302
|
-
}
|
|
303
|
-
function subValueAt(value, path) {
|
|
304
|
-
if (!path) return value;
|
|
305
|
-
let cur = value;
|
|
306
|
-
for (const part of path.split("/")) {
|
|
307
|
-
if (!part) continue;
|
|
308
|
-
if (cur === null || typeof cur !== "object") return;
|
|
309
|
-
cur = cur[part];
|
|
310
|
-
}
|
|
311
|
-
return cur;
|
|
312
|
-
}
|
|
313
|
-
function collectStringCodecNodes(schema, out = []) {
|
|
314
|
-
if (!schema || typeof schema !== "object") return out;
|
|
315
|
-
const ely = schema["~elyTyp"];
|
|
316
|
-
if (ely === ELYSIA_TYPES.ObjectString || ely === ELYSIA_TYPES.ArrayString) {
|
|
317
|
-
const inner = schema.anyOf?.[0];
|
|
318
|
-
const codec = schema.anyOf?.[1];
|
|
319
|
-
if (inner && codec?.["~codec"] && codec["~refine"]) out.push({
|
|
320
|
-
inner,
|
|
321
|
-
codec,
|
|
322
|
-
open: ely === ELYSIA_TYPES.ObjectString ? 123 : 91
|
|
323
|
-
});
|
|
324
|
-
}
|
|
325
|
-
if (schema.properties) for (const k in schema.properties) collectStringCodecNodes(schema.properties[k], out);
|
|
326
|
-
const items = schema.items;
|
|
327
|
-
if (Array.isArray(items)) for (const it of items) collectStringCodecNodes(it, out);
|
|
328
|
-
else if (items) collectStringCodecNodes(items, out);
|
|
329
|
-
if (Array.isArray(schema.anyOf)) for (const b of schema.anyOf) collectStringCodecNodes(b, out);
|
|
330
|
-
return out;
|
|
331
|
-
}
|
|
332
|
-
function reconstructInnerCodecs(ic, schema) {
|
|
333
|
-
const nodes = collectStringCodecNodes(schema);
|
|
334
|
-
for (let i = nodes.length - 1; i >= 0; i--) {
|
|
335
|
-
const entry = ic[i];
|
|
336
|
-
const node = nodes[i];
|
|
337
|
-
if (!entry || !node) continue;
|
|
338
|
-
const innerSchema = node.inner;
|
|
339
|
-
const innerCheck = entry.c(entry.e ? collectExternals(innerSchema) : []);
|
|
340
|
-
const innerMirror = entry.d.x ? instantiateFrozenDecodeMirror(entry.d, innerSchema) : entry.d.s;
|
|
341
|
-
const open = entry.o;
|
|
342
|
-
node.codec["~refine"][0].check = (v) => {
|
|
343
|
-
if (v.charCodeAt(0) !== open) return false;
|
|
344
|
-
try {
|
|
345
|
-
return innerCheck(JSON.parse(v));
|
|
346
|
-
} catch {
|
|
347
|
-
return false;
|
|
348
|
-
}
|
|
349
|
-
};
|
|
350
|
-
node.codec["~codec"].decode = (v) => innerMirror(JSON.parse(v));
|
|
351
|
-
}
|
|
352
|
-
}
|
|
353
|
-
function buildFrozenCheck(build, node) {
|
|
354
|
-
if (!build?.functions?.length || !build.entry) return;
|
|
355
|
-
const vars = build.external.variables;
|
|
356
|
-
if (!externalsMatch(collectExternals(node), vars)) return;
|
|
357
|
-
const cr = reconstructCheck(build);
|
|
358
|
-
return {
|
|
359
|
-
identifier: build.external.identifier,
|
|
360
|
-
checkDefs: cr.defs,
|
|
361
|
-
checkValue: cr.value,
|
|
362
|
-
external: vars.length > 0
|
|
363
|
-
};
|
|
364
|
-
}
|
|
365
|
-
function captureInnerCodec(inner, open, sanitize) {
|
|
366
|
-
let cf;
|
|
367
|
-
try {
|
|
368
|
-
cf = buildFrozenCheck(Build(inner), inner);
|
|
369
|
-
if (!cf) return;
|
|
370
|
-
} catch {
|
|
371
|
-
return;
|
|
372
|
-
}
|
|
373
|
-
let decode;
|
|
374
|
-
try {
|
|
375
|
-
const emitted = createMirror(inner, {
|
|
376
|
-
Compile,
|
|
377
|
-
sanitize,
|
|
378
|
-
decode: true,
|
|
379
|
-
emit: true
|
|
380
|
-
});
|
|
381
|
-
if (typeof emitted?.source !== "string") return;
|
|
382
|
-
const ext = emitted.externals;
|
|
383
|
-
if (ext?.hof) return;
|
|
384
|
-
if (ext?.codecs && !Capture.mirrorCodecs(inner, ext.codecs)) return;
|
|
385
|
-
let u;
|
|
386
|
-
if (ext?.unions && ext.unions.length) {
|
|
387
|
-
u = Capture.mirrorUnions(inner, ext.unions);
|
|
388
|
-
if (!u) return;
|
|
389
|
-
}
|
|
390
|
-
decode = {
|
|
391
|
-
source: emitted.source,
|
|
392
|
-
hasExternals: !!(ext?.codecs || u),
|
|
393
|
-
u
|
|
394
|
-
};
|
|
395
|
-
} catch {
|
|
396
|
-
return;
|
|
397
|
-
}
|
|
398
|
-
if (hasProperty("default", inner)) return;
|
|
399
|
-
return {
|
|
400
|
-
open,
|
|
401
|
-
...cf,
|
|
402
|
-
decode
|
|
403
|
-
};
|
|
404
|
-
}
|
|
405
|
-
function sourceOnlyValidator(schema) {
|
|
406
|
-
const buildResult = Build(schema);
|
|
407
|
-
let full;
|
|
408
|
-
return new Proxy({}, { get(_, prop) {
|
|
409
|
-
if (prop === "buildResult") return buildResult;
|
|
410
|
-
const f = full ??= Compile(schema);
|
|
411
|
-
const value = f[prop];
|
|
412
|
-
return typeof value === "function" ? value.bind(f) : value;
|
|
413
|
-
} });
|
|
414
|
-
}
|
|
415
|
-
var TypeBoxValidator = class extends Validator$1 {
|
|
416
|
-
#decodeMirror;
|
|
417
|
-
#encodeMirror;
|
|
418
|
-
#findCustomError;
|
|
419
|
-
#precomputedDefault;
|
|
420
|
-
#precomputedObjectDefault;
|
|
421
|
-
#noValidate;
|
|
422
|
-
#isForm;
|
|
423
|
-
#hasOptional;
|
|
424
|
-
constructor(schema, options, name, isIntersectable) {
|
|
425
|
-
super();
|
|
426
|
-
this.precomputeSafe = false;
|
|
427
|
-
this.#isForm = false;
|
|
428
|
-
this.#hasOptional = false;
|
|
429
|
-
if (isIntersectable) schema = Evaluate(Intersect([schema, ...options.schemas]));
|
|
430
|
-
const originalElyTyp = schema?.["~elyTyp"];
|
|
431
|
-
const frozen = options?.aot && options.slot ? Compiled.getValidator(options.aot.method, options.aot.path, options.slot) : void 0;
|
|
432
|
-
let schemaHasRef = false;
|
|
433
|
-
if (name && options?.models) schema = moduleCache.getOrInsertComputed(options.models, () => Module(options.models))[name];
|
|
434
|
-
else if (options?.models && typeof name !== "string") {
|
|
435
|
-
schemaHasRef = frozen ? frozen.r === 1 : schemaContainsRef(schema);
|
|
436
|
-
if (schemaHasRef) {
|
|
437
|
-
const id = `inline@${++inlineRefId}`;
|
|
438
|
-
schema = Module({
|
|
439
|
-
...options.models,
|
|
440
|
-
[id]: schema
|
|
441
|
-
})[id];
|
|
442
|
-
}
|
|
443
|
-
}
|
|
444
|
-
const isFrozen = this.#reconstruct(options, frozen);
|
|
445
|
-
this.schema = isFrozen && !this.hasCodec ? schema : applyCoercions(schema, options?.coerces);
|
|
446
|
-
if (options?.normalize === false && options.slot !== "headers" && options.slot !== "cookie") this.schema = nonAdditionalProperties(this.schema);
|
|
447
|
-
if (!isFrozen) {
|
|
448
|
-
const capturing = Capture.isCapturing();
|
|
449
|
-
this.tb = capturing ? sourceOnlyValidator(this.schema) : Compile(this.schema);
|
|
450
|
-
this.hasCodec = HasCodec(this.schema);
|
|
451
|
-
this.isAsync = this.tb.buildResult.external.variables.some(isAsyncPredicate) ?? false;
|
|
452
|
-
this.hasDefault = hasProperty("default", this.schema);
|
|
453
|
-
if (capturing) this.#maybeCapture(options, schemaHasRef);
|
|
454
|
-
else this.#dropCompiledSource();
|
|
455
|
-
}
|
|
456
|
-
if (frozen?.ps === 1) {
|
|
457
|
-
this.precomputeSafe = true;
|
|
458
|
-
this.#precomputedDefault = frozen.pd;
|
|
459
|
-
this.#precomputedObjectDefault = frozen.pod !== void 0 ? Object.freeze(frozen.pod) : void 0;
|
|
460
|
-
} else {
|
|
461
|
-
this.precomputeSafe = this.hasDefault && isPrecomputeSafe(this.schema);
|
|
462
|
-
if (this.precomputeSafe) {
|
|
463
|
-
this.#precomputedDefault = Default(this.schema, void 0);
|
|
464
|
-
const obj = Default(this.schema, nullObject());
|
|
465
|
-
this.#precomputedObjectDefault = obj && typeof obj === "object" && !Array.isArray(obj) ? Object.freeze(obj) : void 0;
|
|
466
|
-
} else {
|
|
467
|
-
this.#precomputedDefault = void 0;
|
|
468
|
-
this.#precomputedObjectDefault = void 0;
|
|
469
|
-
}
|
|
470
|
-
}
|
|
471
|
-
this.#noValidate = originalElyTyp === ELYSIA_TYPES.NoValidate;
|
|
472
|
-
this.#isForm = originalElyTyp === ELYSIA_TYPES.Form;
|
|
473
|
-
this.#hasOptional = !!this.schema?.["~optional"];
|
|
474
|
-
if (frozen?.ic) reconstructInnerCodecs(frozen.ic, this.schema);
|
|
475
|
-
if (isFrozen && frozen.cm) {
|
|
476
|
-
const both = instantiateFrozenBoth(frozen, this.schema, schema);
|
|
477
|
-
this.reconstructedCheck = both.check;
|
|
478
|
-
this.Clean = options?.normalize === false ? void 0 : both.clean;
|
|
479
|
-
} else {
|
|
480
|
-
if (isFrozen) this.reconstructedCheck = frozen.c(frozen.e ? collectExternals(this.schema) : EMPTY_EXTERNALS);
|
|
481
|
-
try {
|
|
482
|
-
this.Clean = options?.normalize === false ? void 0 : options?.normalize === "typebox" ? (value) => Clean(this.schema, value) : this.#setupMirror(schema, options, frozen);
|
|
483
|
-
} catch (error) {
|
|
484
|
-
console.warn("Failed to create exactMirror. Please report the following code to https://github.com/elysiajs/elysia/issues");
|
|
485
|
-
console.warn(schema);
|
|
486
|
-
console.warn(error);
|
|
487
|
-
if (options?.normalize !== false) this.Clean = (value) => Clean(this.schema, value);
|
|
488
|
-
}
|
|
489
|
-
}
|
|
490
|
-
if (this.hasCodec && !this.#isForm && !this.#noValidate && !options?.slot?.startsWith("r") && options?.normalize !== false && options?.normalize !== "typebox") this.#decodeMirror = this.#setupCodecMirror(this.schema, options, frozen, "decode");
|
|
491
|
-
if (this.hasCodec && !this.#isForm && !this.#noValidate && options?.slot?.startsWith("r") && options?.normalize !== false && options?.normalize !== "typebox") this.#encodeMirror = this.#setupCodecMirror(this.schema, options, frozen, "encode");
|
|
492
|
-
if (!this.#noValidate) this.#findCustomError = this.#buildFindCustomError(frozen);
|
|
493
|
-
}
|
|
494
|
-
#buildFindCustomError(frozen) {
|
|
495
|
-
const nodes = collectCustomErrorNodes(this.schema, "", []);
|
|
496
|
-
if (!nodes.length) return;
|
|
497
|
-
const frozenByPath = frozen?.ce ? new Map(frozen.ce.map((e) => [e.p, e])) : void 0;
|
|
498
|
-
const checks = [];
|
|
499
|
-
for (const { path, node, each, union } of nodes) {
|
|
500
|
-
let check;
|
|
501
|
-
const fe = union ? void 0 : frozenByPath?.get(path);
|
|
502
|
-
if (fe) try {
|
|
503
|
-
check = fe.c(fe.e ? collectExternals(node) : EMPTY_EXTERNALS);
|
|
504
|
-
} catch {}
|
|
505
|
-
else try {
|
|
506
|
-
const c = Compile(node);
|
|
507
|
-
check = (v) => c.Check(v);
|
|
508
|
-
} catch {}
|
|
509
|
-
if (check && each) {
|
|
510
|
-
const elementCheck = check;
|
|
511
|
-
check = (v) => !Array.isArray(v) || v.every((x) => elementCheck(x));
|
|
512
|
-
}
|
|
513
|
-
if (!check) continue;
|
|
514
|
-
let gate;
|
|
515
|
-
if (union) {
|
|
516
|
-
let unionCheck;
|
|
517
|
-
try {
|
|
518
|
-
const uc = Compile(union.node);
|
|
519
|
-
unionCheck = (v) => uc.Check(v);
|
|
520
|
-
} catch {}
|
|
521
|
-
if (!unionCheck) continue;
|
|
522
|
-
const unionPath = union.path;
|
|
523
|
-
gate = (root) => unionCheck(subValueAt(root, unionPath));
|
|
524
|
-
}
|
|
525
|
-
checks.push({
|
|
526
|
-
path,
|
|
527
|
-
check,
|
|
528
|
-
gate,
|
|
529
|
-
error: node.error
|
|
530
|
-
});
|
|
531
|
-
}
|
|
532
|
-
if (!checks.length) return;
|
|
533
|
-
checks.sort((a, b) => b.path.length - a.path.length);
|
|
534
|
-
return (value) => {
|
|
535
|
-
for (const c of checks) {
|
|
536
|
-
if (c.gate && c.gate(value)) continue;
|
|
537
|
-
if (!c.check(subValueAt(value, c.path))) return {
|
|
538
|
-
instancePath: c.path,
|
|
539
|
-
error: c.error
|
|
540
|
-
};
|
|
541
|
-
}
|
|
542
|
-
};
|
|
543
|
-
}
|
|
544
|
-
#verr(value, type) {
|
|
545
|
-
return new ValidationError(type, value, () => this.Errors(value), this.schema, this.#findCustomError);
|
|
546
|
-
}
|
|
547
|
-
#setupMirror(schema, options, frozen) {
|
|
548
|
-
const aot = options?.aot;
|
|
549
|
-
const slot = options?.slot;
|
|
550
|
-
if (aot && slot && frozen?.m) {
|
|
551
|
-
const m = frozen.m;
|
|
552
|
-
let clean;
|
|
553
|
-
return (value) => {
|
|
554
|
-
if (clean === void 0) try {
|
|
555
|
-
clean = instantiateFrozenMirror(m, schema);
|
|
556
|
-
} catch (error) {
|
|
557
|
-
console.warn("Failed to create exactMirror. Please report the following code to https://github.com/elysiajs/elysia/issues");
|
|
558
|
-
console.warn(schema);
|
|
559
|
-
console.warn(error);
|
|
560
|
-
clean = (v) => v;
|
|
561
|
-
}
|
|
562
|
-
return clean(value);
|
|
563
|
-
};
|
|
564
|
-
}
|
|
565
|
-
if (aot && slot) {
|
|
566
|
-
if (Capture.isCapturing()) try {
|
|
567
|
-
const emitted = createMirror(schema, {
|
|
568
|
-
Compile,
|
|
569
|
-
sanitize: options?.sanitize,
|
|
570
|
-
emit: true
|
|
571
|
-
});
|
|
572
|
-
if (typeof emitted?.source === "string") {
|
|
573
|
-
const ext = emitted.externals;
|
|
574
|
-
if (!ext) Capture.set({
|
|
575
|
-
method: aot.method,
|
|
576
|
-
path: aot.path,
|
|
577
|
-
slot
|
|
578
|
-
}, { mirror: {
|
|
579
|
-
source: emitted.source,
|
|
580
|
-
hasExternals: false
|
|
581
|
-
} });
|
|
582
|
-
else if (ext.unions && !ext.hof) {
|
|
583
|
-
const u = Capture.mirrorUnions(schema, ext.unions);
|
|
584
|
-
if (u) Capture.set({
|
|
585
|
-
method: aot.method,
|
|
586
|
-
path: aot.path,
|
|
587
|
-
slot
|
|
588
|
-
}, { mirror: {
|
|
589
|
-
source: emitted.source,
|
|
590
|
-
hasExternals: true,
|
|
591
|
-
u
|
|
592
|
-
} });
|
|
593
|
-
}
|
|
594
|
-
}
|
|
595
|
-
} catch {}
|
|
596
|
-
}
|
|
597
|
-
return createMirror(schema, {
|
|
598
|
-
Compile,
|
|
599
|
-
sanitize: options?.sanitize
|
|
600
|
-
});
|
|
601
|
-
}
|
|
602
|
-
#setupCodecMirror(schema, options, frozen, dir) {
|
|
603
|
-
const aot = options?.aot;
|
|
604
|
-
const slot = options?.slot;
|
|
605
|
-
const frozenMirror = dir === "decode" ? frozen?.dm : frozen?.em;
|
|
606
|
-
if (aot && slot && frozenMirror) {
|
|
607
|
-
const m = frozenMirror;
|
|
608
|
-
let run;
|
|
609
|
-
return (value) => {
|
|
610
|
-
if (run === void 0) try {
|
|
611
|
-
run = instantiateFrozenDecodeMirror(m, schema, dir);
|
|
612
|
-
} catch {
|
|
613
|
-
run = dir === "decode" ? (v) => {
|
|
614
|
-
const decoded = DecodeUnsafe(nullObject(), schema, v);
|
|
615
|
-
return this.Clean ? this.Clean(decoded) : decoded;
|
|
616
|
-
} : (v) => {
|
|
617
|
-
const out = Encode(schema, v);
|
|
618
|
-
return this.Clean ? this.Clean(out) : out;
|
|
619
|
-
};
|
|
620
|
-
}
|
|
621
|
-
return run(value);
|
|
622
|
-
};
|
|
623
|
-
}
|
|
624
|
-
const dirOpt = dir === "decode" ? { decode: true } : { encode: true };
|
|
625
|
-
const captureSlot = dir === "decode" ? !slot?.startsWith("response") : !!slot?.startsWith("response");
|
|
626
|
-
if (aot && slot && Capture.isCapturing() && captureSlot) try {
|
|
627
|
-
const emitted = createMirror(schema, {
|
|
628
|
-
Compile,
|
|
629
|
-
sanitize: options?.sanitize,
|
|
630
|
-
...dirOpt,
|
|
631
|
-
emit: true
|
|
632
|
-
});
|
|
633
|
-
if (typeof emitted?.source === "string") {
|
|
634
|
-
const ext = emitted.externals;
|
|
635
|
-
if (ext?.codecs && !ext.hof && Capture.mirrorCodecs(schema, ext.codecs, dir)) {
|
|
636
|
-
let u;
|
|
637
|
-
let freezable = true;
|
|
638
|
-
if (ext.unions && ext.unions.length) {
|
|
639
|
-
u = Capture.mirrorUnions(schema, ext.unions);
|
|
640
|
-
if (!u) freezable = false;
|
|
641
|
-
}
|
|
642
|
-
if (freezable) {
|
|
643
|
-
const mirror = {
|
|
644
|
-
source: emitted.source,
|
|
645
|
-
hasExternals: true,
|
|
646
|
-
u
|
|
647
|
-
};
|
|
648
|
-
Capture.set({
|
|
649
|
-
method: aot.method,
|
|
650
|
-
path: aot.path,
|
|
651
|
-
slot
|
|
652
|
-
}, dir === "decode" ? { decodeMirror: mirror } : { encodeMirror: mirror });
|
|
653
|
-
}
|
|
654
|
-
}
|
|
655
|
-
}
|
|
656
|
-
} catch {}
|
|
657
|
-
try {
|
|
658
|
-
return createMirror(schema, {
|
|
659
|
-
Compile,
|
|
660
|
-
sanitize: options?.sanitize,
|
|
661
|
-
...dirOpt
|
|
662
|
-
});
|
|
663
|
-
} catch {}
|
|
664
|
-
}
|
|
665
|
-
Check(value) {
|
|
666
|
-
if (this.reconstructedCheck) return this.reconstructedCheck(value);
|
|
667
|
-
return this.tb.Check(value);
|
|
668
|
-
}
|
|
669
|
-
#reconstruct(options, frozen) {
|
|
670
|
-
if (!options?.aot || !options.slot || options.normalize === "typebox") return false;
|
|
671
|
-
if (!frozen?.c && !frozen?.cm) return false;
|
|
672
|
-
this.isAsync = frozen.a === 1;
|
|
673
|
-
this.hasDefault = frozen.d === 1;
|
|
674
|
-
this.hasCodec = frozen.k === 1;
|
|
675
|
-
return true;
|
|
676
|
-
}
|
|
677
|
-
#maybeCapture(options, hasRef) {
|
|
678
|
-
const aot = options?.aot;
|
|
679
|
-
const slot = options?.slot;
|
|
680
|
-
if (!aot || !slot || !Capture.isCapturing()) return;
|
|
681
|
-
if (this.hasDefault) {
|
|
682
|
-
const pre = verifyPreallocatableDefault(this.schema);
|
|
683
|
-
if (pre) Capture.set({
|
|
684
|
-
method: aot.method,
|
|
685
|
-
path: aot.path,
|
|
686
|
-
slot
|
|
687
|
-
}, {
|
|
688
|
-
precomputeSafe: true,
|
|
689
|
-
precomputedDefault: pre.pd,
|
|
690
|
-
precomputedObjectDefault: pre.pod
|
|
691
|
-
});
|
|
692
|
-
}
|
|
693
|
-
const ceNodes = collectCustomErrorNodes(this.schema, "", []);
|
|
694
|
-
if (ceNodes.length) {
|
|
695
|
-
const entries = [];
|
|
696
|
-
for (const { path, node, union } of ceNodes) {
|
|
697
|
-
if (union) continue;
|
|
698
|
-
try {
|
|
699
|
-
const cf = buildFrozenCheck(Build(node), node);
|
|
700
|
-
if (cf) entries.push({
|
|
701
|
-
path,
|
|
702
|
-
...cf
|
|
703
|
-
});
|
|
704
|
-
} catch {}
|
|
705
|
-
}
|
|
706
|
-
if (entries.length) Capture.set({
|
|
707
|
-
method: aot.method,
|
|
708
|
-
path: aot.path,
|
|
709
|
-
slot
|
|
710
|
-
}, { customErrors: entries });
|
|
711
|
-
}
|
|
712
|
-
const stringCodecs = collectStringCodecNodes(this.schema);
|
|
713
|
-
if (stringCodecs.length) {
|
|
714
|
-
const entries = [];
|
|
715
|
-
for (const { inner, open } of stringCodecs) {
|
|
716
|
-
const entry = captureInnerCodec(inner, open, options?.sanitize);
|
|
717
|
-
if (!entry) break;
|
|
718
|
-
entries.push(entry);
|
|
719
|
-
}
|
|
720
|
-
if (entries.length === stringCodecs.length) Capture.set({
|
|
721
|
-
method: aot.method,
|
|
722
|
-
path: aot.path,
|
|
723
|
-
slot
|
|
724
|
-
}, { innerCodecs: entries });
|
|
725
|
-
}
|
|
726
|
-
const build = this.tb.buildResult;
|
|
727
|
-
const cf = buildFrozenCheck(build, this.schema);
|
|
728
|
-
if (!cf) return;
|
|
729
|
-
Capture.set({
|
|
730
|
-
method: aot.method,
|
|
731
|
-
path: aot.path,
|
|
732
|
-
slot
|
|
733
|
-
}, {
|
|
734
|
-
...cf,
|
|
735
|
-
async: build.external.variables.some(isAsyncPredicate),
|
|
736
|
-
hasDefault: this.hasDefault,
|
|
737
|
-
hasCodec: this.hasCodec,
|
|
738
|
-
hasRef
|
|
739
|
-
});
|
|
740
|
-
}
|
|
741
|
-
#dropCompiledSource() {
|
|
742
|
-
const tb = this.tb;
|
|
743
|
-
if (!tb) return;
|
|
744
|
-
if (tb.evaluateResult) tb.evaluateResult.code = void 0;
|
|
745
|
-
if (tb.buildResult) tb.buildResult.functions = void 0;
|
|
746
|
-
}
|
|
747
|
-
Errors(value) {
|
|
748
|
-
return Errors(this.schema, value);
|
|
749
|
-
}
|
|
750
|
-
Decode(value) {
|
|
751
|
-
return Decode$1(this.schema, value);
|
|
752
|
-
}
|
|
753
|
-
Encode(value) {
|
|
754
|
-
return this.hasCodec ? Encode(this.schema, value) : value;
|
|
755
|
-
}
|
|
756
|
-
EncodeFrom(value, type) {
|
|
757
|
-
if (this.#isForm) {
|
|
758
|
-
if (!this.#noValidate && !this.Check(value)) throw this.#verr(value, type);
|
|
759
|
-
return value;
|
|
760
|
-
}
|
|
761
|
-
if (!this.hasCodec) {
|
|
762
|
-
if (!this.#noValidate && !this.Check(value)) throw this.#verr(value, type);
|
|
763
|
-
if (this.Clean) value = this.Clean(value);
|
|
764
|
-
return value;
|
|
765
|
-
}
|
|
766
|
-
try {
|
|
767
|
-
if (this.#encodeMirror) {
|
|
768
|
-
const out = this.#encodeMirror(value);
|
|
769
|
-
if (!this.#noValidate && !this.Check(out)) throw new ValidationError(type, out, () => this.Errors(out), this.schema);
|
|
770
|
-
return out;
|
|
771
|
-
}
|
|
772
|
-
const out = this.#noValidate ? EncodeUnsafe(nullObject(), this.schema, value) : Encode(this.schema, value);
|
|
773
|
-
return this.Clean ? this.Clean(out) : out;
|
|
774
|
-
} catch (e) {
|
|
775
|
-
if (this.#noValidate) return this.Clean ? this.Clean(value) : value;
|
|
776
|
-
if (e instanceof ValidationError) throw e;
|
|
777
|
-
if (e?.error) throw e.error;
|
|
778
|
-
if (e?.status) throw e;
|
|
779
|
-
throw new ValidationError(type, value, () => this.Errors(value), this.schema);
|
|
780
|
-
}
|
|
781
|
-
}
|
|
782
|
-
#markForm(value) {
|
|
783
|
-
if (this.#isForm && value !== null && typeof value === "object" && !("~ely-form" in value)) Object.defineProperty(value, "~ely-form", {
|
|
784
|
-
value: 1,
|
|
785
|
-
configurable: true
|
|
786
|
-
});
|
|
787
|
-
}
|
|
788
|
-
#unmarkForm(value) {
|
|
789
|
-
if (this.#isForm && value !== null && typeof value === "object" && "~ely-form" in value && Object.getOwnPropertyDescriptor(value, "~ely-form")?.configurable) delete value["~ely-form"];
|
|
790
|
-
}
|
|
791
|
-
From(value, type) {
|
|
792
|
-
return this.isAsync ? this.FromAsync(value, type) : this.FromSync(value, type);
|
|
793
|
-
}
|
|
794
|
-
#cloneSharedDefault() {
|
|
795
|
-
const value = this.#precomputedDefault;
|
|
796
|
-
if (value === null || typeof value !== "object") return value;
|
|
797
|
-
return structuredClone(value);
|
|
798
|
-
}
|
|
799
|
-
optionalBypass(value) {
|
|
800
|
-
const schema = this.schema;
|
|
801
|
-
if (!schema?.["~optional"]) return;
|
|
802
|
-
if (value === void 0 || value === null) return {
|
|
803
|
-
bypass: true,
|
|
804
|
-
value: schema["~kind"] === "Object" ? nullObject() : value
|
|
805
|
-
};
|
|
806
|
-
if (schema["~kind"] === "Object" && typeof value === "object" && !Array.isArray(value) && Object.keys(value).length === 0) return {
|
|
807
|
-
bypass: true,
|
|
808
|
-
value: nullObject()
|
|
809
|
-
};
|
|
810
|
-
}
|
|
811
|
-
async FromAsync(value, type) {
|
|
812
|
-
if (this.hasDefault) if (this.precomputeSafe) {
|
|
813
|
-
if (value === void 0 || value === null) value = this.#cloneSharedDefault();
|
|
814
|
-
else if (this.#precomputedObjectDefault !== void 0 && typeof value === "object" && !Array.isArray(value)) value = applyPrecomputed(this.#precomputedObjectDefault, value);
|
|
815
|
-
} else value = Default(this.schema, value);
|
|
816
|
-
if (this.#hasOptional) {
|
|
817
|
-
const bypass = this.optionalBypass(value);
|
|
818
|
-
if (bypass) return bypass.value;
|
|
819
|
-
}
|
|
820
|
-
if (this.#isForm) this.#markForm(value);
|
|
821
|
-
if (this.hasCodec) {
|
|
822
|
-
if (!this.#noValidate) {
|
|
823
|
-
collectFileTypeChecks();
|
|
824
|
-
const valid = this.Check(value);
|
|
825
|
-
const pendingFile = takeFileTypeChecks();
|
|
826
|
-
if (!valid) throw this.#verr(value, type);
|
|
827
|
-
if (pendingFile) await enforceFileTypeChecks(pendingFile, type, value, this.schema);
|
|
828
|
-
}
|
|
829
|
-
if (this.#decodeMirror) value = this.#decodeMirror(value);
|
|
830
|
-
else try {
|
|
831
|
-
value = DecodeUnsafe(nullObject(), this.schema, value);
|
|
832
|
-
} catch (e) {
|
|
833
|
-
if (e instanceof ValidationError) throw e;
|
|
834
|
-
if (e?.error) throw e.error;
|
|
835
|
-
if (e?.status) throw e;
|
|
836
|
-
throw new ValidationError(type, value, () => this.Errors(value), this.schema);
|
|
837
|
-
}
|
|
838
|
-
} else if (!this.#noValidate) {
|
|
839
|
-
collectFileTypeChecks();
|
|
840
|
-
const valid = this.Check(value);
|
|
841
|
-
const pendingFile = takeFileTypeChecks();
|
|
842
|
-
if (!valid) throw this.#verr(value, type);
|
|
843
|
-
if (pendingFile) await enforceFileTypeChecks(pendingFile, type, value, this.schema);
|
|
844
|
-
}
|
|
845
|
-
if (this.Clean && !this.#decodeMirror) value = this.Clean(value);
|
|
846
|
-
if (this.#isForm) this.#unmarkForm(value);
|
|
847
|
-
return value;
|
|
848
|
-
}
|
|
849
|
-
FromSync(value, type) {
|
|
850
|
-
if (this.hasDefault) if (this.precomputeSafe) {
|
|
851
|
-
if (value === void 0 || value === null) value = this.#cloneSharedDefault();
|
|
852
|
-
else if (this.#precomputedObjectDefault !== void 0 && typeof value === "object" && !Array.isArray(value)) value = applyPrecomputed(this.#precomputedObjectDefault, value);
|
|
853
|
-
} else value = Default(this.schema, value);
|
|
854
|
-
if (this.#hasOptional) {
|
|
855
|
-
const bypass = this.optionalBypass(value);
|
|
856
|
-
if (bypass) return bypass.value;
|
|
857
|
-
}
|
|
858
|
-
if (this.#isForm) this.#markForm(value);
|
|
859
|
-
if (this.hasCodec) {
|
|
860
|
-
if (!this.#noValidate && !this.Check(value)) throw this.#verr(value, type);
|
|
861
|
-
if (this.#decodeMirror) value = this.#decodeMirror(value);
|
|
862
|
-
else try {
|
|
863
|
-
value = DecodeUnsafe(nullObject(), this.schema, value);
|
|
864
|
-
} catch (e) {
|
|
865
|
-
if (e instanceof ValidationError) throw e;
|
|
866
|
-
if (e?.error) throw e.error;
|
|
867
|
-
if (e?.status) throw e;
|
|
868
|
-
throw new ValidationError(type, value, () => this.Errors(value), this.schema);
|
|
869
|
-
}
|
|
870
|
-
} else if (!this.#noValidate && !this.Check(value)) throw this.#verr(value, type);
|
|
871
|
-
if (this.Clean && !this.#decodeMirror) value = this.Clean(value);
|
|
872
|
-
if (this.#isForm) this.#unmarkForm(value);
|
|
873
|
-
return value;
|
|
874
|
-
}
|
|
875
|
-
};
|
|
876
|
-
const DEFAULT_CACHE_LIMIT = 1024;
|
|
877
|
-
const DEFAULT_GC_TIME = 60 * 1e3;
|
|
878
|
-
var TypeBoxValidatorCache = class TypeBoxValidatorCache {
|
|
879
|
-
static {
|
|
880
|
-
this.EMPTY = nullObject();
|
|
881
|
-
}
|
|
882
|
-
static {
|
|
883
|
-
this.ignoreKeys = new Set([
|
|
884
|
-
"title",
|
|
885
|
-
"description",
|
|
886
|
-
"tags",
|
|
887
|
-
"examples",
|
|
888
|
-
"defaultValue"
|
|
889
|
-
]);
|
|
890
|
-
}
|
|
891
|
-
static {
|
|
892
|
-
this.fnIds = /* @__PURE__ */ new WeakMap();
|
|
893
|
-
}
|
|
894
|
-
static {
|
|
895
|
-
this.nextFnId = 0;
|
|
896
|
-
}
|
|
897
|
-
static fnKey(fn) {
|
|
898
|
-
let id = TypeBoxValidatorCache.fnIds.get(fn);
|
|
899
|
-
if (id === void 0) {
|
|
900
|
-
id = ++TypeBoxValidatorCache.nextFnId;
|
|
901
|
-
TypeBoxValidatorCache.fnIds.set(fn, id);
|
|
902
|
-
}
|
|
903
|
-
return `<fn:${id}>`;
|
|
904
|
-
}
|
|
905
|
-
static ignoreMeta(k, v) {
|
|
906
|
-
if (TypeBoxValidatorCache.ignoreKeys.has(k)) return;
|
|
907
|
-
if (typeof v === "function") return TypeBoxValidatorCache.fnKey(v);
|
|
908
|
-
if (v && typeof v === "object" && v["~optional"] === true) {
|
|
909
|
-
const out = nullObject();
|
|
910
|
-
for (const key in v) out[key] = v[key];
|
|
911
|
-
out["~optional"] = true;
|
|
912
|
-
return out;
|
|
913
|
-
}
|
|
914
|
-
return v;
|
|
915
|
-
}
|
|
916
|
-
static #isOpaqueType(schema, seen = /* @__PURE__ */ new WeakSet()) {
|
|
917
|
-
if (!schema || typeof schema !== "object" || seen.has(schema)) return false;
|
|
918
|
-
seen.add(schema);
|
|
919
|
-
if (schema["~refine"] || schema["~elyTyp"] === ELYSIA_TYPES.NoValidate) return true;
|
|
920
|
-
const props = schema.properties;
|
|
921
|
-
if (props) {
|
|
922
|
-
for (const k in props) if (TypeBoxValidatorCache.#isOpaqueType(props[k], seen)) return true;
|
|
923
|
-
}
|
|
924
|
-
const items = schema.items;
|
|
925
|
-
if (Array.isArray(items)) {
|
|
926
|
-
for (const it of items) if (TypeBoxValidatorCache.#isOpaqueType(it, seen)) return true;
|
|
927
|
-
} else if (items && TypeBoxValidatorCache.#isOpaqueType(items, seen)) return true;
|
|
928
|
-
for (const k of [
|
|
929
|
-
"anyOf",
|
|
930
|
-
"allOf",
|
|
931
|
-
"oneOf"
|
|
932
|
-
]) {
|
|
933
|
-
const arr = schema[k];
|
|
934
|
-
if (Array.isArray(arr)) {
|
|
935
|
-
for (const x of arr) if (TypeBoxValidatorCache.#isOpaqueType(x, seen)) return true;
|
|
936
|
-
}
|
|
937
|
-
}
|
|
938
|
-
if (schema.additionalProperties && typeof schema.additionalProperties === "object" && TypeBoxValidatorCache.#isOpaqueType(schema.additionalProperties, seen)) return true;
|
|
939
|
-
if (schema.not && TypeBoxValidatorCache.#isOpaqueType(schema.not, seen)) return true;
|
|
940
|
-
const pp = schema.patternProperties;
|
|
941
|
-
if (pp) {
|
|
942
|
-
for (const k in pp) if (TypeBoxValidatorCache.#isOpaqueType(pp[k], seen)) return true;
|
|
943
|
-
}
|
|
944
|
-
return false;
|
|
945
|
-
}
|
|
946
|
-
#cache = /* @__PURE__ */ new Map();
|
|
947
|
-
#referenceCache = /* @__PURE__ */ new WeakMap();
|
|
948
|
-
#gc;
|
|
949
|
-
#gcTime;
|
|
950
|
-
#lastSchema;
|
|
951
|
-
#lastMeta;
|
|
952
|
-
#meta(schema) {
|
|
953
|
-
if (this.#lastSchema === schema && this.#lastMeta) return this.#lastMeta;
|
|
954
|
-
const special = HasCodec(schema) || TypeBoxValidatorCache.#isOpaqueType(schema);
|
|
955
|
-
const meta = {
|
|
956
|
-
special,
|
|
957
|
-
key: special ? "" : JSON.stringify(schema, TypeBoxValidatorCache.ignoreMeta)
|
|
958
|
-
};
|
|
959
|
-
this.#lastSchema = schema;
|
|
960
|
-
this.#lastMeta = meta;
|
|
961
|
-
return meta;
|
|
962
|
-
}
|
|
963
|
-
constructor(gcTime) {
|
|
964
|
-
this.#gcTime = gcTime;
|
|
965
|
-
}
|
|
966
|
-
#scheduleClear() {
|
|
967
|
-
if (isCloudflareWorker) return;
|
|
968
|
-
if (this.#gc) clearTimeout(this.#gc);
|
|
969
|
-
this.#gc = setTimeout(() => this.clear(), this.#gcTime ?? DEFAULT_GC_TIME);
|
|
970
|
-
this.#gc.unref?.();
|
|
971
|
-
}
|
|
972
|
-
get(schema, coercions = TypeBoxValidatorCache.EMPTY, normalize = "") {
|
|
973
|
-
const refBucket = this.#referenceCache.get(schema)?.get(normalize);
|
|
974
|
-
if (refBucket?.has(coercions)) return refBucket.get(coercions);
|
|
975
|
-
const meta = this.#meta(schema);
|
|
976
|
-
if (meta.special) return;
|
|
977
|
-
const key = meta.key + "\0" + normalize;
|
|
978
|
-
if (this.#cache.has(key)) {
|
|
979
|
-
const coercionsCache = this.#cache.get(key);
|
|
980
|
-
this.#cache.delete(key);
|
|
981
|
-
this.#cache.set(key, coercionsCache);
|
|
982
|
-
if (coercionsCache.has(coercions)) return coercionsCache.get(coercions);
|
|
983
|
-
}
|
|
984
|
-
}
|
|
985
|
-
#refBucket(schema) {
|
|
986
|
-
let byNormalize = this.#referenceCache.get(schema);
|
|
987
|
-
if (!byNormalize) {
|
|
988
|
-
byNormalize = /* @__PURE__ */ new Map();
|
|
989
|
-
this.#referenceCache.set(schema, byNormalize);
|
|
990
|
-
}
|
|
991
|
-
return byNormalize;
|
|
992
|
-
}
|
|
993
|
-
set(schema, coercions = TypeBoxValidatorCache.EMPTY, validator, normalize = "") {
|
|
994
|
-
this.#scheduleClear();
|
|
995
|
-
const meta = this.#meta(schema);
|
|
996
|
-
if (meta.special) {
|
|
997
|
-
const cache = (/* @__PURE__ */ new WeakMap()).set(coercions, validator);
|
|
998
|
-
this.#refBucket(schema).set(normalize, cache);
|
|
999
|
-
return;
|
|
1000
|
-
}
|
|
1001
|
-
const key = meta.key + "\0" + normalize;
|
|
1002
|
-
if (this.#cache.has(key)) {
|
|
1003
|
-
const cache = this.#cache.get(key).set(coercions, validator);
|
|
1004
|
-
const byNormalize = this.#refBucket(schema);
|
|
1005
|
-
if (byNormalize.has(normalize)) byNormalize.get(normalize).set(coercions, validator);
|
|
1006
|
-
else byNormalize.set(normalize, cache);
|
|
1007
|
-
return;
|
|
1008
|
-
}
|
|
1009
|
-
if (this.#cache.size >= DEFAULT_CACHE_LIMIT) {
|
|
1010
|
-
const oldest = this.#cache.keys().next().value;
|
|
1011
|
-
if (oldest !== void 0) this.#cache.delete(oldest);
|
|
1012
|
-
}
|
|
1013
|
-
const cache = (/* @__PURE__ */ new WeakMap()).set(coercions, validator);
|
|
1014
|
-
this.#cache.set(key, cache);
|
|
1015
|
-
this.#refBucket(schema).set(normalize, cache);
|
|
1016
|
-
}
|
|
1017
|
-
clear() {
|
|
1018
|
-
if (this.#gc) {
|
|
1019
|
-
clearTimeout(this.#gc);
|
|
1020
|
-
this.#gc = void 0;
|
|
1021
|
-
}
|
|
1022
|
-
this.#cache.clear();
|
|
1023
|
-
this.#referenceCache = /* @__PURE__ */ new WeakMap();
|
|
1024
|
-
deferCoercions();
|
|
1025
|
-
}
|
|
1026
|
-
};
|
|
1027
|
-
|
|
1028
|
-
//#endregion
|
|
1029
|
-
export { TypeBoxValidator, TypeBoxValidatorCache };
|