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