effect 4.0.0-beta.30 → 4.0.0-beta.32
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/Array.d.ts +87 -86
- package/dist/Array.d.ts.map +1 -1
- package/dist/Array.js +47 -40
- package/dist/Array.js.map +1 -1
- package/dist/BigDecimal.d.ts +46 -45
- package/dist/BigDecimal.d.ts.map +1 -1
- package/dist/BigDecimal.js +35 -38
- package/dist/BigDecimal.js.map +1 -1
- package/dist/BigInt.d.ts +39 -32
- package/dist/BigInt.d.ts.map +1 -1
- package/dist/BigInt.js +32 -29
- package/dist/BigInt.js.map +1 -1
- package/dist/Channel.d.ts +2 -2
- package/dist/Channel.d.ts.map +1 -1
- package/dist/Channel.js.map +1 -1
- package/dist/Chunk.d.ts +58 -57
- package/dist/Chunk.d.ts.map +1 -1
- package/dist/Chunk.js +20 -21
- package/dist/Chunk.js.map +1 -1
- package/dist/Config.d.ts.map +1 -1
- package/dist/Config.js +4 -1
- package/dist/Config.js.map +1 -1
- package/dist/Cron.d.ts +3 -6
- package/dist/Cron.d.ts.map +1 -1
- package/dist/Cron.js +11 -18
- package/dist/Cron.js.map +1 -1
- package/dist/DateTime.d.ts +49 -92
- package/dist/DateTime.d.ts.map +1 -1
- package/dist/DateTime.js +9 -9
- package/dist/DateTime.js.map +1 -1
- package/dist/Deferred.d.ts +7 -5
- package/dist/Deferred.d.ts.map +1 -1
- package/dist/Deferred.js +7 -5
- package/dist/Deferred.js.map +1 -1
- package/dist/Duration.d.ts +55 -25
- package/dist/Duration.d.ts.map +1 -1
- package/dist/Duration.js +100 -70
- package/dist/Duration.js.map +1 -1
- package/dist/Effect.d.ts.map +1 -1
- package/dist/Effect.js +3 -4
- package/dist/Effect.js.map +1 -1
- package/dist/Fiber.d.ts +2 -1
- package/dist/Fiber.d.ts.map +1 -1
- package/dist/Fiber.js.map +1 -1
- package/dist/FiberHandle.d.ts +9 -8
- package/dist/FiberHandle.d.ts.map +1 -1
- package/dist/FiberHandle.js +8 -7
- package/dist/FiberHandle.js.map +1 -1
- package/dist/FiberMap.d.ts +20 -19
- package/dist/FiberMap.d.ts.map +1 -1
- package/dist/FiberMap.js +6 -6
- package/dist/FiberMap.js.map +1 -1
- package/dist/FileSystem.d.ts +17 -16
- package/dist/FileSystem.d.ts.map +1 -1
- package/dist/FileSystem.js +10 -7
- package/dist/FileSystem.js.map +1 -1
- package/dist/Graph.d.ts +64 -64
- package/dist/Graph.d.ts.map +1 -1
- package/dist/Graph.js +53 -53
- package/dist/Graph.js.map +1 -1
- package/dist/HashMap.d.ts +11 -5
- package/dist/HashMap.d.ts.map +1 -1
- package/dist/HashMap.js +3 -1
- package/dist/HashMap.js.map +1 -1
- package/dist/Iterable.d.ts +11 -11
- package/dist/Iterable.d.ts.map +1 -1
- package/dist/Iterable.js +12 -12
- package/dist/Iterable.js.map +1 -1
- package/dist/Latch.d.ts +69 -3
- package/dist/Latch.d.ts.map +1 -1
- package/dist/Latch.js +58 -0
- package/dist/Latch.js.map +1 -1
- package/dist/Layer.d.ts +181 -2
- package/dist/Layer.d.ts.map +1 -1
- package/dist/Layer.js +35 -2
- package/dist/Layer.js.map +1 -1
- package/dist/Number.d.ts +74 -18
- package/dist/Number.d.ts.map +1 -1
- package/dist/Number.js +34 -15
- package/dist/Number.js.map +1 -1
- package/dist/Option.d.ts +2 -0
- package/dist/Option.d.ts.map +1 -1
- package/dist/Option.js.map +1 -1
- package/dist/PartitionedSemaphore.d.ts +183 -0
- package/dist/PartitionedSemaphore.d.ts.map +1 -0
- package/dist/PartitionedSemaphore.js +231 -0
- package/dist/PartitionedSemaphore.js.map +1 -0
- package/dist/PubSub.d.ts +5 -4
- package/dist/PubSub.d.ts.map +1 -1
- package/dist/PubSub.js +6 -5
- package/dist/PubSub.js.map +1 -1
- package/dist/Queue.d.ts +3 -3
- package/dist/Queue.d.ts.map +1 -1
- package/dist/Queue.js +2 -2
- package/dist/Queue.js.map +1 -1
- package/dist/Record.d.ts +40 -40
- package/dist/Record.d.ts.map +1 -1
- package/dist/Record.js +16 -15
- package/dist/Record.js.map +1 -1
- package/dist/Scheduler.d.ts +25 -123
- package/dist/Scheduler.d.ts.map +1 -1
- package/dist/Scheduler.js +20 -123
- package/dist/Scheduler.js.map +1 -1
- package/dist/Schema.d.ts +2346 -114
- package/dist/Schema.d.ts.map +1 -1
- package/dist/Schema.js +1359 -68
- package/dist/Schema.js.map +1 -1
- package/dist/SchemaAST.d.ts +2 -2
- package/dist/SchemaAST.js +2 -2
- package/dist/SchemaGetter.js +6 -4
- package/dist/SchemaGetter.js.map +1 -1
- package/dist/SchemaRepresentation.d.ts +2 -2
- package/dist/SchemaRepresentation.d.ts.map +1 -1
- package/dist/SchemaRepresentation.js.map +1 -1
- package/dist/SchemaTransformation.d.ts.map +1 -1
- package/dist/SchemaTransformation.js +32 -27
- package/dist/SchemaTransformation.js.map +1 -1
- package/dist/Semaphore.d.ts +149 -53
- package/dist/Semaphore.d.ts.map +1 -1
- package/dist/Semaphore.js +137 -117
- package/dist/Semaphore.js.map +1 -1
- package/dist/ServiceMap.d.ts +26 -0
- package/dist/ServiceMap.d.ts.map +1 -1
- package/dist/ServiceMap.js +36 -21
- package/dist/ServiceMap.js.map +1 -1
- package/dist/Stream.js +1 -1
- package/dist/Stream.js.map +1 -1
- package/dist/String.d.ts +114 -47
- package/dist/String.d.ts.map +1 -1
- package/dist/String.js +29 -47
- package/dist/String.js.map +1 -1
- package/dist/SubscriptionRef.d.ts +1 -2
- package/dist/SubscriptionRef.d.ts.map +1 -1
- package/dist/SubscriptionRef.js +81 -115
- package/dist/SubscriptionRef.js.map +1 -1
- package/dist/Terminal.d.ts +2 -1
- package/dist/Terminal.d.ts.map +1 -1
- package/dist/Terminal.js.map +1 -1
- package/dist/Tracer.d.ts +5 -4
- package/dist/Tracer.d.ts.map +1 -1
- package/dist/Tracer.js +2 -1
- package/dist/Tracer.js.map +1 -1
- package/dist/Trie.d.ts +26 -14
- package/dist/Trie.d.ts.map +1 -1
- package/dist/Trie.js +8 -4
- package/dist/Trie.js.map +1 -1
- package/dist/TxHashMap.d.ts +77 -59
- package/dist/TxHashMap.d.ts.map +1 -1
- package/dist/TxHashMap.js +25 -19
- package/dist/TxHashMap.js.map +1 -1
- package/dist/TxSemaphore.d.ts +162 -2
- package/dist/TxSemaphore.d.ts.map +1 -1
- package/dist/TxSemaphore.js +16 -2
- package/dist/TxSemaphore.js.map +1 -1
- package/dist/index.d.ts +88 -32
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +88 -32
- package/dist/index.js.map +1 -1
- package/dist/internal/dateTime.js +66 -59
- package/dist/internal/dateTime.js.map +1 -1
- package/dist/internal/effect.js +21 -90
- package/dist/internal/effect.js.map +1 -1
- package/dist/internal/hashMap.js +2 -1
- package/dist/internal/hashMap.js.map +1 -1
- package/dist/internal/option.js +6 -0
- package/dist/internal/option.js.map +1 -1
- package/dist/internal/trie.js +3 -3
- package/dist/internal/trie.js.map +1 -1
- package/dist/unstable/ai/AiError.js +1 -1
- package/dist/unstable/ai/AiError.js.map +1 -1
- package/dist/unstable/ai/AnthropicStructuredOutput.d.ts.map +1 -1
- package/dist/unstable/ai/AnthropicStructuredOutput.js +4 -0
- package/dist/unstable/ai/AnthropicStructuredOutput.js.map +1 -1
- package/dist/unstable/ai/Chat.d.ts +3 -4
- package/dist/unstable/ai/Chat.d.ts.map +1 -1
- package/dist/unstable/ai/Chat.js +2 -2
- package/dist/unstable/ai/Chat.js.map +1 -1
- package/dist/unstable/ai/LanguageModel.d.ts +30 -16
- package/dist/unstable/ai/LanguageModel.d.ts.map +1 -1
- package/dist/unstable/ai/LanguageModel.js +10 -87
- package/dist/unstable/ai/LanguageModel.js.map +1 -1
- package/dist/unstable/ai/OpenAiStructuredOutput.d.ts.map +1 -1
- package/dist/unstable/ai/OpenAiStructuredOutput.js +4 -0
- package/dist/unstable/ai/OpenAiStructuredOutput.js.map +1 -1
- package/dist/unstable/ai/Tool.d.ts +14 -0
- package/dist/unstable/ai/Tool.d.ts.map +1 -1
- package/dist/unstable/ai/Tool.js +13 -0
- package/dist/unstable/ai/Tool.js.map +1 -1
- package/dist/unstable/cli/Argument.d.ts +2 -4
- package/dist/unstable/cli/Argument.d.ts.map +1 -1
- package/dist/unstable/cli/Argument.js +1 -3
- package/dist/unstable/cli/Argument.js.map +1 -1
- package/dist/unstable/cli/CliOutput.d.ts +3 -2
- package/dist/unstable/cli/CliOutput.d.ts.map +1 -1
- package/dist/unstable/cli/CliOutput.js +4 -3
- package/dist/unstable/cli/CliOutput.js.map +1 -1
- package/dist/unstable/cli/Command.d.ts +3 -2
- package/dist/unstable/cli/Command.d.ts.map +1 -1
- package/dist/unstable/cli/Command.js +3 -3
- package/dist/unstable/cli/Command.js.map +1 -1
- package/dist/unstable/cli/Completions.d.ts +16 -0
- package/dist/unstable/cli/Completions.d.ts.map +1 -0
- package/dist/unstable/cli/Completions.js +23 -0
- package/dist/unstable/cli/Completions.js.map +1 -0
- package/dist/unstable/cli/Flag.d.ts +2 -2
- package/dist/unstable/cli/Flag.d.ts.map +1 -1
- package/dist/unstable/cli/Flag.js +1 -1
- package/dist/unstable/cli/Flag.js.map +1 -1
- package/dist/unstable/cli/GlobalFlag.d.ts.map +1 -1
- package/dist/unstable/cli/GlobalFlag.js +4 -4
- package/dist/unstable/cli/GlobalFlag.js.map +1 -1
- package/dist/unstable/cli/HelpDoc.d.ts +14 -12
- package/dist/unstable/cli/HelpDoc.d.ts.map +1 -1
- package/dist/unstable/cli/Param.d.ts +4 -4
- package/dist/unstable/cli/Param.d.ts.map +1 -1
- package/dist/unstable/cli/Param.js +7 -7
- package/dist/unstable/cli/Param.js.map +1 -1
- package/dist/unstable/cli/Primitive.d.ts +1 -1
- package/dist/unstable/cli/Primitive.d.ts.map +1 -1
- package/dist/unstable/cli/Primitive.js.map +1 -1
- package/dist/unstable/cli/Prompt.js +71 -67
- package/dist/unstable/cli/Prompt.js.map +1 -1
- package/dist/unstable/cli/index.d.ts +4 -0
- package/dist/unstable/cli/index.d.ts.map +1 -1
- package/dist/unstable/cli/index.js +4 -0
- package/dist/unstable/cli/index.js.map +1 -1
- package/dist/unstable/cli/internal/completions/CommandDescriptor.js +9 -2
- package/dist/unstable/cli/internal/completions/CommandDescriptor.js.map +1 -1
- package/dist/unstable/cli/internal/parser.js +8 -3
- package/dist/unstable/cli/internal/parser.js.map +1 -1
- package/dist/unstable/cluster/ClusterWorkflowEngine.d.ts +3 -2
- package/dist/unstable/cluster/ClusterWorkflowEngine.d.ts.map +1 -1
- package/dist/unstable/cluster/ClusterWorkflowEngine.js +25 -23
- package/dist/unstable/cluster/ClusterWorkflowEngine.js.map +1 -1
- package/dist/unstable/cluster/DeliverAt.js +1 -1
- package/dist/unstable/cluster/DeliverAt.js.map +1 -1
- package/dist/unstable/cluster/Entity.d.ts +3 -2
- package/dist/unstable/cluster/Entity.d.ts.map +1 -1
- package/dist/unstable/cluster/Entity.js +7 -7
- package/dist/unstable/cluster/Entity.js.map +1 -1
- package/dist/unstable/cluster/Message.d.ts +4 -3
- package/dist/unstable/cluster/Message.d.ts.map +1 -1
- package/dist/unstable/cluster/Message.js +3 -2
- package/dist/unstable/cluster/Message.js.map +1 -1
- package/dist/unstable/cluster/MessageStorage.d.ts +10 -10
- package/dist/unstable/cluster/MessageStorage.d.ts.map +1 -1
- package/dist/unstable/cluster/MessageStorage.js +12 -12
- package/dist/unstable/cluster/MessageStorage.js.map +1 -1
- package/dist/unstable/cluster/Reply.d.ts +2 -1
- package/dist/unstable/cluster/Reply.d.ts.map +1 -1
- package/dist/unstable/cluster/Reply.js +4 -3
- package/dist/unstable/cluster/Reply.js.map +1 -1
- package/dist/unstable/cluster/RunnerServer.d.ts.map +1 -1
- package/dist/unstable/cluster/RunnerServer.js +9 -8
- package/dist/unstable/cluster/RunnerServer.js.map +1 -1
- package/dist/unstable/cluster/Runners.d.ts +2 -1
- package/dist/unstable/cluster/Runners.d.ts.map +1 -1
- package/dist/unstable/cluster/Runners.js +7 -6
- package/dist/unstable/cluster/Runners.js.map +1 -1
- package/dist/unstable/cluster/Sharding.d.ts.map +1 -1
- package/dist/unstable/cluster/Sharding.js +19 -13
- package/dist/unstable/cluster/Sharding.js.map +1 -1
- package/dist/unstable/cluster/ShardingConfig.d.ts +5 -4
- package/dist/unstable/cluster/ShardingConfig.d.ts.map +1 -1
- package/dist/unstable/cluster/ShardingConfig.js +4 -4
- package/dist/unstable/cluster/ShardingConfig.js.map +1 -1
- package/dist/unstable/cluster/SqlMessageStorage.d.ts.map +1 -1
- package/dist/unstable/cluster/SqlMessageStorage.js +19 -18
- package/dist/unstable/cluster/SqlMessageStorage.js.map +1 -1
- package/dist/unstable/cluster/internal/entityManager.js +9 -10
- package/dist/unstable/cluster/internal/entityManager.js.map +1 -1
- package/dist/unstable/devtools/DevToolsSchema.d.ts +2 -1
- package/dist/unstable/devtools/DevToolsSchema.d.ts.map +1 -1
- package/dist/unstable/devtools/DevToolsSchema.js +1 -4
- package/dist/unstable/devtools/DevToolsSchema.js.map +1 -1
- package/dist/unstable/encoding/Ndjson.d.ts +8 -8
- package/dist/unstable/encoding/Ndjson.d.ts.map +1 -1
- package/dist/unstable/encoding/Ndjson.js.map +1 -1
- package/dist/unstable/http/Cookies.d.ts +5 -4
- package/dist/unstable/http/Cookies.d.ts.map +1 -1
- package/dist/unstable/http/Cookies.js +3 -4
- package/dist/unstable/http/Cookies.js.map +1 -1
- package/dist/unstable/http/Etag.d.ts.map +1 -1
- package/dist/unstable/http/Etag.js +5 -1
- package/dist/unstable/http/Etag.js.map +1 -1
- package/dist/unstable/http/Headers.d.ts +3 -2
- package/dist/unstable/http/Headers.d.ts.map +1 -1
- package/dist/unstable/http/Headers.js +2 -1
- package/dist/unstable/http/Headers.js.map +1 -1
- package/dist/unstable/http/HttpClient.d.ts.map +1 -1
- package/dist/unstable/http/HttpClient.js +2 -1
- package/dist/unstable/http/HttpClient.js.map +1 -1
- package/dist/unstable/http/HttpClientRequest.d.ts +4 -3
- package/dist/unstable/http/HttpClientRequest.d.ts.map +1 -1
- package/dist/unstable/http/HttpClientRequest.js +8 -6
- package/dist/unstable/http/HttpClientRequest.js.map +1 -1
- package/dist/unstable/http/HttpClientResponse.d.ts.map +1 -1
- package/dist/unstable/http/HttpClientResponse.js +2 -1
- package/dist/unstable/http/HttpClientResponse.js.map +1 -1
- package/dist/unstable/http/HttpIncomingMessage.d.ts +3 -2
- package/dist/unstable/http/HttpIncomingMessage.d.ts.map +1 -1
- package/dist/unstable/http/HttpIncomingMessage.js.map +1 -1
- package/dist/unstable/http/HttpMiddleware.d.ts.map +1 -1
- package/dist/unstable/http/HttpMiddleware.js +16 -15
- package/dist/unstable/http/HttpMiddleware.js.map +1 -1
- package/dist/unstable/http/HttpPlatform.d.ts.map +1 -1
- package/dist/unstable/http/HttpPlatform.js +3 -2
- package/dist/unstable/http/HttpPlatform.js.map +1 -1
- package/dist/unstable/http/HttpRouter.d.ts +2 -1
- package/dist/unstable/http/HttpRouter.d.ts.map +1 -1
- package/dist/unstable/http/HttpRouter.js +7 -7
- package/dist/unstable/http/HttpRouter.js.map +1 -1
- package/dist/unstable/http/HttpServer.d.ts.map +1 -1
- package/dist/unstable/http/HttpServer.js +2 -2
- package/dist/unstable/http/HttpServer.js.map +1 -1
- package/dist/unstable/http/HttpServerError.d.ts +2 -1
- package/dist/unstable/http/HttpServerError.d.ts.map +1 -1
- package/dist/unstable/http/HttpServerError.js +2 -1
- package/dist/unstable/http/HttpServerError.js.map +1 -1
- package/dist/unstable/http/HttpServerRequest.d.ts +3 -2
- package/dist/unstable/http/HttpServerRequest.d.ts.map +1 -1
- package/dist/unstable/http/HttpServerRequest.js +14 -10
- package/dist/unstable/http/HttpServerRequest.js.map +1 -1
- package/dist/unstable/http/HttpServerResponse.d.ts.map +1 -1
- package/dist/unstable/http/HttpServerResponse.js +4 -3
- package/dist/unstable/http/HttpServerResponse.js.map +1 -1
- package/dist/unstable/http/HttpTraceContext.d.ts +3 -2
- package/dist/unstable/http/HttpTraceContext.d.ts.map +1 -1
- package/dist/unstable/http/HttpTraceContext.js +27 -15
- package/dist/unstable/http/HttpTraceContext.js.map +1 -1
- package/dist/unstable/http/UrlParams.d.ts +5 -4
- package/dist/unstable/http/UrlParams.d.ts.map +1 -1
- package/dist/unstable/http/UrlParams.js +5 -6
- package/dist/unstable/http/UrlParams.js.map +1 -1
- package/dist/unstable/httpapi/HttpApiBuilder.d.ts +2 -2
- package/dist/unstable/httpapi/HttpApiBuilder.d.ts.map +1 -1
- package/dist/unstable/httpapi/HttpApiBuilder.js +4 -2
- package/dist/unstable/httpapi/HttpApiBuilder.js.map +1 -1
- package/dist/unstable/httpapi/HttpApiError.d.ts +15 -0
- package/dist/unstable/httpapi/HttpApiError.d.ts.map +1 -1
- package/dist/unstable/httpapi/HttpApiError.js +81 -3
- package/dist/unstable/httpapi/HttpApiError.js.map +1 -1
- package/dist/unstable/observability/OtlpExporter.d.ts.map +1 -1
- package/dist/unstable/observability/OtlpExporter.js +2 -2
- package/dist/unstable/observability/OtlpExporter.js.map +1 -1
- package/dist/unstable/observability/OtlpTracer.d.ts.map +1 -1
- package/dist/unstable/observability/OtlpTracer.js +7 -3
- package/dist/unstable/observability/OtlpTracer.js.map +1 -1
- package/dist/unstable/persistence/Persistable.js +1 -1
- package/dist/unstable/persistence/Persistable.js.map +1 -1
- package/dist/unstable/reactivity/Atom.d.ts.map +1 -1
- package/dist/unstable/reactivity/Atom.js +1 -5
- package/dist/unstable/reactivity/Atom.js.map +1 -1
- package/dist/unstable/reactivity/AtomRegistry.d.ts.map +1 -1
- package/dist/unstable/reactivity/AtomRegistry.js +4 -2
- package/dist/unstable/reactivity/AtomRegistry.js.map +1 -1
- package/dist/unstable/rpc/Rpc.d.ts.map +1 -1
- package/dist/unstable/rpc/Rpc.js +4 -3
- package/dist/unstable/rpc/Rpc.js.map +1 -1
- package/dist/unstable/rpc/RpcClient.d.ts.map +1 -1
- package/dist/unstable/rpc/RpcClient.js +4 -4
- package/dist/unstable/rpc/RpcClient.js.map +1 -1
- package/dist/unstable/rpc/RpcGroup.d.ts +1 -3
- package/dist/unstable/rpc/RpcGroup.d.ts.map +1 -1
- package/dist/unstable/rpc/RpcSchema.d.ts.map +1 -1
- package/dist/unstable/rpc/RpcSchema.js +3 -2
- package/dist/unstable/rpc/RpcSchema.js.map +1 -1
- package/dist/unstable/rpc/RpcSerialization.d.ts.map +1 -1
- package/dist/unstable/rpc/RpcSerialization.js +4 -1
- package/dist/unstable/rpc/RpcSerialization.js.map +1 -1
- package/dist/unstable/rpc/RpcServer.d.ts +1 -1
- package/dist/unstable/rpc/RpcServer.d.ts.map +1 -1
- package/dist/unstable/rpc/RpcServer.js +3 -2
- package/dist/unstable/rpc/RpcServer.js.map +1 -1
- package/dist/unstable/socket/Socket.d.ts.map +1 -1
- package/dist/unstable/socket/Socket.js +2 -1
- package/dist/unstable/socket/Socket.js.map +1 -1
- package/dist/unstable/sql/SqlClient.d.ts.map +1 -1
- package/dist/unstable/sql/SqlClient.js +1 -1
- package/dist/unstable/sql/SqlClient.js.map +1 -1
- package/dist/unstable/workflow/DurableDeferred.js +6 -6
- package/dist/unstable/workflow/DurableDeferred.js.map +1 -1
- package/dist/unstable/workflow/Workflow.d.ts +2 -1
- package/dist/unstable/workflow/Workflow.d.ts.map +1 -1
- package/dist/unstable/workflow/WorkflowEngine.d.ts +5 -4
- package/dist/unstable/workflow/WorkflowEngine.d.ts.map +1 -1
- package/dist/unstable/workflow/WorkflowEngine.js +19 -14
- package/dist/unstable/workflow/WorkflowEngine.js.map +1 -1
- package/package.json +1 -1
- package/src/Array.ts +112 -105
- package/src/BigDecimal.ts +63 -66
- package/src/BigInt.ts +49 -41
- package/src/Channel.ts +3 -3
- package/src/Chunk.ts +68 -63
- package/src/Config.ts +4 -1
- package/src/Cron.ts +13 -18
- package/src/DateTime.ts +51 -92
- package/src/Deferred.ts +8 -6
- package/src/Duration.ts +112 -56
- package/src/Effect.ts +3 -4
- package/src/Fiber.ts +2 -1
- package/src/FiberHandle.ts +10 -9
- package/src/FiberMap.ts +22 -22
- package/src/FileSystem.ts +25 -22
- package/src/Graph.ts +105 -105
- package/src/HashMap.ts +11 -5
- package/src/Iterable.ts +13 -13
- package/src/Latch.ts +85 -3
- package/src/Layer.ts +244 -19
- package/src/Number.ts +85 -26
- package/src/Option.ts +2 -0
- package/src/PartitionedSemaphore.ts +414 -0
- package/src/PubSub.ts +7 -6
- package/src/Queue.ts +5 -5
- package/src/Record.ts +52 -47
- package/src/Scheduler.ts +37 -126
- package/src/Schema.ts +2366 -116
- package/src/SchemaAST.ts +2 -2
- package/src/SchemaGetter.ts +4 -4
- package/src/SchemaRepresentation.ts +2 -2
- package/src/SchemaTransformation.ts +27 -27
- package/src/Semaphore.ts +271 -183
- package/src/ServiceMap.ts +83 -31
- package/src/Stream.ts +1 -1
- package/src/String.ts +122 -69
- package/src/SubscriptionRef.ts +96 -117
- package/src/Terminal.ts +2 -1
- package/src/Tracer.ts +6 -5
- package/src/Trie.ts +26 -14
- package/src/TxHashMap.ts +78 -60
- package/src/TxSemaphore.ts +186 -11
- package/src/index.ts +89 -33
- package/src/internal/dateTime.ts +74 -65
- package/src/internal/effect.ts +30 -122
- package/src/internal/hashMap.ts +4 -3
- package/src/internal/option.ts +7 -0
- package/src/internal/trie.ts +6 -6
- package/src/unstable/ai/AiError.ts +1 -1
- package/src/unstable/ai/AnthropicStructuredOutput.ts +4 -0
- package/src/unstable/ai/Chat.ts +17 -15
- package/src/unstable/ai/LanguageModel.ts +148 -97
- package/src/unstable/ai/OpenAiStructuredOutput.ts +4 -0
- package/src/unstable/ai/Tool.ts +20 -0
- package/src/unstable/cli/Argument.ts +2 -4
- package/src/unstable/cli/CliOutput.ts +7 -5
- package/src/unstable/cli/Command.ts +5 -5
- package/src/unstable/cli/Completions.ts +36 -0
- package/src/unstable/cli/Flag.ts +2 -2
- package/src/unstable/cli/GlobalFlag.ts +4 -5
- package/src/unstable/cli/HelpDoc.ts +14 -12
- package/src/unstable/cli/Param.ts +15 -11
- package/src/unstable/cli/Primitive.ts +1 -1
- package/src/unstable/cli/Prompt.ts +81 -83
- package/src/unstable/cli/index.ts +5 -0
- package/src/unstable/cli/internal/completions/CommandDescriptor.ts +3 -2
- package/src/unstable/cli/internal/parser.ts +7 -4
- package/src/unstable/cluster/ClusterWorkflowEngine.ts +35 -30
- package/src/unstable/cluster/DeliverAt.ts +1 -1
- package/src/unstable/cluster/Entity.ts +13 -12
- package/src/unstable/cluster/Message.ts +6 -5
- package/src/unstable/cluster/MessageStorage.ts +26 -25
- package/src/unstable/cluster/Reply.ts +7 -4
- package/src/unstable/cluster/RunnerServer.ts +10 -13
- package/src/unstable/cluster/Runners.ts +8 -7
- package/src/unstable/cluster/Sharding.ts +24 -17
- package/src/unstable/cluster/ShardingConfig.ts +6 -6
- package/src/unstable/cluster/SqlMessageStorage.ts +21 -18
- package/src/unstable/cluster/internal/entityManager.ts +27 -21
- package/src/unstable/devtools/DevToolsSchema.ts +3 -2
- package/src/unstable/encoding/Ndjson.ts +17 -17
- package/src/unstable/http/Cookies.ts +7 -8
- package/src/unstable/http/Etag.ts +5 -3
- package/src/unstable/http/Headers.ts +6 -5
- package/src/unstable/http/HttpClient.ts +2 -1
- package/src/unstable/http/HttpClientRequest.ts +11 -9
- package/src/unstable/http/HttpClientResponse.ts +5 -4
- package/src/unstable/http/HttpIncomingMessage.ts +3 -2
- package/src/unstable/http/HttpMiddleware.ts +16 -15
- package/src/unstable/http/HttpPlatform.ts +3 -2
- package/src/unstable/http/HttpRouter.ts +9 -9
- package/src/unstable/http/HttpServer.ts +3 -9
- package/src/unstable/http/HttpServerError.ts +3 -2
- package/src/unstable/http/HttpServerRequest.ts +29 -25
- package/src/unstable/http/HttpServerResponse.ts +7 -6
- package/src/unstable/http/HttpTraceContext.ts +31 -17
- package/src/unstable/http/UrlParams.ts +11 -14
- package/src/unstable/httpapi/HttpApiBuilder.ts +13 -7
- package/src/unstable/httpapi/HttpApiError.ts +67 -12
- package/src/unstable/observability/OtlpExporter.ts +5 -2
- package/src/unstable/observability/OtlpTracer.ts +8 -4
- package/src/unstable/persistence/Persistable.ts +1 -1
- package/src/unstable/reactivity/Atom.ts +1 -4
- package/src/unstable/reactivity/AtomRegistry.ts +5 -3
- package/src/unstable/rpc/Rpc.ts +4 -3
- package/src/unstable/rpc/RpcClient.ts +10 -9
- package/src/unstable/rpc/RpcGroup.ts +3 -3
- package/src/unstable/rpc/RpcSchema.ts +6 -5
- package/src/unstable/rpc/RpcSerialization.ts +4 -1
- package/src/unstable/rpc/RpcServer.ts +3 -3
- package/src/unstable/socket/Socket.ts +2 -1
- package/src/unstable/sql/SqlClient.ts +5 -3
- package/src/unstable/workflow/DurableDeferred.ts +6 -6
- package/src/unstable/workflow/Workflow.ts +1 -1
- package/src/unstable/workflow/WorkflowEngine.ts +29 -20
- package/dist/NullOr.d.ts +0 -149
- package/dist/NullOr.d.ts.map +0 -1
- package/dist/NullOr.js +0 -152
- package/dist/NullOr.js.map +0 -1
- package/dist/unstable/cli/internal/completions/Completions.d.ts +0 -2
- package/dist/unstable/cli/internal/completions/Completions.d.ts.map +0 -1
- package/dist/unstable/cli/internal/completions/Completions.js +0 -23
- package/dist/unstable/cli/internal/completions/Completions.js.map +0 -1
- package/src/NullOr.ts +0 -204
- package/src/unstable/cli/internal/completions/Completions.ts +0 -31
package/dist/Schema.js
CHANGED
|
@@ -1,4 +1,86 @@
|
|
|
1
1
|
/**
|
|
2
|
+
* Define data shapes, validate unknown input, and transform values between formats.
|
|
3
|
+
*
|
|
4
|
+
* ## Mental model
|
|
5
|
+
*
|
|
6
|
+
* - **Schema** — a description of a data shape. Every schema carries a decoded
|
|
7
|
+
* *Type* (the value you work with) and an *Encoded* representation (the
|
|
8
|
+
* serialized form, e.g. JSON).
|
|
9
|
+
* - **Decoding** — turning unknown external data (API responses, form
|
|
10
|
+
* submissions, config files) into typed, validated values.
|
|
11
|
+
* - **Encoding** — turning typed values back into a serializable format.
|
|
12
|
+
* - **Codec** — a schema that tracks both Type and Encoded, so it can decode
|
|
13
|
+
* *and* encode. Most concrete schemas are Codecs.
|
|
14
|
+
* - **Check / Filter** — a constraint attached to a schema (e.g. `isMinLength`,
|
|
15
|
+
* `isGreaterThan`). Attach them with `.check(...)`.
|
|
16
|
+
* - **Transformation** — a pair of functions (decode + encode) that convert
|
|
17
|
+
* values between two schemas. Created with {@link decodeTo} / {@link encodeTo}.
|
|
18
|
+
* - **Annotation** — metadata attached to a schema (title, description, custom
|
|
19
|
+
* keys). Attach with `.annotate(...)`.
|
|
20
|
+
*
|
|
21
|
+
* ## Common tasks
|
|
22
|
+
*
|
|
23
|
+
* - Define a struct: {@link Struct}
|
|
24
|
+
* - Define a union: {@link Union}, {@link TaggedUnion}, {@link Literals}
|
|
25
|
+
* - Define an array: {@link Array}, {@link NonEmptyArray}
|
|
26
|
+
* - Define a record: {@link Record}
|
|
27
|
+
* - Define a tuple: {@link Tuple}, {@link TupleWithRest}
|
|
28
|
+
* - Validate unknown data synchronously: {@link decodeUnknownSync}
|
|
29
|
+
* - Validate unknown data (Effect): {@link decodeUnknownEffect}
|
|
30
|
+
* - Encode a value: {@link encodeUnknownSync}, {@link encodeUnknownEffect}
|
|
31
|
+
* - Type guard: {@link is}
|
|
32
|
+
* - Assertion: {@link asserts}
|
|
33
|
+
* - Add constraints: `.check(...)` with filters like {@link isMinLength},
|
|
34
|
+
* {@link isGreaterThan}, {@link isPattern}, {@link isUUID}
|
|
35
|
+
* - Transform between schemas: {@link decodeTo}, {@link encodeTo}
|
|
36
|
+
* - Add a default for missing keys: {@link withDecodingDefault}, {@link withDecodingDefaultKey}
|
|
37
|
+
* - Create branded types: {@link brand}
|
|
38
|
+
* - Define classes with validation: {@link Class}, {@link TaggedClass}
|
|
39
|
+
* - Define error classes: {@link ErrorClass}, {@link TaggedErrorClass}
|
|
40
|
+
* - Generate JSON Schema: {@link toJsonSchemaDocument}
|
|
41
|
+
* - Generate test data: {@link toArbitrary}
|
|
42
|
+
* - Derive equivalence: {@link toEquivalence}
|
|
43
|
+
*
|
|
44
|
+
* ## Gotchas
|
|
45
|
+
*
|
|
46
|
+
* - `Schema.optional` creates `T | undefined` (key can be missing *or*
|
|
47
|
+
* `undefined`). Use `Schema.optionalKey` for exact optional properties.
|
|
48
|
+
* - `decodeTo` is curried: use `from.pipe(Schema.decodeTo(to, ...))`.
|
|
49
|
+
* - `decodeUnknownSync` throws on failure. Use `decodeUnknownExit` or
|
|
50
|
+
* `decodeUnknownOption` for non-throwing alternatives.
|
|
51
|
+
* - Filters do not change the TypeScript type. Use {@link refine} or
|
|
52
|
+
* {@link brand} to narrow the type.
|
|
53
|
+
* - Recursive schemas require {@link suspend} to avoid infinite loops.
|
|
54
|
+
*
|
|
55
|
+
* ## Quickstart
|
|
56
|
+
*
|
|
57
|
+
* **Example** (Validate a user object)
|
|
58
|
+
*
|
|
59
|
+
* ```ts
|
|
60
|
+
* import { Schema } from "effect"
|
|
61
|
+
*
|
|
62
|
+
* const User = Schema.Struct({
|
|
63
|
+
* name: Schema.String.check(Schema.isMinLength(1)),
|
|
64
|
+
* age: Schema.Number.check(Schema.isGreaterThanOrEqualTo(0)),
|
|
65
|
+
* email: Schema.optionalKey(Schema.String)
|
|
66
|
+
* })
|
|
67
|
+
*
|
|
68
|
+
* // Decode unknown input — throws on failure
|
|
69
|
+
* const user = Schema.decodeUnknownSync(User)({
|
|
70
|
+
* name: "Alice",
|
|
71
|
+
* age: 30
|
|
72
|
+
* })
|
|
73
|
+
*
|
|
74
|
+
* console.log(user)
|
|
75
|
+
* // { name: "Alice", age: 30 }
|
|
76
|
+
* ```
|
|
77
|
+
*
|
|
78
|
+
* @see {@link Schema} — type-level view tracking only the decoded Type
|
|
79
|
+
* @see {@link Codec} — type-level view tracking both Type and Encoded
|
|
80
|
+
* @see {@link Struct} — define object shapes
|
|
81
|
+
* @see {@link decodeUnknownSync} — synchronous validation
|
|
82
|
+
* @see {@link decodeTo} — schema transformations
|
|
83
|
+
*
|
|
2
84
|
* @since 4.0.0
|
|
3
85
|
*/
|
|
4
86
|
import * as Arr from "./Array.js";
|
|
@@ -45,9 +127,51 @@ import * as Struct_ from "./Struct.js";
|
|
|
45
127
|
import * as FastCheck from "./testing/FastCheck.js";
|
|
46
128
|
const TypeId = InternalSchema.TypeId;
|
|
47
129
|
/**
|
|
48
|
-
*
|
|
130
|
+
* Creates a schema for a **parametric** type (a generic container such as
|
|
131
|
+
* `Array<A>`, `Option<A>`, etc.) by accepting a list of type-parameter schemas
|
|
132
|
+
* and a decoder factory.
|
|
133
|
+
*
|
|
134
|
+
* The outer call `declareConstructor<T, E, Iso>()` fixes the decoded type `T`,
|
|
135
|
+
* the encoded type `E`, and the optional iso type. The inner call receives:
|
|
136
|
+
* - `typeParameters` — the concrete schemas for each type variable
|
|
137
|
+
* - `run` — a factory that, given resolved codecs for each type parameter,
|
|
138
|
+
* returns a parsing function `(u, ast, options) => Effect<T, Issue>`
|
|
139
|
+
* - `annotations` — optional metadata
|
|
140
|
+
*
|
|
141
|
+
* @see {@link declare} for creating schemas for non-parametric types.
|
|
142
|
+
*
|
|
143
|
+
* **Example** (Schema for a parametric `Box<A>` type)
|
|
144
|
+
*
|
|
145
|
+
* ```ts
|
|
146
|
+
* import { Effect, Schema } from "effect"
|
|
147
|
+
* import * as SchemaParser from "effect/SchemaParser"
|
|
148
|
+
* import * as Issue from "effect/SchemaIssue"
|
|
149
|
+
* import * as Option from "effect/Option"
|
|
150
|
+
*
|
|
151
|
+
* interface Box<A> {
|
|
152
|
+
* readonly value: A
|
|
153
|
+
* }
|
|
49
154
|
*
|
|
50
|
-
*
|
|
155
|
+
* const isBox = (u: unknown): u is Box<unknown> =>
|
|
156
|
+
* typeof u === "object" && u !== null && "value" in u
|
|
157
|
+
*
|
|
158
|
+
* const Box = <A extends Schema.Top>(item: A) =>
|
|
159
|
+
* Schema.declareConstructor<Box<A["Type"]>, Box<A["Encoded"]>>()(
|
|
160
|
+
* [item],
|
|
161
|
+
* ([itemCodec]) =>
|
|
162
|
+
* (u, ast, options) => {
|
|
163
|
+
* if (!isBox(u)) {
|
|
164
|
+
* return Effect.fail(new Issue.InvalidType(ast, Option.some(u)))
|
|
165
|
+
* }
|
|
166
|
+
* return Effect.map(
|
|
167
|
+
* SchemaParser.decodeUnknownEffect(itemCodec)(u.value, options),
|
|
168
|
+
* (value) => ({ value })
|
|
169
|
+
* )
|
|
170
|
+
* }
|
|
171
|
+
* )
|
|
172
|
+
*
|
|
173
|
+
* const schema = Box(Schema.Number)
|
|
174
|
+
* ```
|
|
51
175
|
*
|
|
52
176
|
* @category Constructors
|
|
53
177
|
* @since 4.0.0
|
|
@@ -58,18 +182,60 @@ export function declareConstructor() {
|
|
|
58
182
|
};
|
|
59
183
|
}
|
|
60
184
|
/**
|
|
61
|
-
*
|
|
185
|
+
* Creates a schema for a **non-parametric** opaque type using a type-guard
|
|
186
|
+
* function. The schema accepts any unknown value and succeeds when `is` returns
|
|
187
|
+
* `true`, failing with an `InvalidType` issue otherwise.
|
|
188
|
+
*
|
|
189
|
+
* Use this when the type has no type parameters. For parametric types such as
|
|
190
|
+
* `Option<A>` or `Array<A>`, use {@link declareConstructor} instead.
|
|
191
|
+
*
|
|
192
|
+
* **Example** (Schema for a custom `UserId` branded type)
|
|
193
|
+
*
|
|
194
|
+
* ```ts
|
|
195
|
+
* import { Schema } from "effect"
|
|
196
|
+
*
|
|
197
|
+
* type UserId = string & { readonly _tag: "UserId" }
|
|
198
|
+
*
|
|
199
|
+
* const isUserId = (u: unknown): u is UserId =>
|
|
200
|
+
* typeof u === "string" && u.startsWith("user_")
|
|
201
|
+
*
|
|
202
|
+
* const UserId = Schema.declare<UserId>(isUserId, {
|
|
203
|
+
* title: "UserId",
|
|
204
|
+
* description: "A user identifier starting with 'user_'"
|
|
205
|
+
* })
|
|
206
|
+
* ```
|
|
62
207
|
*
|
|
63
208
|
* @see {@link declareConstructor} for creating schemas for parametric types.
|
|
64
209
|
*
|
|
210
|
+
* @category Constructors
|
|
65
211
|
* @since 4.0.0
|
|
66
212
|
*/
|
|
67
213
|
export function declare(is, annotations) {
|
|
68
214
|
return declareConstructor()([], () => (input, ast) => is(input) ? Effect.succeed(input) : Effect.fail(new Issue.InvalidType(ast, Option_.some(input))), annotations);
|
|
69
215
|
}
|
|
70
216
|
/**
|
|
71
|
-
*
|
|
72
|
-
* parameters.
|
|
217
|
+
* Widens a schema's type to the fully-parameterized {@link Bottom} interface,
|
|
218
|
+
* making all 14 type parameters visible to TypeScript.
|
|
219
|
+
*
|
|
220
|
+
* Normally, concrete schema interfaces (e.g. `Schema<string>`) hide most type
|
|
221
|
+
* parameters. `revealBottom` is useful when writing generic utilities that need
|
|
222
|
+
* to inspect or propagate the complete set of type parameters.
|
|
223
|
+
*
|
|
224
|
+
* **Example** (Inspecting all type parameters of a schema)
|
|
225
|
+
*
|
|
226
|
+
* ```ts
|
|
227
|
+
* import { Schema } from "effect"
|
|
228
|
+
*
|
|
229
|
+
* const schema = Schema.String
|
|
230
|
+
*
|
|
231
|
+
* // Widen to Bottom to access all 14 type parameters
|
|
232
|
+
* const bottom = Schema.revealBottom(schema)
|
|
233
|
+
*
|
|
234
|
+
* // `bottom` now exposes Type, Encoded, DecodingServices, EncodingServices,
|
|
235
|
+
* // ast, ~rebuild.out, ~type.make.in, Iso, ~type.parameters, etc.
|
|
236
|
+
* type T = typeof bottom["Type"] // string
|
|
237
|
+
* type E = typeof bottom["Encoded"] // string
|
|
238
|
+
* ```
|
|
73
239
|
*
|
|
74
240
|
* @since 4.0.0
|
|
75
241
|
*/
|
|
@@ -78,8 +244,24 @@ export function revealBottom(bottom) {
|
|
|
78
244
|
}
|
|
79
245
|
/**
|
|
80
246
|
* Adds metadata annotations to a schema without changing its runtime behavior.
|
|
81
|
-
*
|
|
82
|
-
*
|
|
247
|
+
* This is the pipeable (curried) counterpart of the `.annotate` method.
|
|
248
|
+
*
|
|
249
|
+
* Annotations provide extra context used by documentation generators, JSON
|
|
250
|
+
* Schema converters, error formatters, and other tooling. Common keys include
|
|
251
|
+
* `title`, `description`, `examples`, `message`, and `identifier`.
|
|
252
|
+
*
|
|
253
|
+
* **Example** (Adding a title and description)
|
|
254
|
+
*
|
|
255
|
+
* ```ts
|
|
256
|
+
* import { Schema } from "effect"
|
|
257
|
+
*
|
|
258
|
+
* const Age = Schema.Number.pipe(
|
|
259
|
+
* Schema.annotate({
|
|
260
|
+
* title: "Age",
|
|
261
|
+
* description: "A non-negative integer representing age in years"
|
|
262
|
+
* })
|
|
263
|
+
* )
|
|
264
|
+
* ```
|
|
83
265
|
*
|
|
84
266
|
* @category Annotations
|
|
85
267
|
* @since 4.0.0
|
|
@@ -90,9 +272,29 @@ export function annotate(annotations) {
|
|
|
90
272
|
};
|
|
91
273
|
}
|
|
92
274
|
/**
|
|
93
|
-
* Adds key-
|
|
94
|
-
*
|
|
95
|
-
*
|
|
275
|
+
* Adds key-level annotations to a schema field. This is the pipeable
|
|
276
|
+
* (curried) counterpart of the `.annotateKey` method.
|
|
277
|
+
*
|
|
278
|
+
* Key annotations apply to a field's position inside a `Struct` or `Tuple`
|
|
279
|
+
* rather than to the field's value type. They can carry a
|
|
280
|
+
* `messageMissingKey` to customise the error shown when the field is absent,
|
|
281
|
+
* as well as standard documentation fields such as `title`, `description`,
|
|
282
|
+
* and `examples`.
|
|
283
|
+
*
|
|
284
|
+
* **Example** (Custom missing-key message for a required field)
|
|
285
|
+
*
|
|
286
|
+
* ```ts
|
|
287
|
+
* import { Schema } from "effect"
|
|
288
|
+
*
|
|
289
|
+
* const schema = Schema.Struct({
|
|
290
|
+
* username: Schema.String.pipe(
|
|
291
|
+
* Schema.annotateKey({
|
|
292
|
+
* description: "The username used to log in",
|
|
293
|
+
* messageMissingKey: "Username is required"
|
|
294
|
+
* })
|
|
295
|
+
* )
|
|
296
|
+
* })
|
|
297
|
+
* ```
|
|
96
298
|
*
|
|
97
299
|
* @category Annotations
|
|
98
300
|
* @since 4.0.0
|
|
@@ -103,6 +305,25 @@ export function annotateKey(annotations) {
|
|
|
103
305
|
};
|
|
104
306
|
}
|
|
105
307
|
/**
|
|
308
|
+
* Identity function that widens a value to the full {@link Codec} interface,
|
|
309
|
+
* prompting TypeScript to infer all four type parameters (`T`, `E`, `RD`, `RE`).
|
|
310
|
+
*
|
|
311
|
+
* When a schema is stored in a variable typed as `Schema<T>` or `Top`, the
|
|
312
|
+
* encoded type and service requirements are erased. Passing the value through
|
|
313
|
+
* `revealCodec` recovers those parameters without any runtime cost.
|
|
314
|
+
*
|
|
315
|
+
* **Example** (Recovering encoded type from a schema variable)
|
|
316
|
+
*
|
|
317
|
+
* ```ts
|
|
318
|
+
* import { Schema } from "effect"
|
|
319
|
+
*
|
|
320
|
+
* const schema: Schema.Schema<number> = Schema.NumberFromString
|
|
321
|
+
*
|
|
322
|
+
* // Without revealCodec, Encoded is unknown
|
|
323
|
+
* const codec = Schema.revealCodec(schema)
|
|
324
|
+
* type Enc = typeof codec["Encoded"] // string
|
|
325
|
+
* ```
|
|
326
|
+
*
|
|
106
327
|
* @since 4.0.0
|
|
107
328
|
*/
|
|
108
329
|
export function revealCodec(codec) {
|
|
@@ -110,12 +331,30 @@ export function revealCodec(codec) {
|
|
|
110
331
|
}
|
|
111
332
|
const SchemaErrorTypeId = "~effect/Schema/SchemaError";
|
|
112
333
|
/**
|
|
113
|
-
*
|
|
334
|
+
* Error thrown (or returned as the error channel value) when schema decoding
|
|
335
|
+
* or encoding fails.
|
|
336
|
+
*
|
|
337
|
+
* The `issue` field contains a structured {@link Issue.Issue} tree describing
|
|
338
|
+
* every validation failure, including the path to the problematic value,
|
|
339
|
+
* expected types, and actual values received. `message` renders the issue tree
|
|
340
|
+
* as a human-readable string.
|
|
341
|
+
*
|
|
342
|
+
* Use {@link isSchemaError} to narrow an unknown value to `SchemaError`.
|
|
343
|
+
*
|
|
344
|
+
* **Example** (Catching a SchemaError)
|
|
114
345
|
*
|
|
115
|
-
*
|
|
116
|
-
*
|
|
117
|
-
*
|
|
118
|
-
*
|
|
346
|
+
* ```ts
|
|
347
|
+
* import { Schema } from "effect"
|
|
348
|
+
*
|
|
349
|
+
* try {
|
|
350
|
+
* Schema.decodeUnknownSync(Schema.Number)("not a number")
|
|
351
|
+
* } catch (err) {
|
|
352
|
+
* if (Schema.isSchemaError(err)) {
|
|
353
|
+
* console.log(err.message)
|
|
354
|
+
* // Expected number, actual "not a number"
|
|
355
|
+
* }
|
|
356
|
+
* }
|
|
357
|
+
* ```
|
|
119
358
|
*
|
|
120
359
|
* @since 4.0.0
|
|
121
360
|
*/
|
|
@@ -135,6 +374,22 @@ export class SchemaError {
|
|
|
135
374
|
}
|
|
136
375
|
}
|
|
137
376
|
/**
|
|
377
|
+
* Returns `true` if `u` is a {@link SchemaError}.
|
|
378
|
+
*
|
|
379
|
+
* **Example** (Type guard in a catch block)
|
|
380
|
+
*
|
|
381
|
+
* ```ts
|
|
382
|
+
* import { Schema } from "effect"
|
|
383
|
+
*
|
|
384
|
+
* try {
|
|
385
|
+
* Schema.decodeUnknownSync(Schema.Number)("oops")
|
|
386
|
+
* } catch (err) {
|
|
387
|
+
* if (Schema.isSchemaError(err)) {
|
|
388
|
+
* console.log(err._tag) // "SchemaError"
|
|
389
|
+
* }
|
|
390
|
+
* }
|
|
391
|
+
* ```
|
|
392
|
+
*
|
|
138
393
|
* @since 4.0.0
|
|
139
394
|
*/
|
|
140
395
|
export function isSchemaError(u) {
|
|
@@ -225,7 +480,7 @@ export function toStandardSchemaV1(self, options) {
|
|
|
225
480
|
}), {
|
|
226
481
|
scheduler
|
|
227
482
|
});
|
|
228
|
-
|
|
483
|
+
fiber.currentDispatcher?.flush();
|
|
229
484
|
const exit = fiber.pollUnsafe();
|
|
230
485
|
if (exit) {
|
|
231
486
|
return makeStandardResult(exit);
|
|
@@ -374,6 +629,11 @@ export const is = Parser.is;
|
|
|
374
629
|
*/
|
|
375
630
|
export const asserts = Parser.asserts;
|
|
376
631
|
/**
|
|
632
|
+
* Decodes an `unknown` input against a schema, returning an `Effect` that
|
|
633
|
+
* succeeds with the decoded value or fails with a {@link SchemaError}. Use this
|
|
634
|
+
* when the input type is not statically known. Prefer {@link decodeEffect} when
|
|
635
|
+
* the input is already typed as the schema's `Encoded` type.
|
|
636
|
+
*
|
|
377
637
|
* @category Decoding
|
|
378
638
|
* @since 4.0.0
|
|
379
639
|
*/
|
|
@@ -384,11 +644,22 @@ export function decodeUnknownEffect(schema) {
|
|
|
384
644
|
};
|
|
385
645
|
}
|
|
386
646
|
/**
|
|
647
|
+
* Decodes a typed input (the schema's `Encoded` type) against a schema,
|
|
648
|
+
* returning an `Effect` that succeeds with the decoded value or fails with a
|
|
649
|
+
* {@link SchemaError}. Use this when the input is already typed; for `unknown`
|
|
650
|
+
* input use {@link decodeUnknownEffect}.
|
|
651
|
+
*
|
|
387
652
|
* @category Decoding
|
|
388
653
|
* @since 4.0.0
|
|
389
654
|
*/
|
|
390
655
|
export const decodeEffect = decodeUnknownEffect;
|
|
391
656
|
/**
|
|
657
|
+
* Decodes an `unknown` input against a schema synchronously, returning an
|
|
658
|
+
* `Exit` that is either a `Success` with the decoded value or a `Failure` with
|
|
659
|
+
* a {@link SchemaError}. Only usable with schemas that have no
|
|
660
|
+
* `DecodingServices` requirement. Prefer {@link decodeExit} when the input is
|
|
661
|
+
* already typed as the schema's `Encoded` type.
|
|
662
|
+
*
|
|
392
663
|
* @category Decoding
|
|
393
664
|
* @since 4.0.0
|
|
394
665
|
*/
|
|
@@ -399,41 +670,110 @@ export function decodeUnknownExit(schema) {
|
|
|
399
670
|
};
|
|
400
671
|
}
|
|
401
672
|
/**
|
|
673
|
+
* Decodes a typed input (the schema's `Encoded` type) against a schema
|
|
674
|
+
* synchronously, returning an `Exit` that is either a `Success` with the
|
|
675
|
+
* decoded value or a `Failure` with a {@link SchemaError}. Only usable with
|
|
676
|
+
* schemas that have no `DecodingServices` requirement. For `unknown` input use
|
|
677
|
+
* {@link decodeUnknownExit}.
|
|
678
|
+
*
|
|
402
679
|
* @category Decoding
|
|
403
680
|
* @since 4.0.0
|
|
404
681
|
*/
|
|
405
682
|
export const decodeExit = decodeUnknownExit;
|
|
406
683
|
/**
|
|
684
|
+
* Decodes an `unknown` input against a schema, returning an `Option` that is
|
|
685
|
+
* `Some` with the decoded value on success or `None` on failure. Prefer this
|
|
686
|
+
* over {@link decodeUnknownExit} or {@link decodeUnknownEffect} when you only
|
|
687
|
+
* need to know whether decoding succeeded and don't need error details. For
|
|
688
|
+
* typed input use {@link decodeOption}.
|
|
689
|
+
*
|
|
407
690
|
* @category Decoding
|
|
408
691
|
* @since 4.0.0
|
|
409
692
|
*/
|
|
410
693
|
export const decodeUnknownOption = Parser.decodeUnknownOption;
|
|
411
694
|
/**
|
|
695
|
+
* Decodes a typed input (the schema's `Encoded` type) against a schema,
|
|
696
|
+
* returning an `Option` that is `Some` with the decoded value on success or
|
|
697
|
+
* `None` on failure. For `unknown` input use {@link decodeUnknownOption}.
|
|
698
|
+
*
|
|
412
699
|
* @category Decoding
|
|
413
700
|
* @since 4.0.0
|
|
414
701
|
*/
|
|
415
702
|
export const decodeOption = Parser.decodeOption;
|
|
416
703
|
/**
|
|
704
|
+
* Decodes an `unknown` input against a schema, returning a `Promise` that
|
|
705
|
+
* resolves with the decoded value or rejects with a {@link SchemaError}. Useful
|
|
706
|
+
* for integrating with Promise-based APIs. For typed input use
|
|
707
|
+
* {@link decodePromise}.
|
|
708
|
+
*
|
|
417
709
|
* @category Decoding
|
|
418
710
|
* @since 4.0.0
|
|
419
711
|
*/
|
|
420
712
|
export const decodeUnknownPromise = Parser.decodeUnknownPromise;
|
|
421
713
|
/**
|
|
714
|
+
* Decodes a typed input (the schema's `Encoded` type) against a schema,
|
|
715
|
+
* returning a `Promise` that resolves with the decoded value or rejects with a
|
|
716
|
+
* {@link SchemaError}. For `unknown` input use {@link decodeUnknownPromise}.
|
|
717
|
+
*
|
|
422
718
|
* @category Decoding
|
|
423
719
|
* @since 4.0.0
|
|
424
720
|
*/
|
|
425
721
|
export const decodePromise = Parser.decodePromise;
|
|
426
722
|
/**
|
|
723
|
+
* Decodes an `unknown` input against a schema synchronously, throwing a
|
|
724
|
+
* {@link SchemaError} on failure. Use this when you want to validate data at a
|
|
725
|
+
* boundary and treat a schema mismatch as an unrecoverable error. For
|
|
726
|
+
* non-throwing alternatives see {@link decodeUnknownOption},
|
|
727
|
+
* {@link decodeUnknownExit}, or {@link decodeUnknownEffect}. For typed input
|
|
728
|
+
* use {@link decodeSync}.
|
|
729
|
+
*
|
|
730
|
+
* **Example** (Decoding with a transformation schema)
|
|
731
|
+
*
|
|
732
|
+
* ```ts
|
|
733
|
+
* import { Schema } from "effect"
|
|
734
|
+
*
|
|
735
|
+
* const NumberFromString = Schema.NumberFromString
|
|
736
|
+
*
|
|
737
|
+
* console.log(Schema.decodeUnknownSync(NumberFromString)("42"))
|
|
738
|
+
* // Output: 42
|
|
739
|
+
*
|
|
740
|
+
* Schema.decodeUnknownSync(NumberFromString)("not a number")
|
|
741
|
+
* // throws SchemaError: NumberFromString
|
|
742
|
+
* // └─ Encoded side transformation failure
|
|
743
|
+
* // └─ NumberFromString
|
|
744
|
+
* // └─ Expected a numeric string, actual "not a number"
|
|
745
|
+
* ```
|
|
746
|
+
*
|
|
427
747
|
* @category Decoding
|
|
428
748
|
* @since 4.0.0
|
|
429
749
|
*/
|
|
430
750
|
export const decodeUnknownSync = Parser.decodeUnknownSync;
|
|
431
751
|
/**
|
|
752
|
+
* Decodes a typed input (the schema's `Encoded` type) against a schema
|
|
753
|
+
* synchronously, throwing a {@link SchemaError} on failure. For `unknown` input
|
|
754
|
+
* use {@link decodeUnknownSync}.
|
|
755
|
+
*
|
|
432
756
|
* @category Decoding
|
|
433
757
|
* @since 4.0.0
|
|
434
758
|
*/
|
|
435
759
|
export const decodeSync = Parser.decodeSync;
|
|
436
760
|
/**
|
|
761
|
+
* Encodes an `unknown` input against a schema, returning an `Effect` that
|
|
762
|
+
* succeeds with the encoded value or fails with a {@link SchemaError}. Use this
|
|
763
|
+
* when the input type is not statically known. Prefer {@link encodeEffect} when
|
|
764
|
+
* the input is already typed as the schema's `Type`.
|
|
765
|
+
*
|
|
766
|
+
* **Example** (Encoding a value to a string)
|
|
767
|
+
*
|
|
768
|
+
* ```ts
|
|
769
|
+
* import { Effect, Schema } from "effect"
|
|
770
|
+
*
|
|
771
|
+
* const NumberFromString = Schema.NumberFromString
|
|
772
|
+
*
|
|
773
|
+
* Effect.runPromise(Schema.encodeUnknownEffect(NumberFromString)(42)).then(console.log)
|
|
774
|
+
* // Output: "42"
|
|
775
|
+
* ```
|
|
776
|
+
*
|
|
437
777
|
* @category Encoding
|
|
438
778
|
* @since 4.0.0
|
|
439
779
|
*/
|
|
@@ -444,11 +784,22 @@ export function encodeUnknownEffect(schema) {
|
|
|
444
784
|
};
|
|
445
785
|
}
|
|
446
786
|
/**
|
|
787
|
+
* Encodes a typed input (the schema's `Type`) against a schema, returning an
|
|
788
|
+
* `Effect` that succeeds with the encoded value or fails with a
|
|
789
|
+
* {@link SchemaError}. Use this when the input is already typed; for `unknown`
|
|
790
|
+
* input use {@link encodeUnknownEffect}.
|
|
791
|
+
*
|
|
447
792
|
* @category Encoding
|
|
448
793
|
* @since 4.0.0
|
|
449
794
|
*/
|
|
450
795
|
export const encodeEffect = encodeUnknownEffect;
|
|
451
796
|
/**
|
|
797
|
+
* Encodes an `unknown` input against a schema synchronously, returning an
|
|
798
|
+
* `Exit` that is either a `Success` with the encoded value or a `Failure` with
|
|
799
|
+
* a {@link SchemaError}. Only usable with schemas that have no
|
|
800
|
+
* `EncodingServices` requirement. Prefer {@link encodeExit} when the input is
|
|
801
|
+
* already typed as the schema's `Type`.
|
|
802
|
+
*
|
|
452
803
|
* @category Encoding
|
|
453
804
|
* @since 4.0.0
|
|
454
805
|
*/
|
|
@@ -459,36 +810,72 @@ export function encodeUnknownExit(schema) {
|
|
|
459
810
|
};
|
|
460
811
|
}
|
|
461
812
|
/**
|
|
813
|
+
* Encodes a typed input (the schema's `Type`) against a schema synchronously,
|
|
814
|
+
* returning an `Exit` that is either a `Success` with the encoded value or a
|
|
815
|
+
* `Failure` with a {@link SchemaError}. Only usable with schemas that have no
|
|
816
|
+
* `EncodingServices` requirement. For `unknown` input use
|
|
817
|
+
* {@link encodeUnknownExit}.
|
|
818
|
+
*
|
|
462
819
|
* @category Encoding
|
|
463
820
|
* @since 4.0.0
|
|
464
821
|
*/
|
|
465
822
|
export const encodeExit = encodeUnknownExit;
|
|
466
823
|
/**
|
|
824
|
+
* Encodes an `unknown` input against a schema, returning an `Option` that is
|
|
825
|
+
* `Some` with the encoded value on success or `None` on failure. Prefer this
|
|
826
|
+
* over {@link encodeUnknownExit} or {@link encodeUnknownEffect} when you only
|
|
827
|
+
* need to know whether encoding succeeded and don't need error details. For
|
|
828
|
+
* typed input use {@link encodeOption}.
|
|
829
|
+
*
|
|
467
830
|
* @category Encoding
|
|
468
831
|
* @since 4.0.0
|
|
469
832
|
*/
|
|
470
833
|
export const encodeUnknownOption = Parser.encodeUnknownOption;
|
|
471
834
|
/**
|
|
835
|
+
* Encodes a typed input (the schema's `Type`) against a schema, returning an
|
|
836
|
+
* `Option` that is `Some` with the encoded value on success or `None` on
|
|
837
|
+
* failure. For `unknown` input use {@link encodeUnknownOption}.
|
|
838
|
+
*
|
|
472
839
|
* @category Encoding
|
|
473
840
|
* @since 4.0.0
|
|
474
841
|
*/
|
|
475
842
|
export const encodeOption = Parser.encodeOption;
|
|
476
843
|
/**
|
|
844
|
+
* Encodes an `unknown` input against a schema, returning a `Promise` that
|
|
845
|
+
* resolves with the encoded value or rejects with a {@link SchemaError}. Useful
|
|
846
|
+
* for integrating with Promise-based APIs. For typed input use
|
|
847
|
+
* {@link encodePromise}.
|
|
848
|
+
*
|
|
477
849
|
* @category Encoding
|
|
478
850
|
* @since 4.0.0
|
|
479
851
|
*/
|
|
480
852
|
export const encodeUnknownPromise = Parser.encodeUnknownPromise;
|
|
481
853
|
/**
|
|
854
|
+
* Encodes a typed input (the schema's `Type`) against a schema, returning a
|
|
855
|
+
* `Promise` that resolves with the encoded value or rejects with a
|
|
856
|
+
* {@link SchemaError}. For `unknown` input use {@link encodeUnknownPromise}.
|
|
857
|
+
*
|
|
482
858
|
* @category Encoding
|
|
483
859
|
* @since 4.0.0
|
|
484
860
|
*/
|
|
485
861
|
export const encodePromise = Parser.encodePromise;
|
|
486
862
|
/**
|
|
863
|
+
* Encodes an `unknown` input against a schema synchronously, throwing a
|
|
864
|
+
* {@link SchemaError} on failure. Use this when you want to serialize data at a
|
|
865
|
+
* boundary and treat a schema mismatch as an unrecoverable error. For
|
|
866
|
+
* non-throwing alternatives see {@link encodeUnknownOption},
|
|
867
|
+
* {@link encodeUnknownExit}, or {@link encodeUnknownEffect}. For typed input
|
|
868
|
+
* use {@link encodeSync}.
|
|
869
|
+
*
|
|
487
870
|
* @category Encoding
|
|
488
871
|
* @since 4.0.0
|
|
489
872
|
*/
|
|
490
873
|
export const encodeUnknownSync = Parser.encodeUnknownSync;
|
|
491
874
|
/**
|
|
875
|
+
* Encodes a typed input (the schema's `Type`) against a schema synchronously,
|
|
876
|
+
* throwing a {@link SchemaError} on failure. For `unknown` input use
|
|
877
|
+
* {@link encodeUnknownSync}.
|
|
878
|
+
*
|
|
492
879
|
* @category Encoding
|
|
493
880
|
* @since 4.0.0
|
|
494
881
|
*/
|
|
@@ -543,57 +930,74 @@ export const optionalKey = /*#__PURE__*/Struct_.lambda(schema => make(AST.option
|
|
|
543
930
|
schema
|
|
544
931
|
}));
|
|
545
932
|
/**
|
|
933
|
+
* Reverses {@link optionalKey}, returning the inner required schema. Only
|
|
934
|
+
* applicable to schemas already wrapped with `optionalKey`.
|
|
935
|
+
*
|
|
546
936
|
* @since 4.0.0
|
|
547
937
|
*/
|
|
548
938
|
export const requiredKey = /*#__PURE__*/Struct_.lambda(self => self.schema);
|
|
549
939
|
/**
|
|
550
|
-
*
|
|
940
|
+
* Marks a struct field as optional, allowing the key to be absent or
|
|
551
941
|
* `undefined`.
|
|
552
942
|
*
|
|
553
|
-
*
|
|
554
|
-
*
|
|
555
|
-
*
|
|
556
|
-
*
|
|
557
|
-
* - Can contain the specified schema type
|
|
943
|
+
* explicitly set to `undefined`. Equivalent to `optionalKey(UndefinedOr(S))`.
|
|
944
|
+
*
|
|
945
|
+
* Use {@link optionalKey} instead if you want exact optional semantics (absent
|
|
946
|
+
* only, not `undefined`).
|
|
558
947
|
*
|
|
559
|
-
* **Example** (
|
|
948
|
+
* **Example** (Optional field accepting undefined)
|
|
560
949
|
*
|
|
561
950
|
* ```ts
|
|
562
951
|
* import { Schema } from "effect"
|
|
563
952
|
*
|
|
564
953
|
* const schema = Schema.Struct({
|
|
565
954
|
* name: Schema.String,
|
|
566
|
-
* age: Schema.
|
|
955
|
+
* age: Schema.optional(Schema.Number)
|
|
567
956
|
* })
|
|
568
957
|
*
|
|
569
|
-
* //
|
|
570
|
-
* type Person = typeof schema
|
|
958
|
+
* // { readonly name: string; readonly age?: number | undefined }
|
|
959
|
+
* type Person = typeof schema.Type
|
|
571
960
|
* ```
|
|
572
961
|
*
|
|
573
962
|
* @since 4.0.0
|
|
574
963
|
*/
|
|
575
964
|
export const optional = /*#__PURE__*/Struct_.lambda(self => optionalKey(UndefinedOr(self)));
|
|
576
965
|
/**
|
|
966
|
+
* Reverses {@link optional}, returning the inner schema (unwrapping `UndefinedOr`).
|
|
967
|
+
* Only applicable to schemas already wrapped with `optional`.
|
|
968
|
+
*
|
|
577
969
|
* @since 4.0.0
|
|
578
970
|
*/
|
|
579
971
|
export const required = /*#__PURE__*/Struct_.lambda(self => self.schema.members[0]);
|
|
580
972
|
/**
|
|
973
|
+
* Makes a struct field mutable (removes the `readonly` modifier on the property).
|
|
974
|
+
* Use {@link readonlyKey} to reverse.
|
|
975
|
+
*
|
|
581
976
|
* @since 4.0.0
|
|
582
977
|
*/
|
|
583
978
|
export const mutableKey = /*#__PURE__*/Struct_.lambda(schema => make(AST.mutableKey(schema.ast), {
|
|
584
979
|
schema
|
|
585
980
|
}));
|
|
586
981
|
/**
|
|
982
|
+
* Reverses {@link mutableKey}, returning the inner schema as readonly again.
|
|
983
|
+
* Only applicable to schemas already wrapped with `mutableKey`.
|
|
984
|
+
*
|
|
587
985
|
* @since 4.0.0
|
|
588
986
|
*/
|
|
589
987
|
export const readonlyKey = /*#__PURE__*/Struct_.lambda(self => self.schema);
|
|
590
988
|
/**
|
|
989
|
+
* Extracts the type-side schema: sets `Encoded` to equal the decoded `Type`,
|
|
990
|
+
* discarding the encoding transformation path.
|
|
991
|
+
*
|
|
591
992
|
* @since 4.0.0
|
|
592
993
|
*/
|
|
593
994
|
export const toType = /*#__PURE__*/Struct_.lambda(schema => make(AST.toType(schema.ast), {
|
|
594
995
|
schema
|
|
595
996
|
}));
|
|
596
997
|
/**
|
|
998
|
+
* Extracts the encoded-side schema: sets `Type` to equal the `Encoded`,
|
|
999
|
+
* discarding the decoding transformation path.
|
|
1000
|
+
*
|
|
597
1001
|
* @since 4.0.0
|
|
598
1002
|
*/
|
|
599
1003
|
export const toEncoded = /*#__PURE__*/Struct_.lambda(schema => make(AST.toEncoded(schema.ast), {
|
|
@@ -613,6 +1017,16 @@ export function flip(schema) {
|
|
|
613
1017
|
});
|
|
614
1018
|
}
|
|
615
1019
|
/**
|
|
1020
|
+
* Creates a schema for a single literal value (string, number, bigint, boolean, or null).
|
|
1021
|
+
*
|
|
1022
|
+
* **Example** (String literal)
|
|
1023
|
+
* ```ts
|
|
1024
|
+
* import { Schema } from "effect"
|
|
1025
|
+
*
|
|
1026
|
+
* const schema = Schema.Literal("hello")
|
|
1027
|
+
* // Type: Schema.Literal<"hello">
|
|
1028
|
+
* ```
|
|
1029
|
+
*
|
|
616
1030
|
* @see {@link Literals} for a schema that represents a union of literals.
|
|
617
1031
|
* @see {@link tag} for a schema that represents a literal value that can be
|
|
618
1032
|
* used as a discriminator field in tagged unions and has a constructor default.
|
|
@@ -634,6 +1048,18 @@ function templateLiteralFromParts(parts) {
|
|
|
634
1048
|
return new AST.TemplateLiteral(parts.map(part => isSchema(part) ? part.ast : new AST.Literal(part)));
|
|
635
1049
|
}
|
|
636
1050
|
/**
|
|
1051
|
+
* Creates a schema that validates strings matching a template literal pattern. Each part can be
|
|
1052
|
+
* a literal string/number/bigint or a schema whose encoded type is a string, number, or bigint.
|
|
1053
|
+
*
|
|
1054
|
+
* **Example** (URL path pattern)
|
|
1055
|
+
* ```ts
|
|
1056
|
+
* import { Schema } from "effect"
|
|
1057
|
+
*
|
|
1058
|
+
* const schema = Schema.TemplateLiteral(["/user/", Schema.Number])
|
|
1059
|
+
* // matches strings like "/user/123", "/user/42", etc.
|
|
1060
|
+
* ```
|
|
1061
|
+
*
|
|
1062
|
+
* @see {@link TemplateLiteralParser} for a schema that also parses matched parts into a tuple.
|
|
637
1063
|
* @since 4.0.0
|
|
638
1064
|
*/
|
|
639
1065
|
export function TemplateLiteral(parts) {
|
|
@@ -642,6 +1068,18 @@ export function TemplateLiteral(parts) {
|
|
|
642
1068
|
});
|
|
643
1069
|
}
|
|
644
1070
|
/**
|
|
1071
|
+
* Like {@link TemplateLiteral} but decodes the matched string into a readonly tuple of typed values,
|
|
1072
|
+
* one element per schema part.
|
|
1073
|
+
*
|
|
1074
|
+
* **Example** (Parse path parameters)
|
|
1075
|
+
* ```ts
|
|
1076
|
+
* import { Schema } from "effect"
|
|
1077
|
+
*
|
|
1078
|
+
* const schema = Schema.TemplateLiteralParser(["/user/", Schema.NumberFromString])
|
|
1079
|
+
* // decodes "/user/42" => readonly ["/user/", 42]
|
|
1080
|
+
* ```
|
|
1081
|
+
*
|
|
1082
|
+
* @see {@link TemplateLiteral} for a validation-only version that keeps the string encoded.
|
|
645
1083
|
* @since 4.0.0
|
|
646
1084
|
*/
|
|
647
1085
|
export function TemplateLiteralParser(parts) {
|
|
@@ -650,6 +1088,21 @@ export function TemplateLiteralParser(parts) {
|
|
|
650
1088
|
});
|
|
651
1089
|
}
|
|
652
1090
|
/**
|
|
1091
|
+
* Creates a schema from a TypeScript enum object. Validates that the input is one of the enum's values.
|
|
1092
|
+
*
|
|
1093
|
+
* **Example** (Direction enum)
|
|
1094
|
+
* ```ts
|
|
1095
|
+
* import { Schema } from "effect"
|
|
1096
|
+
*
|
|
1097
|
+
* enum Direction {
|
|
1098
|
+
* Up = "Up",
|
|
1099
|
+
* Down = "Down"
|
|
1100
|
+
* }
|
|
1101
|
+
*
|
|
1102
|
+
* const schema = Schema.Enum(Direction)
|
|
1103
|
+
* // accepts "Up" or "Down"
|
|
1104
|
+
* ```
|
|
1105
|
+
*
|
|
653
1106
|
* @since 4.0.0
|
|
654
1107
|
*/
|
|
655
1108
|
export function Enum(enums) {
|
|
@@ -658,84 +1111,102 @@ export function Enum(enums) {
|
|
|
658
1111
|
});
|
|
659
1112
|
}
|
|
660
1113
|
/**
|
|
1114
|
+
* Schema for the `never` type. Always fails validation — no value satisfies it.
|
|
1115
|
+
*
|
|
661
1116
|
* @since 4.0.0
|
|
662
1117
|
*/
|
|
663
1118
|
export const Never = /*#__PURE__*/make(AST.never);
|
|
664
1119
|
/**
|
|
1120
|
+
* Schema for the `any` type. Accepts any value without validation.
|
|
1121
|
+
*
|
|
1122
|
+
* @see {@link Unknown} for a safer alternative that uses `unknown`.
|
|
665
1123
|
* @since 4.0.0
|
|
666
1124
|
*/
|
|
667
1125
|
export const Any = /*#__PURE__*/make(AST.any);
|
|
668
1126
|
/**
|
|
1127
|
+
* Schema for the `unknown` type. Accepts any value without validation.
|
|
1128
|
+
*
|
|
1129
|
+
* @see {@link Any} for the `any` variant.
|
|
669
1130
|
* @since 4.0.0
|
|
670
1131
|
*/
|
|
671
1132
|
export const Unknown = /*#__PURE__*/make(AST.unknown);
|
|
672
1133
|
/**
|
|
1134
|
+
* Schema for the `null` literal. Validates that the input is strictly `null`.
|
|
1135
|
+
*
|
|
1136
|
+
* @see {@link NullOr} for a union with another schema.
|
|
673
1137
|
* @since 4.0.0
|
|
674
1138
|
*/
|
|
675
1139
|
export const Null = /*#__PURE__*/make(AST.null);
|
|
676
1140
|
/**
|
|
1141
|
+
* Schema for the `undefined` literal. Validates that the input is strictly `undefined`.
|
|
1142
|
+
*
|
|
1143
|
+
* @see {@link UndefinedOr} for a union with another schema.
|
|
677
1144
|
* @since 4.0.0
|
|
678
1145
|
*/
|
|
679
1146
|
export const Undefined = /*#__PURE__*/make(AST.undefined);
|
|
680
1147
|
/**
|
|
681
|
-
*
|
|
1148
|
+
* Schema for `string` values. Validates that the input is `typeof` `"string"`.
|
|
682
1149
|
*
|
|
683
1150
|
* @since 4.0.0
|
|
684
1151
|
*/
|
|
685
1152
|
export const String = /*#__PURE__*/make(AST.string);
|
|
686
1153
|
/**
|
|
687
|
-
*
|
|
1154
|
+
* Schema for `number` values, including `NaN`, `Infinity`, and `-Infinity`.
|
|
688
1155
|
*
|
|
689
1156
|
* **Default Json Serializer**
|
|
690
1157
|
*
|
|
691
|
-
* -
|
|
692
|
-
* -
|
|
1158
|
+
* - Finite numbers are serialized as numbers.
|
|
1159
|
+
* - Non-finite values are serialized as strings (`"NaN"`, `"Infinity"`, `"-Infinity"`).
|
|
693
1160
|
*
|
|
1161
|
+
* @see {@link Finite} for a schema that excludes non-finite values.
|
|
694
1162
|
* @since 4.0.0
|
|
695
1163
|
*/
|
|
696
1164
|
export const Number = /*#__PURE__*/make(AST.number);
|
|
697
1165
|
/**
|
|
698
|
-
*
|
|
1166
|
+
* Schema for `boolean` values. Validates that the input is `typeof` `"boolean"`.
|
|
699
1167
|
*
|
|
700
1168
|
* @category Boolean
|
|
701
1169
|
* @since 4.0.0
|
|
702
1170
|
*/
|
|
703
1171
|
export const Boolean = /*#__PURE__*/make(AST.boolean);
|
|
704
1172
|
/**
|
|
705
|
-
*
|
|
1173
|
+
* Schema for `symbol` values. Validates that the input is `typeof` `"symbol"`.
|
|
706
1174
|
*
|
|
1175
|
+
* @see {@link UniqueSymbol} for a schema that matches a specific symbol.
|
|
707
1176
|
* @since 4.0.0
|
|
708
1177
|
*/
|
|
709
1178
|
export const Symbol = /*#__PURE__*/make(AST.symbol);
|
|
710
1179
|
/**
|
|
711
|
-
*
|
|
1180
|
+
* Schema for `bigint` values. Validates that the input is `typeof` `"bigint"`.
|
|
712
1181
|
*
|
|
713
1182
|
* @since 4.0.0
|
|
714
1183
|
*/
|
|
715
1184
|
export const BigInt = /*#__PURE__*/make(AST.bigInt);
|
|
716
1185
|
/**
|
|
717
|
-
*
|
|
1186
|
+
* Schema for the `void` type. Accepts `undefined` as the encoded value.
|
|
718
1187
|
*
|
|
719
1188
|
* @since 4.0.0
|
|
720
1189
|
*/
|
|
721
1190
|
export const Void = /*#__PURE__*/make(AST.void);
|
|
722
1191
|
/**
|
|
723
|
-
*
|
|
1192
|
+
* Schema for the `object` type. Validates that the input is a non-null object or function
|
|
1193
|
+
* (i.e. `typeof value === "object" && value !== null || typeof value === "function"`).
|
|
724
1194
|
*
|
|
725
1195
|
* @since 4.0.0
|
|
726
1196
|
*/
|
|
727
1197
|
export const ObjectKeyword = /*#__PURE__*/make(AST.objectKeyword);
|
|
728
1198
|
/**
|
|
729
|
-
*
|
|
730
|
-
*
|
|
731
|
-
* **Example**
|
|
1199
|
+
* Creates a schema for a specific symbol. Only that exact symbol satisfies the schema.
|
|
732
1200
|
*
|
|
1201
|
+
* **Example** (Specific symbol)
|
|
733
1202
|
* ```ts
|
|
734
1203
|
* import { Schema } from "effect"
|
|
735
1204
|
*
|
|
736
|
-
* const
|
|
737
|
-
* const schema = Schema.UniqueSymbol(
|
|
1205
|
+
* const mySymbol = Symbol.for("mySymbol")
|
|
1206
|
+
* const schema = Schema.UniqueSymbol(mySymbol)
|
|
738
1207
|
* ```
|
|
1208
|
+
*
|
|
1209
|
+
* @see {@link Symbol} for a schema that accepts any symbol.
|
|
739
1210
|
* @since 4.0.0
|
|
740
1211
|
*/
|
|
741
1212
|
export function UniqueSymbol(symbol) {
|
|
@@ -751,6 +1222,34 @@ function makeStruct(ast, fields) {
|
|
|
751
1222
|
});
|
|
752
1223
|
}
|
|
753
1224
|
/**
|
|
1225
|
+
* Defines a struct schema from a map of field schemas.
|
|
1226
|
+
*
|
|
1227
|
+
* Each field value is a schema. Use {@link optionalKey} or {@link optional} to
|
|
1228
|
+
* mark fields as optional, and {@link mutableKey} to mark them as mutable.
|
|
1229
|
+
*
|
|
1230
|
+
* The resulting schema's `Type` is a readonly object type with the fields'
|
|
1231
|
+
* decoded types. The `Encoded` form mirrors the field schemas' encoded types.
|
|
1232
|
+
*
|
|
1233
|
+
* **Example** (Basic struct)
|
|
1234
|
+
*
|
|
1235
|
+
* ```ts
|
|
1236
|
+
* import { Schema } from "effect"
|
|
1237
|
+
*
|
|
1238
|
+
* const Person = Schema.Struct({
|
|
1239
|
+
* name: Schema.String,
|
|
1240
|
+
* age: Schema.Number,
|
|
1241
|
+
* email: Schema.optionalKey(Schema.String)
|
|
1242
|
+
* })
|
|
1243
|
+
*
|
|
1244
|
+
* // { readonly name: string; readonly age: number; readonly email?: string }
|
|
1245
|
+
* type Person = typeof Person.Type
|
|
1246
|
+
*
|
|
1247
|
+
* const alice = Schema.decodeUnknownSync(Person)({ name: "Alice", age: 30 })
|
|
1248
|
+
* console.log(alice)
|
|
1249
|
+
* // { name: 'Alice', age: 30 }
|
|
1250
|
+
* ```
|
|
1251
|
+
*
|
|
1252
|
+
* @category Constructors
|
|
754
1253
|
* @since 4.0.0
|
|
755
1254
|
*/
|
|
756
1255
|
export function Struct(fields) {
|
|
@@ -778,6 +1277,26 @@ export function fieldsAssign(fields) {
|
|
|
778
1277
|
return Struct_.lambda(struct => struct.mapFields(Struct_.assign(fields)));
|
|
779
1278
|
}
|
|
780
1279
|
/**
|
|
1280
|
+
* Renames struct keys in the encoded form without changing the decoded type.
|
|
1281
|
+
*
|
|
1282
|
+
* Takes a partial mapping `{ decodedKey: encodedKey }` and produces a
|
|
1283
|
+
* transformation schema that decodes from the renamed keys and encodes back to
|
|
1284
|
+
* the renamed keys. Keys not present in the mapping are left unchanged.
|
|
1285
|
+
*
|
|
1286
|
+
* **Example** (Rename `name` to `full_name` in the encoded form)
|
|
1287
|
+
*
|
|
1288
|
+
* ```ts
|
|
1289
|
+
* import { Schema } from "effect"
|
|
1290
|
+
*
|
|
1291
|
+
* const Person = Schema.Struct({ name: Schema.String, age: Schema.Number })
|
|
1292
|
+
* const Encoded = Person.pipe(Schema.encodeKeys({ name: "full_name" }))
|
|
1293
|
+
*
|
|
1294
|
+
* // Decodes { full_name: "Alice", age: 30 } → { name: "Alice", age: 30 }
|
|
1295
|
+
* const alice = Schema.decodeUnknownSync(Encoded)({ full_name: "Alice", age: 30 })
|
|
1296
|
+
* console.log(alice)
|
|
1297
|
+
* // { name: 'Alice', age: 30 }
|
|
1298
|
+
* ```
|
|
1299
|
+
*
|
|
781
1300
|
* @category Struct transformations
|
|
782
1301
|
* @since 4.0.0
|
|
783
1302
|
*/
|
|
@@ -801,6 +1320,31 @@ export function encodeKeys(mapping) {
|
|
|
801
1320
|
};
|
|
802
1321
|
}
|
|
803
1322
|
/**
|
|
1323
|
+
* Adds derived fields to a struct schema during decoding.
|
|
1324
|
+
*
|
|
1325
|
+
* Each new field is derived from the decoded struct value via a function that
|
|
1326
|
+
* returns `Option`. On encoding the derived fields are stripped. This allows
|
|
1327
|
+
* computed or enriched fields to live in the decoded type without appearing in
|
|
1328
|
+
* the encoded form.
|
|
1329
|
+
*
|
|
1330
|
+
* **Example** (Add a computed `fullName` field)
|
|
1331
|
+
*
|
|
1332
|
+
* ```ts
|
|
1333
|
+
* import { Option, Schema } from "effect"
|
|
1334
|
+
*
|
|
1335
|
+
* const Person = Schema.Struct({ first: Schema.String, last: Schema.String })
|
|
1336
|
+
* const Extended = Person.pipe(
|
|
1337
|
+
* Schema.extendTo(
|
|
1338
|
+
* { fullName: Schema.String },
|
|
1339
|
+
* { fullName: (p) => Option.some(`${p.first} ${p.last}`) }
|
|
1340
|
+
* )
|
|
1341
|
+
* )
|
|
1342
|
+
*
|
|
1343
|
+
* const alice = Schema.decodeUnknownSync(Extended)({ first: "Alice", last: "Smith" })
|
|
1344
|
+
* console.log(alice.fullName)
|
|
1345
|
+
* // Alice Smith
|
|
1346
|
+
* ```
|
|
1347
|
+
*
|
|
804
1348
|
* @since 4.0.0
|
|
805
1349
|
* @experimental
|
|
806
1350
|
*/
|
|
@@ -840,6 +1384,24 @@ derive) {
|
|
|
840
1384
|
};
|
|
841
1385
|
}
|
|
842
1386
|
/**
|
|
1387
|
+
* Defines a record (dictionary) schema with typed keys and values.
|
|
1388
|
+
*
|
|
1389
|
+
* **Example** (String-keyed record of numbers)
|
|
1390
|
+
*
|
|
1391
|
+
* ```ts
|
|
1392
|
+
* import { Schema } from "effect"
|
|
1393
|
+
*
|
|
1394
|
+
* const schema = Schema.Record(Schema.String, Schema.Number)
|
|
1395
|
+
*
|
|
1396
|
+
* // { readonly [x: string]: number }
|
|
1397
|
+
* type R = typeof schema.Type
|
|
1398
|
+
*
|
|
1399
|
+
* const result = Schema.decodeUnknownSync(schema)({ a: 1, b: 2 })
|
|
1400
|
+
* console.log(result)
|
|
1401
|
+
* // { a: 1, b: 2 }
|
|
1402
|
+
* ```
|
|
1403
|
+
*
|
|
1404
|
+
* @category Constructors
|
|
843
1405
|
* @since 4.0.0
|
|
844
1406
|
*/
|
|
845
1407
|
export function Record(key, value, options) {
|
|
@@ -850,6 +1412,24 @@ export function Record(key, value, options) {
|
|
|
850
1412
|
});
|
|
851
1413
|
}
|
|
852
1414
|
/**
|
|
1415
|
+
* Extends a struct schema with one or more record (index-signature) schemas,
|
|
1416
|
+
* producing a schema whose decoded type intersects the struct and all records.
|
|
1417
|
+
*
|
|
1418
|
+
* **Example** (Struct with string-indexed extra keys)
|
|
1419
|
+
*
|
|
1420
|
+
* ```ts
|
|
1421
|
+
* import { Schema } from "effect"
|
|
1422
|
+
*
|
|
1423
|
+
* const schema = Schema.StructWithRest(
|
|
1424
|
+
* Schema.Struct({ id: Schema.Number }),
|
|
1425
|
+
* [Schema.Record(Schema.String, Schema.String)]
|
|
1426
|
+
* )
|
|
1427
|
+
*
|
|
1428
|
+
* // { readonly id: number } & { readonly [x: string]: string }
|
|
1429
|
+
* type T = typeof schema.Type
|
|
1430
|
+
* ```
|
|
1431
|
+
*
|
|
1432
|
+
* @category Constructors
|
|
853
1433
|
* @since 4.0.0
|
|
854
1434
|
*/
|
|
855
1435
|
export function StructWithRest(schema, records) {
|
|
@@ -868,6 +1448,20 @@ function makeTuple(ast, elements) {
|
|
|
868
1448
|
});
|
|
869
1449
|
}
|
|
870
1450
|
/**
|
|
1451
|
+
* Defines a fixed-length tuple schema from an array of element schemas.
|
|
1452
|
+
*
|
|
1453
|
+
* **Example** (Pair of string and number)
|
|
1454
|
+
*
|
|
1455
|
+
* ```ts
|
|
1456
|
+
* import { Schema } from "effect"
|
|
1457
|
+
*
|
|
1458
|
+
* const schema = Schema.Tuple([Schema.String, Schema.Number])
|
|
1459
|
+
*
|
|
1460
|
+
* const pair = Schema.decodeUnknownSync(schema)(["hello", 42])
|
|
1461
|
+
* console.log(pair)
|
|
1462
|
+
* // [ 'hello', 42 ]
|
|
1463
|
+
* ```
|
|
1464
|
+
*
|
|
871
1465
|
* @category Constructors
|
|
872
1466
|
* @since 4.0.0
|
|
873
1467
|
*/
|
|
@@ -875,6 +1469,26 @@ export function Tuple(elements) {
|
|
|
875
1469
|
return makeTuple(AST.tuple(elements), elements);
|
|
876
1470
|
}
|
|
877
1471
|
/**
|
|
1472
|
+
* Extends a fixed-length tuple schema with rest elements, creating a variadic
|
|
1473
|
+
* tuple that starts with the fixed elements and ends with zero or more rest
|
|
1474
|
+
* elements.
|
|
1475
|
+
*
|
|
1476
|
+
* **Example** (Tuple with rest)
|
|
1477
|
+
*
|
|
1478
|
+
* ```ts
|
|
1479
|
+
* import { Schema } from "effect"
|
|
1480
|
+
*
|
|
1481
|
+
* // [string, number, ...boolean[]]
|
|
1482
|
+
* const schema = Schema.TupleWithRest(
|
|
1483
|
+
* Schema.Tuple([Schema.String, Schema.Number]),
|
|
1484
|
+
* [Schema.Boolean]
|
|
1485
|
+
* )
|
|
1486
|
+
*
|
|
1487
|
+
* const result = Schema.decodeUnknownSync(schema)(["hello", 1, true, false])
|
|
1488
|
+
* console.log(result)
|
|
1489
|
+
* // [ 'hello', 1, true, false ]
|
|
1490
|
+
* ```
|
|
1491
|
+
*
|
|
878
1492
|
* @category Constructors
|
|
879
1493
|
* @since 4.0.0
|
|
880
1494
|
*/
|
|
@@ -885,6 +1499,20 @@ export function TupleWithRest(schema, rest) {
|
|
|
885
1499
|
});
|
|
886
1500
|
}
|
|
887
1501
|
/**
|
|
1502
|
+
* Defines a `ReadonlyArray` schema for a given element schema.
|
|
1503
|
+
*
|
|
1504
|
+
* **Example** (Array of strings)
|
|
1505
|
+
*
|
|
1506
|
+
* ```ts
|
|
1507
|
+
* import { Schema } from "effect"
|
|
1508
|
+
*
|
|
1509
|
+
* const schema = Schema.Array(Schema.String)
|
|
1510
|
+
*
|
|
1511
|
+
* const result = Schema.decodeUnknownSync(schema)(["a", "b", "c"])
|
|
1512
|
+
* console.log(result)
|
|
1513
|
+
* // [ 'a', 'b', 'c' ]
|
|
1514
|
+
* ```
|
|
1515
|
+
*
|
|
888
1516
|
* @category Constructors
|
|
889
1517
|
* @since 4.0.0
|
|
890
1518
|
*/
|
|
@@ -892,6 +1520,20 @@ export const Array = /*#__PURE__*/Struct_.lambda(schema => make(new AST.Arrays(f
|
|
|
892
1520
|
schema
|
|
893
1521
|
}));
|
|
894
1522
|
/**
|
|
1523
|
+
* Defines a non-empty `ReadonlyArray` schema — at least one element required.
|
|
1524
|
+
* Type is `readonly [T, ...T[]]`.
|
|
1525
|
+
*
|
|
1526
|
+
* **Example** (Non-empty array of numbers)
|
|
1527
|
+
*
|
|
1528
|
+
* ```ts
|
|
1529
|
+
* import { Schema } from "effect"
|
|
1530
|
+
*
|
|
1531
|
+
* const schema = Schema.NonEmptyArray(Schema.Number)
|
|
1532
|
+
*
|
|
1533
|
+
* Schema.decodeUnknownSync(schema)([1, 2, 3]) // ok
|
|
1534
|
+
* Schema.decodeUnknownSync(schema)([]) // throws
|
|
1535
|
+
* ```
|
|
1536
|
+
*
|
|
895
1537
|
* @category Constructors
|
|
896
1538
|
* @since 4.0.0
|
|
897
1539
|
*/
|
|
@@ -911,7 +1553,18 @@ export function UniqueArray(item) {
|
|
|
911
1553
|
return Array(item).check(isUnique());
|
|
912
1554
|
}
|
|
913
1555
|
/**
|
|
914
|
-
* Makes
|
|
1556
|
+
* Makes an array or tuple schema mutable, removing the `readonly` modifier.
|
|
1557
|
+
*
|
|
1558
|
+
* **Example** (Mutable array)
|
|
1559
|
+
*
|
|
1560
|
+
* ```ts
|
|
1561
|
+
* import { Schema } from "effect"
|
|
1562
|
+
*
|
|
1563
|
+
* const schema = Schema.mutable(Schema.Array(Schema.Number))
|
|
1564
|
+
*
|
|
1565
|
+
* // number[] (mutable)
|
|
1566
|
+
* type T = typeof schema.Type
|
|
1567
|
+
* ```
|
|
915
1568
|
*
|
|
916
1569
|
* @since 4.0.0
|
|
917
1570
|
*/
|
|
@@ -930,12 +1583,23 @@ function makeUnion(ast, members) {
|
|
|
930
1583
|
});
|
|
931
1584
|
}
|
|
932
1585
|
/**
|
|
933
|
-
* Creates a schema
|
|
1586
|
+
* Creates a union schema from an array of member schemas. Members are tested in
|
|
1587
|
+
* order; the first match is returned.
|
|
1588
|
+
*
|
|
1589
|
+
* Optionally, specify `mode`:
|
|
1590
|
+
* - `"anyOf"` (default) — matches if any member matches.
|
|
1591
|
+
* - `"oneOf"` — matches if exactly one member matches.
|
|
1592
|
+
*
|
|
1593
|
+
* **Example** (String or number union)
|
|
934
1594
|
*
|
|
935
|
-
*
|
|
1595
|
+
* ```ts
|
|
1596
|
+
* import { Schema } from "effect"
|
|
936
1597
|
*
|
|
937
|
-
*
|
|
938
|
-
*
|
|
1598
|
+
* const schema = Schema.Union([Schema.String, Schema.Number])
|
|
1599
|
+
*
|
|
1600
|
+
* Schema.decodeUnknownSync(schema)("hello") // "hello"
|
|
1601
|
+
* Schema.decodeUnknownSync(schema)(42) // 42
|
|
1602
|
+
* ```
|
|
939
1603
|
*
|
|
940
1604
|
* @category Constructors
|
|
941
1605
|
* @since 4.0.0
|
|
@@ -944,6 +1608,16 @@ export function Union(members, options) {
|
|
|
944
1608
|
return makeUnion(AST.union(members, options?.mode ?? "anyOf", undefined), members);
|
|
945
1609
|
}
|
|
946
1610
|
/**
|
|
1611
|
+
* Creates a union schema from an array of literal values.
|
|
1612
|
+
*
|
|
1613
|
+
* **Example** (Status codes)
|
|
1614
|
+
* ```ts
|
|
1615
|
+
* import { Schema } from "effect"
|
|
1616
|
+
*
|
|
1617
|
+
* const schema = Schema.Literals(["active", "inactive", "pending"])
|
|
1618
|
+
* // accepts "active", "inactive", or "pending"
|
|
1619
|
+
* ```
|
|
1620
|
+
*
|
|
947
1621
|
* @see {@link Literal} for a schema that represents a single literal.
|
|
948
1622
|
* @category Constructors
|
|
949
1623
|
* @since 4.0.0
|
|
@@ -965,16 +1639,21 @@ export function Literals(literals) {
|
|
|
965
1639
|
});
|
|
966
1640
|
}
|
|
967
1641
|
/**
|
|
1642
|
+
* Creates a union schema of `S | null`.
|
|
1643
|
+
*
|
|
968
1644
|
* @category Constructors
|
|
969
1645
|
* @since 4.0.0
|
|
970
1646
|
*/
|
|
971
1647
|
export const NullOr = /*#__PURE__*/Struct_.lambda(self => Union([self, Null]));
|
|
972
1648
|
/**
|
|
1649
|
+
* Creates a union schema of `S | undefined`.
|
|
1650
|
+
*
|
|
973
1651
|
* @category Constructors
|
|
974
1652
|
* @since 4.0.0
|
|
975
1653
|
*/
|
|
976
1654
|
export const UndefinedOr = /*#__PURE__*/Struct_.lambda(self => Union([self, Undefined]));
|
|
977
1655
|
/**
|
|
1656
|
+
* Creates a union schema of `S | null | undefined`.
|
|
978
1657
|
* @category Constructors
|
|
979
1658
|
* @since 4.0.0
|
|
980
1659
|
*/
|
|
@@ -984,6 +1663,21 @@ export const NullishOr = /*#__PURE__*/Struct_.lambda(self => Union([self, Null,
|
|
|
984
1663
|
* essential for creating recursive schemas where a schema references itself,
|
|
985
1664
|
* preventing infinite recursion during schema definition.
|
|
986
1665
|
*
|
|
1666
|
+
* **Example** (Recursive tree schema)
|
|
1667
|
+
* ```ts
|
|
1668
|
+
* import { Schema } from "effect"
|
|
1669
|
+
*
|
|
1670
|
+
* interface Tree {
|
|
1671
|
+
* readonly value: number
|
|
1672
|
+
* readonly children: ReadonlyArray<Tree>
|
|
1673
|
+
* }
|
|
1674
|
+
*
|
|
1675
|
+
* const Tree = Schema.Struct({
|
|
1676
|
+
* value: Schema.Number,
|
|
1677
|
+
* children: Schema.Array(Schema.suspend((): Schema.Codec<Tree> => Tree))
|
|
1678
|
+
* })
|
|
1679
|
+
* ```
|
|
1680
|
+
*
|
|
987
1681
|
* @category Constructors
|
|
988
1682
|
* @since 4.0.0
|
|
989
1683
|
*/
|
|
@@ -991,6 +1685,18 @@ export function suspend(f) {
|
|
|
991
1685
|
return make(new AST.Suspend(() => f().ast));
|
|
992
1686
|
}
|
|
993
1687
|
/**
|
|
1688
|
+
* Pipeable function that attaches one or more filter checks to a schema without
|
|
1689
|
+
* changing the TypeScript type.
|
|
1690
|
+
*
|
|
1691
|
+
* **Example** (Adding checks to a schema)
|
|
1692
|
+
* ```ts
|
|
1693
|
+
* import { Schema } from "effect"
|
|
1694
|
+
*
|
|
1695
|
+
* const AgeSchema = Schema.Number.pipe(
|
|
1696
|
+
* Schema.check(Schema.isGreaterThanOrEqualTo(0), Schema.isLessThanOrEqualTo(120))
|
|
1697
|
+
* )
|
|
1698
|
+
* ```
|
|
1699
|
+
*
|
|
994
1700
|
* @category Filtering
|
|
995
1701
|
* @since 4.0.0
|
|
996
1702
|
*/
|
|
@@ -998,6 +1704,9 @@ export function check(...checks) {
|
|
|
998
1704
|
return self => self.check(...checks);
|
|
999
1705
|
}
|
|
1000
1706
|
/**
|
|
1707
|
+
* Narrows the TypeScript type of a schema's output via a type guard predicate,
|
|
1708
|
+
* attaching the guard as a runtime filter check.
|
|
1709
|
+
*
|
|
1001
1710
|
* @category Filtering
|
|
1002
1711
|
* @since 4.0.0
|
|
1003
1712
|
*/
|
|
@@ -1007,7 +1716,8 @@ export function refine(refinement, annotations) {
|
|
|
1007
1716
|
});
|
|
1008
1717
|
}
|
|
1009
1718
|
/**
|
|
1010
|
-
* Adds a brand to a schema
|
|
1719
|
+
* Adds a nominal brand to a schema, intersecting the output type with
|
|
1720
|
+
* `Brand.Brand<B>` to prevent accidental mixing of structurally identical types.
|
|
1011
1721
|
*
|
|
1012
1722
|
* @category Branding
|
|
1013
1723
|
* @since 4.0.0
|
|
@@ -1019,7 +1729,10 @@ export function brand(identifier) {
|
|
|
1019
1729
|
});
|
|
1020
1730
|
}
|
|
1021
1731
|
/**
|
|
1022
|
-
* @
|
|
1732
|
+
* Creates a branded schema from a {@link Brand.Constructor}, applying the
|
|
1733
|
+
* constructor's checks and brand tag to the underlying schema.
|
|
1734
|
+
*
|
|
1735
|
+
* @category Branding
|
|
1023
1736
|
* @since 4.0.0
|
|
1024
1737
|
*/
|
|
1025
1738
|
export function fromBrand(identifier, ctor) {
|
|
@@ -1028,6 +1741,25 @@ export function fromBrand(identifier, ctor) {
|
|
|
1028
1741
|
};
|
|
1029
1742
|
}
|
|
1030
1743
|
/**
|
|
1744
|
+
* Intercepts the decoding pipeline of a schema.
|
|
1745
|
+
*
|
|
1746
|
+
* The provided function receives the current decoding `Effect` and `ParseOptions`,
|
|
1747
|
+
* and returns a new `Effect` — potentially adding service requirements (`RD`),
|
|
1748
|
+
* recovering from errors, or augmenting the result.
|
|
1749
|
+
*
|
|
1750
|
+
* **Example** (Logging decode failures)
|
|
1751
|
+
*
|
|
1752
|
+
* ```ts
|
|
1753
|
+
* import { Effect, Schema } from "effect"
|
|
1754
|
+
*
|
|
1755
|
+
* const Logged = Schema.String.pipe(
|
|
1756
|
+
* Schema.middlewareDecoding((effect) =>
|
|
1757
|
+
* Effect.tapError(effect, (issue) => Effect.log("decode failed", issue))
|
|
1758
|
+
* )
|
|
1759
|
+
* )
|
|
1760
|
+
* ```
|
|
1761
|
+
*
|
|
1762
|
+
* @see {@link catchDecoding} for a simpler error-recovery variant
|
|
1031
1763
|
* @since 4.0.0
|
|
1032
1764
|
*/
|
|
1033
1765
|
export function middlewareDecoding(decode) {
|
|
@@ -1036,6 +1768,25 @@ export function middlewareDecoding(decode) {
|
|
|
1036
1768
|
});
|
|
1037
1769
|
}
|
|
1038
1770
|
/**
|
|
1771
|
+
* Intercepts the encoding pipeline of a schema.
|
|
1772
|
+
*
|
|
1773
|
+
* The provided function receives the current encoding `Effect` and `ParseOptions`,
|
|
1774
|
+
* and returns a new `Effect` — potentially adding service requirements (`RE`),
|
|
1775
|
+
* recovering from errors, or augmenting the result.
|
|
1776
|
+
*
|
|
1777
|
+
* **Example** (Logging encode failures)
|
|
1778
|
+
*
|
|
1779
|
+
* ```ts
|
|
1780
|
+
* import { Effect, Schema } from "effect"
|
|
1781
|
+
*
|
|
1782
|
+
* const Logged = Schema.String.pipe(
|
|
1783
|
+
* Schema.middlewareEncoding((effect) =>
|
|
1784
|
+
* Effect.tapError(effect, (issue) => Effect.log("encode failed", issue))
|
|
1785
|
+
* )
|
|
1786
|
+
* )
|
|
1787
|
+
* ```
|
|
1788
|
+
*
|
|
1789
|
+
* @see {@link catchEncoding} for a simpler error-recovery variant
|
|
1039
1790
|
* @since 4.0.0
|
|
1040
1791
|
*/
|
|
1041
1792
|
export function middlewareEncoding(encode) {
|
|
@@ -1044,24 +1795,50 @@ export function middlewareEncoding(encode) {
|
|
|
1044
1795
|
});
|
|
1045
1796
|
}
|
|
1046
1797
|
/**
|
|
1798
|
+
* Recovers from a decoding error by providing a fallback value.
|
|
1799
|
+
*
|
|
1800
|
+
* The handler receives the `Issue` and returns an `Effect` that either
|
|
1801
|
+
* succeeds with a fallback value or re-fails with a (possibly different) issue.
|
|
1802
|
+
*
|
|
1803
|
+
* **Example** (Returning a default on decode failure)
|
|
1804
|
+
*
|
|
1805
|
+
* ```ts
|
|
1806
|
+
* import { Effect, Option, Schema } from "effect"
|
|
1807
|
+
*
|
|
1808
|
+
* const schema = Schema.Number.pipe(
|
|
1809
|
+
* Schema.catchDecoding((_issue) => Effect.succeed(Option.some(0)))
|
|
1810
|
+
* )
|
|
1811
|
+
* ```
|
|
1812
|
+
*
|
|
1813
|
+
* @see {@link catchDecodingWithContext} to add service requirements to the handler
|
|
1047
1814
|
* @since 4.0.0
|
|
1048
1815
|
*/
|
|
1049
1816
|
export function catchDecoding(f) {
|
|
1050
1817
|
return catchDecodingWithContext(f);
|
|
1051
1818
|
}
|
|
1052
1819
|
/**
|
|
1820
|
+
* Like {@link catchDecoding}, but the handler may require Effect services (`R`).
|
|
1821
|
+
*
|
|
1053
1822
|
* @since 4.0.0
|
|
1054
1823
|
*/
|
|
1055
1824
|
export function catchDecodingWithContext(f) {
|
|
1056
1825
|
return self => self.pipe(middlewareDecoding(Effect.catchEager(f)));
|
|
1057
1826
|
}
|
|
1058
1827
|
/**
|
|
1828
|
+
* Recovers from an encoding error by providing a fallback value.
|
|
1829
|
+
*
|
|
1830
|
+
* The handler receives the `Issue` and returns an `Effect` that either
|
|
1831
|
+
* succeeds with a fallback value or re-fails with a (possibly different) issue.
|
|
1832
|
+
*
|
|
1833
|
+
* @see {@link catchEncodingWithContext} to add service requirements to the handler
|
|
1059
1834
|
* @since 4.0.0
|
|
1060
1835
|
*/
|
|
1061
1836
|
export function catchEncoding(f) {
|
|
1062
1837
|
return catchEncodingWithContext(f);
|
|
1063
1838
|
}
|
|
1064
1839
|
/**
|
|
1840
|
+
* Like {@link catchEncoding}, but the handler may require Effect services (`R`).
|
|
1841
|
+
*
|
|
1065
1842
|
* @since 4.0.0
|
|
1066
1843
|
*/
|
|
1067
1844
|
export function catchEncodingWithContext(f) {
|
|
@@ -1123,6 +1900,25 @@ export function encodeTo(to, transformation) {
|
|
|
1123
1900
|
};
|
|
1124
1901
|
}
|
|
1125
1902
|
/**
|
|
1903
|
+
* Applies a transformation to a schema's encoded type, creating a new schema where encoding/decoding
|
|
1904
|
+
* operate on `S["Encoded"]` rather than `S["Type"]`.
|
|
1905
|
+
*
|
|
1906
|
+
* The `decode` getter maps `S["Encoded"]` → `S["Encoded"]` (applied during decoding),
|
|
1907
|
+
* and the `encode` getter maps `S["Encoded"]` → `S["Encoded"]` (applied during encoding).
|
|
1908
|
+
*
|
|
1909
|
+
* **Example** (Upper-casing encoded strings)
|
|
1910
|
+
*
|
|
1911
|
+
* ```ts
|
|
1912
|
+
* import { Schema, SchemaGetter } from "effect"
|
|
1913
|
+
*
|
|
1914
|
+
* const UpperFromLower = Schema.String.pipe(
|
|
1915
|
+
* Schema.encode({
|
|
1916
|
+
* decode: SchemaGetter.transform((s: string) => s.toLowerCase()),
|
|
1917
|
+
* encode: SchemaGetter.transform((s: string) => s.toUpperCase())
|
|
1918
|
+
* })
|
|
1919
|
+
* )
|
|
1920
|
+
* ```
|
|
1921
|
+
*
|
|
1126
1922
|
* @since 4.0.0
|
|
1127
1923
|
*/
|
|
1128
1924
|
export function encode(transformation) {
|
|
@@ -1131,6 +1927,32 @@ export function encode(transformation) {
|
|
|
1131
1927
|
};
|
|
1132
1928
|
}
|
|
1133
1929
|
/**
|
|
1930
|
+
* Attaches a constructor default value to a schema field.
|
|
1931
|
+
*
|
|
1932
|
+
* The `defaultValue` function receives `Option.some(undefined)` when the field is
|
|
1933
|
+
* explicitly set to `undefined`, or `Option.none()` when the field is absent.
|
|
1934
|
+
* Return `Option.some(value)` to supply a default, or `Option.none()` to leave the
|
|
1935
|
+
* field required. An `Effect` may be returned for async or service-dependent defaults.
|
|
1936
|
+
*
|
|
1937
|
+
* Constructor defaults are applied only during `makeUnsafe` / `make`, not during
|
|
1938
|
+
* decoding or encoding.
|
|
1939
|
+
*
|
|
1940
|
+
* **Example** (Optional field with a static default)
|
|
1941
|
+
*
|
|
1942
|
+
* ```ts
|
|
1943
|
+
* import { Option, Schema } from "effect"
|
|
1944
|
+
*
|
|
1945
|
+
* const MySchema = Schema.Struct({
|
|
1946
|
+
* name: Schema.String.pipe(
|
|
1947
|
+
* Schema.optionalKey,
|
|
1948
|
+
* Schema.withConstructorDefault(() => Option.some("anonymous"))
|
|
1949
|
+
* )
|
|
1950
|
+
* })
|
|
1951
|
+
*
|
|
1952
|
+
* const value = MySchema.makeUnsafe({})
|
|
1953
|
+
* // value: { name: "anonymous" }
|
|
1954
|
+
* ```
|
|
1955
|
+
*
|
|
1134
1956
|
* @since 4.0.0
|
|
1135
1957
|
*/
|
|
1136
1958
|
export function withConstructorDefault(defaultValue) {
|
|
@@ -1147,6 +1969,27 @@ export function withConstructorDefault(defaultValue) {
|
|
|
1147
1969
|
* - `passthrough`: (default) Pass the default value through to the output.
|
|
1148
1970
|
* - `omit`: Omit the value from the output.
|
|
1149
1971
|
*
|
|
1972
|
+
* Provides a default value for a missing key during decoding.
|
|
1973
|
+
*
|
|
1974
|
+
* When the key is absent from the input, `defaultValue()` is called to supply a value.
|
|
1975
|
+
* During encoding, the behavior is controlled by `options.encodingStrategy`:
|
|
1976
|
+
* - `"passthrough"` (default): include the value in the output
|
|
1977
|
+
* - `"omit"`: omit the key from the output
|
|
1978
|
+
*
|
|
1979
|
+
* **Example** (Default for a missing struct key)
|
|
1980
|
+
*
|
|
1981
|
+
* ```ts
|
|
1982
|
+
* import { Schema } from "effect"
|
|
1983
|
+
*
|
|
1984
|
+
* const MySchema = Schema.Struct({
|
|
1985
|
+
* name: Schema.String.pipe(Schema.withDecodingDefaultKey(() => "anonymous"))
|
|
1986
|
+
* })
|
|
1987
|
+
*
|
|
1988
|
+
* const result = Schema.decodeUnknownSync(MySchema)({})
|
|
1989
|
+
* // result: { name: "anonymous" }
|
|
1990
|
+
* ```
|
|
1991
|
+
*
|
|
1992
|
+
* @see {@link withDecodingDefault} for the `optional` (value-level) variant
|
|
1150
1993
|
* @since 4.0.0
|
|
1151
1994
|
*/
|
|
1152
1995
|
export function withDecodingDefaultKey(defaultValue, options) {
|
|
@@ -1165,6 +2008,30 @@ export function withDecodingDefaultKey(defaultValue, options) {
|
|
|
1165
2008
|
* - `passthrough`: (default) Pass the default value through to the output.
|
|
1166
2009
|
* - `omit`: Omit the value from the output.
|
|
1167
2010
|
*
|
|
2011
|
+
* Provides a default value for an `optional` field during decoding.
|
|
2012
|
+
*
|
|
2013
|
+
* Similar to {@link withDecodingDefaultKey} but works on `optional` (value-level optional)
|
|
2014
|
+
* rather than `optionalKey` (key-level optional).
|
|
2015
|
+
*
|
|
2016
|
+
* When the value is `undefined` or absent, `defaultValue()` is called to supply a value.
|
|
2017
|
+
* During encoding, the behavior is controlled by `options.encodingStrategy`:
|
|
2018
|
+
* - `"passthrough"` (default): include the value in the output
|
|
2019
|
+
* - `"omit"`: omit the value from the output
|
|
2020
|
+
*
|
|
2021
|
+
* **Example** (Default for an optional field value)
|
|
2022
|
+
*
|
|
2023
|
+
* ```ts
|
|
2024
|
+
* import { Schema } from "effect"
|
|
2025
|
+
*
|
|
2026
|
+
* const MySchema = Schema.Struct({
|
|
2027
|
+
* name: Schema.String.pipe(Schema.optional, Schema.withDecodingDefault(() => "anonymous"))
|
|
2028
|
+
* })
|
|
2029
|
+
*
|
|
2030
|
+
* const result = Schema.decodeUnknownSync(MySchema)({ name: undefined })
|
|
2031
|
+
* // result: { name: "anonymous" }
|
|
2032
|
+
* ```
|
|
2033
|
+
*
|
|
2034
|
+
* @see {@link withDecodingDefaultKey} for the key-level variant
|
|
1168
2035
|
* @since 4.0.0
|
|
1169
2036
|
*/
|
|
1170
2037
|
export function withDecodingDefault(defaultValue, options) {
|
|
@@ -1177,23 +2044,50 @@ export function withDecodingDefault(defaultValue, options) {
|
|
|
1177
2044
|
};
|
|
1178
2045
|
}
|
|
1179
2046
|
/**
|
|
1180
|
-
*
|
|
1181
|
-
*
|
|
2047
|
+
* Combines a {@link Literal} schema with {@link withConstructorDefault}, making it ideal
|
|
2048
|
+
* for discriminator fields in tagged unions. When constructing via `makeUnsafe`, the
|
|
2049
|
+
* `_tag` field can be omitted and will be filled automatically.
|
|
2050
|
+
*
|
|
2051
|
+
* **Example** (Discriminated union tag)
|
|
2052
|
+
*
|
|
2053
|
+
* ```ts
|
|
2054
|
+
* import { Schema } from "effect"
|
|
2055
|
+
*
|
|
2056
|
+
* const A = Schema.Struct({ _tag: Schema.tag("A"), value: Schema.Number })
|
|
1182
2057
|
*
|
|
1183
|
-
*
|
|
1184
|
-
*
|
|
1185
|
-
*
|
|
1186
|
-
*
|
|
2058
|
+
* // _tag is optional in makeUnsafe, auto-filled to "A"
|
|
2059
|
+
* const a = A.makeUnsafe({ value: 42 })
|
|
2060
|
+
* // a: { _tag: "A", value: 42 }
|
|
2061
|
+
* ```
|
|
1187
2062
|
*
|
|
2063
|
+
* @see {@link tagDefaultOmit} to also omit the tag during encoding
|
|
2064
|
+
* @see {@link TaggedStruct} for a shorthand that adds `_tag` automatically
|
|
1188
2065
|
* @since 4.0.0
|
|
1189
2066
|
*/
|
|
1190
2067
|
export function tag(literal) {
|
|
1191
2068
|
return Literal(literal).pipe(withConstructorDefault(() => Option_.some(literal)));
|
|
1192
2069
|
}
|
|
1193
2070
|
/**
|
|
1194
|
-
*
|
|
1195
|
-
* the
|
|
2071
|
+
* Like {@link tag}, but additionally omits the tag field from the encoded output.
|
|
2072
|
+
* Useful when the encoded form (e.g. JSON) does not include the discriminator key,
|
|
2073
|
+
* but the decoded type and constructor still need it.
|
|
2074
|
+
*
|
|
2075
|
+
* **Example** (Tag omitted during encoding)
|
|
1196
2076
|
*
|
|
2077
|
+
* ```ts
|
|
2078
|
+
* import { Schema } from "effect"
|
|
2079
|
+
*
|
|
2080
|
+
* const A = Schema.Struct({
|
|
2081
|
+
* _tag: Schema.tagDefaultOmit("A"),
|
|
2082
|
+
* value: Schema.Number
|
|
2083
|
+
* })
|
|
2084
|
+
*
|
|
2085
|
+
* // Encode strips the _tag field
|
|
2086
|
+
* const encoded = Schema.encodeUnknownSync(A)({ _tag: "A", value: 1 })
|
|
2087
|
+
* // encoded: { value: 1 }
|
|
2088
|
+
* ```
|
|
2089
|
+
*
|
|
2090
|
+
* @see {@link tag} for the variant that keeps the tag during encoding
|
|
1197
2091
|
* @since 4.0.0
|
|
1198
2092
|
*/
|
|
1199
2093
|
export function tagDefaultOmit(literal) {
|
|
@@ -1250,6 +2144,26 @@ export function TaggedStruct(value, fields) {
|
|
|
1250
2144
|
});
|
|
1251
2145
|
}
|
|
1252
2146
|
/**
|
|
2147
|
+
* Augments an existing {@link Union} of tagged structs with utility methods keyed by the discriminant field.
|
|
2148
|
+
*
|
|
2149
|
+
* **Example** (Adding tagged-union utilities to an existing union)
|
|
2150
|
+
*
|
|
2151
|
+
* ```ts
|
|
2152
|
+
* import { Schema } from "effect"
|
|
2153
|
+
*
|
|
2154
|
+
* const A = Schema.TaggedStruct("A", { value: Schema.Number })
|
|
2155
|
+
* const B = Schema.TaggedStruct("B", { name: Schema.String })
|
|
2156
|
+
*
|
|
2157
|
+
* const MyUnion = Schema.Union([A, B]).pipe(Schema.toTaggedUnion("_tag"))
|
|
2158
|
+
*
|
|
2159
|
+
* // Pattern-match on the union
|
|
2160
|
+
* const result = MyUnion.match({ _tag: "A", value: 1 }, {
|
|
2161
|
+
* A: (a) => `number: ${a.value}`,
|
|
2162
|
+
* B: (b) => `name: ${b.name}`
|
|
2163
|
+
* })
|
|
2164
|
+
* ```
|
|
2165
|
+
*
|
|
2166
|
+
* @see {@link TaggedUnion} for a shorthand that builds the union from scratch
|
|
1253
2167
|
* @since 4.0.0
|
|
1254
2168
|
* @experimental
|
|
1255
2169
|
*/
|
|
@@ -1295,6 +2209,28 @@ export function toTaggedUnion(tag) {
|
|
|
1295
2209
|
};
|
|
1296
2210
|
}
|
|
1297
2211
|
/**
|
|
2212
|
+
* Builds a discriminated union from a record of field sets, one per variant.
|
|
2213
|
+
* Each key becomes the `_tag` literal and the value is passed to {@link TaggedStruct}.
|
|
2214
|
+
* The result includes `cases`, `guards`, `isAnyOf`, and `match` utilities.
|
|
2215
|
+
*
|
|
2216
|
+
* **Example** (Discriminated union with pattern matching)
|
|
2217
|
+
*
|
|
2218
|
+
* ```ts
|
|
2219
|
+
* import { Schema } from "effect"
|
|
2220
|
+
*
|
|
2221
|
+
* const Shape = Schema.TaggedUnion({
|
|
2222
|
+
* Circle: { radius: Schema.Number },
|
|
2223
|
+
* Rectangle: { width: Schema.Number, height: Schema.Number }
|
|
2224
|
+
* })
|
|
2225
|
+
*
|
|
2226
|
+
* // Pattern-match on a decoded value
|
|
2227
|
+
* const area = Shape.match({ _tag: "Circle", radius: 5 }, {
|
|
2228
|
+
* Circle: (c) => Math.PI * c.radius ** 2,
|
|
2229
|
+
* Rectangle: (r) => r.width * r.height
|
|
2230
|
+
* })
|
|
2231
|
+
* ```
|
|
2232
|
+
*
|
|
2233
|
+
* @see {@link toTaggedUnion} to augment an existing union instead
|
|
1298
2234
|
* @category Constructors
|
|
1299
2235
|
* @since 4.0.0
|
|
1300
2236
|
*/
|
|
@@ -1318,6 +2254,23 @@ export function TaggedUnion(casesByTag) {
|
|
|
1318
2254
|
});
|
|
1319
2255
|
}
|
|
1320
2256
|
/**
|
|
2257
|
+
* Wraps a schema so that its decoded `Type` becomes a nominally distinct type `Self`.
|
|
2258
|
+
* Useful for creating opaque types that are structurally identical to a base schema
|
|
2259
|
+
* but type-incompatible with it.
|
|
2260
|
+
*
|
|
2261
|
+
* **Example** (Opaque user ID)
|
|
2262
|
+
*
|
|
2263
|
+
* ```ts
|
|
2264
|
+
* import { Schema } from "effect"
|
|
2265
|
+
*
|
|
2266
|
+
* type UserId = string & { readonly _tag: "UserId" }
|
|
2267
|
+
* const UserId = Schema.Opaque<UserId>()(Schema.String)
|
|
2268
|
+
*
|
|
2269
|
+
* // Decoded value is UserId, not plain string
|
|
2270
|
+
* const id = Schema.decodeUnknownSync(UserId)("abc")
|
|
2271
|
+
* // id: UserId
|
|
2272
|
+
* ```
|
|
2273
|
+
*
|
|
1321
2274
|
* @since 4.0.0
|
|
1322
2275
|
*/
|
|
1323
2276
|
export function Opaque() {
|
|
@@ -1329,7 +2282,19 @@ export function Opaque() {
|
|
|
1329
2282
|
};
|
|
1330
2283
|
}
|
|
1331
2284
|
/**
|
|
1332
|
-
* Creates a schema that validates
|
|
2285
|
+
* Creates a schema that validates values using `instanceof`.
|
|
2286
|
+
* Decoding and encoding pass the value through unchanged.
|
|
2287
|
+
*
|
|
2288
|
+
* **Example** (Schema for a built-in class)
|
|
2289
|
+
*
|
|
2290
|
+
* ```ts
|
|
2291
|
+
* import { Schema } from "effect"
|
|
2292
|
+
*
|
|
2293
|
+
* const DateSchema = Schema.instanceOf(Date)
|
|
2294
|
+
*
|
|
2295
|
+
* const decoded = Schema.decodeUnknownSync(DateSchema)(new Date("2024-01-01"))
|
|
2296
|
+
* // decoded: Date
|
|
2297
|
+
* ```
|
|
1333
2298
|
*
|
|
1334
2299
|
* @category Constructors
|
|
1335
2300
|
* @since 4.0.0
|
|
@@ -1338,6 +2303,9 @@ export function instanceOf(constructor, annotations) {
|
|
|
1338
2303
|
return declare(u => u instanceof constructor, annotations);
|
|
1339
2304
|
}
|
|
1340
2305
|
/**
|
|
2306
|
+
* Constructs an `AST.Link` that describes how a value of type `T` encodes to and decodes from a `To` schema.
|
|
2307
|
+
* Used when building low-level AST transformations that bridge two schema types.
|
|
2308
|
+
*
|
|
1341
2309
|
* @since 4.0.0
|
|
1342
2310
|
* @experimental
|
|
1343
2311
|
*/
|
|
@@ -1350,11 +2318,30 @@ export function link() {
|
|
|
1350
2318
|
// Checks
|
|
1351
2319
|
// -----------------------------------------------------------------------------
|
|
1352
2320
|
/**
|
|
2321
|
+
* Creates a custom filter check from a predicate function. The predicate
|
|
2322
|
+
* receives the input value, the schema's AST, and parse options, and returns
|
|
2323
|
+
* `true`/`undefined` on success or a failure description on error.
|
|
2324
|
+
*
|
|
2325
|
+
* **Example** (Custom filter check)
|
|
2326
|
+
* ```ts
|
|
2327
|
+
* import { Schema } from "effect"
|
|
2328
|
+
*
|
|
2329
|
+
* // Check that a number is even
|
|
2330
|
+
* const isEven = Schema.makeFilter(
|
|
2331
|
+
* (n: number) => n % 2 === 0 || "expected an even number"
|
|
2332
|
+
* )
|
|
2333
|
+
*
|
|
2334
|
+
* const EvenNumber = Schema.Number.check(isEven)
|
|
2335
|
+
* ```
|
|
2336
|
+
*
|
|
1353
2337
|
* @category Checks Constructors
|
|
1354
2338
|
* @since 4.0.0
|
|
1355
2339
|
*/
|
|
1356
2340
|
export const makeFilter = AST.makeFilter;
|
|
1357
2341
|
/**
|
|
2342
|
+
* Groups multiple checks into a single {@link AST.FilterGroup}, applying
|
|
2343
|
+
* optional shared annotations to the group as a whole.
|
|
2344
|
+
*
|
|
1358
2345
|
* @category Checks Constructors
|
|
1359
2346
|
* @since 4.0.0
|
|
1360
2347
|
*/
|
|
@@ -1842,6 +2829,9 @@ export function isFinite(annotations) {
|
|
|
1842
2829
|
});
|
|
1843
2830
|
}
|
|
1844
2831
|
/**
|
|
2832
|
+
* Generic factory for creating a "greater than" (`>`) check for any ordered
|
|
2833
|
+
* type by supplying an {@link Order.Order} instance.
|
|
2834
|
+
*
|
|
1845
2835
|
* @category Order checks
|
|
1846
2836
|
* @since 4.0.0
|
|
1847
2837
|
*/
|
|
@@ -1857,6 +2847,9 @@ export function makeIsGreaterThan(options) {
|
|
|
1857
2847
|
};
|
|
1858
2848
|
}
|
|
1859
2849
|
/**
|
|
2850
|
+
* Generic factory for creating a ">=" check for any ordered type by supplying
|
|
2851
|
+
* an {@link Order.Order} instance.
|
|
2852
|
+
*
|
|
1860
2853
|
* @category Order checks
|
|
1861
2854
|
* @since 4.0.0
|
|
1862
2855
|
*/
|
|
@@ -1872,6 +2865,9 @@ export function makeIsGreaterThanOrEqualTo(options) {
|
|
|
1872
2865
|
};
|
|
1873
2866
|
}
|
|
1874
2867
|
/**
|
|
2868
|
+
* Generic factory for creating a "<" check for any ordered type by supplying
|
|
2869
|
+
* an {@link Order.Order} instance.
|
|
2870
|
+
*
|
|
1875
2871
|
* @category Order checks
|
|
1876
2872
|
* @since 4.0.0
|
|
1877
2873
|
*/
|
|
@@ -1887,6 +2883,9 @@ export function makeIsLessThan(options) {
|
|
|
1887
2883
|
};
|
|
1888
2884
|
}
|
|
1889
2885
|
/**
|
|
2886
|
+
* Generic factory for creating a "<=" check for any ordered type by supplying
|
|
2887
|
+
* an {@link Order.Order} instance.
|
|
2888
|
+
*
|
|
1890
2889
|
* @category Order checks
|
|
1891
2890
|
* @since 4.0.0
|
|
1892
2891
|
*/
|
|
@@ -1902,6 +2901,9 @@ export function makeIsLessThanOrEqualTo(options) {
|
|
|
1902
2901
|
};
|
|
1903
2902
|
}
|
|
1904
2903
|
/**
|
|
2904
|
+
* Generic factory for creating an inclusive/exclusive range check for any
|
|
2905
|
+
* ordered type by supplying an {@link Order.Order} instance.
|
|
2906
|
+
*
|
|
1905
2907
|
* @category Order checks
|
|
1906
2908
|
* @since 4.0.0
|
|
1907
2909
|
*/
|
|
@@ -1922,6 +2924,9 @@ export function makeIsBetween(deriveOptions) {
|
|
|
1922
2924
|
};
|
|
1923
2925
|
}
|
|
1924
2926
|
/**
|
|
2927
|
+
* Generic factory for creating a divisibility check for any numeric type by
|
|
2928
|
+
* supplying a remainder function and a zero value.
|
|
2929
|
+
*
|
|
1925
2930
|
* @category Numeric checks
|
|
1926
2931
|
* @since 4.0.0
|
|
1927
2932
|
*/
|
|
@@ -2590,6 +3595,14 @@ export const isBetweenBigDecimal = /*#__PURE__*/makeIsBetween({
|
|
|
2590
3595
|
* constraint to ensure generated strings or arrays have at least the required
|
|
2591
3596
|
* length.
|
|
2592
3597
|
*
|
|
3598
|
+
* **Example** (Minimum length check)
|
|
3599
|
+
* ```ts
|
|
3600
|
+
* import { Schema } from "effect"
|
|
3601
|
+
*
|
|
3602
|
+
* const NonEmptyStringSchema = Schema.String.check(Schema.isMinLength(1))
|
|
3603
|
+
* const NonEmptyArraySchema = Schema.Array(Schema.Number).check(Schema.isMinLength(1))
|
|
3604
|
+
* ```
|
|
3605
|
+
*
|
|
2593
3606
|
* @category Length checks
|
|
2594
3607
|
* @since 4.0.0
|
|
2595
3608
|
*/
|
|
@@ -2982,6 +3995,7 @@ export function isPropertyNames(keySchema, annotations) {
|
|
|
2982
3995
|
* constraint using the provided equivalence function to ensure generated arrays
|
|
2983
3996
|
* contain only unique items.
|
|
2984
3997
|
*
|
|
3998
|
+
* @category Array checks
|
|
2985
3999
|
* @since 4.0.0
|
|
2986
4000
|
*/
|
|
2987
4001
|
export function isUnique(annotations) {
|
|
@@ -3016,6 +4030,8 @@ export const NonEmptyString = /*#__PURE__*/String.check(/*#__PURE__*/isNonEmpty(
|
|
|
3016
4030
|
*/
|
|
3017
4031
|
export const Char = /*#__PURE__*/String.check(/*#__PURE__*/isLengthBetween(1, 1));
|
|
3018
4032
|
/**
|
|
4033
|
+
* Creates a schema for `Option<A>`. See {@link Option} for details.
|
|
4034
|
+
*
|
|
3019
4035
|
* @category Option
|
|
3020
4036
|
* @since 4.0.0
|
|
3021
4037
|
*/
|
|
@@ -3159,6 +4175,35 @@ export function OptionFromOptional(schema) {
|
|
|
3159
4175
|
return optional(schema).pipe(decodeTo(Option(toType(schema)), Transformation.optionFromOptional()));
|
|
3160
4176
|
}
|
|
3161
4177
|
/**
|
|
4178
|
+
* Decodes an optional or `null` or `undefined` value `A` to a required `Option<A>`
|
|
4179
|
+
* value.
|
|
4180
|
+
*
|
|
4181
|
+
* Decoding:
|
|
4182
|
+
* - a missing key is decoded as `None`
|
|
4183
|
+
* - a present key with an `undefined` value is decoded as `None`
|
|
4184
|
+
* - a present key with a `null` value is decoded as `None`
|
|
4185
|
+
* - all other values are decoded as `Some`
|
|
4186
|
+
*
|
|
4187
|
+
* Encoding (controlled by `options.onNoneEncoding`):
|
|
4188
|
+
* - `"omit"` (default): `None` is encoded as a missing key
|
|
4189
|
+
* - `null`: `None` is encoded as `null`
|
|
4190
|
+
* - `undefined`: `None` is encoded as `undefined`
|
|
4191
|
+
* - `Some` is always encoded as the value
|
|
4192
|
+
*
|
|
4193
|
+
* @category Option
|
|
4194
|
+
* @since 4.0.0
|
|
4195
|
+
*/
|
|
4196
|
+
export function OptionFromOptionalNullOr(schema, options) {
|
|
4197
|
+
const onNoneEncoding = options === undefined ? "omit" : options.onNoneEncoding;
|
|
4198
|
+
const noneValue = onNoneEncoding === null ? null : undefined;
|
|
4199
|
+
return optional(NullOr(schema)).pipe(decodeTo(Option(toType(schema)), Transformation.transformOptional({
|
|
4200
|
+
decode: oe => oe.pipe(Option_.filter(Predicate.isNotNullish), Option_.some),
|
|
4201
|
+
encode: onNoneEncoding === "omit" ? Option_.flatten : ot => Option_.some(Option_.getOrElse(Option_.flatten(ot), () => noneValue))
|
|
4202
|
+
})));
|
|
4203
|
+
}
|
|
4204
|
+
/**
|
|
4205
|
+
* Creates a schema for `Result<A, E>`. See {@link Result} for details.
|
|
4206
|
+
*
|
|
3162
4207
|
* @category Result
|
|
3163
4208
|
* @since 4.0.0
|
|
3164
4209
|
*/
|
|
@@ -3287,6 +4332,9 @@ export function Redacted(value, options) {
|
|
|
3287
4332
|
});
|
|
3288
4333
|
}
|
|
3289
4334
|
/**
|
|
4335
|
+
* Middleware that wraps decoded errors in `Redacted`, preventing sensitive
|
|
4336
|
+
* schema details from leaking in error messages.
|
|
4337
|
+
*
|
|
3290
4338
|
* @category Redacted
|
|
3291
4339
|
* @since 4.0.0
|
|
3292
4340
|
*/
|
|
@@ -3294,6 +4342,10 @@ export function redact(schema) {
|
|
|
3294
4342
|
return schema.pipe(middlewareDecoding(Effect.mapErrorEager(Issue.redact)));
|
|
3295
4343
|
}
|
|
3296
4344
|
/**
|
|
4345
|
+
* Decodes a value and wraps it in `Redacted<A>`. Unlike {@link Redacted} which
|
|
4346
|
+
* expects the input to already be a `Redacted` instance, this schema decodes
|
|
4347
|
+
* the raw value and wraps it.
|
|
4348
|
+
*
|
|
3297
4349
|
* @category Redacted
|
|
3298
4350
|
* @since 4.0.0
|
|
3299
4351
|
*/
|
|
@@ -3306,6 +4358,8 @@ export function RedactedFromValue(value, options) {
|
|
|
3306
4358
|
}));
|
|
3307
4359
|
}
|
|
3308
4360
|
/**
|
|
4361
|
+
* Creates a schema for `Cause.Reason<E>`. See {@link CauseReason} for details.
|
|
4362
|
+
*
|
|
3309
4363
|
* @category CauseReason
|
|
3310
4364
|
* @since 4.0.0
|
|
3311
4365
|
*/
|
|
@@ -3405,6 +4459,8 @@ function causeReasonToFormatter(error, defect) {
|
|
|
3405
4459
|
};
|
|
3406
4460
|
}
|
|
3407
4461
|
/**
|
|
4462
|
+
* Creates a schema for `Cause<E>`. See {@link Cause} for details.
|
|
4463
|
+
*
|
|
3408
4464
|
* @category Cause
|
|
3409
4465
|
* @since 4.0.0
|
|
3410
4466
|
*/
|
|
@@ -3538,6 +4594,8 @@ export const DefectWithStack = /*#__PURE__*/Union([/*#__PURE__*/ErrorJsonEncoded
|
|
|
3538
4594
|
toArbitrary: () => fc => fc.json()
|
|
3539
4595
|
}), defectTransformation))]);
|
|
3540
4596
|
/**
|
|
4597
|
+
* Creates a schema for `Exit<A, E>`. See {@link Exit} for details.
|
|
4598
|
+
*
|
|
3541
4599
|
* @category Exit
|
|
3542
4600
|
* @since 4.0.0
|
|
3543
4601
|
*/
|
|
@@ -3882,6 +4940,11 @@ export function Chunk(value) {
|
|
|
3882
4940
|
});
|
|
3883
4941
|
}
|
|
3884
4942
|
/**
|
|
4943
|
+
* Schema for JavaScript `RegExp` objects.
|
|
4944
|
+
*
|
|
4945
|
+
* The default JSON serializer encodes a `RegExp` as `{ source, flags }`.
|
|
4946
|
+
*
|
|
4947
|
+
* @category Schemas
|
|
3885
4948
|
* @since 4.0.0
|
|
3886
4949
|
*/
|
|
3887
4950
|
export const RegExp = /*#__PURE__*/instanceOf(globalThis.RegExp, {
|
|
@@ -3959,8 +5022,19 @@ export const URLFromString = /*#__PURE__*/String.annotate({
|
|
|
3959
5022
|
* A schema for JavaScript `Date` objects.
|
|
3960
5023
|
*
|
|
3961
5024
|
* This schema accepts any `Date` instance, including invalid dates (e.g., `new
|
|
3962
|
-
* Date("invalid")`). For validating only valid dates, use
|
|
5025
|
+
* Date("invalid")`). For validating only valid dates, use {@link DateValid}
|
|
5026
|
+
* instead. The default JSON serializer encodes `Date` as an ISO 8601 string.
|
|
5027
|
+
*
|
|
5028
|
+
* **Example** (Date schema)
|
|
3963
5029
|
*
|
|
5030
|
+
* ```ts
|
|
5031
|
+
* import { Schema } from "effect"
|
|
5032
|
+
*
|
|
5033
|
+
* Schema.decodeUnknownSync(Schema.Date)(new Date("2024-01-01"))
|
|
5034
|
+
* // => Date { 2024-01-01T00:00:00.000Z }
|
|
5035
|
+
* ```
|
|
5036
|
+
*
|
|
5037
|
+
* @category Schemas
|
|
3964
5038
|
* @since 4.0.0
|
|
3965
5039
|
*/
|
|
3966
5040
|
export const Date = /*#__PURE__*/instanceOf(globalThis.Date, {
|
|
@@ -3992,9 +5066,20 @@ export const DateValid = /*#__PURE__*/Date.check(/*#__PURE__*/isDateValid());
|
|
|
3992
5066
|
/**
|
|
3993
5067
|
* A schema for `Duration` values.
|
|
3994
5068
|
*
|
|
3995
|
-
*
|
|
5069
|
+
* The default JSON serializer encodes `Duration` as a tagged object with the
|
|
5070
|
+
* duration type and value.
|
|
5071
|
+
*
|
|
5072
|
+
* **Example** (Duration schema)
|
|
3996
5073
|
*
|
|
3997
|
-
*
|
|
5074
|
+
* ```ts
|
|
5075
|
+
* import { Schema } from "effect"
|
|
5076
|
+
* import { Duration } from "effect"
|
|
5077
|
+
*
|
|
5078
|
+
* Schema.decodeUnknownSync(Schema.Duration)(Duration.seconds(5))
|
|
5079
|
+
* // => Duration(5s)
|
|
5080
|
+
* ```
|
|
5081
|
+
*
|
|
5082
|
+
* @category Duration
|
|
3998
5083
|
*
|
|
3999
5084
|
* @since 4.0.0
|
|
4000
5085
|
*/
|
|
@@ -4212,6 +5297,12 @@ export function fromJsonString(schema) {
|
|
|
4212
5297
|
}).pipe(decodeTo(schema, Transformation.fromJsonString));
|
|
4213
5298
|
}
|
|
4214
5299
|
/**
|
|
5300
|
+
* Schema for JavaScript `File` objects.
|
|
5301
|
+
*
|
|
5302
|
+
* The default JSON serializer encodes a `File` as `{ data, type, name, lastModified }`
|
|
5303
|
+
* where `data` is base64-encoded.
|
|
5304
|
+
*
|
|
5305
|
+
* @category Schemas
|
|
4215
5306
|
* @since 4.0.0
|
|
4216
5307
|
*/
|
|
4217
5308
|
export const File = /*#__PURE__*/instanceOf(globalThis.File, {
|
|
@@ -4258,6 +5349,12 @@ export const File = /*#__PURE__*/instanceOf(globalThis.File, {
|
|
|
4258
5349
|
}))
|
|
4259
5350
|
});
|
|
4260
5351
|
/**
|
|
5352
|
+
* Schema for JavaScript `FormData` objects.
|
|
5353
|
+
*
|
|
5354
|
+
* The default JSON serializer encodes a `FormData` as an array of `[key, entry]`
|
|
5355
|
+
* pairs where each entry is tagged as `"String"` or `"File"`.
|
|
5356
|
+
*
|
|
5357
|
+
* @category Schemas
|
|
4261
5358
|
* @since 4.0.0
|
|
4262
5359
|
*/
|
|
4263
5360
|
export const FormData = /*#__PURE__*/instanceOf(globalThis.FormData, {
|
|
@@ -4387,6 +5484,11 @@ export function fromFormData(schema) {
|
|
|
4387
5484
|
return FormData.pipe(decodeTo(schema, Transformation.fromFormData));
|
|
4388
5485
|
}
|
|
4389
5486
|
/**
|
|
5487
|
+
* Schema for JavaScript `URLSearchParams` objects.
|
|
5488
|
+
*
|
|
5489
|
+
* The default JSON serializer encodes a `URLSearchParams` as a query string.
|
|
5490
|
+
*
|
|
5491
|
+
* @category Schemas
|
|
4390
5492
|
* @since 4.0.0
|
|
4391
5493
|
*/
|
|
4392
5494
|
export const URLSearchParams = /*#__PURE__*/instanceOf(globalThis.URLSearchParams, {
|
|
@@ -4544,6 +5646,9 @@ export const Trim = /*#__PURE__*/String.annotate({
|
|
|
4544
5646
|
expected: "a string that will be decoded as a trimmed string"
|
|
4545
5647
|
}).pipe(/*#__PURE__*/decodeTo(Trimmed, /*#__PURE__*/Transformation.trim()));
|
|
4546
5648
|
/**
|
|
5649
|
+
* A union schema for JavaScript property keys: `number | symbol | string`.
|
|
5650
|
+
*
|
|
5651
|
+
* @category Schemas
|
|
4547
5652
|
* @since 4.0.0
|
|
4548
5653
|
*/
|
|
4549
5654
|
export const PropertyKey = /*#__PURE__*/Union([Finite, Symbol, String]);
|
|
@@ -4948,6 +6053,46 @@ function isStruct(schema) {
|
|
|
4948
6053
|
return isSchema(schema);
|
|
4949
6054
|
}
|
|
4950
6055
|
/**
|
|
6056
|
+
* Creates a schema-backed class whose constructor validates input against a
|
|
6057
|
+
* {@link Struct} schema. Construction throws a {@link SchemaError} on invalid
|
|
6058
|
+
* input (unless `disableValidation` is set in the options).
|
|
6059
|
+
*
|
|
6060
|
+
* Pass the desired class type as the first type parameter. The second optional
|
|
6061
|
+
* type parameter can be used to add nominal brands.
|
|
6062
|
+
*
|
|
6063
|
+
* **Example** (Basic class)
|
|
6064
|
+
*
|
|
6065
|
+
* ```ts
|
|
6066
|
+
* import { Schema } from "effect"
|
|
6067
|
+
*
|
|
6068
|
+
* class Person extends Schema.Class<Person>("Person")({
|
|
6069
|
+
* name: Schema.String,
|
|
6070
|
+
* age: Schema.Number
|
|
6071
|
+
* }) {}
|
|
6072
|
+
*
|
|
6073
|
+
* const alice = new Person({ name: "Alice", age: 30 })
|
|
6074
|
+
* console.log(alice.name) // "Alice"
|
|
6075
|
+
* console.log(`${alice}`) // "Person({ name: Alice, age: 30 })"
|
|
6076
|
+
* ```
|
|
6077
|
+
*
|
|
6078
|
+
* **Example** (Extending a class)
|
|
6079
|
+
*
|
|
6080
|
+
* ```ts
|
|
6081
|
+
* import { Schema } from "effect"
|
|
6082
|
+
*
|
|
6083
|
+
* class Animal extends Schema.Class<Animal>("Animal")({
|
|
6084
|
+
* name: Schema.String
|
|
6085
|
+
* }) {}
|
|
6086
|
+
*
|
|
6087
|
+
* class Dog extends Animal.extend<Dog>("Dog")({
|
|
6088
|
+
* breed: Schema.String
|
|
6089
|
+
* }) {}
|
|
6090
|
+
*
|
|
6091
|
+
* const dog = new Dog({ name: "Rex", breed: "Labrador" })
|
|
6092
|
+
* console.log(dog.name) // "Rex"
|
|
6093
|
+
* console.log(dog.breed) // "Labrador"
|
|
6094
|
+
* ```
|
|
6095
|
+
*
|
|
4951
6096
|
* @category Constructors
|
|
4952
6097
|
* @since 4.0.0
|
|
4953
6098
|
*/
|
|
@@ -4956,6 +6101,27 @@ export const Class = identifier => (schema, annotations) => {
|
|
|
4956
6101
|
return makeClass(Data.Class, identifier, struct, annotations);
|
|
4957
6102
|
};
|
|
4958
6103
|
/**
|
|
6104
|
+
* Like {@link Class} but automatically adds a `_tag` literal field set to the
|
|
6105
|
+
* given `tag` value. This makes instances compatible with tagged union
|
|
6106
|
+
* discrimination patterns.
|
|
6107
|
+
*
|
|
6108
|
+
* The optional `identifier` parameter overrides the schema identifier;
|
|
6109
|
+
* it defaults to the `tag` value.
|
|
6110
|
+
*
|
|
6111
|
+
* **Example** (Tagged class)
|
|
6112
|
+
*
|
|
6113
|
+
* ```ts
|
|
6114
|
+
* import { Schema } from "effect"
|
|
6115
|
+
*
|
|
6116
|
+
* class Circle extends Schema.TaggedClass<Circle>()("Circle", {
|
|
6117
|
+
* radius: Schema.Number
|
|
6118
|
+
* }) {}
|
|
6119
|
+
*
|
|
6120
|
+
* const c = new Circle({ radius: 5 })
|
|
6121
|
+
* console.log(c._tag) // "Circle"
|
|
6122
|
+
* console.log(c.radius) // 5
|
|
6123
|
+
* ```
|
|
6124
|
+
*
|
|
4959
6125
|
* @category Constructors
|
|
4960
6126
|
* @since 4.0.0
|
|
4961
6127
|
*/
|
|
@@ -4970,6 +6136,25 @@ export const TaggedClass = identifier => {
|
|
|
4970
6136
|
};
|
|
4971
6137
|
};
|
|
4972
6138
|
/**
|
|
6139
|
+
* Creates a schema-backed error class that can be used as a typed,
|
|
6140
|
+
* yieldable error in Effect programs. Combines {@link Class} validation with
|
|
6141
|
+
* the `YieldableError` interface so instances can be yielded directly inside
|
|
6142
|
+
* `Effect.gen`.
|
|
6143
|
+
*
|
|
6144
|
+
* **Example** (Schema-backed error)
|
|
6145
|
+
*
|
|
6146
|
+
* ```ts
|
|
6147
|
+
* import { Effect, Schema } from "effect"
|
|
6148
|
+
*
|
|
6149
|
+
* class NotFound extends Schema.ErrorClass<NotFound>("NotFound")({
|
|
6150
|
+
* id: Schema.Number
|
|
6151
|
+
* }) {}
|
|
6152
|
+
*
|
|
6153
|
+
* const program = Effect.gen(function*() {
|
|
6154
|
+
* yield* new NotFound({ id: 1 })
|
|
6155
|
+
* })
|
|
6156
|
+
* ```
|
|
6157
|
+
*
|
|
4973
6158
|
* @category Constructors
|
|
4974
6159
|
* @since 4.0.0
|
|
4975
6160
|
*/
|
|
@@ -4980,6 +6165,24 @@ export const ErrorClass = identifier => (schema, annotations) => {
|
|
|
4980
6165
|
return self;
|
|
4981
6166
|
};
|
|
4982
6167
|
/**
|
|
6168
|
+
* Like {@link ErrorClass} but automatically adds a `_tag` literal field. The
|
|
6169
|
+
* resulting class is both a schema-validated, yieldable error and a tagged
|
|
6170
|
+
* union member.
|
|
6171
|
+
*
|
|
6172
|
+
* **Example** (Tagged error class)
|
|
6173
|
+
*
|
|
6174
|
+
* ```ts
|
|
6175
|
+
* import { Effect, Schema } from "effect"
|
|
6176
|
+
*
|
|
6177
|
+
* class NotFound extends Schema.TaggedErrorClass<NotFound>()("NotFound", {
|
|
6178
|
+
* id: Schema.Number
|
|
6179
|
+
* }) {}
|
|
6180
|
+
*
|
|
6181
|
+
* const program = Effect.gen(function*() {
|
|
6182
|
+
* yield* new NotFound({ id: 42 })
|
|
6183
|
+
* })
|
|
6184
|
+
* ```
|
|
6185
|
+
*
|
|
4983
6186
|
* @category Constructors
|
|
4984
6187
|
* @since 4.0.0
|
|
4985
6188
|
*/
|
|
@@ -4994,6 +6197,11 @@ export const TaggedErrorClass = identifier => {
|
|
|
4994
6197
|
};
|
|
4995
6198
|
};
|
|
4996
6199
|
/**
|
|
6200
|
+
* Derives a {@link LazyArbitrary} from a schema. The result is memoized so
|
|
6201
|
+
* repeated calls with the same schema are cheap.
|
|
6202
|
+
*
|
|
6203
|
+
* Prefer {@link toArbitrary} when you just need the arbitrary directly.
|
|
6204
|
+
*
|
|
4997
6205
|
* @category Arbitrary
|
|
4998
6206
|
* @since 4.0.0
|
|
4999
6207
|
*/
|
|
@@ -5002,6 +6210,24 @@ export function toArbitraryLazy(schema) {
|
|
|
5002
6210
|
return fc => lawc(fc, {});
|
|
5003
6211
|
}
|
|
5004
6212
|
/**
|
|
6213
|
+
* Derives a `fast-check` `Arbitrary` from a schema for property-based
|
|
6214
|
+
* testing. The derived arbitrary generates values that satisfy the schema.
|
|
6215
|
+
*
|
|
6216
|
+
* **Example** (Generating arbitrary values)
|
|
6217
|
+
*
|
|
6218
|
+
* ```ts
|
|
6219
|
+
* import { Schema } from "effect"
|
|
6220
|
+
* import * as FastCheck from "fast-check"
|
|
6221
|
+
*
|
|
6222
|
+
* const PersonArb = Schema.toArbitrary(
|
|
6223
|
+
* Schema.Struct({ name: Schema.String, age: Schema.Number })
|
|
6224
|
+
* )
|
|
6225
|
+
*
|
|
6226
|
+
* // Sample a random value
|
|
6227
|
+
* const sample = FastCheck.sample(PersonArb, 1)[0]
|
|
6228
|
+
* console.log(typeof sample.name) // "string"
|
|
6229
|
+
* ```
|
|
6230
|
+
*
|
|
5005
6231
|
* @category Arbitrary
|
|
5006
6232
|
* @since 4.0.0
|
|
5007
6233
|
*/
|
|
@@ -5028,6 +6254,13 @@ export function overrideToFormatter(toFormatter) {
|
|
|
5028
6254
|
};
|
|
5029
6255
|
}
|
|
5030
6256
|
/**
|
|
6257
|
+
* Derives a string formatter function from a schema. The formatter converts
|
|
6258
|
+
* a value to its human-readable string representation, recursing into structs,
|
|
6259
|
+
* arrays, and unions.
|
|
6260
|
+
*
|
|
6261
|
+
* The optional `onBefore` hook lets you intercept specific AST nodes before
|
|
6262
|
+
* the default formatting logic runs.
|
|
6263
|
+
*
|
|
5031
6264
|
* @category Formatter
|
|
5032
6265
|
* @since 4.0.0
|
|
5033
6266
|
*/
|
|
@@ -5166,10 +6399,9 @@ export function toFormatter(schema, options) {
|
|
|
5166
6399
|
// Equivalence
|
|
5167
6400
|
// -----------------------------------------------------------------------------
|
|
5168
6401
|
/**
|
|
5169
|
-
*
|
|
5170
|
-
*
|
|
5171
|
-
*
|
|
5172
|
-
* the schema invariant.
|
|
6402
|
+
* Overrides the equivalence derivation for a schema by supplying a custom
|
|
6403
|
+
* `Equivalence`. Use this when the default structural equivalence derived by
|
|
6404
|
+
* {@link toEquivalence} is not appropriate for a type.
|
|
5173
6405
|
*
|
|
5174
6406
|
* @category Equivalence
|
|
5175
6407
|
* @since 4.0.0
|
|
@@ -5180,6 +6412,21 @@ export function overrideToEquivalence(toEquivalence) {
|
|
|
5180
6412
|
});
|
|
5181
6413
|
}
|
|
5182
6414
|
/**
|
|
6415
|
+
* Derives an `Equivalence` from a schema. Two values are considered equal when
|
|
6416
|
+
* every field (and nested field) compares equal according to the schema
|
|
6417
|
+
* structure.
|
|
6418
|
+
*
|
|
6419
|
+
* **Example** (Struct equivalence)
|
|
6420
|
+
*
|
|
6421
|
+
* ```ts
|
|
6422
|
+
* import { Schema } from "effect"
|
|
6423
|
+
*
|
|
6424
|
+
* const eq = Schema.toEquivalence(Schema.Struct({ id: Schema.Number, name: Schema.String }))
|
|
6425
|
+
*
|
|
6426
|
+
* console.log(eq({ id: 1, name: "Alice" }, { id: 1, name: "Alice" })) // true
|
|
6427
|
+
* console.log(eq({ id: 1, name: "Alice" }, { id: 2, name: "Alice" })) // false
|
|
6428
|
+
* ```
|
|
6429
|
+
*
|
|
5183
6430
|
* @category Equivalence
|
|
5184
6431
|
* @since 4.0.0
|
|
5185
6432
|
*/
|
|
@@ -5190,6 +6437,10 @@ export function toEquivalence(schema) {
|
|
|
5190
6437
|
// Representation
|
|
5191
6438
|
// -----------------------------------------------------------------------------
|
|
5192
6439
|
/**
|
|
6440
|
+
* Derives an intermediate `SchemaRepresentation.Document` from a schema. This
|
|
6441
|
+
* document is used internally by {@link toJsonSchemaDocument} and related
|
|
6442
|
+
* functions to produce JSON Schema output.
|
|
6443
|
+
*
|
|
5193
6444
|
* @category Representation
|
|
5194
6445
|
* @since 4.0.0
|
|
5195
6446
|
*/
|
|
@@ -5217,6 +6468,9 @@ export function toJsonSchemaDocument(schema, options) {
|
|
|
5217
6468
|
// Canonical Codecs
|
|
5218
6469
|
// -----------------------------------------------------------------------------
|
|
5219
6470
|
/**
|
|
6471
|
+
* Derives a canonical JSON codec from a schema. The encoded form is `unknown`
|
|
6472
|
+
* (any JSON-compatible value), decoded to the schema's `Type`.
|
|
6473
|
+
*
|
|
5220
6474
|
* @category Canonical Codecs
|
|
5221
6475
|
* @since 4.0.0
|
|
5222
6476
|
*/
|
|
@@ -5224,6 +6478,9 @@ export function toCodecJson(schema) {
|
|
|
5224
6478
|
return make(InternalToCodec.toCodecJson(schema.ast));
|
|
5225
6479
|
}
|
|
5226
6480
|
/**
|
|
6481
|
+
* Derives an isomorphism codec from a schema. The encoded form is the
|
|
6482
|
+
* schema's `Iso` type — the intermediate representation used for round-tripping.
|
|
6483
|
+
*
|
|
5227
6484
|
* @category Canonical Codecs
|
|
5228
6485
|
* @since 4.0.0
|
|
5229
6486
|
*/
|
|
@@ -5238,6 +6495,10 @@ export function toCodecStringTree(schema, options) {
|
|
|
5238
6495
|
}
|
|
5239
6496
|
}
|
|
5240
6497
|
/**
|
|
6498
|
+
* Derives an XML encoder from a codec. Encodes a value to an XML string by
|
|
6499
|
+
* first converting it through {@link toCodecStringTree}, then serializing the
|
|
6500
|
+
* resulting tree to XML.
|
|
6501
|
+
*
|
|
5241
6502
|
* @category Canonical Codecs
|
|
5242
6503
|
* @since 4.0.0
|
|
5243
6504
|
*/
|
|
@@ -5450,6 +6711,9 @@ function onSerializerEnsureArray(ast) {
|
|
|
5450
6711
|
// Optic APIs
|
|
5451
6712
|
// -----------------------------------------------------------------------------
|
|
5452
6713
|
/**
|
|
6714
|
+
* Derives an `Iso` optic from a schema that isomorphically converts between
|
|
6715
|
+
* the schema's `Type` and its `Iso` (intermediate / serialized form).
|
|
6716
|
+
*
|
|
5453
6717
|
* @category Optic
|
|
5454
6718
|
* @since 4.0.0
|
|
5455
6719
|
*/
|
|
@@ -5458,6 +6722,8 @@ export function toIso(schema) {
|
|
|
5458
6722
|
return Optic_.makeIso(Parser.encodeSync(serializer), Parser.decodeSync(serializer));
|
|
5459
6723
|
}
|
|
5460
6724
|
/**
|
|
6725
|
+
* Returns an identity `Iso` over the schema's source (`Type`) side.
|
|
6726
|
+
*
|
|
5461
6727
|
* @category Optic
|
|
5462
6728
|
* @since 4.0.0
|
|
5463
6729
|
*/
|
|
@@ -5465,6 +6731,8 @@ export function toIsoSource(_) {
|
|
|
5465
6731
|
return Optic_.id();
|
|
5466
6732
|
}
|
|
5467
6733
|
/**
|
|
6734
|
+
* Returns an identity `Iso` over the schema's focus (`Iso`) side.
|
|
6735
|
+
*
|
|
5468
6736
|
* @category Optic
|
|
5469
6737
|
* @since 4.0.0
|
|
5470
6738
|
*/
|
|
@@ -5472,10 +6740,11 @@ export function toIsoFocus(_) {
|
|
|
5472
6740
|
return Optic_.id();
|
|
5473
6741
|
}
|
|
5474
6742
|
/**
|
|
5475
|
-
*
|
|
5476
|
-
*
|
|
5477
|
-
*
|
|
5478
|
-
* the
|
|
6743
|
+
* Overrides the ISO codec derivation for a schema by providing a target codec
|
|
6744
|
+
* and explicit `decode`/`encode` getters. The resulting schema carries a
|
|
6745
|
+
* custom `Iso` type, which changes the schema's type parameter — use
|
|
6746
|
+
* {@link overrideToCodecIso} when the default ISO transformation is not
|
|
6747
|
+
* appropriate.
|
|
5479
6748
|
*
|
|
5480
6749
|
* @category Optic
|
|
5481
6750
|
* @since 4.0.0
|
|
@@ -5493,11 +6762,15 @@ export function overrideToCodecIso(to, transformation) {
|
|
|
5493
6762
|
// Differ APIs
|
|
5494
6763
|
// -----------------------------------------------------------------------------
|
|
5495
6764
|
/**
|
|
6765
|
+
* Derives a JSON Patch differ from a codec. Serializes values to JSON (via
|
|
6766
|
+
* {@link toCodecJson}), computes RFC 6902 JSON Patch operations between old
|
|
6767
|
+
* and new values, and can apply patches back to the typed value.
|
|
6768
|
+
*
|
|
5496
6769
|
* @category JsonPatch
|
|
5497
6770
|
* @since 4.0.0
|
|
5498
6771
|
*/
|
|
5499
6772
|
export function toDifferJsonPatch(schema) {
|
|
5500
|
-
const serializer = toCodecJson(schema);
|
|
6773
|
+
const serializer = toCodecJson(schema);
|
|
5501
6774
|
const get = Parser.encodeSync(serializer);
|
|
5502
6775
|
const set = Parser.decodeSync(serializer);
|
|
5503
6776
|
return {
|
|
@@ -5512,6 +6785,10 @@ export function toDifferJsonPatch(schema) {
|
|
|
5512
6785
|
};
|
|
5513
6786
|
}
|
|
5514
6787
|
/**
|
|
6788
|
+
* Creates a recursive schema for a {@link Tree} of values described by `node`.
|
|
6789
|
+
* The resulting schema accepts a single node value, an array of trees, or an
|
|
6790
|
+
* object whose values are trees.
|
|
6791
|
+
*
|
|
5515
6792
|
* @category Tree
|
|
5516
6793
|
* @since 4.0.0
|
|
5517
6794
|
*/
|
|
@@ -5521,11 +6798,25 @@ export function Tree(node) {
|
|
|
5521
6798
|
return Tree;
|
|
5522
6799
|
}
|
|
5523
6800
|
/**
|
|
6801
|
+
* Schema that accepts and validates any immutable JSON-compatible value.
|
|
6802
|
+
*
|
|
6803
|
+
* **Example** (Validating a JSON value)
|
|
6804
|
+
*
|
|
6805
|
+
* ```ts
|
|
6806
|
+
* import { Schema } from "effect"
|
|
6807
|
+
*
|
|
6808
|
+
* const result = Schema.decodeUnknownOption(Schema.Json)({ key: [1, true, null] })
|
|
6809
|
+
* console.log(result._tag) // "Some"
|
|
6810
|
+
* ```
|
|
6811
|
+
*
|
|
5524
6812
|
* @category JSON
|
|
5525
6813
|
* @since 4.0.0
|
|
5526
6814
|
*/
|
|
5527
6815
|
export const Json = /*#__PURE__*/make(AST.Json);
|
|
5528
6816
|
/**
|
|
6817
|
+
* Schema that accepts any mutable JSON-compatible value. See {@link Json} for
|
|
6818
|
+
* the immutable variant.
|
|
6819
|
+
*
|
|
5529
6820
|
* @category JSON
|
|
5530
6821
|
* @since 4.0.0
|
|
5531
6822
|
*/
|