elysia 1.4.28 → 2.0.0-beta.0
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 +29 -0
- package/dist/_virtual/_rolldown/runtime.mjs +38 -0
- package/dist/adapter/bun/index.d.ts +7 -6
- package/dist/adapter/bun/index.js +219 -380
- package/dist/adapter/bun/index.mjs +217 -382
- package/dist/adapter/constants.d.ts +6 -0
- package/dist/adapter/constants.js +10 -0
- package/dist/adapter/constants.mjs +9 -0
- package/dist/adapter/default-headers.d.ts +4 -0
- package/dist/adapter/default-headers.js +7 -0
- package/dist/adapter/default-headers.mjs +5 -0
- package/dist/adapter/index.d.ts +8 -1
- package/dist/adapter/index.js +7 -14
- package/dist/adapter/index.mjs +5 -0
- package/dist/adapter/origin.d.ts +63 -0
- package/dist/adapter/origin.js +64 -0
- package/dist/adapter/origin.mjs +62 -0
- package/dist/adapter/response-ownership.d.ts +5 -0
- package/dist/adapter/response-ownership.js +13 -0
- package/dist/adapter/response-ownership.mjs +10 -0
- package/dist/adapter/skip-clone.d.ts +4 -0
- package/dist/adapter/skip-clone.js +7 -0
- package/dist/adapter/skip-clone.mjs +5 -0
- package/dist/adapter/types.d.ts +50 -125
- package/dist/adapter/types.js +1 -14
- package/dist/adapter/types.mjs +1 -0
- package/dist/adapter/utils.d.ts +47 -19
- package/dist/adapter/utils.js +561 -311
- package/dist/adapter/utils.mjs +551 -283
- package/dist/adapter/web-standard/handler.d.ts +11 -9
- package/dist/adapter/web-standard/handler.js +146 -415
- package/dist/adapter/web-standard/handler.mjs +144 -400
- package/dist/adapter/web-standard/index.d.ts +5 -2
- package/dist/adapter/web-standard/index.js +68 -161
- package/dist/adapter/web-standard/index.mjs +68 -146
- package/dist/adapter/web-standard/utils.d.ts +4 -0
- package/dist/adapter/web-standard/utils.js +153 -0
- package/dist/adapter/web-standard/utils.mjs +152 -0
- package/dist/base.d.ts +898 -0
- package/dist/base.js +1564 -0
- package/dist/base.mjs +1561 -0
- package/dist/compile/analysis-cache.d.ts +6 -0
- package/dist/compile/analysis-cache.js +12 -0
- package/dist/compile/analysis-cache.mjs +11 -0
- package/dist/compile/aot-capture.d.ts +24 -0
- package/dist/compile/aot-capture.js +376 -0
- package/dist/compile/aot-capture.mjs +365 -0
- package/dist/compile/aot-emit.d.ts +29 -0
- package/dist/compile/aot-emit.js +107 -0
- package/dist/compile/aot-emit.mjs +101 -0
- package/dist/compile/aot-reconstruct.d.ts +30 -0
- package/dist/compile/aot-reconstruct.js +156 -0
- package/dist/compile/aot-reconstruct.mjs +147 -0
- package/dist/compile/aot.d.ts +287 -0
- package/dist/compile/aot.js +220 -0
- package/dist/compile/aot.mjs +200 -0
- package/dist/compile/handler/descriptor.d.ts +77 -0
- package/dist/compile/handler/descriptor.js +199 -0
- package/dist/compile/handler/descriptor.mjs +194 -0
- package/dist/compile/handler/frozen-validator.d.ts +39 -0
- package/dist/compile/handler/frozen-validator.js +319 -0
- package/dist/compile/handler/frozen-validator.mjs +313 -0
- package/dist/compile/handler/index.d.ts +19 -0
- package/dist/compile/handler/index.js +379 -0
- package/dist/compile/handler/index.mjs +371 -0
- package/dist/compile/handler/jit.d.ts +43 -0
- package/dist/compile/handler/jit.js +536 -0
- package/dist/compile/handler/jit.mjs +532 -0
- package/dist/compile/handler/params.d.ts +28 -0
- package/dist/compile/handler/params.js +78 -0
- package/dist/compile/handler/params.mjs +76 -0
- package/dist/compile/handler/reconstruct.d.ts +12 -0
- package/dist/compile/handler/reconstruct.js +50 -0
- package/dist/compile/handler/reconstruct.mjs +49 -0
- package/dist/compile/handler/utils.d.ts +45 -0
- package/dist/compile/handler/utils.js +514 -0
- package/dist/compile/handler/utils.mjs +498 -0
- package/dist/compile/jit-probe.d.ts +15 -0
- package/dist/compile/jit-probe.js +28 -0
- package/dist/compile/jit-probe.mjs +26 -0
- package/dist/compile/lexer.d.ts +7 -0
- package/dist/compile/lexer.js +53 -0
- package/dist/compile/lexer.mjs +48 -0
- package/dist/compile/utils.d.ts +8 -0
- package/dist/compile/utils.js +23 -0
- package/dist/compile/utils.mjs +19 -0
- package/dist/compiled.d.ts +17 -0
- package/dist/compiled.js +20 -0
- package/dist/compiled.mjs +16 -0
- package/dist/constants.d.ts +137 -0
- package/dist/constants.js +96 -0
- package/dist/constants.mjs +88 -0
- package/dist/context.d.ts +86 -129
- package/dist/context.js +110 -13
- package/dist/context.mjs +108 -0
- package/dist/cookie/config.d.ts +31 -0
- package/dist/cookie/config.js +98 -0
- package/dist/cookie/config.mjs +95 -0
- package/dist/cookie/cookie.d.ts +26 -0
- package/dist/cookie/cookie.js +105 -0
- package/dist/cookie/cookie.mjs +104 -0
- package/dist/cookie/crypto.d.ts +20 -0
- package/dist/cookie/crypto.js +168 -0
- package/dist/cookie/crypto.mjs +151 -0
- package/dist/cookie/error.d.ts +17 -0
- package/dist/cookie/error.js +25 -0
- package/dist/cookie/error.mjs +24 -0
- package/dist/cookie/index.d.ts +7 -0
- package/dist/cookie/index.js +13 -0
- package/dist/cookie/index.mjs +7 -0
- package/dist/cookie/lib.d.ts +7 -0
- package/dist/cookie/lib.js +76 -0
- package/dist/cookie/lib.mjs +74 -0
- package/dist/cookie/serialize.d.ts +6 -0
- package/dist/cookie/serialize.js +32 -0
- package/dist/cookie/serialize.mjs +31 -0
- package/dist/cookie/types.d.ts +113 -0
- package/dist/cookie/types.js +1 -0
- package/dist/cookie/types.mjs +1 -0
- package/dist/cookie/utils.d.ts +25 -0
- package/dist/cookie/utils.js +182 -0
- package/dist/cookie/utils.mjs +171 -0
- package/dist/error.d.ts +226 -188
- package/dist/error.js +519 -357
- package/dist/error.mjs +503 -327
- package/dist/generation.d.ts +30 -0
- package/dist/generation.js +23 -0
- package/dist/generation.mjs +17 -0
- package/dist/handler/error.d.ts +7 -0
- package/dist/handler/error.js +105 -0
- package/dist/handler/error.mjs +104 -0
- package/dist/handler/fetch.d.ts +8 -0
- package/dist/handler/fetch.js +371 -0
- package/dist/handler/fetch.mjs +367 -0
- package/dist/handler/index.d.ts +2 -0
- package/dist/handler/index.js +5 -0
- package/dist/handler/index.mjs +3 -0
- package/dist/handler/utils.d.ts +15 -0
- package/dist/handler/utils.js +48 -0
- package/dist/handler/utils.mjs +42 -0
- package/dist/index.d.ts +23 -2198
- package/dist/index.js +62 -1948
- package/dist/index.mjs +20 -1986
- package/dist/memory.d.ts +4 -0
- package/dist/memory.js +17 -0
- package/dist/memory.mjs +16 -0
- package/dist/package.js +11 -0
- package/dist/package.mjs +5 -0
- package/dist/parse-query.d.ts +7 -9
- package/dist/parse-query.js +154 -164
- package/dist/parse-query.mjs +150 -133
- package/dist/plugin/aot/bun.d.ts +26 -0
- package/dist/plugin/aot/bun.js +35 -0
- package/dist/plugin/aot/bun.mjs +34 -0
- package/dist/plugin/aot/core.d.ts +291 -0
- package/dist/plugin/aot/core.js +505 -0
- package/dist/plugin/aot/core.mjs +481 -0
- package/dist/plugin/aot/esbuild.d.ts +28 -0
- package/dist/plugin/aot/esbuild.js +48 -0
- package/dist/plugin/aot/esbuild.mjs +46 -0
- package/dist/plugin/aot/hooks.d.ts +28 -0
- package/dist/plugin/aot/hooks.js +114 -0
- package/dist/plugin/aot/hooks.mjs +112 -0
- package/dist/plugin/aot/rspack-loader.d.ts +23 -0
- package/dist/plugin/aot/rspack-loader.js +44 -0
- package/dist/plugin/aot/rspack-loader.mjs +40 -0
- package/dist/plugin/aot/rspack.d.ts +23 -0
- package/dist/plugin/aot/rspack.js +85 -0
- package/dist/plugin/aot/rspack.mjs +82 -0
- package/dist/plugin/aot/source.d.ts +60 -0
- package/dist/plugin/aot/source.js +422 -0
- package/dist/plugin/aot/source.mjs +418 -0
- package/dist/plugin/aot/treeshake.d.ts +4 -0
- package/dist/plugin/aot/treeshake.js +95 -0
- package/dist/plugin/aot/treeshake.mjs +94 -0
- package/dist/plugin/aot/unplugin.d.ts +50 -0
- package/dist/plugin/aot/unplugin.js +64 -0
- package/dist/plugin/aot/unplugin.mjs +62 -0
- package/dist/plugin/aot/vite.d.ts +30 -0
- package/dist/plugin/aot/vite.js +34 -0
- package/dist/plugin/aot/vite.mjs +33 -0
- package/dist/plugin/aot/worker.d.ts +1 -0
- package/dist/plugin/aot/worker.js +28 -0
- package/dist/plugin/aot/worker.mjs +27 -0
- package/dist/plugin/auto-head.d.ts +6 -0
- package/dist/plugin/auto-head.js +38 -0
- package/dist/plugin/auto-head.mjs +36 -0
- package/dist/plugin/trace.d.ts +10 -0
- package/dist/plugin/trace.js +39 -0
- package/dist/plugin/trace.mjs +36 -0
- package/dist/plugin/websocket.d.ts +6 -0
- package/dist/plugin/websocket.js +61 -0
- package/dist/plugin/websocket.mjs +60 -0
- package/dist/route-table.d.ts +28 -0
- package/dist/route-table.js +64 -0
- package/dist/route-table.mjs +61 -0
- package/dist/schema-snapshot.d.ts +9 -0
- package/dist/schema-snapshot.js +271 -0
- package/dist/schema-snapshot.mjs +269 -0
- package/dist/sucrose.d.ts +28 -63
- package/dist/sucrose.js +814 -306
- package/dist/sucrose.mjs +805 -274
- package/dist/trace.d.ts +145 -130
- package/dist/trace.js +387 -135
- package/dist/trace.mjs +385 -113
- package/dist/type/bridge-live.d.ts +15 -0
- package/dist/type/bridge-live.js +39 -0
- package/dist/type/bridge-live.mjs +17 -0
- package/dist/type/bridge.d.ts +56 -0
- package/dist/type/bridge.js +180 -0
- package/dist/type/bridge.mjs +63 -0
- package/dist/type/coerce-plan.d.ts +38 -0
- package/dist/type/coerce-plan.js +107 -0
- package/dist/type/coerce-plan.mjs +98 -0
- package/dist/type/coerce.d.ts +43 -0
- package/dist/type/coerce.js +351 -0
- package/dist/type/coerce.mjs +339 -0
- package/dist/type/compat.d.ts +8 -0
- package/dist/type/compat.js +46 -0
- package/dist/type/compat.mjs +44 -0
- package/dist/type/constants.d.ts +25 -0
- package/dist/type/constants.js +36 -0
- package/dist/type/constants.mjs +32 -0
- package/dist/type/elysia/accelerate.d.ts +6 -0
- package/dist/type/elysia/accelerate.js +12 -0
- package/dist/type/elysia/accelerate.mjs +10 -0
- package/dist/type/elysia/array-buffer.d.ts +6 -0
- package/dist/type/elysia/array-buffer.js +27 -0
- package/dist/type/elysia/array-buffer.mjs +25 -0
- package/dist/type/elysia/array-string.d.ts +7 -0
- package/dist/type/elysia/array-string.js +28 -0
- package/dist/type/elysia/array-string.mjs +26 -0
- package/dist/type/elysia/array.d.ts +6 -0
- package/dist/type/elysia/array.js +32 -0
- package/dist/type/elysia/array.mjs +31 -0
- package/dist/type/elysia/boolean-string.d.ts +6 -0
- package/dist/type/elysia/boolean-string.js +24 -0
- package/dist/type/elysia/boolean-string.mjs +22 -0
- package/dist/type/elysia/boolean.d.ts +6 -0
- package/dist/type/elysia/boolean.js +20 -0
- package/dist/type/elysia/boolean.mjs +18 -0
- package/dist/type/elysia/cookie.d.ts +15 -0
- package/dist/type/elysia/cookie.js +51 -0
- package/dist/type/elysia/cookie.mjs +50 -0
- package/dist/type/elysia/date.d.ts +7 -0
- package/dist/type/elysia/date.js +98 -0
- package/dist/type/elysia/date.mjs +96 -0
- package/dist/type/elysia/file-type.d.ts +25 -0
- package/dist/type/elysia/file-type.js +81 -0
- package/dist/type/elysia/file-type.mjs +71 -0
- package/dist/type/elysia/file.d.ts +9 -0
- package/dist/type/elysia/file.js +64 -0
- package/dist/type/elysia/file.mjs +48 -0
- package/dist/type/elysia/files.d.ts +8 -0
- package/dist/type/elysia/files.js +45 -0
- package/dist/type/elysia/files.mjs +43 -0
- package/dist/type/elysia/form.d.ts +8 -0
- package/dist/type/elysia/form.js +17 -0
- package/dist/type/elysia/form.mjs +15 -0
- package/dist/type/elysia/integer-string.d.ts +7 -0
- package/dist/type/elysia/integer-string.js +40 -0
- package/dist/type/elysia/integer-string.mjs +38 -0
- package/dist/type/elysia/integer.d.ts +6 -0
- package/dist/type/elysia/integer.js +20 -0
- package/dist/type/elysia/integer.mjs +18 -0
- package/dist/type/elysia/intersect.d.ts +6 -0
- package/dist/type/elysia/intersect.js +20 -0
- package/dist/type/elysia/intersect.mjs +19 -0
- package/dist/type/elysia/maybe-empty.d.ts +7 -0
- package/dist/type/elysia/maybe-empty.js +21 -0
- package/dist/type/elysia/maybe-empty.mjs +19 -0
- package/dist/type/elysia/no-validate.d.ts +6 -0
- package/dist/type/elysia/no-validate.js +9 -0
- package/dist/type/elysia/no-validate.mjs +8 -0
- package/dist/type/elysia/nullable.d.ts +7 -0
- package/dist/type/elysia/nullable.js +15 -0
- package/dist/type/elysia/nullable.mjs +13 -0
- package/dist/type/elysia/number.d.ts +6 -0
- package/dist/type/elysia/number.js +20 -0
- package/dist/type/elysia/number.mjs +18 -0
- package/dist/type/elysia/numeric-enum.d.ts +11 -0
- package/dist/type/elysia/numeric-enum.js +26 -0
- package/dist/type/elysia/numeric-enum.mjs +24 -0
- package/dist/type/elysia/numeric.d.ts +7 -0
- package/dist/type/elysia/numeric.js +62 -0
- package/dist/type/elysia/numeric.mjs +60 -0
- package/dist/type/elysia/object-string.d.ts +7 -0
- package/dist/type/elysia/object-string.js +28 -0
- package/dist/type/elysia/object-string.mjs +26 -0
- package/dist/type/elysia/object.d.ts +6 -0
- package/dist/type/elysia/object.js +47 -0
- package/dist/type/elysia/object.mjs +46 -0
- package/dist/type/elysia/optional.d.ts +6 -0
- package/dist/type/elysia/optional.js +22 -0
- package/dist/type/elysia/optional.mjs +21 -0
- package/dist/type/elysia/string.d.ts +6 -0
- package/dist/type/elysia/string.js +43 -0
- package/dist/type/elysia/string.mjs +41 -0
- package/dist/type/elysia/uint8-array.d.ts +6 -0
- package/dist/type/elysia/uint8-array.js +27 -0
- package/dist/type/elysia/uint8-array.mjs +25 -0
- package/dist/type/elysia/union-enum.d.ts +8 -0
- package/dist/type/elysia/union-enum.js +31 -0
- package/dist/type/elysia/union-enum.mjs +29 -0
- package/dist/type/elysia/union.d.ts +6 -0
- package/dist/type/elysia/union.js +20 -0
- package/dist/type/elysia/union.mjs +19 -0
- package/dist/type/elysia/utils.d.ts +53 -0
- package/dist/type/elysia/utils.js +98 -0
- package/dist/type/elysia/utils.mjs +88 -0
- package/dist/type/exports.d.ts +36 -0
- package/dist/type/exports.js +68 -0
- package/dist/type/exports.mjs +36 -0
- package/dist/type/index.d.ts +13 -0
- package/dist/type/index.js +81 -0
- package/dist/type/index.mjs +68 -0
- package/dist/type/shared.d.ts +27 -0
- package/dist/type/shared.js +171 -0
- package/dist/type/shared.mjs +161 -0
- package/dist/type/types.d.ts +206 -0
- package/dist/type/types.js +1 -0
- package/dist/type/types.mjs +1 -0
- package/dist/type/utils.d.ts +7 -0
- package/dist/type/utils.js +43 -0
- package/dist/type/utils.mjs +41 -0
- package/dist/type/validator/clean-safe.d.ts +8 -0
- package/dist/type/validator/clean-safe.js +82 -0
- package/dist/type/validator/clean-safe.mjs +77 -0
- package/dist/type/validator/custom-error.d.ts +10 -0
- package/dist/type/validator/custom-error.js +191 -0
- package/dist/type/validator/custom-error.mjs +188 -0
- package/dist/type/validator/default-precompute.d.ts +26 -0
- package/dist/type/validator/default-precompute.js +432 -0
- package/dist/type/validator/default-precompute.mjs +421 -0
- package/dist/type/validator/exact-mirror.d.ts +7 -0
- package/dist/type/validator/exact-mirror.js +19 -0
- package/dist/type/validator/exact-mirror.mjs +15 -0
- package/dist/type/validator/frozen-check.d.ts +11 -0
- package/dist/type/validator/frozen-check.js +20 -0
- package/dist/type/validator/frozen-check.mjs +19 -0
- package/dist/type/validator/index.d.ts +32 -0
- package/dist/type/validator/index.js +657 -0
- package/dist/type/validator/index.mjs +651 -0
- package/dist/type/validator/string-codec-aot.d.ts +8 -0
- package/dist/type/validator/string-codec-aot.js +68 -0
- package/dist/type/validator/string-codec-aot.mjs +66 -0
- package/dist/type/validator/validator-cache.d.ts +26 -0
- package/dist/type/validator/validator-cache.js +247 -0
- package/dist/type/validator/validator-cache.mjs +242 -0
- package/dist/types.d.ts +938 -1235
- package/dist/types.js +1 -14
- package/dist/types.mjs +1 -0
- package/dist/universal/constants.d.ts +6 -0
- package/dist/universal/constants.js +19 -0
- package/dist/universal/constants.mjs +15 -0
- package/dist/universal/env.d.ts +5 -1
- package/dist/universal/env.js +12 -26
- package/dist/universal/env.mjs +10 -5
- package/dist/universal/file.d.ts +55 -86
- package/dist/universal/file.js +116 -157
- package/dist/universal/file.mjs +111 -132
- package/dist/universal/index.d.ts +5 -3
- package/dist/universal/index.js +8 -27
- package/dist/universal/index.mjs +4 -5
- package/dist/universal/is-production.d.ts +5 -0
- package/dist/universal/is-production.js +10 -0
- package/dist/universal/is-production.mjs +8 -0
- package/dist/universal/server.d.ts +318 -313
- package/dist/universal/server.js +1 -14
- package/dist/universal/server.mjs +1 -0
- package/dist/utils.d.ts +162 -191
- package/dist/utils.js +732 -693
- package/dist/utils.mjs +683 -631
- package/dist/validator/index.d.ts +63 -0
- package/dist/validator/index.js +299 -0
- package/dist/validator/index.mjs +296 -0
- package/dist/validator/route.d.ts +32 -0
- package/dist/validator/route.js +51 -0
- package/dist/validator/route.mjs +50 -0
- package/dist/ws/context.d.ts +49 -0
- package/dist/ws/context.js +152 -0
- package/dist/ws/context.mjs +150 -0
- package/dist/ws/index.d.ts +5 -77
- package/dist/ws/index.js +11 -180
- package/dist/ws/index.mjs +5 -157
- package/dist/ws/parser.d.ts +5 -0
- package/dist/ws/parser.js +69 -0
- package/dist/ws/parser.mjs +66 -0
- package/dist/ws/route.d.ts +16 -0
- package/dist/ws/route.js +560 -0
- package/dist/ws/route.mjs +554 -0
- package/dist/ws/types.d.ts +154 -57
- package/dist/ws/types.js +1 -14
- package/dist/ws/types.mjs +1 -0
- package/package.json +179 -120
- package/bunfig.toml +0 -2
- package/dist/adapter/bun/compose.d.ts +0 -3
- package/dist/adapter/bun/compose.js +0 -90
- package/dist/adapter/bun/compose.mjs +0 -74
- package/dist/adapter/bun/handler-native.d.ts +0 -3
- package/dist/adapter/bun/handler-native.js +0 -42
- package/dist/adapter/bun/handler-native.mjs +0 -22
- package/dist/adapter/bun/handler.d.ts +0 -7
- package/dist/adapter/bun/handler.js +0 -363
- package/dist/adapter/bun/handler.mjs +0 -346
- package/dist/adapter/cloudflare-worker/index.d.ts +0 -20
- package/dist/adapter/cloudflare-worker/index.js +0 -59
- package/dist/adapter/cloudflare-worker/index.mjs +0 -39
- package/dist/compose.d.ts +0 -30
- package/dist/compose.js +0 -1324
- package/dist/compose.mjs +0 -1336
- package/dist/cookies.d.ts +0 -158
- package/dist/cookies.js +0 -250
- package/dist/cookies.mjs +0 -221
- package/dist/dynamic-handle.d.ts +0 -15
- package/dist/dynamic-handle.js +0 -521
- package/dist/dynamic-handle.mjs +0 -509
- package/dist/formats.d.ts +0 -79
- package/dist/formats.js +0 -153
- package/dist/formats.mjs +0 -132
- package/dist/manifest.d.ts +0 -0
- package/dist/manifest.js +0 -1
- package/dist/manifest.mjs +0 -0
- package/dist/replace-schema.d.ts +0 -60
- package/dist/replace-schema.js +0 -140
- package/dist/replace-schema.mjs +0 -115
- package/dist/schema.d.ts +0 -107
- package/dist/schema.js +0 -833
- package/dist/schema.mjs +0 -814
- package/dist/type-system/format.d.ts +0 -55
- package/dist/type-system/format.js +0 -197
- package/dist/type-system/format.mjs +0 -175
- package/dist/type-system/index.d.ts +0 -45
- package/dist/type-system/index.js +0 -406
- package/dist/type-system/index.mjs +0 -394
- package/dist/type-system/types.d.ts +0 -155
- package/dist/type-system/types.js +0 -14
- package/dist/type-system/types.mjs +0 -0
- package/dist/type-system/utils.d.ts +0 -62
- package/dist/type-system/utils.js +0 -141
- package/dist/type-system/utils.mjs +0 -111
- package/dist/universal/request.d.ts +0 -29
- package/dist/universal/request.js +0 -126
- package/dist/universal/request.mjs +0 -105
- package/dist/universal/types.d.ts +0 -124
- package/dist/universal/types.js +0 -41
- package/dist/universal/types.mjs +0 -19
- package/dist/universal/utils.d.ts +0 -3
- package/dist/universal/utils.js +0 -41
- package/dist/universal/utils.mjs +0 -18
- package/dist/ws/bun.d.ts +0 -419
- package/dist/ws/bun.js +0 -14
- package/dist/ws/bun.mjs +0 -0
- package/knip.json +0 -22
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { ElysiaConfig, MaybePromise } from "../types.js";
|
|
2
|
+
import { AnySchema, StandardSchemaV1Like } from "../type/types.js";
|
|
3
|
+
import { TypeBoxValidator } from "../type/bridge.js";
|
|
4
|
+
import { CoerceOption } from "../type/coerce.js";
|
|
5
|
+
import { ProgramId, ValidatorSlot } from "../compile/aot.js";
|
|
6
|
+
import { TSchema } from "typebox/type";
|
|
7
|
+
import { TLocalizedValidationError } from "typebox/error";
|
|
8
|
+
|
|
9
|
+
//#region src/validator/index.d.ts
|
|
10
|
+
interface ValidatorOptions {
|
|
11
|
+
app?: {
|
|
12
|
+
['~programId']?: ProgramId;
|
|
13
|
+
};
|
|
14
|
+
models?: Record<keyof any, AnySchema>;
|
|
15
|
+
schemas?: AnySchema[];
|
|
16
|
+
coerces?: CoerceOption[];
|
|
17
|
+
normalize?: boolean | 'exactMirror' | 'typebox';
|
|
18
|
+
sanitize?: ElysiaConfig<any, any>['sanitize'];
|
|
19
|
+
aot?: {
|
|
20
|
+
method: string;
|
|
21
|
+
path: string;
|
|
22
|
+
};
|
|
23
|
+
slot?: ValidatorSlot;
|
|
24
|
+
eager?: boolean;
|
|
25
|
+
}
|
|
26
|
+
interface ResponseValidatorOptions extends Omit<ValidatorOptions, 'schemas'> {
|
|
27
|
+
schemas?: Record<number, AnySchema>[];
|
|
28
|
+
}
|
|
29
|
+
type ToSubTypeValidator<T> = T extends AnySchema ? T extends TSchema ? TypeBoxValidator<T> : StandardValidator : never;
|
|
30
|
+
declare abstract class Validator {
|
|
31
|
+
isAsync: boolean;
|
|
32
|
+
mayReturnPromise: boolean;
|
|
33
|
+
abstract Check(value: unknown): boolean;
|
|
34
|
+
abstract Errors(value: unknown): TLocalizedValidationError[];
|
|
35
|
+
static reference(schema: string | TSchema | StandardSchemaV1Like, models: Record<keyof any, AnySchema> | undefined): AnySchema;
|
|
36
|
+
From?(value: unknown, type?: string, allowAsync?: boolean): MaybePromise<unknown>;
|
|
37
|
+
Clean: ((value: unknown) => unknown) | undefined;
|
|
38
|
+
static create<const Schema extends TSchema>(schema: Schema, options?: ValidatorOptions): TypeBoxValidator<Schema>;
|
|
39
|
+
static create<const Schema extends StandardSchemaV1Like>(schema: Schema, options?: ValidatorOptions): StandardValidator;
|
|
40
|
+
static create(schema: undefined | null, options?: ValidatorOptions): Validator | undefined;
|
|
41
|
+
static response(schema: TSchema | StandardSchemaV1Like | Record<number, TSchema | StandardSchemaV1Like> | undefined | null, options?: ResponseValidatorOptions): Record<number, Validator> | undefined;
|
|
42
|
+
static clear(): void;
|
|
43
|
+
}
|
|
44
|
+
declare class StandardValidator extends Validator {
|
|
45
|
+
#private;
|
|
46
|
+
mayReturnPromise: boolean;
|
|
47
|
+
constructor(schema: StandardSchemaV1Like);
|
|
48
|
+
Check(value: unknown): boolean;
|
|
49
|
+
Errors(value: unknown): TLocalizedValidationError[];
|
|
50
|
+
EncodeFrom(value: unknown): unknown;
|
|
51
|
+
From(value: unknown, type?: string, allowAsync?: boolean): unknown;
|
|
52
|
+
}
|
|
53
|
+
declare class MultiValidator extends Validator {
|
|
54
|
+
#private;
|
|
55
|
+
isAsync: boolean;
|
|
56
|
+
constructor(schema: TSchema | StandardSchemaV1Like, options: ValidatorOptions);
|
|
57
|
+
Check(value: unknown): boolean;
|
|
58
|
+
Errors(value: unknown): TLocalizedValidationError[];
|
|
59
|
+
EncodeFrom(value: unknown): unknown;
|
|
60
|
+
From(value: unknown, type?: string, allowAsync?: boolean): MaybePromise<unknown>;
|
|
61
|
+
}
|
|
62
|
+
//#endregion
|
|
63
|
+
export { MultiValidator, ResponseValidatorOptions, StandardValidator, ToSubTypeValidator, Validator, ValidatorOptions };
|
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
+
const require_type_bridge = require('../type/bridge.js');
|
|
3
|
+
const require_error = require('../error.js');
|
|
4
|
+
const require_compile_aot = require('../compile/aot.js');
|
|
5
|
+
const require_compile_utils = require('../compile/utils.js');
|
|
6
|
+
const require_type_shared = require('../type/shared.js');
|
|
7
|
+
const require_type_elysia_file_type = require('../type/elysia/file-type.js');
|
|
8
|
+
const require_type_utils = require('../type/utils.js');
|
|
9
|
+
|
|
10
|
+
//#region src/validator/index.ts
|
|
11
|
+
var Validator = class Validator {
|
|
12
|
+
constructor() {
|
|
13
|
+
this.isAsync = true;
|
|
14
|
+
this.mayReturnPromise = false;
|
|
15
|
+
}
|
|
16
|
+
static reference(schema, models) {
|
|
17
|
+
if (typeof schema !== "string") return schema;
|
|
18
|
+
if (models && schema in models) return models[schema];
|
|
19
|
+
throw new Error(`Schema reference "${schema}" not found in models`);
|
|
20
|
+
}
|
|
21
|
+
static create(name, options) {
|
|
22
|
+
if (name == null) {
|
|
23
|
+
if (!options?.schemas?.length) return;
|
|
24
|
+
name = options.schemas[0];
|
|
25
|
+
options = {
|
|
26
|
+
...options,
|
|
27
|
+
schemas: options.schemas.slice(1)
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
const schema = Validator.reference(name, options?.models);
|
|
31
|
+
if (options?.schemas?.some((v) => typeof v === "string")) {
|
|
32
|
+
const models = options.models;
|
|
33
|
+
options = {
|
|
34
|
+
...options,
|
|
35
|
+
schemas: options.schemas.map((v) => Validator.reference(v, models))
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
if (isCompiledSchema(schema)) {
|
|
39
|
+
const message = "[Elysia] Compiled schema detected. Please pass t.Schema instead.";
|
|
40
|
+
if (require_compile_aot.Capture.isCapturing()) throw new Error(`${message} build plugin cannot serialize a pre-compiled schema.`);
|
|
41
|
+
console.warn(message);
|
|
42
|
+
}
|
|
43
|
+
let isIntersectable = false;
|
|
44
|
+
if (options?.schemas?.length) if ("~kind" in schema && options.schemas.every((v) => "~kind" in v || "~elyAcl" in v)) isIntersectable = true;
|
|
45
|
+
else return new MultiValidator(schema, options);
|
|
46
|
+
if ("~kind" in schema || "~elyAcl" in schema) {
|
|
47
|
+
const skipCache = options?.normalize === false || !!options?.sanitize;
|
|
48
|
+
const aot = options?.aot;
|
|
49
|
+
const slot = options?.slot;
|
|
50
|
+
const normalizeKey = (options?.normalize === "typebox" ? "typebox" : "") + (slot?.startsWith("response") ? "\0r" : "") + (options?.eager ? "\0e" : "");
|
|
51
|
+
const appHasFrozen = !!aot && !!slot && require_compile_aot.Compiled.hasValidator(aot.method, aot.path, slot, options?.app?.["~programId"]);
|
|
52
|
+
const appSpecific = appHasFrozen || require_compile_aot.Capture.isCapturing();
|
|
53
|
+
const app = options?.app;
|
|
54
|
+
const bypassCache = !!aot && !!slot && require_compile_aot.Capture.isCapturing() || appHasFrozen && options?.normalize !== "typebox" || appSpecific && !app;
|
|
55
|
+
let cache = appSpecific ? app ? tbCaches.get(app) : void 0 : tbCache;
|
|
56
|
+
if (!isIntersectable && !skipCache && !bypassCache && cache) {
|
|
57
|
+
const cached = cache.get(schema, options?.coerces, normalizeKey, options?.models);
|
|
58
|
+
if (cached) return cached;
|
|
59
|
+
} else if (!cache && !bypassCache) {
|
|
60
|
+
const created = new require_type_bridge.TypeBoxValidatorCache();
|
|
61
|
+
cache = created;
|
|
62
|
+
if (appSpecific && app) tbCaches.set(app, created);
|
|
63
|
+
else tbCache = created;
|
|
64
|
+
}
|
|
65
|
+
const validator = new require_type_bridge.TypeBoxValidator(schema, options, typeof name === "string" ? name : void 0, isIntersectable);
|
|
66
|
+
if (!isIntersectable && !skipCache && !bypassCache) cache.set(schema, options?.coerces, validator, normalizeKey, options?.models);
|
|
67
|
+
return validator;
|
|
68
|
+
}
|
|
69
|
+
if ("~standard" in schema) return new StandardValidator(schema);
|
|
70
|
+
throw new Error("Elysia Validator support only TypeBox and Standard Schema");
|
|
71
|
+
}
|
|
72
|
+
static response(schema, options) {
|
|
73
|
+
if (schema == null) {
|
|
74
|
+
if (!options?.schemas?.length) return;
|
|
75
|
+
schema = options.schemas[0];
|
|
76
|
+
options = {
|
|
77
|
+
...options,
|
|
78
|
+
schemas: options.schemas.slice(1)
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
schema = Validator.reference(schema, options?.models);
|
|
82
|
+
const responseSlot = (status) => options?.aot ? `response:${status}` : void 0;
|
|
83
|
+
if (isSingleSchema(schema)) return { 200: Validator.create(schema, {
|
|
84
|
+
...options,
|
|
85
|
+
slot: responseSlot(200),
|
|
86
|
+
schemas: options?.schemas?.map((s) => toStatusBased(s)[200])?.filter(Boolean)
|
|
87
|
+
}) };
|
|
88
|
+
const entries = Object.entries(schema);
|
|
89
|
+
return Object.fromEntries(entries.map(([k, v]) => [k, v instanceof Validator ? v : Validator.create(v, {
|
|
90
|
+
...options,
|
|
91
|
+
slot: responseSlot(k),
|
|
92
|
+
schemas: options?.schemas?.map((s) => toStatusBased(s)[k])?.filter(Boolean)
|
|
93
|
+
})]));
|
|
94
|
+
}
|
|
95
|
+
static clear() {
|
|
96
|
+
tbCache?.clear();
|
|
97
|
+
tbCaches = /* @__PURE__ */ new WeakMap();
|
|
98
|
+
require_type_shared.clearCoerceLeafCache();
|
|
99
|
+
require_type_shared.clearSharedReferenceCaches();
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
const isCompiledSchema = (schema) => schema != null && typeof schema.Check === "function" && "buildResult" in schema;
|
|
103
|
+
const isSingleSchema = (schema) => "~kind" in schema || "~elyAcl" in schema || "~standard" in schema;
|
|
104
|
+
const toStatusBased = (schema) => isSingleSchema(schema) ? { 200: schema } : schema;
|
|
105
|
+
const isAsyncStandardSchema = (schema) => require_compile_utils.isAsyncFunction(schema["~standard"].validate);
|
|
106
|
+
const asyncStandardSchemaError = () => /* @__PURE__ */ new Error("[Elysia] An asynchronous Standard Schema was used where only synchronous validation is supported.");
|
|
107
|
+
var StandardValidator = class extends Validator {
|
|
108
|
+
#validate;
|
|
109
|
+
constructor(schema) {
|
|
110
|
+
super();
|
|
111
|
+
this.mayReturnPromise = true;
|
|
112
|
+
this.#validate = schema["~standard"].validate;
|
|
113
|
+
this.isAsync = isAsyncStandardSchema(schema);
|
|
114
|
+
}
|
|
115
|
+
#sync(value) {
|
|
116
|
+
const q = this.#validate(value);
|
|
117
|
+
if (typeof q?.then === "function") throw asyncStandardSchemaError();
|
|
118
|
+
return q;
|
|
119
|
+
}
|
|
120
|
+
Check(value) {
|
|
121
|
+
return "value" in this.#sync(value);
|
|
122
|
+
}
|
|
123
|
+
Errors(value) {
|
|
124
|
+
return this.#sync(value).issues ?? [];
|
|
125
|
+
}
|
|
126
|
+
EncodeFrom(value) {
|
|
127
|
+
return this.From(value);
|
|
128
|
+
}
|
|
129
|
+
From(value, type, allowAsync = this.isAsync) {
|
|
130
|
+
const q = this.#validate(value);
|
|
131
|
+
if (typeof q?.then === "function") {
|
|
132
|
+
if (!allowAsync) throw asyncStandardSchemaError();
|
|
133
|
+
return Promise.resolve(q).then((resolved) => {
|
|
134
|
+
if ("issues" in resolved) throw new require_error.ValidationError(type, value, resolved.issues);
|
|
135
|
+
return resolved.value;
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
if (q.issues) throw new require_error.ValidationError(type, value, q.issues);
|
|
139
|
+
return q.value;
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
var MultiValidator = class MultiValidator extends Validator {
|
|
143
|
+
#schemas;
|
|
144
|
+
#codecs;
|
|
145
|
+
#hasDefaults;
|
|
146
|
+
#tbSchemas;
|
|
147
|
+
#asyncMembers;
|
|
148
|
+
constructor(schema, options) {
|
|
149
|
+
super();
|
|
150
|
+
this.isAsync = false;
|
|
151
|
+
let typeboxObjects;
|
|
152
|
+
const schemas = [schema].concat(options.schemas);
|
|
153
|
+
const codecs = [];
|
|
154
|
+
const hasDefaults = [];
|
|
155
|
+
const tbSchemas = [];
|
|
156
|
+
const asyncMembers = [];
|
|
157
|
+
const shouldClose = options?.normalize === false;
|
|
158
|
+
const compileMember = (rawSchema, hd) => {
|
|
159
|
+
let coercedSchema = require_type_bridge.applyCoercions(rawSchema, options?.coerces);
|
|
160
|
+
if (shouldClose) coercedSchema = require_type_shared.nonAdditionalProperties(coercedSchema);
|
|
161
|
+
const compiled = require_type_bridge.Compile(coercedSchema);
|
|
162
|
+
codecs.push(require_type_bridge.HasCodec(coercedSchema));
|
|
163
|
+
hasDefaults.push(hd);
|
|
164
|
+
tbSchemas.push(hd ? coercedSchema : null);
|
|
165
|
+
if (compiled.buildResult?.external?.variables?.some(require_type_elysia_file_type.isAsyncPredicate) ?? false) {
|
|
166
|
+
this.isAsync = true;
|
|
167
|
+
asyncMembers.push(Validator.create(rawSchema, {
|
|
168
|
+
coerces: options?.coerces,
|
|
169
|
+
normalize: options?.normalize,
|
|
170
|
+
models: options?.models,
|
|
171
|
+
sanitize: options?.sanitize
|
|
172
|
+
}));
|
|
173
|
+
} else asyncMembers.push(null);
|
|
174
|
+
return compiled;
|
|
175
|
+
};
|
|
176
|
+
for (let i = 0; i < schemas.length; i++) {
|
|
177
|
+
const schema = schemas[i];
|
|
178
|
+
const isTypeBox = "~kind" in schema;
|
|
179
|
+
if (!isTypeBox && !("~standard" in schema)) throw new Error("Elysia Validator support only TypeBox and Standard Schema");
|
|
180
|
+
if (isTypeBox) if (schema["~kind"] === "Object") {
|
|
181
|
+
typeboxObjects ??= [];
|
|
182
|
+
typeboxObjects.push(schema);
|
|
183
|
+
schemas.splice(i, 1);
|
|
184
|
+
i--;
|
|
185
|
+
} else schemas[i] = compileMember(schema, require_type_utils.hasProperty("default", schema));
|
|
186
|
+
else {
|
|
187
|
+
this.mayReturnPromise = true;
|
|
188
|
+
if (isAsyncStandardSchema(schema)) this.isAsync = true;
|
|
189
|
+
codecs.push(false);
|
|
190
|
+
hasDefaults.push(false);
|
|
191
|
+
tbSchemas.push(null);
|
|
192
|
+
asyncMembers.push(null);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
if (typeboxObjects) schemas.push(compileMember(require_type_bridge.Intersect(typeboxObjects), typeboxObjects.some((s) => require_type_utils.hasProperty("default", s))));
|
|
196
|
+
this.#schemas = schemas;
|
|
197
|
+
this.#codecs = codecs;
|
|
198
|
+
this.#hasDefaults = hasDefaults;
|
|
199
|
+
this.#tbSchemas = tbSchemas;
|
|
200
|
+
this.#asyncMembers = asyncMembers;
|
|
201
|
+
}
|
|
202
|
+
static #merge(snapshot, result) {
|
|
203
|
+
if (snapshot === void 0) return result;
|
|
204
|
+
if (Array.isArray(snapshot) && Array.isArray(result)) {
|
|
205
|
+
snapshot.push(...result);
|
|
206
|
+
return snapshot;
|
|
207
|
+
}
|
|
208
|
+
if (typeof snapshot === "object" && typeof result === "object") return Object.assign(snapshot, result);
|
|
209
|
+
throw new Error("Unable to merged value with different type");
|
|
210
|
+
}
|
|
211
|
+
static #syncStandard(schema, value) {
|
|
212
|
+
const q = schema["~standard"].validate(value);
|
|
213
|
+
if (typeof q?.then === "function") throw asyncStandardSchemaError();
|
|
214
|
+
return q;
|
|
215
|
+
}
|
|
216
|
+
Check(value) {
|
|
217
|
+
for (let i = 0; i < this.#schemas.length; i++) {
|
|
218
|
+
const validator = this.#schemas[i];
|
|
219
|
+
if ("~standard" in validator) {
|
|
220
|
+
if (MultiValidator.#syncStandard(validator, value).issues) return false;
|
|
221
|
+
continue;
|
|
222
|
+
}
|
|
223
|
+
const compiled = validator;
|
|
224
|
+
if (!this.#codecs[i]) {
|
|
225
|
+
if (!compiled.Check(value)) return false;
|
|
226
|
+
continue;
|
|
227
|
+
}
|
|
228
|
+
try {
|
|
229
|
+
compiled.Decode(value);
|
|
230
|
+
} catch {
|
|
231
|
+
return false;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
return true;
|
|
235
|
+
}
|
|
236
|
+
Errors(value) {
|
|
237
|
+
const errors = [];
|
|
238
|
+
for (const schema of this.#schemas) if ("~standard" in schema) {
|
|
239
|
+
const issues = MultiValidator.#syncStandard(schema, value).issues;
|
|
240
|
+
if (issues) errors.push(...issues);
|
|
241
|
+
} else errors.push(...schema.Errors(value));
|
|
242
|
+
return errors;
|
|
243
|
+
}
|
|
244
|
+
#cloneForMember(value) {
|
|
245
|
+
return this.#schemas.length > 1 && value !== null && typeof value === "object" ? require_type_bridge.Clone(value) : value;
|
|
246
|
+
}
|
|
247
|
+
EncodeFrom(value) {
|
|
248
|
+
return this.From(value);
|
|
249
|
+
}
|
|
250
|
+
#fromTypeBox(compiled, index, value, type) {
|
|
251
|
+
let v = this.#cloneForMember(value);
|
|
252
|
+
if (this.#hasDefaults[index]) v = compiled.Default ? compiled.Default(v) : require_type_bridge.Default(this.#tbSchemas[index], v);
|
|
253
|
+
if (this.#codecs[index]) try {
|
|
254
|
+
return compiled.Clean(compiled.Decode(v));
|
|
255
|
+
} catch {
|
|
256
|
+
throw new require_error.ValidationError(type, value, () => compiled.Errors(value));
|
|
257
|
+
}
|
|
258
|
+
if (!compiled.Check(v)) throw new require_error.ValidationError(type, value, () => compiled.Errors(v));
|
|
259
|
+
return compiled.Clean(v);
|
|
260
|
+
}
|
|
261
|
+
#fromTypeBoxAsync(tbv, value, type) {
|
|
262
|
+
return tbv.FromAsync(this.#cloneForMember(value), type);
|
|
263
|
+
}
|
|
264
|
+
From(value, type, allowAsync = this.isAsync) {
|
|
265
|
+
return this.#fromLoop(value, 0, void 0, type, allowAsync);
|
|
266
|
+
}
|
|
267
|
+
#fromLoop(value, start, snapshot, type, allowAsync = this.isAsync) {
|
|
268
|
+
for (let i = start; i < this.#schemas.length; i++) {
|
|
269
|
+
const validator = this.#schemas[i];
|
|
270
|
+
if ("~standard" in validator) {
|
|
271
|
+
const q = validator["~standard"].validate(value);
|
|
272
|
+
if (typeof q?.then === "function") {
|
|
273
|
+
if (!allowAsync) throw asyncStandardSchemaError();
|
|
274
|
+
return Promise.resolve(q).then((resolved) => {
|
|
275
|
+
if (resolved.issues) throw new require_error.ValidationError(type, value, resolved.issues);
|
|
276
|
+
return this.#fromLoop(value, i + 1, MultiValidator.#merge(snapshot, resolved.value), type, allowAsync);
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
if (q.issues) throw new require_error.ValidationError(type, value, q.issues);
|
|
280
|
+
snapshot = MultiValidator.#merge(snapshot, q.value);
|
|
281
|
+
continue;
|
|
282
|
+
}
|
|
283
|
+
const asyncTbv = this.#asyncMembers[i];
|
|
284
|
+
if (asyncTbv) {
|
|
285
|
+
if (!allowAsync) throw asyncStandardSchemaError();
|
|
286
|
+
return this.#fromTypeBoxAsync(asyncTbv, value, type).then((result) => this.#fromLoop(value, i + 1, MultiValidator.#merge(snapshot, result), type, allowAsync));
|
|
287
|
+
}
|
|
288
|
+
snapshot = MultiValidator.#merge(snapshot, this.#fromTypeBox(validator, i, value, type));
|
|
289
|
+
}
|
|
290
|
+
return snapshot;
|
|
291
|
+
}
|
|
292
|
+
};
|
|
293
|
+
let tbCache;
|
|
294
|
+
let tbCaches = /* @__PURE__ */ new WeakMap();
|
|
295
|
+
|
|
296
|
+
//#endregion
|
|
297
|
+
exports.MultiValidator = MultiValidator;
|
|
298
|
+
exports.StandardValidator = StandardValidator;
|
|
299
|
+
exports.Validator = Validator;
|
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
import { Clone, Compile, Default, HasCodec, Intersect, TypeBoxValidator, TypeBoxValidatorCache, applyCoercions } from "../type/bridge.mjs";
|
|
2
|
+
import { ValidationError } from "../error.mjs";
|
|
3
|
+
import { Capture, Compiled } from "../compile/aot.mjs";
|
|
4
|
+
import { isAsyncFunction } from "../compile/utils.mjs";
|
|
5
|
+
import { clearCoerceLeafCache, clearSharedReferenceCaches, nonAdditionalProperties } from "../type/shared.mjs";
|
|
6
|
+
import { isAsyncPredicate } from "../type/elysia/file-type.mjs";
|
|
7
|
+
import { hasProperty } from "../type/utils.mjs";
|
|
8
|
+
|
|
9
|
+
//#region src/validator/index.ts
|
|
10
|
+
var Validator = class Validator {
|
|
11
|
+
constructor() {
|
|
12
|
+
this.isAsync = true;
|
|
13
|
+
this.mayReturnPromise = false;
|
|
14
|
+
}
|
|
15
|
+
static reference(schema, models) {
|
|
16
|
+
if (typeof schema !== "string") return schema;
|
|
17
|
+
if (models && schema in models) return models[schema];
|
|
18
|
+
throw new Error(`Schema reference "${schema}" not found in models`);
|
|
19
|
+
}
|
|
20
|
+
static create(name, options) {
|
|
21
|
+
if (name == null) {
|
|
22
|
+
if (!options?.schemas?.length) return;
|
|
23
|
+
name = options.schemas[0];
|
|
24
|
+
options = {
|
|
25
|
+
...options,
|
|
26
|
+
schemas: options.schemas.slice(1)
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
const schema = Validator.reference(name, options?.models);
|
|
30
|
+
if (options?.schemas?.some((v) => typeof v === "string")) {
|
|
31
|
+
const models = options.models;
|
|
32
|
+
options = {
|
|
33
|
+
...options,
|
|
34
|
+
schemas: options.schemas.map((v) => Validator.reference(v, models))
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
if (isCompiledSchema(schema)) {
|
|
38
|
+
const message = "[Elysia] Compiled schema detected. Please pass t.Schema instead.";
|
|
39
|
+
if (Capture.isCapturing()) throw new Error(`${message} build plugin cannot serialize a pre-compiled schema.`);
|
|
40
|
+
console.warn(message);
|
|
41
|
+
}
|
|
42
|
+
let isIntersectable = false;
|
|
43
|
+
if (options?.schemas?.length) if ("~kind" in schema && options.schemas.every((v) => "~kind" in v || "~elyAcl" in v)) isIntersectable = true;
|
|
44
|
+
else return new MultiValidator(schema, options);
|
|
45
|
+
if ("~kind" in schema || "~elyAcl" in schema) {
|
|
46
|
+
const skipCache = options?.normalize === false || !!options?.sanitize;
|
|
47
|
+
const aot = options?.aot;
|
|
48
|
+
const slot = options?.slot;
|
|
49
|
+
const normalizeKey = (options?.normalize === "typebox" ? "typebox" : "") + (slot?.startsWith("response") ? "\0r" : "") + (options?.eager ? "\0e" : "");
|
|
50
|
+
const appHasFrozen = !!aot && !!slot && Compiled.hasValidator(aot.method, aot.path, slot, options?.app?.["~programId"]);
|
|
51
|
+
const appSpecific = appHasFrozen || Capture.isCapturing();
|
|
52
|
+
const app = options?.app;
|
|
53
|
+
const bypassCache = !!aot && !!slot && Capture.isCapturing() || appHasFrozen && options?.normalize !== "typebox" || appSpecific && !app;
|
|
54
|
+
let cache = appSpecific ? app ? tbCaches.get(app) : void 0 : tbCache;
|
|
55
|
+
if (!isIntersectable && !skipCache && !bypassCache && cache) {
|
|
56
|
+
const cached = cache.get(schema, options?.coerces, normalizeKey, options?.models);
|
|
57
|
+
if (cached) return cached;
|
|
58
|
+
} else if (!cache && !bypassCache) {
|
|
59
|
+
const created = new TypeBoxValidatorCache();
|
|
60
|
+
cache = created;
|
|
61
|
+
if (appSpecific && app) tbCaches.set(app, created);
|
|
62
|
+
else tbCache = created;
|
|
63
|
+
}
|
|
64
|
+
const validator = new TypeBoxValidator(schema, options, typeof name === "string" ? name : void 0, isIntersectable);
|
|
65
|
+
if (!isIntersectable && !skipCache && !bypassCache) cache.set(schema, options?.coerces, validator, normalizeKey, options?.models);
|
|
66
|
+
return validator;
|
|
67
|
+
}
|
|
68
|
+
if ("~standard" in schema) return new StandardValidator(schema);
|
|
69
|
+
throw new Error("Elysia Validator support only TypeBox and Standard Schema");
|
|
70
|
+
}
|
|
71
|
+
static response(schema, options) {
|
|
72
|
+
if (schema == null) {
|
|
73
|
+
if (!options?.schemas?.length) return;
|
|
74
|
+
schema = options.schemas[0];
|
|
75
|
+
options = {
|
|
76
|
+
...options,
|
|
77
|
+
schemas: options.schemas.slice(1)
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
schema = Validator.reference(schema, options?.models);
|
|
81
|
+
const responseSlot = (status) => options?.aot ? `response:${status}` : void 0;
|
|
82
|
+
if (isSingleSchema(schema)) return { 200: Validator.create(schema, {
|
|
83
|
+
...options,
|
|
84
|
+
slot: responseSlot(200),
|
|
85
|
+
schemas: options?.schemas?.map((s) => toStatusBased(s)[200])?.filter(Boolean)
|
|
86
|
+
}) };
|
|
87
|
+
const entries = Object.entries(schema);
|
|
88
|
+
return Object.fromEntries(entries.map(([k, v]) => [k, v instanceof Validator ? v : Validator.create(v, {
|
|
89
|
+
...options,
|
|
90
|
+
slot: responseSlot(k),
|
|
91
|
+
schemas: options?.schemas?.map((s) => toStatusBased(s)[k])?.filter(Boolean)
|
|
92
|
+
})]));
|
|
93
|
+
}
|
|
94
|
+
static clear() {
|
|
95
|
+
tbCache?.clear();
|
|
96
|
+
tbCaches = /* @__PURE__ */ new WeakMap();
|
|
97
|
+
clearCoerceLeafCache();
|
|
98
|
+
clearSharedReferenceCaches();
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
const isCompiledSchema = (schema) => schema != null && typeof schema.Check === "function" && "buildResult" in schema;
|
|
102
|
+
const isSingleSchema = (schema) => "~kind" in schema || "~elyAcl" in schema || "~standard" in schema;
|
|
103
|
+
const toStatusBased = (schema) => isSingleSchema(schema) ? { 200: schema } : schema;
|
|
104
|
+
const isAsyncStandardSchema = (schema) => isAsyncFunction(schema["~standard"].validate);
|
|
105
|
+
const asyncStandardSchemaError = () => /* @__PURE__ */ new Error("[Elysia] An asynchronous Standard Schema was used where only synchronous validation is supported.");
|
|
106
|
+
var StandardValidator = class extends Validator {
|
|
107
|
+
#validate;
|
|
108
|
+
constructor(schema) {
|
|
109
|
+
super();
|
|
110
|
+
this.mayReturnPromise = true;
|
|
111
|
+
this.#validate = schema["~standard"].validate;
|
|
112
|
+
this.isAsync = isAsyncStandardSchema(schema);
|
|
113
|
+
}
|
|
114
|
+
#sync(value) {
|
|
115
|
+
const q = this.#validate(value);
|
|
116
|
+
if (typeof q?.then === "function") throw asyncStandardSchemaError();
|
|
117
|
+
return q;
|
|
118
|
+
}
|
|
119
|
+
Check(value) {
|
|
120
|
+
return "value" in this.#sync(value);
|
|
121
|
+
}
|
|
122
|
+
Errors(value) {
|
|
123
|
+
return this.#sync(value).issues ?? [];
|
|
124
|
+
}
|
|
125
|
+
EncodeFrom(value) {
|
|
126
|
+
return this.From(value);
|
|
127
|
+
}
|
|
128
|
+
From(value, type, allowAsync = this.isAsync) {
|
|
129
|
+
const q = this.#validate(value);
|
|
130
|
+
if (typeof q?.then === "function") {
|
|
131
|
+
if (!allowAsync) throw asyncStandardSchemaError();
|
|
132
|
+
return Promise.resolve(q).then((resolved) => {
|
|
133
|
+
if ("issues" in resolved) throw new ValidationError(type, value, resolved.issues);
|
|
134
|
+
return resolved.value;
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
if (q.issues) throw new ValidationError(type, value, q.issues);
|
|
138
|
+
return q.value;
|
|
139
|
+
}
|
|
140
|
+
};
|
|
141
|
+
var MultiValidator = class MultiValidator extends Validator {
|
|
142
|
+
#schemas;
|
|
143
|
+
#codecs;
|
|
144
|
+
#hasDefaults;
|
|
145
|
+
#tbSchemas;
|
|
146
|
+
#asyncMembers;
|
|
147
|
+
constructor(schema, options) {
|
|
148
|
+
super();
|
|
149
|
+
this.isAsync = false;
|
|
150
|
+
let typeboxObjects;
|
|
151
|
+
const schemas = [schema].concat(options.schemas);
|
|
152
|
+
const codecs = [];
|
|
153
|
+
const hasDefaults = [];
|
|
154
|
+
const tbSchemas = [];
|
|
155
|
+
const asyncMembers = [];
|
|
156
|
+
const shouldClose = options?.normalize === false;
|
|
157
|
+
const compileMember = (rawSchema, hd) => {
|
|
158
|
+
let coercedSchema = applyCoercions(rawSchema, options?.coerces);
|
|
159
|
+
if (shouldClose) coercedSchema = nonAdditionalProperties(coercedSchema);
|
|
160
|
+
const compiled = Compile(coercedSchema);
|
|
161
|
+
codecs.push(HasCodec(coercedSchema));
|
|
162
|
+
hasDefaults.push(hd);
|
|
163
|
+
tbSchemas.push(hd ? coercedSchema : null);
|
|
164
|
+
if (compiled.buildResult?.external?.variables?.some(isAsyncPredicate) ?? false) {
|
|
165
|
+
this.isAsync = true;
|
|
166
|
+
asyncMembers.push(Validator.create(rawSchema, {
|
|
167
|
+
coerces: options?.coerces,
|
|
168
|
+
normalize: options?.normalize,
|
|
169
|
+
models: options?.models,
|
|
170
|
+
sanitize: options?.sanitize
|
|
171
|
+
}));
|
|
172
|
+
} else asyncMembers.push(null);
|
|
173
|
+
return compiled;
|
|
174
|
+
};
|
|
175
|
+
for (let i = 0; i < schemas.length; i++) {
|
|
176
|
+
const schema = schemas[i];
|
|
177
|
+
const isTypeBox = "~kind" in schema;
|
|
178
|
+
if (!isTypeBox && !("~standard" in schema)) throw new Error("Elysia Validator support only TypeBox and Standard Schema");
|
|
179
|
+
if (isTypeBox) if (schema["~kind"] === "Object") {
|
|
180
|
+
typeboxObjects ??= [];
|
|
181
|
+
typeboxObjects.push(schema);
|
|
182
|
+
schemas.splice(i, 1);
|
|
183
|
+
i--;
|
|
184
|
+
} else schemas[i] = compileMember(schema, hasProperty("default", schema));
|
|
185
|
+
else {
|
|
186
|
+
this.mayReturnPromise = true;
|
|
187
|
+
if (isAsyncStandardSchema(schema)) this.isAsync = true;
|
|
188
|
+
codecs.push(false);
|
|
189
|
+
hasDefaults.push(false);
|
|
190
|
+
tbSchemas.push(null);
|
|
191
|
+
asyncMembers.push(null);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
if (typeboxObjects) schemas.push(compileMember(Intersect(typeboxObjects), typeboxObjects.some((s) => hasProperty("default", s))));
|
|
195
|
+
this.#schemas = schemas;
|
|
196
|
+
this.#codecs = codecs;
|
|
197
|
+
this.#hasDefaults = hasDefaults;
|
|
198
|
+
this.#tbSchemas = tbSchemas;
|
|
199
|
+
this.#asyncMembers = asyncMembers;
|
|
200
|
+
}
|
|
201
|
+
static #merge(snapshot, result) {
|
|
202
|
+
if (snapshot === void 0) return result;
|
|
203
|
+
if (Array.isArray(snapshot) && Array.isArray(result)) {
|
|
204
|
+
snapshot.push(...result);
|
|
205
|
+
return snapshot;
|
|
206
|
+
}
|
|
207
|
+
if (typeof snapshot === "object" && typeof result === "object") return Object.assign(snapshot, result);
|
|
208
|
+
throw new Error("Unable to merged value with different type");
|
|
209
|
+
}
|
|
210
|
+
static #syncStandard(schema, value) {
|
|
211
|
+
const q = schema["~standard"].validate(value);
|
|
212
|
+
if (typeof q?.then === "function") throw asyncStandardSchemaError();
|
|
213
|
+
return q;
|
|
214
|
+
}
|
|
215
|
+
Check(value) {
|
|
216
|
+
for (let i = 0; i < this.#schemas.length; i++) {
|
|
217
|
+
const validator = this.#schemas[i];
|
|
218
|
+
if ("~standard" in validator) {
|
|
219
|
+
if (MultiValidator.#syncStandard(validator, value).issues) return false;
|
|
220
|
+
continue;
|
|
221
|
+
}
|
|
222
|
+
const compiled = validator;
|
|
223
|
+
if (!this.#codecs[i]) {
|
|
224
|
+
if (!compiled.Check(value)) return false;
|
|
225
|
+
continue;
|
|
226
|
+
}
|
|
227
|
+
try {
|
|
228
|
+
compiled.Decode(value);
|
|
229
|
+
} catch {
|
|
230
|
+
return false;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
return true;
|
|
234
|
+
}
|
|
235
|
+
Errors(value) {
|
|
236
|
+
const errors = [];
|
|
237
|
+
for (const schema of this.#schemas) if ("~standard" in schema) {
|
|
238
|
+
const issues = MultiValidator.#syncStandard(schema, value).issues;
|
|
239
|
+
if (issues) errors.push(...issues);
|
|
240
|
+
} else errors.push(...schema.Errors(value));
|
|
241
|
+
return errors;
|
|
242
|
+
}
|
|
243
|
+
#cloneForMember(value) {
|
|
244
|
+
return this.#schemas.length > 1 && value !== null && typeof value === "object" ? Clone(value) : value;
|
|
245
|
+
}
|
|
246
|
+
EncodeFrom(value) {
|
|
247
|
+
return this.From(value);
|
|
248
|
+
}
|
|
249
|
+
#fromTypeBox(compiled, index, value, type) {
|
|
250
|
+
let v = this.#cloneForMember(value);
|
|
251
|
+
if (this.#hasDefaults[index]) v = compiled.Default ? compiled.Default(v) : Default(this.#tbSchemas[index], v);
|
|
252
|
+
if (this.#codecs[index]) try {
|
|
253
|
+
return compiled.Clean(compiled.Decode(v));
|
|
254
|
+
} catch {
|
|
255
|
+
throw new ValidationError(type, value, () => compiled.Errors(value));
|
|
256
|
+
}
|
|
257
|
+
if (!compiled.Check(v)) throw new ValidationError(type, value, () => compiled.Errors(v));
|
|
258
|
+
return compiled.Clean(v);
|
|
259
|
+
}
|
|
260
|
+
#fromTypeBoxAsync(tbv, value, type) {
|
|
261
|
+
return tbv.FromAsync(this.#cloneForMember(value), type);
|
|
262
|
+
}
|
|
263
|
+
From(value, type, allowAsync = this.isAsync) {
|
|
264
|
+
return this.#fromLoop(value, 0, void 0, type, allowAsync);
|
|
265
|
+
}
|
|
266
|
+
#fromLoop(value, start, snapshot, type, allowAsync = this.isAsync) {
|
|
267
|
+
for (let i = start; i < this.#schemas.length; i++) {
|
|
268
|
+
const validator = this.#schemas[i];
|
|
269
|
+
if ("~standard" in validator) {
|
|
270
|
+
const q = validator["~standard"].validate(value);
|
|
271
|
+
if (typeof q?.then === "function") {
|
|
272
|
+
if (!allowAsync) throw asyncStandardSchemaError();
|
|
273
|
+
return Promise.resolve(q).then((resolved) => {
|
|
274
|
+
if (resolved.issues) throw new ValidationError(type, value, resolved.issues);
|
|
275
|
+
return this.#fromLoop(value, i + 1, MultiValidator.#merge(snapshot, resolved.value), type, allowAsync);
|
|
276
|
+
});
|
|
277
|
+
}
|
|
278
|
+
if (q.issues) throw new ValidationError(type, value, q.issues);
|
|
279
|
+
snapshot = MultiValidator.#merge(snapshot, q.value);
|
|
280
|
+
continue;
|
|
281
|
+
}
|
|
282
|
+
const asyncTbv = this.#asyncMembers[i];
|
|
283
|
+
if (asyncTbv) {
|
|
284
|
+
if (!allowAsync) throw asyncStandardSchemaError();
|
|
285
|
+
return this.#fromTypeBoxAsync(asyncTbv, value, type).then((result) => this.#fromLoop(value, i + 1, MultiValidator.#merge(snapshot, result), type, allowAsync));
|
|
286
|
+
}
|
|
287
|
+
snapshot = MultiValidator.#merge(snapshot, this.#fromTypeBox(validator, i, value, type));
|
|
288
|
+
}
|
|
289
|
+
return snapshot;
|
|
290
|
+
}
|
|
291
|
+
};
|
|
292
|
+
let tbCache;
|
|
293
|
+
let tbCaches = /* @__PURE__ */ new WeakMap();
|
|
294
|
+
|
|
295
|
+
//#endregion
|
|
296
|
+
export { MultiValidator, StandardValidator, Validator };
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { AnySchema } from "../type/types.js";
|
|
2
|
+
import { ToSubTypeValidator, ValidatorOptions } from "./index.js";
|
|
3
|
+
//#region src/validator/route.d.ts
|
|
4
|
+
interface RouteSchema {
|
|
5
|
+
body?: AnySchema;
|
|
6
|
+
headers?: AnySchema;
|
|
7
|
+
query?: AnySchema;
|
|
8
|
+
params?: AnySchema;
|
|
9
|
+
cookie?: AnySchema;
|
|
10
|
+
response?: Record<number, AnySchema>;
|
|
11
|
+
}
|
|
12
|
+
interface RouteValidatorOptions extends Omit<ValidatorOptions, 'coerces' | 'schemas' | 'slot'> {
|
|
13
|
+
schemas?: {
|
|
14
|
+
body: AnySchema;
|
|
15
|
+
headers: AnySchema;
|
|
16
|
+
query: AnySchema;
|
|
17
|
+
params: AnySchema;
|
|
18
|
+
cookie: AnySchema;
|
|
19
|
+
response: Record<number, AnySchema>;
|
|
20
|
+
}[];
|
|
21
|
+
}
|
|
22
|
+
declare class RouteValidator<const in out T extends RouteSchema> {
|
|
23
|
+
body: ToSubTypeValidator<T['body']> | undefined;
|
|
24
|
+
headers: ToSubTypeValidator<T['headers']> | undefined;
|
|
25
|
+
query: ToSubTypeValidator<T['query']> | undefined;
|
|
26
|
+
params: ToSubTypeValidator<T['params']> | undefined;
|
|
27
|
+
cookie: ToSubTypeValidator<T['cookie']> | undefined;
|
|
28
|
+
response: { [Status in keyof T['response']]: ToSubTypeValidator<T['response'][Status]> } | undefined;
|
|
29
|
+
constructor(route: T, options?: RouteValidatorOptions);
|
|
30
|
+
}
|
|
31
|
+
//#endregion
|
|
32
|
+
export { RouteValidator, RouteValidatorOptions };
|