@luckylillia-dev/luckylillia 0.0.0-stage → 9.0.1
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/chunks/preload-helper-D5R8FHQt.js +342 -0
- package/chunks/preload-helper-D5R8FHQt.js.map +1 -0
- package/default_config.json +83 -0
- package/llbot-core.js +35013 -0
- package/llbot-core.js.map +1 -0
- package/llbot.js +138 -0
- package/llbot.js.map +1 -0
- package/node_modules/@luckylillia-dev/plugin-sdk/lib/commands.d.ts +86 -0
- package/node_modules/@luckylillia-dev/plugin-sdk/lib/commands.js +1 -0
- package/node_modules/@luckylillia-dev/plugin-sdk/lib/config.d.ts +14 -0
- package/node_modules/@luckylillia-dev/plugin-sdk/lib/config.js +1 -0
- package/node_modules/@luckylillia-dev/plugin-sdk/lib/ffmpeg.d.ts +41 -0
- package/node_modules/@luckylillia-dev/plugin-sdk/lib/ffmpeg.js +1 -0
- package/node_modules/@luckylillia-dev/plugin-sdk/lib/http.d.ts +88 -0
- package/node_modules/@luckylillia-dev/plugin-sdk/lib/http.js +1 -0
- package/node_modules/@luckylillia-dev/plugin-sdk/lib/index.d.ts +17 -0
- package/node_modules/@luckylillia-dev/plugin-sdk/lib/index.js +6 -0
- package/node_modules/@luckylillia-dev/plugin-sdk/lib/internal.d.ts +430 -0
- package/node_modules/@luckylillia-dev/plugin-sdk/lib/internal.js +7 -0
- package/node_modules/@luckylillia-dev/plugin-sdk/lib/manifest.d.ts +68 -0
- package/node_modules/@luckylillia-dev/plugin-sdk/lib/manifest.js +1 -0
- package/node_modules/@luckylillia-dev/plugin-sdk/lib/server.d.ts +23 -0
- package/node_modules/@luckylillia-dev/plugin-sdk/lib/server.js +1 -0
- package/node_modules/@luckylillia-dev/plugin-sdk/lib/storage.d.ts +21 -0
- package/node_modules/@luckylillia-dev/plugin-sdk/lib/storage.js +1 -0
- package/node_modules/@luckylillia-dev/plugin-sdk/lib/testing.d.ts +71 -0
- package/node_modules/@luckylillia-dev/plugin-sdk/lib/testing.js +1123 -0
- package/node_modules/@luckylillia-dev/plugin-sdk/lib/universal.d.ts +299 -0
- package/node_modules/@luckylillia-dev/plugin-sdk/lib/universal.js +1 -0
- package/node_modules/@luckylillia-dev/plugin-sdk/lib/webui.d.ts +762 -0
- package/node_modules/@luckylillia-dev/plugin-sdk/lib/webui.js +9 -0
- package/node_modules/@luckylillia-dev/plugin-sdk/package.json +63 -0
- package/node_modules/@satorijs/element/jsx-dev-runtime.d.ts +3 -0
- package/node_modules/@satorijs/element/jsx-runtime.d.ts +3 -0
- package/node_modules/@satorijs/element/lib/index.cjs +447 -0
- package/node_modules/@satorijs/element/lib/index.d.ts +116 -0
- package/node_modules/@satorijs/element/lib/index.mjs +455 -0
- package/node_modules/@satorijs/element/package.json +60 -0
- package/node_modules/@satorijs/element/src/index.ts +559 -0
- package/node_modules/@satorijs/protocol/lib/index.cjs +187 -0
- package/node_modules/@satorijs/protocol/lib/index.d.ts +328 -0
- package/node_modules/@satorijs/protocol/lib/index.mjs +148 -0
- package/node_modules/@satorijs/protocol/package.json +44 -0
- package/node_modules/@satorijs/protocol/src/index.ts +513 -0
- package/node_modules/@standard-schema/spec/LICENSE +21 -0
- package/node_modules/@standard-schema/spec/README.md +198 -0
- package/node_modules/@standard-schema/spec/dist/index.cjs +18 -0
- package/node_modules/@standard-schema/spec/dist/index.d.cts +119 -0
- package/node_modules/@standard-schema/spec/dist/index.d.ts +119 -0
- package/node_modules/@standard-schema/spec/dist/index.js +0 -0
- package/node_modules/@standard-schema/spec/package.json +52 -0
- package/node_modules/cordis/README.md +10 -0
- package/node_modules/cordis/bin.js +16 -0
- package/node_modules/cordis/lib/context.d.ts +32 -0
- package/node_modules/cordis/lib/events.d.ts +62 -0
- package/node_modules/cordis/lib/fiber.d.ts +85 -0
- package/node_modules/cordis/lib/index.d.ts +7 -0
- package/node_modules/cordis/lib/index.js +1577 -0
- package/node_modules/cordis/lib/logger.d.ts +74 -0
- package/node_modules/cordis/lib/reflect.d.ts +54 -0
- package/node_modules/cordis/lib/registry.d.ts +78 -0
- package/node_modules/cordis/lib/service.d.ts +19 -0
- package/node_modules/cordis/lib/utils.d.ts +50 -0
- package/node_modules/cordis/package.json +49 -0
- package/node_modules/cosmokit/LICENSE +21 -0
- package/node_modules/cosmokit/README.md +24 -0
- package/node_modules/cosmokit/lib/index.cjs +419 -0
- package/node_modules/cosmokit/lib/index.cjs.map +6 -0
- package/node_modules/cosmokit/lib/index.d.ts +142 -0
- package/node_modules/cosmokit/lib/index.mjs +357 -0
- package/node_modules/cosmokit/lib/index.mjs.map +6 -0
- package/node_modules/cosmokit/package.json +57 -0
- package/node_modules/cosmokit/src/array.ts +35 -0
- package/node_modules/cosmokit/src/index.ts +5 -0
- package/node_modules/cosmokit/src/misc.ts +57 -0
- package/node_modules/cosmokit/src/string.ts +101 -0
- package/node_modules/cosmokit/src/time.ts +91 -0
- package/node_modules/cosmokit/src/types.ts +131 -0
- package/node_modules/schemastery/README.md +342 -0
- package/node_modules/schemastery/lib/index.cjs +648 -0
- package/node_modules/schemastery/lib/index.cjs.map +6 -0
- package/node_modules/schemastery/lib/index.d.ts +138 -0
- package/node_modules/schemastery/lib/index.mjs +656 -0
- package/node_modules/schemastery/lib/index.mjs.map +6 -0
- package/node_modules/schemastery/package.json +32 -0
- package/node_modules/schemastery/src/index.ts +841 -0
- package/node_modules/undici/LICENSE +21 -0
- package/node_modules/undici/README.md +741 -0
- package/node_modules/undici/docs/docs/api/Agent.md +84 -0
- package/node_modules/undici/docs/docs/api/BalancedPool.md +99 -0
- package/node_modules/undici/docs/docs/api/CacheStorage.md +30 -0
- package/node_modules/undici/docs/docs/api/CacheStore.md +164 -0
- package/node_modules/undici/docs/docs/api/Client.md +290 -0
- package/node_modules/undici/docs/docs/api/ClientStats.md +27 -0
- package/node_modules/undici/docs/docs/api/Connector.md +115 -0
- package/node_modules/undici/docs/docs/api/ContentType.md +57 -0
- package/node_modules/undici/docs/docs/api/Cookies.md +128 -0
- package/node_modules/undici/docs/docs/api/Debug.md +62 -0
- package/node_modules/undici/docs/docs/api/DiagnosticsChannel.md +317 -0
- package/node_modules/undici/docs/docs/api/Dispatcher.md +1393 -0
- package/node_modules/undici/docs/docs/api/EnvHttpProxyAgent.md +159 -0
- package/node_modules/undici/docs/docs/api/Errors.md +49 -0
- package/node_modules/undici/docs/docs/api/EventSource.md +45 -0
- package/node_modules/undici/docs/docs/api/Fetch.md +60 -0
- package/node_modules/undici/docs/docs/api/GlobalInstallation.md +139 -0
- package/node_modules/undici/docs/docs/api/H2CClient.md +263 -0
- package/node_modules/undici/docs/docs/api/MockAgent.md +603 -0
- package/node_modules/undici/docs/docs/api/MockCallHistory.md +197 -0
- package/node_modules/undici/docs/docs/api/MockCallHistoryLog.md +43 -0
- package/node_modules/undici/docs/docs/api/MockClient.md +81 -0
- package/node_modules/undici/docs/docs/api/MockErrors.md +12 -0
- package/node_modules/undici/docs/docs/api/MockPool.md +555 -0
- package/node_modules/undici/docs/docs/api/Pool.md +84 -0
- package/node_modules/undici/docs/docs/api/PoolStats.md +35 -0
- package/node_modules/undici/docs/docs/api/ProxyAgent.md +229 -0
- package/node_modules/undici/docs/docs/api/RedirectHandler.md +93 -0
- package/node_modules/undici/docs/docs/api/RetryAgent.md +50 -0
- package/node_modules/undici/docs/docs/api/RetryHandler.md +118 -0
- package/node_modules/undici/docs/docs/api/RoundRobinPool.md +145 -0
- package/node_modules/undici/docs/docs/api/SnapshotAgent.md +616 -0
- package/node_modules/undici/docs/docs/api/Socks5ProxyAgent.md +275 -0
- package/node_modules/undici/docs/docs/api/Util.md +25 -0
- package/node_modules/undici/docs/docs/api/WebSocket.md +141 -0
- package/node_modules/undici/docs/docs/api/api-lifecycle.md +91 -0
- package/node_modules/undici/docs/docs/best-practices/client-certificate.md +64 -0
- package/node_modules/undici/docs/docs/best-practices/crawling.md +58 -0
- package/node_modules/undici/docs/docs/best-practices/mocking-request.md +190 -0
- package/node_modules/undici/docs/docs/best-practices/proxy.md +127 -0
- package/node_modules/undici/docs/docs/best-practices/undici-vs-builtin-fetch.md +224 -0
- package/node_modules/undici/docs/docs/best-practices/writing-tests.md +63 -0
- package/node_modules/undici/index-fetch.js +65 -0
- package/node_modules/undici/index.d.ts +3 -0
- package/node_modules/undici/index.js +234 -0
- package/node_modules/undici/lib/api/abort-signal.js +59 -0
- package/node_modules/undici/lib/api/api-connect.js +110 -0
- package/node_modules/undici/lib/api/api-pipeline.js +252 -0
- package/node_modules/undici/lib/api/api-request.js +214 -0
- package/node_modules/undici/lib/api/api-stream.js +209 -0
- package/node_modules/undici/lib/api/api-upgrade.js +111 -0
- package/node_modules/undici/lib/api/index.js +7 -0
- package/node_modules/undici/lib/api/readable.js +580 -0
- package/node_modules/undici/lib/cache/memory-cache-store.js +279 -0
- package/node_modules/undici/lib/cache/sqlite-cache-store.js +467 -0
- package/node_modules/undici/lib/core/connect.js +159 -0
- package/node_modules/undici/lib/core/constants.js +143 -0
- package/node_modules/undici/lib/core/diagnostics.js +227 -0
- package/node_modules/undici/lib/core/errors.js +477 -0
- package/node_modules/undici/lib/core/request.js +485 -0
- package/node_modules/undici/lib/core/socks5-client.js +422 -0
- package/node_modules/undici/lib/core/socks5-utils.js +212 -0
- package/node_modules/undici/lib/core/symbols.js +76 -0
- package/node_modules/undici/lib/core/tree.js +160 -0
- package/node_modules/undici/lib/core/util.js +992 -0
- package/node_modules/undici/lib/dispatcher/agent.js +162 -0
- package/node_modules/undici/lib/dispatcher/balanced-pool.js +221 -0
- package/node_modules/undici/lib/dispatcher/client-h1.js +1744 -0
- package/node_modules/undici/lib/dispatcher/client-h2.js +1056 -0
- package/node_modules/undici/lib/dispatcher/client.js +669 -0
- package/node_modules/undici/lib/dispatcher/dispatcher-base.js +196 -0
- package/node_modules/undici/lib/dispatcher/dispatcher.js +48 -0
- package/node_modules/undici/lib/dispatcher/env-http-proxy-agent.js +146 -0
- package/node_modules/undici/lib/dispatcher/fixed-queue.js +135 -0
- package/node_modules/undici/lib/dispatcher/h2c-client.js +51 -0
- package/node_modules/undici/lib/dispatcher/pool-base.js +214 -0
- package/node_modules/undici/lib/dispatcher/pool.js +118 -0
- package/node_modules/undici/lib/dispatcher/proxy-agent.js +319 -0
- package/node_modules/undici/lib/dispatcher/retry-agent.js +35 -0
- package/node_modules/undici/lib/dispatcher/round-robin-pool.js +137 -0
- package/node_modules/undici/lib/dispatcher/socks5-proxy-agent.js +260 -0
- package/node_modules/undici/lib/encoding/index.js +33 -0
- package/node_modules/undici/lib/global.js +59 -0
- package/node_modules/undici/lib/handler/cache-handler.js +824 -0
- package/node_modules/undici/lib/handler/cache-revalidation-handler.js +124 -0
- package/node_modules/undici/lib/handler/decorator-handler.js +67 -0
- package/node_modules/undici/lib/handler/deduplication-handler.js +504 -0
- package/node_modules/undici/lib/handler/redirect-handler.js +245 -0
- package/node_modules/undici/lib/handler/retry-handler.js +504 -0
- package/node_modules/undici/lib/handler/unwrap-handler.js +106 -0
- package/node_modules/undici/lib/handler/wrap-handler.js +105 -0
- package/node_modules/undici/lib/interceptor/cache.js +621 -0
- package/node_modules/undici/lib/interceptor/decompress.js +561 -0
- package/node_modules/undici/lib/interceptor/deduplicate.js +117 -0
- package/node_modules/undici/lib/interceptor/dns.js +571 -0
- package/node_modules/undici/lib/interceptor/dump.js +99 -0
- package/node_modules/undici/lib/interceptor/redirect.js +21 -0
- package/node_modules/undici/lib/interceptor/response-error.js +95 -0
- package/node_modules/undici/lib/interceptor/retry.js +19 -0
- package/node_modules/undici/lib/llhttp/constants.d.ts +195 -0
- package/node_modules/undici/lib/llhttp/constants.js +531 -0
- package/node_modules/undici/lib/llhttp/llhttp-wasm.js +15 -0
- package/node_modules/undici/lib/llhttp/llhttp_simd-wasm.js +15 -0
- package/node_modules/undici/lib/llhttp/utils.d.ts +2 -0
- package/node_modules/undici/lib/llhttp/utils.js +12 -0
- package/node_modules/undici/lib/mock/mock-agent.js +232 -0
- package/node_modules/undici/lib/mock/mock-call-history.js +248 -0
- package/node_modules/undici/lib/mock/mock-client.js +68 -0
- package/node_modules/undici/lib/mock/mock-errors.js +29 -0
- package/node_modules/undici/lib/mock/mock-interceptor.js +209 -0
- package/node_modules/undici/lib/mock/mock-pool.js +68 -0
- package/node_modules/undici/lib/mock/mock-symbols.js +32 -0
- package/node_modules/undici/lib/mock/mock-utils.js +486 -0
- package/node_modules/undici/lib/mock/pending-interceptors-formatter.js +43 -0
- package/node_modules/undici/lib/mock/snapshot-agent.js +353 -0
- package/node_modules/undici/lib/mock/snapshot-recorder.js +588 -0
- package/node_modules/undici/lib/mock/snapshot-utils.js +158 -0
- package/node_modules/undici/lib/util/cache.js +676 -0
- package/node_modules/undici/lib/util/date.js +670 -0
- package/node_modules/undici/lib/util/promise.js +28 -0
- package/node_modules/undici/lib/util/runtime-features.js +124 -0
- package/node_modules/undici/lib/util/stats.js +32 -0
- package/node_modules/undici/lib/util/timers.js +425 -0
- package/node_modules/undici/lib/web/cache/cache.js +864 -0
- package/node_modules/undici/lib/web/cache/cachestorage.js +152 -0
- package/node_modules/undici/lib/web/cache/util.js +45 -0
- package/node_modules/undici/lib/web/cookies/constants.js +12 -0
- package/node_modules/undici/lib/web/cookies/index.js +201 -0
- package/node_modules/undici/lib/web/cookies/parse.js +315 -0
- package/node_modules/undici/lib/web/cookies/util.js +371 -0
- package/node_modules/undici/lib/web/eventsource/eventsource-stream.js +575 -0
- package/node_modules/undici/lib/web/eventsource/eventsource.js +504 -0
- package/node_modules/undici/lib/web/eventsource/util.js +29 -0
- package/node_modules/undici/lib/web/fetch/LICENSE +21 -0
- package/node_modules/undici/lib/web/fetch/body.js +509 -0
- package/node_modules/undici/lib/web/fetch/constants.js +131 -0
- package/node_modules/undici/lib/web/fetch/data-url.js +596 -0
- package/node_modules/undici/lib/web/fetch/formdata-parser.js +586 -0
- package/node_modules/undici/lib/web/fetch/formdata.js +259 -0
- package/node_modules/undici/lib/web/fetch/global.js +40 -0
- package/node_modules/undici/lib/web/fetch/headers.js +719 -0
- package/node_modules/undici/lib/web/fetch/index.js +2413 -0
- package/node_modules/undici/lib/web/fetch/request.js +1115 -0
- package/node_modules/undici/lib/web/fetch/response.js +641 -0
- package/node_modules/undici/lib/web/fetch/util.js +1522 -0
- package/node_modules/undici/lib/web/infra/index.js +243 -0
- package/node_modules/undici/lib/web/subresource-integrity/Readme.md +9 -0
- package/node_modules/undici/lib/web/subresource-integrity/subresource-integrity.js +307 -0
- package/node_modules/undici/lib/web/webidl/index.js +1006 -0
- package/node_modules/undici/lib/web/websocket/connection.js +339 -0
- package/node_modules/undici/lib/web/websocket/constants.js +126 -0
- package/node_modules/undici/lib/web/websocket/events.js +331 -0
- package/node_modules/undici/lib/web/websocket/frame.js +133 -0
- package/node_modules/undici/lib/web/websocket/permessage-deflate.js +105 -0
- package/node_modules/undici/lib/web/websocket/receiver.js +520 -0
- package/node_modules/undici/lib/web/websocket/sender.js +109 -0
- package/node_modules/undici/lib/web/websocket/stream/websocketerror.js +104 -0
- package/node_modules/undici/lib/web/websocket/stream/websocketstream.js +496 -0
- package/node_modules/undici/lib/web/websocket/util.js +347 -0
- package/node_modules/undici/lib/web/websocket/websocket.js +758 -0
- package/node_modules/undici/package.json +152 -0
- package/node_modules/undici/scripts/strip-comments.js +10 -0
- package/node_modules/undici/types/README.md +6 -0
- package/node_modules/undici/types/agent.d.ts +32 -0
- package/node_modules/undici/types/api.d.ts +43 -0
- package/node_modules/undici/types/balanced-pool.d.ts +30 -0
- package/node_modules/undici/types/cache-interceptor.d.ts +179 -0
- package/node_modules/undici/types/cache.d.ts +36 -0
- package/node_modules/undici/types/client-stats.d.ts +15 -0
- package/node_modules/undici/types/client.d.ts +153 -0
- package/node_modules/undici/types/connector.d.ts +36 -0
- package/node_modules/undici/types/content-type.d.ts +21 -0
- package/node_modules/undici/types/cookies.d.ts +30 -0
- package/node_modules/undici/types/diagnostics-channel.d.ts +74 -0
- package/node_modules/undici/types/dispatcher.d.ts +275 -0
- package/node_modules/undici/types/env-http-proxy-agent.d.ts +22 -0
- package/node_modules/undici/types/errors.d.ts +177 -0
- package/node_modules/undici/types/eventsource.d.ts +66 -0
- package/node_modules/undici/types/fetch.d.ts +231 -0
- package/node_modules/undici/types/formdata.d.ts +114 -0
- package/node_modules/undici/types/global-dispatcher.d.ts +9 -0
- package/node_modules/undici/types/global-origin.d.ts +7 -0
- package/node_modules/undici/types/h2c-client.d.ts +73 -0
- package/node_modules/undici/types/handlers.d.ts +14 -0
- package/node_modules/undici/types/header.d.ts +160 -0
- package/node_modules/undici/types/index.d.ts +91 -0
- package/node_modules/undici/types/interceptors.d.ts +82 -0
- package/node_modules/undici/types/mock-agent.d.ts +68 -0
- package/node_modules/undici/types/mock-call-history.d.ts +111 -0
- package/node_modules/undici/types/mock-client.d.ts +27 -0
- package/node_modules/undici/types/mock-errors.d.ts +12 -0
- package/node_modules/undici/types/mock-interceptor.d.ts +94 -0
- package/node_modules/undici/types/mock-pool.d.ts +27 -0
- package/node_modules/undici/types/patch.d.ts +29 -0
- package/node_modules/undici/types/pool-stats.d.ts +19 -0
- package/node_modules/undici/types/pool.d.ts +41 -0
- package/node_modules/undici/types/proxy-agent.d.ts +29 -0
- package/node_modules/undici/types/readable.d.ts +68 -0
- package/node_modules/undici/types/retry-agent.d.ts +8 -0
- package/node_modules/undici/types/retry-handler.d.ts +125 -0
- package/node_modules/undici/types/round-robin-pool.d.ts +41 -0
- package/node_modules/undici/types/snapshot-agent.d.ts +109 -0
- package/node_modules/undici/types/socks5-proxy-agent.d.ts +25 -0
- package/node_modules/undici/types/util.d.ts +18 -0
- package/node_modules/undici/types/utility.d.ts +7 -0
- package/node_modules/undici/types/webidl.d.ts +347 -0
- package/node_modules/undici/types/websocket.d.ts +188 -0
- package/node_modules/ws/LICENSE +20 -0
- package/node_modules/ws/README.md +548 -0
- package/node_modules/ws/browser.js +8 -0
- package/node_modules/ws/index.js +22 -0
- package/node_modules/ws/lib/buffer-util.js +131 -0
- package/node_modules/ws/lib/constants.js +19 -0
- package/node_modules/ws/lib/event-target.js +292 -0
- package/node_modules/ws/lib/extension.js +203 -0
- package/node_modules/ws/lib/limiter.js +55 -0
- package/node_modules/ws/lib/permessage-deflate.js +530 -0
- package/node_modules/ws/lib/receiver.js +743 -0
- package/node_modules/ws/lib/sender.js +607 -0
- package/node_modules/ws/lib/stream.js +161 -0
- package/node_modules/ws/lib/subprotocol.js +62 -0
- package/node_modules/ws/lib/validation.js +152 -0
- package/node_modules/ws/lib/websocket-server.js +562 -0
- package/node_modules/ws/lib/websocket.js +1407 -0
- package/node_modules/ws/package.json +74 -0
- package/node_modules/ws/wrapper.mjs +21 -0
- package/package.json +17 -4
- package/webui/assets/group-bot-profile-CYK81678.jpg +0 -0
- package/webui/assets/group-bot-settings-BBQBbfgh.jpg +0 -0
- package/webui/assets/index-CuH0UYYo.js +28 -0
- package/webui/assets/index-DjbPiYmB.css +2 -0
- package/webui/favicon.ico +0 -0
- package/webui/index.html +26 -0
- package/webui/logo-128.webp +0 -0
- package/webui/logo-192.webp +0 -0
- package/webui/logo-256.webp +0 -0
- package/webui/logo-64.webp +0 -0
- package/webui/logo.jpg +0 -0
- package/webui/plugin-shared/plugin-sdk-webui.js +3 -0
- package/webui/plugin-shared/react-dom.js +3 -0
- package/webui/plugin-shared/react-jsx-runtime.js +3 -0
- package/webui/plugin-shared/react.js +3 -0
- package//344/275/277/347/224/250/350/257/264/346/230/216.txt +6 -0
- package//346/233/264/346/226/260/346/227/245/345/277/227.txt +1091 -0
- package/README.md +0 -3
|
@@ -0,0 +1,841 @@
|
|
|
1
|
+
import { Binary, clone, deepEqual, Dict, filterKeys, isNullable, isPlainObject, pick, valueMap } from 'cosmokit'
|
|
2
|
+
import type { StandardSchemaV1 } from '@standard-schema/spec'
|
|
3
|
+
|
|
4
|
+
const kSchema = Symbol.for('schemastery')
|
|
5
|
+
const kValidationError = Symbol.for('ValidationError')
|
|
6
|
+
|
|
7
|
+
declare global {
|
|
8
|
+
namespace Schemastery {
|
|
9
|
+
export type From<X> =
|
|
10
|
+
| X extends string | number | boolean ? Schema<X>
|
|
11
|
+
: X extends Schema ? X
|
|
12
|
+
: X extends typeof String ? Schema<string>
|
|
13
|
+
: X extends typeof Number ? Schema<number>
|
|
14
|
+
: X extends typeof Boolean ? Schema<boolean>
|
|
15
|
+
: X extends typeof Function ? Schema<Function, (...args: any[]) => any>
|
|
16
|
+
: X extends Constructor<infer S> ? Schema<S>
|
|
17
|
+
: never
|
|
18
|
+
|
|
19
|
+
type TypeS1<X> = X extends Schema<infer S, unknown> ? S : never
|
|
20
|
+
type Inverse<X> = X extends Schema<any, infer Y> ? (arg: Y) => void : never
|
|
21
|
+
|
|
22
|
+
export type TypeS<X> = TypeS1<From<X>>
|
|
23
|
+
export type TypeT<X> = ReturnType<From<X>>
|
|
24
|
+
export type Resolve = (data: any, schema: Schema, options: Options, strict?: boolean) => [any, any?]
|
|
25
|
+
|
|
26
|
+
export type IntersectS<X> = From<X> extends Schema<infer S, unknown> ? S : never
|
|
27
|
+
export type IntersectT<X> = Inverse<From<X>> extends ((arg: infer T) => void) ? T : never
|
|
28
|
+
|
|
29
|
+
type TupleS<X extends readonly any[]> = X extends readonly [infer L, ...infer R] ? [TypeS<L>?, ...TupleS<R>] : any[]
|
|
30
|
+
type TupleT<X extends readonly any[]> = X extends readonly [infer L, ...infer R] ? [TypeT<L>?, ...TupleT<R>] : any[]
|
|
31
|
+
type ObjectS<X extends Dict> = { [K in keyof X]?: TypeS<X[K]> | null } & Dict
|
|
32
|
+
type ObjectT<X extends Dict> = { [K in keyof X]: TypeT<X[K]> } & Dict
|
|
33
|
+
type Constructor<T = any> = new (...args: any[]) => T
|
|
34
|
+
|
|
35
|
+
export interface Static {
|
|
36
|
+
<T = any>(options: Partial<Schema<T>>): Schema<T>
|
|
37
|
+
new <T = any>(options: Partial<Schema<T>>): Schema<T>
|
|
38
|
+
prototype: Schema
|
|
39
|
+
resolve: Resolve
|
|
40
|
+
from<X = any>(source?: X): From<X>
|
|
41
|
+
extend(type: string, resolve: Resolve): void
|
|
42
|
+
any<T = any>(): Schema<T>
|
|
43
|
+
never(): Schema<never>
|
|
44
|
+
const<const T>(value: T): Schema<T>
|
|
45
|
+
string(): Schema<string>
|
|
46
|
+
number(): Schema<number>
|
|
47
|
+
natural(): Schema<number>
|
|
48
|
+
percent(): Schema<number>
|
|
49
|
+
boolean(): Schema<boolean>
|
|
50
|
+
date(): Schema<string | Date, Date>
|
|
51
|
+
regExp(flag?: string): Schema<string | RegExp, RegExp>
|
|
52
|
+
arrayBuffer(): Schema<Binary.Source, ArrayBufferLike>
|
|
53
|
+
arrayBuffer(encoding: 'hex' | 'base64'): Schema<Binary.Source | string, ArrayBufferLike>
|
|
54
|
+
bitset<K extends string>(bits: Partial<Record<K, number>>): Schema<number | readonly K[], number>
|
|
55
|
+
function(): Schema<Function, (...args: any[]) => any>
|
|
56
|
+
is(constructor: string): Schema
|
|
57
|
+
is<T>(constructor: Constructor<T>): Schema<T>
|
|
58
|
+
array<X>(inner: X): Schema<TypeS<X>[], TypeT<X>[]>
|
|
59
|
+
dict<X, Y extends Schema<any, string> = Schema<string>>(inner: X, sKey?: Y): Schema<Dict<TypeS<X>, TypeS<Y>>, Dict<TypeT<X>, TypeT<Y>>>
|
|
60
|
+
tuple<const X extends readonly any[]>(list: X): Schema<TupleS<X>, TupleT<X>>
|
|
61
|
+
object<X extends Dict>(dict: X): Schema<ObjectS<X>, ObjectT<X>>
|
|
62
|
+
union<const X>(list: readonly X[]): Schema<TypeS<X>, TypeT<X>>
|
|
63
|
+
intersect<const X>(list: readonly X[]): Schema<IntersectS<X>, IntersectT<X>>
|
|
64
|
+
transform<X, T>(inner: X, callback: (value: TypeS<X>, options: Schemastery.Options) => T, preserve?: boolean): Schema<TypeS<X>, T>
|
|
65
|
+
lazy<X extends Schema>(callback: () => X): X
|
|
66
|
+
ValidationError: typeof ValidationError
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
interface Options {
|
|
70
|
+
autofix?: boolean
|
|
71
|
+
ignore?(data: any, schema: Schema): boolean
|
|
72
|
+
path?: (keyof any)[]
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export interface Meta<T = any> {
|
|
76
|
+
default?: T extends {} ? Partial<T> : T
|
|
77
|
+
required?: boolean
|
|
78
|
+
disabled?: boolean
|
|
79
|
+
collapse?: boolean
|
|
80
|
+
badges?: { text: string; type: string }[]
|
|
81
|
+
hidden?: boolean
|
|
82
|
+
loose?: boolean
|
|
83
|
+
role?: string
|
|
84
|
+
extra?: any
|
|
85
|
+
link?: string
|
|
86
|
+
description?: string | Dict<string>
|
|
87
|
+
comment?: string
|
|
88
|
+
pattern?: { source: string; flags?: string }
|
|
89
|
+
max?: number
|
|
90
|
+
min?: number
|
|
91
|
+
step?: number
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
interface Schemastery<S = any, T = S> {
|
|
96
|
+
(data?: S | null, options?: Schemastery.Options): T
|
|
97
|
+
new (data?: S | null, options?: Schemastery.Options): T
|
|
98
|
+
[kSchema]: true
|
|
99
|
+
uid: number
|
|
100
|
+
meta: Schemastery.Meta<T>
|
|
101
|
+
type: string
|
|
102
|
+
sKey?: Schema
|
|
103
|
+
inner?: Schema
|
|
104
|
+
list?: Schema[]
|
|
105
|
+
dict?: Dict<Schema>
|
|
106
|
+
bits?: Dict<number>
|
|
107
|
+
callback?: Function
|
|
108
|
+
constructor?: string | Function
|
|
109
|
+
builder?: Function
|
|
110
|
+
value?: T
|
|
111
|
+
refs?: Dict<Schema>
|
|
112
|
+
preserve?: boolean
|
|
113
|
+
'~standard': StandardSchemaV1.Props // <S, T>
|
|
114
|
+
toString(inline?: boolean): string
|
|
115
|
+
toJSON(): Schema<S, T>
|
|
116
|
+
required(value?: boolean): Schema<S, T>
|
|
117
|
+
hidden(value?: boolean): Schema<S, T>
|
|
118
|
+
loose(value?: boolean): Schema<S, T>
|
|
119
|
+
role(text: string, extra?: any): Schema<S, T>
|
|
120
|
+
link(link: string): Schema<S, T>
|
|
121
|
+
default(value: T): Schema<S, T>
|
|
122
|
+
comment(text: string): Schema<S, T>
|
|
123
|
+
description(text: string): Schema<S, T>
|
|
124
|
+
disabled(value?: boolean): Schema<S, T>
|
|
125
|
+
collapse(value?: boolean): Schema<S, T>
|
|
126
|
+
deprecated(): Schema<S, T>
|
|
127
|
+
experimental(): Schema<S, T>
|
|
128
|
+
pattern(regexp: RegExp): Schema<S, T>
|
|
129
|
+
max(value: number): Schema<S, T>
|
|
130
|
+
min(value: number): Schema<S, T>
|
|
131
|
+
step(value: number): Schema<S, T>
|
|
132
|
+
set(key: string, value: Schema): Schema<S, T>
|
|
133
|
+
push(value: Schema): Schema<S, T>
|
|
134
|
+
simplify(value?: any): any
|
|
135
|
+
i18n(messages: Dict): Schema<S, T>
|
|
136
|
+
extra<K extends keyof Schemastery.Meta>(key: K, value: Schemastery.Meta[K]): Schema<S, T>
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
declare namespace globalThis {
|
|
141
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
142
|
+
export let __schemastery_index__: number
|
|
143
|
+
export let __schemastery_refs__: Record<number, Schema> | undefined
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
globalThis.__schemastery_index__ ??= 0
|
|
147
|
+
globalThis.__schemastery_refs__ = undefined
|
|
148
|
+
|
|
149
|
+
class ValidationError extends TypeError {
|
|
150
|
+
name = 'ValidationError'
|
|
151
|
+
|
|
152
|
+
constructor(message: string, public options: Schemastery.Options) {
|
|
153
|
+
let prefix = '$'
|
|
154
|
+
for (const segment of options.path || []) {
|
|
155
|
+
if (typeof segment === 'string') {
|
|
156
|
+
prefix += '.' + segment
|
|
157
|
+
} else if (typeof segment === 'number') {
|
|
158
|
+
prefix += '[' + segment + ']'
|
|
159
|
+
} else if (typeof segment === 'symbol') {
|
|
160
|
+
prefix += `[Symbol(${segment.toString()})]`
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
if (prefix.startsWith('.')) prefix = prefix.slice(1)
|
|
164
|
+
super((prefix === '$' ? '' : `${prefix} `) + message)
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
static is(error: any): error is ValidationError {
|
|
168
|
+
return !!error?.[kValidationError]
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
Object.defineProperty(ValidationError.prototype, kValidationError, {
|
|
173
|
+
value: true,
|
|
174
|
+
})
|
|
175
|
+
|
|
176
|
+
type Schema<S = any, T = S> = Schemastery<S, T>
|
|
177
|
+
|
|
178
|
+
const Schema = function (options: Schema) {
|
|
179
|
+
const schema = function (data: any, options: Schemastery.Options = {}) {
|
|
180
|
+
return Schema.resolve(data, schema, options)[0]
|
|
181
|
+
} as Schema
|
|
182
|
+
|
|
183
|
+
if (options.refs) {
|
|
184
|
+
const refs = valueMap(options.refs, options => new Schema(options))
|
|
185
|
+
const getRef = (uid: any) => refs[uid]!
|
|
186
|
+
for (const key in refs) {
|
|
187
|
+
const options = refs[key]!
|
|
188
|
+
options.sKey = getRef(options.sKey)
|
|
189
|
+
options.inner = getRef(options.inner)
|
|
190
|
+
options.list = options.list && options.list.map(getRef)
|
|
191
|
+
options.dict = options.dict && valueMap(options.dict, getRef)
|
|
192
|
+
}
|
|
193
|
+
return refs[options.uid!]
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
Object.assign(schema, options)
|
|
197
|
+
if (typeof schema.callback === 'string') {
|
|
198
|
+
try {
|
|
199
|
+
// eslint-disable-next-line no-new-func
|
|
200
|
+
schema.callback = new Function('return ' + schema.callback)()
|
|
201
|
+
} catch {}
|
|
202
|
+
}
|
|
203
|
+
Object.defineProperty(schema, 'uid', { value: globalThis.__schemastery_index__++ })
|
|
204
|
+
Object.setPrototypeOf(schema, Schema.prototype)
|
|
205
|
+
schema.meta ||= {}
|
|
206
|
+
schema.toString = schema.toString.bind(schema)
|
|
207
|
+
return schema
|
|
208
|
+
} as Schemastery.Static
|
|
209
|
+
|
|
210
|
+
Schema.prototype = Object.create(Function.prototype)
|
|
211
|
+
|
|
212
|
+
Schema.prototype[kSchema] = true
|
|
213
|
+
|
|
214
|
+
Object.defineProperty(Schema.prototype, '~standard', {
|
|
215
|
+
get(this: Schema) {
|
|
216
|
+
return {
|
|
217
|
+
version: 1,
|
|
218
|
+
vendor: 'schemastery',
|
|
219
|
+
validate: (value: unknown) => {
|
|
220
|
+
try {
|
|
221
|
+
return { value: Schema.resolve(value, this, {})[0] }
|
|
222
|
+
} catch (error) {
|
|
223
|
+
if (ValidationError.is(error)) {
|
|
224
|
+
return { issues: [{ message: error.message, path: error.options.path }] }
|
|
225
|
+
}
|
|
226
|
+
throw error
|
|
227
|
+
}
|
|
228
|
+
},
|
|
229
|
+
}
|
|
230
|
+
},
|
|
231
|
+
})
|
|
232
|
+
|
|
233
|
+
Schema.ValidationError = ValidationError
|
|
234
|
+
|
|
235
|
+
Schema.prototype.toJSON = function toJSON() {
|
|
236
|
+
if (globalThis.__schemastery_refs__) {
|
|
237
|
+
globalThis.__schemastery_refs__[this.uid] ??= JSON.parse(JSON.stringify({ ...this }))
|
|
238
|
+
return this.uid as any
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
globalThis.__schemastery_refs__ = { [this.uid]: { ...this } as Schema }
|
|
242
|
+
globalThis.__schemastery_refs__[this.uid] = JSON.parse(JSON.stringify({ ...this }))
|
|
243
|
+
const result = { uid: this.uid, refs: globalThis.__schemastery_refs__ }
|
|
244
|
+
globalThis.__schemastery_refs__ = undefined
|
|
245
|
+
return result
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
Schema.prototype.set = function set(key, value) {
|
|
249
|
+
this.dict![key] = value
|
|
250
|
+
return this
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
Schema.prototype.push = function push(value) {
|
|
254
|
+
this.list!.push(value)
|
|
255
|
+
return this
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
function mergeDesc(original: undefined | string | Dict<string>, messages: Dict) {
|
|
259
|
+
const result: Dict<string> = typeof original === 'string' ? { '': original } : { ...original }
|
|
260
|
+
for (const locale in messages) {
|
|
261
|
+
const value = messages[locale]
|
|
262
|
+
if (value?.$description || value?.$desc) {
|
|
263
|
+
result[locale] = value.$description || value.$desc
|
|
264
|
+
} else if (typeof value === 'string') {
|
|
265
|
+
result[locale] = value
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
return result
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
function getInner(value: any) {
|
|
272
|
+
return value?.$value ?? value?.$inner
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
function extractKeys(data: any) {
|
|
276
|
+
return filterKeys(data ?? {}, key => !key.startsWith('$'))
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
Schema.prototype.i18n = function i18n(messages) {
|
|
280
|
+
const schema = Schema(this)
|
|
281
|
+
const desc = mergeDesc(schema.meta.description, messages)
|
|
282
|
+
if (Object.keys(desc).length) schema.meta.description = desc
|
|
283
|
+
if (schema.dict) {
|
|
284
|
+
schema.dict = valueMap(schema.dict, (inner, key) => {
|
|
285
|
+
return inner.i18n(valueMap(messages, (data) => getInner(data)?.[key] ?? data?.[key]))
|
|
286
|
+
})
|
|
287
|
+
}
|
|
288
|
+
if (schema.list) {
|
|
289
|
+
schema.list = schema.list!.map((inner, index) => {
|
|
290
|
+
return inner.i18n(valueMap(messages, (data = {}) => {
|
|
291
|
+
if (Array.isArray(getInner(data))) return getInner(data)[index]
|
|
292
|
+
if (Array.isArray(data)) return data[index]
|
|
293
|
+
return extractKeys(data)
|
|
294
|
+
}))
|
|
295
|
+
})
|
|
296
|
+
}
|
|
297
|
+
if (schema.inner) {
|
|
298
|
+
schema.inner = schema.inner.i18n(valueMap(messages, (data) => {
|
|
299
|
+
if (getInner(data)) return getInner(data)
|
|
300
|
+
return extractKeys(data)
|
|
301
|
+
}))
|
|
302
|
+
}
|
|
303
|
+
if (schema.sKey) {
|
|
304
|
+
schema.sKey = schema.sKey.i18n(valueMap(messages, (data) => data?.$key))
|
|
305
|
+
}
|
|
306
|
+
return schema
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
Schema.prototype.extra = function extra(key, value) {
|
|
310
|
+
const schema = Schema(this)
|
|
311
|
+
schema.meta = { ...schema.meta, [key]: value }
|
|
312
|
+
return schema
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
for (const key of ['required', 'disabled', 'collapse', 'hidden', 'loose']) {
|
|
316
|
+
Object.assign(Schema.prototype, {
|
|
317
|
+
[key](this: Schema, value = true) {
|
|
318
|
+
const schema = Schema(this)
|
|
319
|
+
schema.meta = { ...schema.meta, [key]: value }
|
|
320
|
+
return schema
|
|
321
|
+
},
|
|
322
|
+
})
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
Schema.prototype.deprecated = function deprecated() {
|
|
326
|
+
const schema = Schema(this)
|
|
327
|
+
schema.meta.badges ||= []
|
|
328
|
+
schema.meta.badges.push({ text: 'deprecated', type: 'danger' })
|
|
329
|
+
return schema
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
Schema.prototype.experimental = function experimental() {
|
|
333
|
+
const schema = Schema(this)
|
|
334
|
+
schema.meta.badges ||= []
|
|
335
|
+
schema.meta.badges.push({ text: 'experimental', type: 'warning' })
|
|
336
|
+
return schema
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
Schema.prototype.pattern = function pattern(regexp) {
|
|
340
|
+
const schema = Schema(this)
|
|
341
|
+
const pattern = pick(regexp, ['source', 'flags'])
|
|
342
|
+
schema.meta = { ...schema.meta, pattern }
|
|
343
|
+
return schema
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
Schema.prototype.simplify = function simplify(this: Schema, value) {
|
|
347
|
+
if (deepEqual(value, this.meta.default, this.type === 'dict')) return null
|
|
348
|
+
if (isNullable(value)) return value
|
|
349
|
+
if (this.type === 'object' || this.type === 'dict') {
|
|
350
|
+
const result: Dict = {}
|
|
351
|
+
for (const key in value) {
|
|
352
|
+
const schema = this.type === 'object' ? this.dict![key] : this.inner
|
|
353
|
+
const item = schema?.simplify(value[key])
|
|
354
|
+
if (this.type === 'dict' || !isNullable(item)) result[key] = item
|
|
355
|
+
}
|
|
356
|
+
if (deepEqual(result, this.meta.default, this.type === 'dict')) return null
|
|
357
|
+
return result
|
|
358
|
+
} else if (this.type === 'array' || this.type === 'tuple') {
|
|
359
|
+
const result: any[] = []
|
|
360
|
+
;(value as any[]).forEach((value, index) => {
|
|
361
|
+
const schema = this.type === 'array' ? this.inner : this.list![index]
|
|
362
|
+
const item = schema ? schema.simplify(value) : value
|
|
363
|
+
result.push(item)
|
|
364
|
+
})
|
|
365
|
+
return result
|
|
366
|
+
} else if (this.type === 'intersect') {
|
|
367
|
+
const result: Dict = {}
|
|
368
|
+
for (const item of this.list!) {
|
|
369
|
+
Object.assign(result, item.simplify(value))
|
|
370
|
+
}
|
|
371
|
+
return result
|
|
372
|
+
} else if (this.type === 'union') {
|
|
373
|
+
for (const schema of this.list!) {
|
|
374
|
+
try {
|
|
375
|
+
Schema.resolve(value, schema, {})
|
|
376
|
+
return schema.simplify(value)
|
|
377
|
+
} catch {}
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
return value
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
Schema.prototype.toString = function toString(inline?: boolean) {
|
|
384
|
+
return formatters[this.type]?.(this, inline) ?? `Schema<${this.type}>`
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
Schema.prototype.role = function role(role, extra) {
|
|
388
|
+
const schema = Schema(this)
|
|
389
|
+
schema.meta = { ...schema.meta, role, extra }
|
|
390
|
+
return schema
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
for (const key of ['default', 'link', 'comment', 'description', 'max', 'min', 'step']) {
|
|
394
|
+
Object.assign(Schema.prototype, {
|
|
395
|
+
[key](this: Schema, value: any) {
|
|
396
|
+
const schema = Schema(this)
|
|
397
|
+
schema.meta = { ...schema.meta, [key]: value }
|
|
398
|
+
return schema
|
|
399
|
+
},
|
|
400
|
+
})
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
const resolvers: Dict<Schemastery.Resolve> = {}
|
|
404
|
+
|
|
405
|
+
Schema.extend = function extend(type, resolve) {
|
|
406
|
+
resolvers[type] = resolve
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
Schema.resolve = function resolve(data, schema, options = {}, strict = false) {
|
|
410
|
+
if (!schema) return [data]
|
|
411
|
+
if (options.ignore?.(data, schema)) return [data]
|
|
412
|
+
|
|
413
|
+
if (isNullable(data) && schema.type !== 'lazy') {
|
|
414
|
+
if (schema.meta.required) throw new ValidationError(`missing required value`, options)
|
|
415
|
+
let current = schema
|
|
416
|
+
let fallback = schema.meta.default
|
|
417
|
+
while (current?.type === 'intersect' && isNullable(fallback)) {
|
|
418
|
+
current = current.list![0]
|
|
419
|
+
fallback = current?.meta.default
|
|
420
|
+
}
|
|
421
|
+
if (isNullable(fallback)) return [data]
|
|
422
|
+
data = clone(fallback)
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
const callback = resolvers[schema.type]
|
|
426
|
+
if (!callback) throw new ValidationError(`unsupported type "${schema.type}"`, options)
|
|
427
|
+
|
|
428
|
+
try {
|
|
429
|
+
return callback(data, schema, options, strict)
|
|
430
|
+
} catch (error) {
|
|
431
|
+
if (!schema.meta.loose) throw error
|
|
432
|
+
return [schema.meta.default]
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
Schema.from = function from(source: any) {
|
|
437
|
+
if (isNullable(source)) {
|
|
438
|
+
return Schema.any()
|
|
439
|
+
} else if (['string', 'number', 'boolean'].includes(typeof source)) {
|
|
440
|
+
return Schema.const(source).required()
|
|
441
|
+
} else if (source[kSchema]) {
|
|
442
|
+
return source
|
|
443
|
+
} else if (typeof source === 'function') {
|
|
444
|
+
switch (source) {
|
|
445
|
+
case String: return Schema.string().required()
|
|
446
|
+
case Number: return Schema.number().required()
|
|
447
|
+
case Boolean: return Schema.boolean().required()
|
|
448
|
+
case Function: return Schema.function().required()
|
|
449
|
+
default: return Schema.is(source).required()
|
|
450
|
+
}
|
|
451
|
+
} else {
|
|
452
|
+
throw new TypeError(`cannot infer schema from ${source}`)
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
Schema.lazy = function lazy(builder) {
|
|
457
|
+
const toJSON = () => {
|
|
458
|
+
if (!schema.inner![kSchema]) {
|
|
459
|
+
schema.inner = schema.builder!()
|
|
460
|
+
schema.inner!.meta = { ...schema.meta, ...schema.inner!.meta }
|
|
461
|
+
}
|
|
462
|
+
return schema.inner!.toJSON()
|
|
463
|
+
}
|
|
464
|
+
const schema = new Schema({ type: 'lazy', builder, inner: { toJSON } as any })
|
|
465
|
+
return schema as any
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
Schema.natural = function natural() {
|
|
469
|
+
return Schema.number().step(1).min(0)
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
Schema.percent = function percent() {
|
|
473
|
+
return Schema.number().step(0.01).min(0).max(1).role('slider')
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
Schema.date = function date() {
|
|
477
|
+
return Schema.union([
|
|
478
|
+
Schema.is(Date),
|
|
479
|
+
Schema.transform(Schema.string().role('datetime'), (value, options) => {
|
|
480
|
+
const date = new Date(value)
|
|
481
|
+
if (isNaN(+date)) throw new ValidationError(`invalid date "${value}"`, options)
|
|
482
|
+
return date
|
|
483
|
+
}, true),
|
|
484
|
+
])
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
Schema.regExp = function regExp(flag = '') {
|
|
488
|
+
return Schema.union([
|
|
489
|
+
Schema.is(RegExp),
|
|
490
|
+
Schema.transform(Schema.string().role('regexp', { flag }), (value, options) => {
|
|
491
|
+
try {
|
|
492
|
+
return new RegExp(value, flag)
|
|
493
|
+
} catch (e: any) {
|
|
494
|
+
throw new ValidationError(e.message, options)
|
|
495
|
+
}
|
|
496
|
+
}, true),
|
|
497
|
+
])
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
Schema.arrayBuffer = function arrayBuffer(encoding?: 'hex' | 'base64'): any {
|
|
501
|
+
return Schema.union([
|
|
502
|
+
Schema.is(ArrayBuffer),
|
|
503
|
+
Schema.is(SharedArrayBuffer),
|
|
504
|
+
Schema.transform(Schema.any<ArrayBufferView>(), (value, options) => {
|
|
505
|
+
if (Binary.isSource(value)) return Binary.fromSource(value)
|
|
506
|
+
throw new ValidationError(`expected ArrayBufferSource but got ${value}`, options)
|
|
507
|
+
}, true),
|
|
508
|
+
...encoding ? [Schema.transform(Schema.string(), (value, options) => {
|
|
509
|
+
try {
|
|
510
|
+
return encoding === 'base64'
|
|
511
|
+
? Binary.fromBase64(value)
|
|
512
|
+
: Binary.fromHex(value)
|
|
513
|
+
} catch (e: any) {
|
|
514
|
+
throw new ValidationError(e.message, options)
|
|
515
|
+
}
|
|
516
|
+
}, true)] as const : [],
|
|
517
|
+
])
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
Schema.extend('lazy', (data, schema, options, strict) => {
|
|
521
|
+
if (!schema.inner![kSchema]) {
|
|
522
|
+
schema.inner = schema.builder!()
|
|
523
|
+
schema.inner!.meta = { ...schema.meta, ...schema.inner!.meta }
|
|
524
|
+
}
|
|
525
|
+
return Schema.resolve(data, schema.inner!, options, strict)
|
|
526
|
+
})
|
|
527
|
+
|
|
528
|
+
Schema.extend('any', (data) => {
|
|
529
|
+
return [data]
|
|
530
|
+
})
|
|
531
|
+
|
|
532
|
+
Schema.extend('never', (data, _, options) => {
|
|
533
|
+
throw new ValidationError(`expected nullable but got ${data}`, options)
|
|
534
|
+
})
|
|
535
|
+
|
|
536
|
+
Schema.extend('const', (data, { value }, options) => {
|
|
537
|
+
if (deepEqual(data, value)) return [value]
|
|
538
|
+
throw new ValidationError(`expected ${value} but got ${data}`, options)
|
|
539
|
+
})
|
|
540
|
+
|
|
541
|
+
function checkWithinRange(data: number, meta: Schemastery.Meta<any>, description: string, options: Schemastery.Options, skipMin = false) {
|
|
542
|
+
const { max = Infinity, min = -Infinity } = meta
|
|
543
|
+
if (data > max) throw new ValidationError(`expected ${description} <= ${max} but got ${data}`, options)
|
|
544
|
+
if (data < min && !skipMin) throw new ValidationError(`expected ${description} >= ${min} but got ${data}`, options)
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
Schema.extend('string', (data, { meta }, options) => {
|
|
548
|
+
if (typeof data !== 'string') throw new ValidationError(`expected string but got ${data}`, options)
|
|
549
|
+
if (meta.pattern) {
|
|
550
|
+
const regexp = new RegExp(meta.pattern.source, meta.pattern.flags)
|
|
551
|
+
if (!regexp.test(data)) throw new ValidationError(`expect string to match regexp ${regexp}`, options)
|
|
552
|
+
}
|
|
553
|
+
checkWithinRange(data.length, meta, 'string length', options)
|
|
554
|
+
return [data]
|
|
555
|
+
})
|
|
556
|
+
|
|
557
|
+
function decimalShift(data: number, digits: number) {
|
|
558
|
+
const str = data.toString()
|
|
559
|
+
if (str.includes('e')) return data * Math.pow(10, digits)
|
|
560
|
+
const index = str.indexOf('.')
|
|
561
|
+
if (index === -1) return data * Math.pow(10, digits)
|
|
562
|
+
const frac = str.slice(index + 1)
|
|
563
|
+
const integer = str.slice(0, index)
|
|
564
|
+
if (frac.length <= digits) return +(integer + frac.padEnd(digits, '0'))
|
|
565
|
+
return +(integer + frac.slice(0, digits) + '.' + frac.slice(digits))
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
function isMultipleOf(data: number, min: number, step: number) {
|
|
569
|
+
step = Math.abs(step)
|
|
570
|
+
if (!/^\d+\.\d+$/.test(step.toString())) {
|
|
571
|
+
return (data - min) % step === 0
|
|
572
|
+
}
|
|
573
|
+
const index = step.toString().indexOf('.')
|
|
574
|
+
const digits = step.toString().slice(index + 1).length
|
|
575
|
+
return Math.abs(decimalShift(data, digits) - decimalShift(min, digits)) % decimalShift(step, digits) === 0
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
Schema.extend('number', (data, { meta }, options) => {
|
|
579
|
+
if (typeof data !== 'number') throw new ValidationError(`expected number but got ${data}`, options)
|
|
580
|
+
checkWithinRange(data, meta, 'number', options)
|
|
581
|
+
const { step } = meta
|
|
582
|
+
if (step && !isMultipleOf(data, meta.min ?? 0, step)) {
|
|
583
|
+
throw new ValidationError(`expected number multiple of ${step} but got ${data}`, options)
|
|
584
|
+
}
|
|
585
|
+
return [data]
|
|
586
|
+
})
|
|
587
|
+
|
|
588
|
+
Schema.extend('boolean', (data, _, options) => {
|
|
589
|
+
if (typeof data === 'boolean') return [data]
|
|
590
|
+
throw new ValidationError(`expected boolean but got ${data}`, options)
|
|
591
|
+
})
|
|
592
|
+
|
|
593
|
+
Schema.extend('bitset', (data, { bits, meta }, options) => {
|
|
594
|
+
let value = 0, keys: string[] = []
|
|
595
|
+
if (typeof data === 'number') {
|
|
596
|
+
value = data
|
|
597
|
+
for (const key in bits!) {
|
|
598
|
+
if (data & bits![key]!) {
|
|
599
|
+
keys.push(key)
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
} else if (Array.isArray(data)) {
|
|
603
|
+
keys = data
|
|
604
|
+
for (const key of keys) {
|
|
605
|
+
if (typeof key !== 'string') throw new ValidationError(`expected string but got ${key}`, options)
|
|
606
|
+
if (key in bits!) value |= bits![key]!
|
|
607
|
+
}
|
|
608
|
+
} else {
|
|
609
|
+
throw new ValidationError(`expected number or array but got ${data}`, options)
|
|
610
|
+
}
|
|
611
|
+
if (value === meta.default) return [value]
|
|
612
|
+
return [value, keys]
|
|
613
|
+
})
|
|
614
|
+
|
|
615
|
+
Schema.extend('function', (data, _, options) => {
|
|
616
|
+
if (typeof data === 'function') return [data]
|
|
617
|
+
throw new ValidationError(`expected function but got ${data}`, options)
|
|
618
|
+
})
|
|
619
|
+
|
|
620
|
+
Schema.extend('is', (data, { constructor }, options) => {
|
|
621
|
+
if (typeof constructor === 'function') {
|
|
622
|
+
if (data instanceof constructor) return [data]
|
|
623
|
+
throw new ValidationError(`expected ${constructor.name} but got ${data}`, options)
|
|
624
|
+
} else {
|
|
625
|
+
if (isNullable(data)) {
|
|
626
|
+
throw new ValidationError(`expected ${constructor} but got ${data}`, options)
|
|
627
|
+
}
|
|
628
|
+
let prototype = Object.getPrototypeOf(data)
|
|
629
|
+
while (prototype) {
|
|
630
|
+
if (prototype.constructor?.name === constructor) return [data]
|
|
631
|
+
prototype = Object.getPrototypeOf(prototype)
|
|
632
|
+
}
|
|
633
|
+
throw new ValidationError(`expected ${constructor} but got ${data}`, options)
|
|
634
|
+
}
|
|
635
|
+
})
|
|
636
|
+
|
|
637
|
+
function property(data: any, key: keyof any, schema: Schema, options: Schemastery.Options) {
|
|
638
|
+
try {
|
|
639
|
+
const [value, adapted] = Schema.resolve(data[key], schema, {
|
|
640
|
+
...options,
|
|
641
|
+
path: [...options.path || [], key],
|
|
642
|
+
})
|
|
643
|
+
if (adapted !== undefined) data[key] = adapted
|
|
644
|
+
return value
|
|
645
|
+
} catch (e) {
|
|
646
|
+
if (!options?.autofix) throw e
|
|
647
|
+
delete data[key]
|
|
648
|
+
return schema.meta.default
|
|
649
|
+
}
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
Schema.extend('array', (data, { inner, meta }, options) => {
|
|
653
|
+
if (!Array.isArray(data)) throw new ValidationError(`expected array but got ${data}`, options)
|
|
654
|
+
checkWithinRange(data.length, meta, 'array length', options, !isNullable(inner!.meta.default))
|
|
655
|
+
return [data.map((_, index) => property(data, index, inner!, options))]
|
|
656
|
+
})
|
|
657
|
+
|
|
658
|
+
Schema.extend('dict', (data, { inner, sKey }, options, strict) => {
|
|
659
|
+
if (!isPlainObject(data)) throw new ValidationError(`expected object but got ${data}`, options)
|
|
660
|
+
const result: any = {}
|
|
661
|
+
for (const key in data) {
|
|
662
|
+
let rKey: string
|
|
663
|
+
try {
|
|
664
|
+
rKey = Schema.resolve(key, sKey!, options)[0]
|
|
665
|
+
} catch (error) {
|
|
666
|
+
if (strict) continue
|
|
667
|
+
throw error
|
|
668
|
+
}
|
|
669
|
+
result[rKey] = property(data, key, inner!, options)
|
|
670
|
+
data[rKey] = data[key]
|
|
671
|
+
if (key !== rKey) delete data[key]
|
|
672
|
+
}
|
|
673
|
+
return [result]
|
|
674
|
+
})
|
|
675
|
+
|
|
676
|
+
Schema.extend('tuple', (data, { list }, options, strict) => {
|
|
677
|
+
if (!Array.isArray(data)) throw new ValidationError(`expected array but got ${data}`, options)
|
|
678
|
+
const result = list!.map((inner, index) => property(data, index, inner, options))
|
|
679
|
+
if (strict) return [result]
|
|
680
|
+
result.push(...data.slice(list!.length))
|
|
681
|
+
return [result]
|
|
682
|
+
})
|
|
683
|
+
|
|
684
|
+
function merge(result: any, data: any) {
|
|
685
|
+
for (const key in data) {
|
|
686
|
+
if (key in result) continue
|
|
687
|
+
result[key] = data[key]
|
|
688
|
+
}
|
|
689
|
+
}
|
|
690
|
+
|
|
691
|
+
Schema.extend('object', (data, { dict }, options, strict) => {
|
|
692
|
+
if (!isPlainObject(data)) throw new ValidationError(`expected object but got ${data}`, options)
|
|
693
|
+
const result: any = {}
|
|
694
|
+
for (const key in dict) {
|
|
695
|
+
const value = property(data, key, dict![key]!, options)
|
|
696
|
+
if (!isNullable(value) || key in data) {
|
|
697
|
+
result[key] = value
|
|
698
|
+
}
|
|
699
|
+
}
|
|
700
|
+
if (!strict) merge(result, data)
|
|
701
|
+
return [result]
|
|
702
|
+
})
|
|
703
|
+
|
|
704
|
+
Schema.extend('union', (data, { list, toString }, options, strict) => {
|
|
705
|
+
const messages: any[] = []
|
|
706
|
+
for (const inner of list!) {
|
|
707
|
+
try {
|
|
708
|
+
return Schema.resolve(data, inner, options, strict)
|
|
709
|
+
} catch (error) {
|
|
710
|
+
messages.push(error)
|
|
711
|
+
}
|
|
712
|
+
}
|
|
713
|
+
throw new ValidationError(`expected ${toString()} but got ${JSON.stringify(data)}`, options)
|
|
714
|
+
})
|
|
715
|
+
|
|
716
|
+
Schema.extend('intersect', (data, { list, toString }, options, strict) => {
|
|
717
|
+
if (!list!.length) return [data]
|
|
718
|
+
let result
|
|
719
|
+
for (const inner of list!) {
|
|
720
|
+
const value: any = Schema.resolve(data, inner, options, true)[0]
|
|
721
|
+
if (isNullable(value)) continue
|
|
722
|
+
if (isNullable(result)) {
|
|
723
|
+
result = value
|
|
724
|
+
} else if (typeof result !== typeof value) {
|
|
725
|
+
throw new ValidationError(`expected ${toString()} but got ${JSON.stringify(data)}`, options)
|
|
726
|
+
} else if (typeof value === 'object') {
|
|
727
|
+
merge(result ??= {}, value)
|
|
728
|
+
} else if (result !== value) {
|
|
729
|
+
throw new ValidationError(`expected ${toString()} but got ${JSON.stringify(data)}`, options)
|
|
730
|
+
}
|
|
731
|
+
}
|
|
732
|
+
if (!strict && isPlainObject(data)) merge(result, data)
|
|
733
|
+
return [result]
|
|
734
|
+
})
|
|
735
|
+
|
|
736
|
+
Schema.extend('transform', (data, { inner, callback, preserve }, options) => {
|
|
737
|
+
const [result, adapted = data] = Schema.resolve(data, inner!, options, true)
|
|
738
|
+
if (preserve) {
|
|
739
|
+
return [callback!(result)]
|
|
740
|
+
// } else if (isPlainObject(data)) {
|
|
741
|
+
// const temp: any = {}
|
|
742
|
+
// for (const key in result) {
|
|
743
|
+
// if (!(key in data)) continue
|
|
744
|
+
// temp[key] = data[key]
|
|
745
|
+
// delete data[key]
|
|
746
|
+
// }
|
|
747
|
+
// Object.assign(data, callback!(temp))
|
|
748
|
+
// return [callback!(result)]
|
|
749
|
+
} else {
|
|
750
|
+
return [callback!(result), callback!(adapted)]
|
|
751
|
+
}
|
|
752
|
+
})
|
|
753
|
+
|
|
754
|
+
type Formatter = (schema: Schema, inline?: boolean) => string
|
|
755
|
+
const formatters: Dict<Formatter> = {}
|
|
756
|
+
|
|
757
|
+
function defineMethod(name: string, keys: (keyof Schema)[], format: Formatter) {
|
|
758
|
+
formatters[name] = format
|
|
759
|
+
Object.assign(Schema, {
|
|
760
|
+
[name](...args: any[]) {
|
|
761
|
+
const schema = new Schema({ type: name } as Schema)
|
|
762
|
+
keys.forEach((key, index) => {
|
|
763
|
+
switch (key) {
|
|
764
|
+
case 'sKey': schema.sKey = args[index] ?? Schema.string(); break
|
|
765
|
+
case 'inner': schema.inner = Schema.from(args[index]); break
|
|
766
|
+
case 'list': schema.list = args[index].map(Schema.from); break
|
|
767
|
+
case 'dict': schema.dict = valueMap(args[index], Schema.from); break
|
|
768
|
+
case 'bits': {
|
|
769
|
+
schema.bits = {}
|
|
770
|
+
for (const key in args[index]) {
|
|
771
|
+
if (typeof args[index][key] !== 'number') continue
|
|
772
|
+
schema.bits[key] = args[index][key]
|
|
773
|
+
}
|
|
774
|
+
break
|
|
775
|
+
}
|
|
776
|
+
case 'callback': {
|
|
777
|
+
const callback = schema.callback = args[index]
|
|
778
|
+
;callback['toJSON'] ||= () => callback.toString()
|
|
779
|
+
break
|
|
780
|
+
}
|
|
781
|
+
case 'constructor': {
|
|
782
|
+
const constructor = schema.constructor = args[index]
|
|
783
|
+
if (typeof constructor === 'function') {
|
|
784
|
+
;constructor['toJSON'] ||= () => constructor['name']
|
|
785
|
+
}
|
|
786
|
+
break
|
|
787
|
+
}
|
|
788
|
+
default: schema[key] = args[index] as never
|
|
789
|
+
}
|
|
790
|
+
})
|
|
791
|
+
if (name === 'object' || name === 'dict') {
|
|
792
|
+
schema.meta.default = {}
|
|
793
|
+
} else if (name === 'array' || name === 'tuple') {
|
|
794
|
+
schema.meta.default = []
|
|
795
|
+
} else if (name === 'bitset') {
|
|
796
|
+
schema.meta.default = 0
|
|
797
|
+
}
|
|
798
|
+
return schema
|
|
799
|
+
},
|
|
800
|
+
})
|
|
801
|
+
}
|
|
802
|
+
|
|
803
|
+
defineMethod('is', ['constructor'], ({ constructor }) => {
|
|
804
|
+
if (typeof constructor === 'function') {
|
|
805
|
+
return constructor.name
|
|
806
|
+
} else {
|
|
807
|
+
return constructor!
|
|
808
|
+
}
|
|
809
|
+
})
|
|
810
|
+
|
|
811
|
+
defineMethod('any', [], () => 'any')
|
|
812
|
+
defineMethod('never', [], () => 'never')
|
|
813
|
+
defineMethod('const', ['value'], ({ value }) => typeof value === 'string' ? JSON.stringify(value) : value)
|
|
814
|
+
defineMethod('string', [], () => 'string')
|
|
815
|
+
defineMethod('number', [], () => 'number')
|
|
816
|
+
defineMethod('boolean', [], () => 'boolean')
|
|
817
|
+
defineMethod('bitset', ['bits'], () => 'bitset')
|
|
818
|
+
defineMethod('function', [], () => 'function')
|
|
819
|
+
defineMethod('array', ['inner'], ({ inner }) => `${inner!.toString(true)}[]`)
|
|
820
|
+
defineMethod('dict', ['inner', 'sKey'], ({ inner, sKey }) => `{ [key: ${sKey!.toString()}]: ${inner!.toString()} }`)
|
|
821
|
+
defineMethod('tuple', ['list'], ({ list }) => `[${list!.map((inner) => inner.toString()).join(', ')}]`)
|
|
822
|
+
|
|
823
|
+
defineMethod('object', ['dict'], ({ dict }) => {
|
|
824
|
+
if (Object.keys(dict!).length === 0) return '{}'
|
|
825
|
+
return `{ ${Object.entries(dict!).map(([key, inner]) => {
|
|
826
|
+
return `${key}${inner!.meta.required ? '' : '?'}: ${inner!.toString()}`
|
|
827
|
+
}).join(', ')} }`
|
|
828
|
+
})
|
|
829
|
+
|
|
830
|
+
defineMethod('union', ['list'], ({ list }, inline) => {
|
|
831
|
+
const result = list!.map(({ toString: format }) => format()).join(' | ')
|
|
832
|
+
return inline ? `(${result})` : result
|
|
833
|
+
})
|
|
834
|
+
|
|
835
|
+
defineMethod('intersect', ['list'], ({ list }) => {
|
|
836
|
+
return `${list!.map((inner) => inner.toString(true)).join(' & ')}`
|
|
837
|
+
})
|
|
838
|
+
|
|
839
|
+
defineMethod('transform', ['inner', 'callback', 'preserve'], ({ inner }, isInner) => inner!.toString(isInner))
|
|
840
|
+
|
|
841
|
+
export = Schema
|