effect 4.0.0-beta.84 → 4.0.0-beta.86

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.
Files changed (292) hide show
  1. package/dist/ChannelSchema.d.ts +8 -8
  2. package/dist/ChannelSchema.d.ts.map +1 -1
  3. package/dist/Config.d.ts +2 -2
  4. package/dist/Config.d.ts.map +1 -1
  5. package/dist/Config.js +10 -3
  6. package/dist/Config.js.map +1 -1
  7. package/dist/Context.d.ts.map +1 -1
  8. package/dist/Context.js +4 -3
  9. package/dist/Context.js.map +1 -1
  10. package/dist/Cron.d.ts.map +1 -1
  11. package/dist/Cron.js +4 -0
  12. package/dist/Cron.js.map +1 -1
  13. package/dist/Layer.js +4 -3
  14. package/dist/Layer.js.map +1 -1
  15. package/dist/LayerMap.d.ts.map +1 -1
  16. package/dist/LayerMap.js +4 -3
  17. package/dist/LayerMap.js.map +1 -1
  18. package/dist/Random.d.ts +31 -0
  19. package/dist/Random.d.ts.map +1 -1
  20. package/dist/Random.js +32 -0
  21. package/dist/Random.js.map +1 -1
  22. package/dist/Schema.d.ts +793 -399
  23. package/dist/Schema.d.ts.map +1 -1
  24. package/dist/Schema.js +121 -75
  25. package/dist/Schema.js.map +1 -1
  26. package/dist/SchemaAST.d.ts +8 -7
  27. package/dist/SchemaAST.d.ts.map +1 -1
  28. package/dist/SchemaAST.js +207 -157
  29. package/dist/SchemaAST.js.map +1 -1
  30. package/dist/SchemaParser.d.ts +24 -24
  31. package/dist/SchemaParser.d.ts.map +1 -1
  32. package/dist/SchemaParser.js +43 -39
  33. package/dist/SchemaParser.js.map +1 -1
  34. package/dist/SchemaRepresentation.d.ts +300 -300
  35. package/dist/internal/effect.js +16 -17
  36. package/dist/internal/effect.js.map +1 -1
  37. package/dist/internal/matcher.js +0 -2
  38. package/dist/internal/matcher.js.map +1 -1
  39. package/dist/internal/schema/arbitrary.js +17 -1
  40. package/dist/internal/schema/arbitrary.js.map +1 -1
  41. package/dist/internal/schema/representation.js +1 -5
  42. package/dist/internal/schema/representation.js.map +1 -1
  43. package/dist/internal/schema/schema.js.map +1 -1
  44. package/dist/internal/stackTraceLimit.d.ts +2 -0
  45. package/dist/internal/stackTraceLimit.d.ts.map +1 -0
  46. package/dist/internal/stackTraceLimit.js +40 -0
  47. package/dist/internal/stackTraceLimit.js.map +1 -0
  48. package/dist/internal/tracer.d.ts.map +1 -1
  49. package/dist/internal/tracer.js +4 -3
  50. package/dist/internal/tracer.js.map +1 -1
  51. package/dist/testing/TestSchema.d.ts +9 -9
  52. package/dist/testing/TestSchema.d.ts.map +1 -1
  53. package/dist/testing/TestSchema.js +3 -2
  54. package/dist/testing/TestSchema.js.map +1 -1
  55. package/dist/unstable/ai/AnthropicStructuredOutput.d.ts +2 -2
  56. package/dist/unstable/ai/AnthropicStructuredOutput.d.ts.map +1 -1
  57. package/dist/unstable/ai/AnthropicStructuredOutput.js.map +1 -1
  58. package/dist/unstable/ai/Chat.d.ts +1 -1
  59. package/dist/unstable/ai/Chat.d.ts.map +1 -1
  60. package/dist/unstable/ai/LanguageModel.d.ts +4 -4
  61. package/dist/unstable/ai/LanguageModel.d.ts.map +1 -1
  62. package/dist/unstable/ai/McpSchema.d.ts +13 -6
  63. package/dist/unstable/ai/McpSchema.d.ts.map +1 -1
  64. package/dist/unstable/ai/McpSchema.js.map +1 -1
  65. package/dist/unstable/ai/McpServer.d.ts +4 -4
  66. package/dist/unstable/ai/McpServer.d.ts.map +1 -1
  67. package/dist/unstable/ai/McpServer.js.map +1 -1
  68. package/dist/unstable/ai/OpenAiStructuredOutput.d.ts +2 -2
  69. package/dist/unstable/ai/OpenAiStructuredOutput.d.ts.map +1 -1
  70. package/dist/unstable/ai/OpenAiStructuredOutput.js.map +1 -1
  71. package/dist/unstable/ai/Response.d.ts +2 -2
  72. package/dist/unstable/ai/Response.d.ts.map +1 -1
  73. package/dist/unstable/ai/Response.js.map +1 -1
  74. package/dist/unstable/ai/Tool.d.ts +26 -26
  75. package/dist/unstable/ai/Tool.d.ts.map +1 -1
  76. package/dist/unstable/ai/Tool.js +4 -5
  77. package/dist/unstable/ai/Tool.js.map +1 -1
  78. package/dist/unstable/cli/Argument.d.ts +8 -7
  79. package/dist/unstable/cli/Argument.d.ts.map +1 -1
  80. package/dist/unstable/cli/Argument.js.map +1 -1
  81. package/dist/unstable/cli/Flag.d.ts +8 -7
  82. package/dist/unstable/cli/Flag.d.ts.map +1 -1
  83. package/dist/unstable/cli/Flag.js.map +1 -1
  84. package/dist/unstable/cli/Param.d.ts +1806 -168
  85. package/dist/unstable/cli/Param.d.ts.map +1 -1
  86. package/dist/unstable/cli/Param.js +0 -70
  87. package/dist/unstable/cli/Param.js.map +1 -1
  88. package/dist/unstable/cli/Primitive.d.ts +3 -4
  89. package/dist/unstable/cli/Primitive.d.ts.map +1 -1
  90. package/dist/unstable/cli/Primitive.js.map +1 -1
  91. package/dist/unstable/cli/internal/ansi.d.ts.map +1 -1
  92. package/dist/unstable/cli/internal/ansi.js +0 -22
  93. package/dist/unstable/cli/internal/ansi.js.map +1 -1
  94. package/dist/unstable/cli/internal/config.d.ts +48 -0
  95. package/dist/unstable/cli/internal/config.d.ts.map +1 -1
  96. package/dist/unstable/cli/internal/config.js +0 -6
  97. package/dist/unstable/cli/internal/config.js.map +1 -1
  98. package/dist/unstable/cluster/ClusterWorkflowEngine.d.ts +3 -3
  99. package/dist/unstable/cluster/Reply.d.ts +2 -2
  100. package/dist/unstable/cluster/Reply.d.ts.map +1 -1
  101. package/dist/unstable/cluster/Reply.js.map +1 -1
  102. package/dist/unstable/devtools/DevToolsSchema.js.map +1 -1
  103. package/dist/unstable/encoding/Msgpack.d.ts +6 -6
  104. package/dist/unstable/encoding/Msgpack.d.ts.map +1 -1
  105. package/dist/unstable/encoding/Msgpack.js.map +1 -1
  106. package/dist/unstable/encoding/Ndjson.d.ts +8 -8
  107. package/dist/unstable/encoding/Ndjson.d.ts.map +1 -1
  108. package/dist/unstable/encoding/Sse.d.ts +1 -1
  109. package/dist/unstable/encoding/Sse.d.ts.map +1 -1
  110. package/dist/unstable/encoding/Sse.js.map +1 -1
  111. package/dist/unstable/eventlog/EventLogMessage.d.ts +22 -22
  112. package/dist/unstable/eventlog/EventLogMessage.d.ts.map +1 -1
  113. package/dist/unstable/eventlog/EventLogRemote.d.ts +15 -15
  114. package/dist/unstable/http/HttpBody.d.ts +1 -1
  115. package/dist/unstable/http/HttpBody.d.ts.map +1 -1
  116. package/dist/unstable/http/HttpClientRequest.d.ts +1 -1
  117. package/dist/unstable/http/HttpClientRequest.d.ts.map +1 -1
  118. package/dist/unstable/http/HttpIncomingMessage.d.ts +1 -1
  119. package/dist/unstable/http/HttpIncomingMessage.d.ts.map +1 -1
  120. package/dist/unstable/http/HttpIncomingMessage.js.map +1 -1
  121. package/dist/unstable/http/HttpMiddleware.d.ts.map +1 -1
  122. package/dist/unstable/http/HttpMiddleware.js +12 -3
  123. package/dist/unstable/http/HttpMiddleware.js.map +1 -1
  124. package/dist/unstable/httpapi/HttpApi.d.ts +1 -1
  125. package/dist/unstable/httpapi/HttpApiBuilder.js.map +1 -1
  126. package/dist/unstable/httpapi/HttpApiClient.d.ts +2 -2
  127. package/dist/unstable/httpapi/HttpApiClient.d.ts.map +1 -1
  128. package/dist/unstable/httpapi/HttpApiClient.js.map +1 -1
  129. package/dist/unstable/httpapi/HttpApiEndpoint.d.ts +32 -32
  130. package/dist/unstable/httpapi/HttpApiEndpoint.d.ts.map +1 -1
  131. package/dist/unstable/httpapi/HttpApiEndpoint.js.map +1 -1
  132. package/dist/unstable/httpapi/HttpApiMiddleware.d.ts +1 -1
  133. package/dist/unstable/httpapi/HttpApiMiddleware.d.ts.map +1 -1
  134. package/dist/unstable/httpapi/HttpApiMiddleware.js +4 -3
  135. package/dist/unstable/httpapi/HttpApiMiddleware.js.map +1 -1
  136. package/dist/unstable/httpapi/HttpApiSchema.d.ts +13 -6
  137. package/dist/unstable/httpapi/HttpApiSchema.d.ts.map +1 -1
  138. package/dist/unstable/httpapi/HttpApiSchema.js +0 -13
  139. package/dist/unstable/httpapi/HttpApiSchema.js.map +1 -1
  140. package/dist/unstable/httpapi/OpenApi.js.map +1 -1
  141. package/dist/unstable/observability/internal/otlpProtobuf.js +0 -68
  142. package/dist/unstable/observability/internal/otlpProtobuf.js.map +1 -1
  143. package/dist/unstable/observability/internal/protobuf.js +0 -28
  144. package/dist/unstable/observability/internal/protobuf.js.map +1 -1
  145. package/dist/unstable/persistence/KeyValueStore.d.ts +2 -2
  146. package/dist/unstable/persistence/KeyValueStore.d.ts.map +1 -1
  147. package/dist/unstable/persistence/KeyValueStore.js.map +1 -1
  148. package/dist/unstable/persistence/Persistable.d.ts +6 -6
  149. package/dist/unstable/persistence/Persistable.d.ts.map +1 -1
  150. package/dist/unstable/persistence/PersistedQueue.d.ts +3 -3
  151. package/dist/unstable/persistence/PersistedQueue.d.ts.map +1 -1
  152. package/dist/unstable/persistence/PersistedQueue.js +1 -1
  153. package/dist/unstable/persistence/PersistedQueue.js.map +1 -1
  154. package/dist/unstable/persistence/Persistence.d.ts +5 -5
  155. package/dist/unstable/persistence/Persistence.d.ts.map +1 -1
  156. package/dist/unstable/reactivity/AsyncResult.d.ts +2 -2
  157. package/dist/unstable/reactivity/AsyncResult.d.ts.map +1 -1
  158. package/dist/unstable/reactivity/Atom.d.ts +1 -1
  159. package/dist/unstable/reactivity/Atom.d.ts.map +1 -1
  160. package/dist/unstable/reactivity/AtomHttpApi.d.ts +1 -1
  161. package/dist/unstable/reactivity/AtomHttpApi.d.ts.map +1 -1
  162. package/dist/unstable/rpc/Rpc.d.ts +3 -4
  163. package/dist/unstable/rpc/Rpc.d.ts.map +1 -1
  164. package/dist/unstable/rpc/Rpc.js +1 -1
  165. package/dist/unstable/rpc/Rpc.js.map +1 -1
  166. package/dist/unstable/rpc/RpcClient.d.ts.map +1 -1
  167. package/dist/unstable/rpc/RpcClient.js +25 -4
  168. package/dist/unstable/rpc/RpcClient.js.map +1 -1
  169. package/dist/unstable/rpc/RpcMessage.d.ts +7 -0
  170. package/dist/unstable/rpc/RpcMessage.d.ts.map +1 -1
  171. package/dist/unstable/rpc/RpcMessage.js +20 -0
  172. package/dist/unstable/rpc/RpcMessage.js.map +1 -1
  173. package/dist/unstable/rpc/RpcMiddleware.d.ts +2 -2
  174. package/dist/unstable/rpc/RpcMiddleware.d.ts.map +1 -1
  175. package/dist/unstable/rpc/RpcMiddleware.js +4 -3
  176. package/dist/unstable/rpc/RpcMiddleware.js.map +1 -1
  177. package/dist/unstable/rpc/RpcSchema.d.ts +10 -3
  178. package/dist/unstable/rpc/RpcSchema.d.ts.map +1 -1
  179. package/dist/unstable/rpc/RpcSchema.js.map +1 -1
  180. package/dist/unstable/rpc/RpcWorker.d.ts +3 -3
  181. package/dist/unstable/rpc/RpcWorker.d.ts.map +1 -1
  182. package/dist/unstable/schema/Model.d.ts +2 -2
  183. package/dist/unstable/schema/Model.d.ts.map +1 -1
  184. package/dist/unstable/schema/Model.js.map +1 -1
  185. package/dist/unstable/schema/VariantSchema.d.ts +16 -2
  186. package/dist/unstable/schema/VariantSchema.d.ts.map +1 -1
  187. package/dist/unstable/schema/VariantSchema.js.map +1 -1
  188. package/dist/unstable/sql/SqlConnection.d.ts +1 -0
  189. package/dist/unstable/sql/SqlConnection.d.ts.map +1 -1
  190. package/dist/unstable/sql/SqlConnection.js.map +1 -1
  191. package/dist/unstable/sql/SqlModel.js.map +1 -1
  192. package/dist/unstable/sql/SqlResolver.d.ts +4 -4
  193. package/dist/unstable/sql/SqlResolver.d.ts.map +1 -1
  194. package/dist/unstable/sql/SqlResolver.js.map +1 -1
  195. package/dist/unstable/sql/SqlSchema.d.ts +5 -5
  196. package/dist/unstable/sql/SqlSchema.d.ts.map +1 -1
  197. package/dist/unstable/sql/Statement.d.ts +1 -0
  198. package/dist/unstable/sql/Statement.d.ts.map +1 -1
  199. package/dist/unstable/sql/Statement.js +3 -0
  200. package/dist/unstable/sql/Statement.js.map +1 -1
  201. package/dist/unstable/workflow/Activity.d.ts +2 -2
  202. package/dist/unstable/workflow/Activity.d.ts.map +1 -1
  203. package/dist/unstable/workflow/DurableDeferred.d.ts +18 -18
  204. package/dist/unstable/workflow/DurableDeferred.d.ts.map +1 -1
  205. package/dist/unstable/workflow/Workflow.d.ts +3 -3
  206. package/dist/unstable/workflow/Workflow.d.ts.map +1 -1
  207. package/dist/unstable/workflow/Workflow.js.map +1 -1
  208. package/dist/unstable/workflow/WorkflowEngine.d.ts +3 -3
  209. package/dist/unstable/workflow/WorkflowEngine.d.ts.map +1 -1
  210. package/package.json +1 -1
  211. package/src/ChannelSchema.ts +9 -9
  212. package/src/Config.ts +12 -5
  213. package/src/Context.ts +4 -5
  214. package/src/Cron.ts +4 -0
  215. package/src/Layer.ts +4 -4
  216. package/src/LayerMap.ts +4 -3
  217. package/src/Random.ts +42 -0
  218. package/src/Schema.ts +1038 -710
  219. package/src/SchemaAST.ts +239 -169
  220. package/src/SchemaParser.ts +85 -77
  221. package/src/internal/effect.ts +17 -33
  222. package/src/internal/matcher.ts +0 -5
  223. package/src/internal/schema/arbitrary.ts +24 -3
  224. package/src/internal/schema/representation.ts +1 -5
  225. package/src/internal/schema/schema.ts +1 -1
  226. package/src/internal/stackTraceLimit.ts +63 -0
  227. package/src/internal/tracer.ts +4 -3
  228. package/src/testing/TestSchema.ts +14 -13
  229. package/src/unstable/ai/AnthropicStructuredOutput.ts +2 -2
  230. package/src/unstable/ai/Chat.ts +1 -1
  231. package/src/unstable/ai/LanguageModel.ts +7 -7
  232. package/src/unstable/ai/McpSchema.ts +16 -14
  233. package/src/unstable/ai/McpServer.ts +7 -7
  234. package/src/unstable/ai/OpenAiStructuredOutput.ts +2 -2
  235. package/src/unstable/ai/Response.ts +9 -5
  236. package/src/unstable/ai/Tool.ts +53 -50
  237. package/src/unstable/cli/Argument.ts +9 -8
  238. package/src/unstable/cli/Flag.ts +9 -8
  239. package/src/unstable/cli/Param.ts +6 -146
  240. package/src/unstable/cli/Primitive.ts +5 -6
  241. package/src/unstable/cli/internal/ansi.ts +0 -33
  242. package/src/unstable/cli/internal/config.ts +0 -8
  243. package/src/unstable/cli/internal/parser.ts +2 -2
  244. package/src/unstable/cluster/MessageStorage.ts +1 -1
  245. package/src/unstable/cluster/Reply.ts +6 -2
  246. package/src/unstable/devtools/DevToolsSchema.ts +1 -1
  247. package/src/unstable/encoding/Msgpack.ts +9 -7
  248. package/src/unstable/encoding/Ndjson.ts +10 -10
  249. package/src/unstable/encoding/Sse.ts +1 -1
  250. package/src/unstable/eventlog/Event.ts +3 -3
  251. package/src/unstable/http/HttpBody.ts +1 -1
  252. package/src/unstable/http/HttpClientRequest.ts +1 -1
  253. package/src/unstable/http/HttpIncomingMessage.ts +1 -1
  254. package/src/unstable/http/HttpMiddleware.ts +12 -3
  255. package/src/unstable/httpapi/HttpApi.ts +1 -1
  256. package/src/unstable/httpapi/HttpApiBuilder.ts +9 -5
  257. package/src/unstable/httpapi/HttpApiClient.ts +10 -10
  258. package/src/unstable/httpapi/HttpApiEndpoint.ts +45 -33
  259. package/src/unstable/httpapi/HttpApiMiddleware.ts +6 -5
  260. package/src/unstable/httpapi/HttpApiSchema.ts +16 -43
  261. package/src/unstable/httpapi/OpenApi.ts +5 -5
  262. package/src/unstable/observability/internal/otlpProtobuf.ts +0 -75
  263. package/src/unstable/observability/internal/protobuf.ts +0 -42
  264. package/src/unstable/persistence/KeyValueStore.ts +2 -2
  265. package/src/unstable/persistence/Persistable.ts +7 -7
  266. package/src/unstable/persistence/PersistedQueue.ts +4 -4
  267. package/src/unstable/persistence/Persistence.ts +5 -5
  268. package/src/unstable/reactivity/AsyncResult.ts +4 -4
  269. package/src/unstable/reactivity/Atom.ts +1 -1
  270. package/src/unstable/reactivity/AtomHttpApi.ts +1 -1
  271. package/src/unstable/rpc/Rpc.ts +7 -8
  272. package/src/unstable/rpc/RpcClient.ts +26 -4
  273. package/src/unstable/rpc/RpcMessage.ts +19 -0
  274. package/src/unstable/rpc/RpcMiddleware.ts +6 -5
  275. package/src/unstable/rpc/RpcSchema.ts +12 -9
  276. package/src/unstable/rpc/RpcWorker.ts +3 -3
  277. package/src/unstable/schema/Model.ts +2 -2
  278. package/src/unstable/schema/VariantSchema.ts +19 -17
  279. package/src/unstable/sql/SqlConnection.ts +5 -0
  280. package/src/unstable/sql/SqlModel.ts +2 -2
  281. package/src/unstable/sql/SqlResolver.ts +6 -6
  282. package/src/unstable/sql/SqlSchema.ts +5 -5
  283. package/src/unstable/sql/Statement.ts +11 -0
  284. package/src/unstable/workflow/Activity.ts +6 -6
  285. package/src/unstable/workflow/DurableDeferred.ts +33 -33
  286. package/src/unstable/workflow/Workflow.ts +5 -5
  287. package/src/unstable/workflow/WorkflowEngine.ts +10 -10
  288. package/dist/internal/concurrency.d.ts +0 -2
  289. package/dist/internal/concurrency.d.ts.map +0 -1
  290. package/dist/internal/concurrency.js +0 -29
  291. package/dist/internal/concurrency.js.map +0 -1
  292. package/src/internal/concurrency.ts +0 -52
package/src/SchemaAST.ts CHANGED
@@ -25,7 +25,6 @@ import * as InternalSchemaCause from "./internal/schema/cause.ts"
25
25
  import * as Option from "./Option.ts"
26
26
  import * as Pipeable from "./Pipeable.ts"
27
27
  import * as Predicate from "./Predicate.ts"
28
- import * as RegEx from "./RegExp.ts"
29
28
  import * as Result from "./Result.ts"
30
29
  import type * as Schema from "./Schema.ts"
31
30
  import * as SchemaGetter from "./SchemaGetter.ts"
@@ -679,19 +678,19 @@ export class Declaration extends Base {
679
678
  return Effect.mapEager(run(oinput.value, this, options), Option.some)
680
679
  }
681
680
  }
682
- private rebuild(recur: (ast: AST) => AST, checks: Checks | undefined, encodingChecks: Checks | undefined) {
681
+ private _rebuild(recur: (ast: AST) => AST, checks: Checks | undefined, encodingChecks: Checks | undefined) {
683
682
  const tps = mapOrSame(this.typeParameters, recur)
684
- return tps === this.typeParameters ?
683
+ return tps === this.typeParameters && checks === this.checks && encodingChecks === this.encodingChecks ?
685
684
  this :
686
685
  new Declaration(tps, this.run, this.annotations, checks, undefined, this.context, encodingChecks)
687
686
  }
688
687
  /** @internal */
689
688
  recur(recur: (ast: AST) => AST) {
690
- return this.rebuild(recur, this.checks, this.encodingChecks)
689
+ return this._rebuild(recur, this.checks, this.encodingChecks)
691
690
  }
692
691
  /** @internal */
693
692
  flip(recur: (ast: AST) => AST) {
694
- return this.rebuild(recur, this.encodingChecks, this.checks)
693
+ return this._rebuild(recur, this.encodingChecks, this.checks)
695
694
  }
696
695
  /** @internal */
697
696
  getExpected(): string {
@@ -1066,11 +1065,12 @@ function isTemplateLiteralPart(ast: AST): ast is TemplateLiteralPart {
1066
1065
  case "String":
1067
1066
  case "Number":
1068
1067
  case "BigInt":
1068
+ return true
1069
1069
  case "Literal":
1070
1070
  case "TemplateLiteral":
1071
- return true
1071
+ return ast.checks === undefined
1072
1072
  case "Union":
1073
- return ast.types.every(isTemplateLiteralPart)
1073
+ return ast.checks === undefined && ast.types.every(isTemplateLiteralPart)
1074
1074
  default:
1075
1075
  return false
1076
1076
  }
@@ -1082,8 +1082,7 @@ function isTemplateLiteralPart(ast: AST): ast is TemplateLiteralPart {
1082
1082
  *
1083
1083
  * **Details**
1084
1084
  *
1085
- * `parts` is an array of AST nodes; each part contributes to the
1086
- * template literal pattern. A regex is derived from the parts to validate
1085
+ * `parts` is an array of AST nodes; each part contributes to matching
1087
1086
  * strings at runtime.
1088
1087
  *
1089
1088
  * @see {@link isTemplateLiteral}
@@ -1130,19 +1129,22 @@ export class TemplateLiteral extends Base {
1130
1129
  return "string"
1131
1130
  }
1132
1131
  /** @internal */
1132
+ matchPart(s: string, options: ParseOptions): string | undefined {
1133
+ return segmentTemplateLiteralParts(this.encodedParts, s, options) === undefined ? undefined : s
1134
+ }
1135
+ /** @internal */
1133
1136
  asTemplateLiteralParser(): Arrays {
1134
- const tuple = new Arrays(false, this.parts.map(templateLiteralPartFromString), [])
1135
- const regExp = getTemplateLiteralRegExp(this)
1137
+ const tuple = new Arrays(false, this.parts.map(partFromString), [])
1136
1138
  return decodeTo(
1137
1139
  string,
1138
1140
  tuple,
1139
1141
  new SchemaTransformation.Transformation(
1140
- SchemaGetter.transformOrFail((s: string) => {
1141
- const match = regExp.exec(s)
1142
- if (match) return Effect.succeed(match.slice(1, this.parts.length + 1))
1142
+ SchemaGetter.transformOrFail((s: string, options) => {
1143
+ const segments = segmentTemplateLiteralParts(this.encodedParts, s, options)
1144
+ if (segments !== undefined) return Effect.succeed(segments)
1143
1145
  return Effect.fail(
1144
1146
  new SchemaIssue.InvalidValue(Option.some(s), {
1145
- message: `Expected a value matching ${regExp.source}, got ${format(s)}`
1147
+ message: `Expected a string matching template literal parts, got ${format(s)}`
1146
1148
  })
1147
1149
  )
1148
1150
  }),
@@ -1248,6 +1250,10 @@ export class Literal extends Base {
1248
1250
  return fromConst(this, this.literal)
1249
1251
  }
1250
1252
  /** @internal */
1253
+ matchPart(s: string, _options: ParseOptions): LiteralValue | undefined {
1254
+ return s === globalThis.String(this.literal) ? this.literal : undefined
1255
+ }
1256
+ /** @internal */
1251
1257
  toCodecJson(): AST {
1252
1258
  return typeof this.literal === "bigint" ? literalToString(this) : this
1253
1259
  }
@@ -1290,6 +1296,10 @@ export class String extends Base {
1290
1296
  return fromRefinement(this, Predicate.isString)
1291
1297
  }
1292
1298
  /** @internal */
1299
+ matchPart(s: string, options: ParseOptions): string | undefined {
1300
+ return applyTemplateLiteralPartChecks(this, s, options)
1301
+ }
1302
+ /** @internal */
1293
1303
  getExpected(): string {
1294
1304
  return "string"
1295
1305
  }
@@ -1336,6 +1346,19 @@ export class Number extends Base {
1336
1346
  return fromRefinement(this, Predicate.isNumber)
1337
1347
  }
1338
1348
  /** @internal */
1349
+ matchKey(s: string, options: ParseOptions): number | undefined {
1350
+ return this._match(isStringNumberRegExp, s, options)
1351
+ }
1352
+ /** @internal */
1353
+ matchPart(s: string, options: ParseOptions): number | undefined {
1354
+ return this._match(isStringFiniteRegExp, s, options)
1355
+ }
1356
+ private _match(regexp: RegExp, s: string, options: ParseOptions): number | undefined {
1357
+ return regexp.test(s)
1358
+ ? applyTemplateLiteralPartChecks(this, globalThis.Number(s), options)
1359
+ : undefined
1360
+ }
1361
+ /** @internal */
1339
1362
  toCodecJson(): AST {
1340
1363
  if (this.checks && (hasCheck(this.checks, "isFinite") || hasCheck(this.checks, "isInt"))) {
1341
1364
  return this
@@ -1445,6 +1468,10 @@ export class Symbol extends Base {
1445
1468
  return fromRefinement(this, Predicate.isSymbol)
1446
1469
  }
1447
1470
  /** @internal */
1471
+ matchKey(s: symbol, options: ParseOptions): symbol | undefined {
1472
+ return applyTemplateLiteralPartChecks(this, s, options)
1473
+ }
1474
+ /** @internal */
1448
1475
  toCodecStringTree(): AST {
1449
1476
  return replaceEncoding(this, [symbolToString])
1450
1477
  }
@@ -1494,6 +1521,12 @@ export class BigInt extends Base {
1494
1521
  return fromRefinement(this, Predicate.isBigInt)
1495
1522
  }
1496
1523
  /** @internal */
1524
+ matchPart(s: string, options: ParseOptions): bigint | undefined {
1525
+ return isStringBigIntRegExp.test(s)
1526
+ ? applyTemplateLiteralPartChecks(this, globalThis.BigInt(s), options)
1527
+ : undefined
1528
+ }
1529
+ /** @internal */
1497
1530
  toCodecStringTree(): AST {
1498
1531
  return replaceEncoding(this, [bigIntToString])
1499
1532
  }
@@ -1668,10 +1701,11 @@ export class Arrays extends Base {
1668
1701
  return Option.some(state.output)
1669
1702
  })
1670
1703
  }
1671
- private rebuild(recur: (ast: AST) => AST, checks: Checks | undefined, encodingChecks: Checks | undefined) {
1704
+ private _rebuild(recur: (ast: AST) => AST, checks: Checks | undefined, encodingChecks: Checks | undefined) {
1672
1705
  const elements = mapOrSame(this.elements, recur)
1673
1706
  const rest = mapOrSame(this.rest, recur)
1674
- return elements === this.elements && rest === this.rest ?
1707
+ return elements === this.elements && rest === this.rest && checks === this.checks &&
1708
+ encodingChecks === this.encodingChecks ?
1675
1709
  this :
1676
1710
  new Arrays(
1677
1711
  this.isMutable,
@@ -1686,11 +1720,11 @@ export class Arrays extends Base {
1686
1720
  }
1687
1721
  /** @internal */
1688
1722
  recur(recur: (ast: AST) => AST) {
1689
- return this.rebuild(recur, this.checks, this.encodingChecks)
1723
+ return this._rebuild(recur, this.checks, this.encodingChecks)
1690
1724
  }
1691
1725
  /** @internal */
1692
1726
  flip(recur: (ast: AST) => AST) {
1693
- return this.rebuild(recur, this.encodingChecks, this.checks)
1727
+ return this._rebuild(recur, this.encodingChecks, this.checks)
1694
1728
  }
1695
1729
  /** @internal */
1696
1730
  getExpected(): string {
@@ -1783,33 +1817,36 @@ const wrapPropertyKeyIssue = (
1783
1817
  */
1784
1818
  export const FINITE_PATTERN = "[+-]?\\d*\\.?\\d+(?:[Ee][+-]?\\d+)?"
1785
1819
 
1786
- const isNumberStringRegExp = new globalThis.RegExp(`(?:${FINITE_PATTERN}|Infinity|-Infinity|NaN)`)
1787
-
1788
1820
  /**
1789
1821
  * Returns the object keys that match the index signature parameter schema.
1790
1822
  * @internal
1791
1823
  */
1792
1824
  export function getIndexSignatureKeys(
1793
1825
  input: { readonly [x: PropertyKey]: unknown },
1794
- parameter: AST
1826
+ parameter: IndexSignatureParameter,
1827
+ options: ParseOptions = defaultParseOptions
1795
1828
  ): ReadonlyArray<PropertyKey> {
1796
- const encoded = toEncoded(parameter)
1797
- switch (encoded._tag) {
1798
- case "String":
1799
- return Object.keys(input)
1800
- case "TemplateLiteral": {
1801
- const regExp = getTemplateLiteralRegExp(encoded)
1802
- return Object.keys(input).filter((k) => regExp.test(k))
1829
+ let stringKeys: ReadonlyArray<string> | undefined
1830
+ let symbolKeys: ReadonlyArray<symbol> | undefined
1831
+
1832
+ function go(parameter: AST): ReadonlyArray<PropertyKey> {
1833
+ switch (parameter._tag) {
1834
+ case "String":
1835
+ case "TemplateLiteral":
1836
+ return (stringKeys ??= Object.keys(input)).filter((k) => parameter.matchPart(k, options) !== undefined)
1837
+ case "Number":
1838
+ return (stringKeys ??= Object.keys(input)).filter((k) => parameter.matchKey(k, options) !== undefined)
1839
+ case "Symbol":
1840
+ return (symbolKeys ??= Object.getOwnPropertySymbols(input)).filter((k) =>
1841
+ parameter.matchKey(k, options) !== undefined
1842
+ )
1843
+ case "Union":
1844
+ return [...new Set(parameter.types.flatMap(go))]
1845
+ default:
1846
+ return []
1803
1847
  }
1804
- case "Symbol":
1805
- return Object.getOwnPropertySymbols(input)
1806
- case "Number":
1807
- return Object.keys(input).filter((k) => isNumberStringRegExp.test(k))
1808
- case "Union":
1809
- return [...new Set(encoded.types.flatMap((t) => getIndexSignatureKeys(input, t)))]
1810
- default:
1811
- return []
1812
1848
  }
1849
+ return go(parameterFromPropertyKey(toEncoded(parameter)))
1813
1850
  }
1814
1851
 
1815
1852
  /**
@@ -1869,6 +1906,31 @@ export class KeyValueCombiner {
1869
1906
  }
1870
1907
  }
1871
1908
 
1909
+ type IndexSignatureParameter =
1910
+ | String
1911
+ | Number
1912
+ | Symbol
1913
+ | TemplateLiteral
1914
+ | Union<IndexSignatureParameter>
1915
+
1916
+ function isIndexSignatureParameterSide(ast: AST): ast is IndexSignatureParameter {
1917
+ switch (ast._tag) {
1918
+ case "String":
1919
+ case "Number":
1920
+ case "Symbol":
1921
+ case "TemplateLiteral":
1922
+ return true
1923
+ case "Union":
1924
+ return ast.types.every(isIndexSignatureParameterSide)
1925
+ default:
1926
+ return false
1927
+ }
1928
+ }
1929
+
1930
+ function isIndexSignatureParameter(ast: AST): ast is IndexSignatureParameter {
1931
+ return isIndexSignatureParameterSide(ast) && isIndexSignatureParameterSide(toEncoded(ast))
1932
+ }
1933
+
1872
1934
  /**
1873
1935
  * Represents an index signature entry within an {@link Objects} node.
1874
1936
  *
@@ -1895,7 +1957,7 @@ export class KeyValueCombiner {
1895
1957
  * @since 3.10.0
1896
1958
  */
1897
1959
  export class IndexSignature {
1898
- readonly parameter: AST
1960
+ readonly parameter: IndexSignatureParameter
1899
1961
  readonly type: AST
1900
1962
  readonly merge: KeyValueCombiner | undefined
1901
1963
 
@@ -1904,6 +1966,9 @@ export class IndexSignature {
1904
1966
  type: AST,
1905
1967
  merge: KeyValueCombiner | undefined
1906
1968
  ) {
1969
+ if (!isIndexSignatureParameter(parameter)) {
1970
+ throw new Error(`Invalid index signature parameter ${parameter._tag}`)
1971
+ }
1907
1972
  this.parameter = parameter
1908
1973
  this.type = type
1909
1974
  this.merge = merge
@@ -2026,7 +2091,7 @@ export class Objects extends Base {
2026
2091
  s,
2027
2092
  [key, is]
2028
2093
  ) {
2029
- const parserKey = recur(indexSignatureParameterFromString(is.parameter))
2094
+ const parserKey = recur(parameterFromPropertyKey(is.parameter))
2030
2095
  const effKey = parserKey(Option.some(key), s.options)
2031
2096
  const exitKey = (effectIsExit(effKey) ? effKey : yield* Effect.exit(effKey)) as Exit.Exit<
2032
2097
  Option.Option<PropertyKey>,
@@ -2133,7 +2198,7 @@ export class Objects extends Base {
2133
2198
  const keyPairs = Arr.empty<[PropertyKey, IndexSignature]>()
2134
2199
  for (let i = 0; i < indexCount; i++) {
2135
2200
  const is = ast.indexSignatures[i]
2136
- const keys = getIndexSignatureKeys(input, is.parameter)
2201
+ const keys = getIndexSignatureKeys(input, is.parameter, options)
2137
2202
  for (let j = 0; j < keys.length; j++) {
2138
2203
  const key = keys[j]
2139
2204
  keyPairs.push([key, is])
@@ -2160,8 +2225,9 @@ export class Objects extends Base {
2160
2225
  return Option.some(out)
2161
2226
  })
2162
2227
  }
2163
- private rebuild(
2228
+ private _rebuild(
2164
2229
  recur: (ast: AST) => AST,
2230
+ recurParameter: (ast: AST) => AST,
2165
2231
  flipMerge: boolean,
2166
2232
  checks: Checks | undefined,
2167
2233
  encodingChecks: Checks | undefined
@@ -2172,7 +2238,7 @@ export class Objects extends Base {
2172
2238
  })
2173
2239
 
2174
2240
  const indexes = mapOrSame(this.indexSignatures, (is) => {
2175
- const p = recur(is.parameter)
2241
+ const p = recurParameter(is.parameter)
2176
2242
  const t = recur(is.type)
2177
2243
  const merge = flipMerge ? is.merge?.flip() : is.merge
2178
2244
  return p === is.parameter && t === is.type && merge === is.merge
@@ -2180,7 +2246,8 @@ export class Objects extends Base {
2180
2246
  : new IndexSignature(p, t, merge)
2181
2247
  })
2182
2248
 
2183
- return props === this.propertySignatures && indexes === this.indexSignatures
2249
+ return props === this.propertySignatures && indexes === this.indexSignatures && checks === this.checks &&
2250
+ encodingChecks === this.encodingChecks
2184
2251
  ? this
2185
2252
  : new Objects(
2186
2253
  props,
@@ -2194,11 +2261,11 @@ export class Objects extends Base {
2194
2261
  }
2195
2262
  /** @internal */
2196
2263
  flip(recur: (ast: AST) => AST): AST {
2197
- return this.rebuild(recur, true, this.encodingChecks, this.checks)
2264
+ return this._rebuild(recur, recur, true, this.encodingChecks, this.checks)
2198
2265
  }
2199
2266
  /** @internal */
2200
- recur(recur: (ast: AST) => AST): AST {
2201
- return this.rebuild(recur, false, this.checks, this.encodingChecks)
2267
+ recur(recur: (ast: AST) => AST, recurParameter: (ast: AST) => AST = recur): AST {
2268
+ return this._rebuild(recur, recurParameter, false, this.checks, this.encodingChecks)
2202
2269
  }
2203
2270
  /** @internal */
2204
2271
  getExpected(): string {
@@ -2257,14 +2324,10 @@ const parseProperties = iterateEager<{
2257
2324
  }
2258
2325
  })
2259
2326
 
2260
- function mergeChecks(checks: Checks | undefined, b: AST): Checks | undefined {
2261
- if (!checks) {
2262
- return b.checks
2263
- }
2264
- if (!b.checks) {
2265
- return checks
2266
- }
2267
- return [...checks, ...b.checks]
2327
+ function combineChecks(a: Checks | undefined, b: Checks | undefined): Checks | undefined {
2328
+ if (!a) return b
2329
+ if (!b) return a
2330
+ return [...a, ...b]
2268
2331
  }
2269
2332
 
2270
2333
  /** @internal */
@@ -2284,7 +2347,7 @@ export function struct<Fields extends Schema.Struct.Fields>(
2284
2347
  }
2285
2348
 
2286
2349
  /** @internal */
2287
- export function getAST<S extends Schema.Top>(self: S): S["ast"] {
2350
+ export function getAST<S extends { readonly ast: AST }>(self: S): S["ast"] {
2288
2351
  return self.ast
2289
2352
  }
2290
2353
 
@@ -2297,7 +2360,7 @@ export function tuple<Elements extends Schema.Tuple.Elements>(
2297
2360
  }
2298
2361
 
2299
2362
  /** @internal */
2300
- export function union<Members extends ReadonlyArray<Schema.Top>>(
2363
+ export function union<Members extends ReadonlyArray<{ readonly ast: AST }>>(
2301
2364
  members: Members,
2302
2365
  mode: "anyOf" | "oneOf",
2303
2366
  checks: Checks | undefined
@@ -2313,10 +2376,10 @@ export function structWithRest(ast: Objects, records: ReadonlyArray<Objects>): O
2313
2376
  let propertySignatures = ast.propertySignatures
2314
2377
  let indexSignatures = ast.indexSignatures
2315
2378
  let checks = ast.checks
2316
- for (const r of records) {
2317
- propertySignatures = propertySignatures.concat(r.propertySignatures)
2318
- indexSignatures = indexSignatures.concat(r.indexSignatures)
2319
- checks = mergeChecks(checks, r)
2379
+ for (const record of records) {
2380
+ propertySignatures = propertySignatures.concat(record.propertySignatures)
2381
+ indexSignatures = indexSignatures.concat(record.indexSignatures)
2382
+ checks = combineChecks(checks, record.checks)
2320
2383
  }
2321
2384
  return new Objects(propertySignatures, indexSignatures, undefined, checks)
2322
2385
  }
@@ -2598,19 +2661,27 @@ export class Union<A extends AST = AST> extends Base {
2598
2661
  })
2599
2662
  }
2600
2663
  }
2601
- private rebuild(recur: (ast: AST) => AST, checks: Checks | undefined, encodingChecks: Checks | undefined) {
2664
+ private _rebuild(recur: (ast: AST) => AST, checks: Checks | undefined, encodingChecks: Checks | undefined) {
2602
2665
  const types = mapOrSame(this.types, recur)
2603
- return types === this.types ?
2666
+ return types === this.types && checks === this.checks && encodingChecks === this.encodingChecks ?
2604
2667
  this :
2605
2668
  new Union(types, this.mode, this.annotations, checks, undefined, this.context, encodingChecks)
2606
2669
  }
2607
2670
  /** @internal */
2608
2671
  recur(recur: (ast: AST) => AST) {
2609
- return this.rebuild(recur, this.checks, this.encodingChecks)
2672
+ return this._rebuild(recur, this.checks, this.encodingChecks)
2610
2673
  }
2611
2674
  /** @internal */
2612
2675
  flip(recur: (ast: AST) => AST) {
2613
- return this.rebuild(recur, this.encodingChecks, this.checks)
2676
+ return this._rebuild(recur, this.encodingChecks, this.checks)
2677
+ }
2678
+ /** @internal */
2679
+ matchPart(s: string, options: ParseOptions): LiteralValue | undefined {
2680
+ for (const type of this.types) {
2681
+ const out = (type as TemplateLiteralPart).matchPart(s, options)
2682
+ if (out !== undefined) return out
2683
+ }
2684
+ return undefined
2614
2685
  }
2615
2686
  /** @internal */
2616
2687
  getExpected(getExpected: (ast: AST) => string): string {
@@ -2791,19 +2862,6 @@ export class Suspend extends Base {
2791
2862
  }
2792
2863
  }
2793
2864
 
2794
- /** @internal */
2795
- export function getEncodingChecks(ast: AST): Checks | undefined {
2796
- switch (ast._tag) {
2797
- case "Declaration":
2798
- case "Arrays":
2799
- case "Objects":
2800
- case "Union":
2801
- return ast.encodingChecks
2802
- default:
2803
- return undefined
2804
- }
2805
- }
2806
-
2807
2865
  // -----------------------------------------------------------------------------
2808
2866
  // Checks
2809
2867
  // -----------------------------------------------------------------------------
@@ -3052,8 +3110,8 @@ export function replaceChecks<A extends AST>(ast: A, checks: Checks | undefined)
3052
3110
  }
3053
3111
 
3054
3112
  /** @internal */
3055
- export function appendChecks<A extends AST>(ast: A, checks: Checks): A {
3056
- return replaceChecks(ast, ast.checks ? [...ast.checks, ...checks] : checks)
3113
+ export function appendChecks<A extends AST>(ast: A, checks: Checks | undefined): A {
3114
+ return replaceChecks(ast, combineChecks(ast.checks, checks))
3057
3115
  }
3058
3116
 
3059
3117
  function updateLastLink(encoding: Encoding, f: (ast: AST) => AST): Encoding {
@@ -3071,6 +3129,14 @@ export function applyToLastLink(f: (ast: AST) => AST) {
3071
3129
  return <A extends AST>(ast: A): A => ast.encoding ? replaceEncoding(ast, updateLastLink(ast.encoding, f)) : ast
3072
3130
  }
3073
3131
 
3132
+ /** @internal */
3133
+ export function applyToSelfOrLastLinkEncoding(f: (ast: AST) => AST) {
3134
+ function out(ast: AST): AST {
3135
+ return ast.encoding ? replaceEncoding(ast, updateLastLink(ast.encoding, out)) : f(ast)
3136
+ }
3137
+ return memoize(out)
3138
+ }
3139
+
3074
3140
  /** @internal */
3075
3141
  export function middlewareDecoding(
3076
3142
  ast: AST,
@@ -3222,39 +3288,31 @@ function parseParameter(ast: AST): {
3222
3288
  literals: ReadonlyArray<PropertyKey>
3223
3289
  parameters: ReadonlyArray<AST>
3224
3290
  } {
3225
- switch (ast._tag) {
3226
- case "Literal":
3227
- return {
3228
- literals: Predicate.isPropertyKey(ast.literal) ? [ast.literal] : [],
3229
- parameters: []
3230
- }
3231
- case "UniqueSymbol":
3232
- return {
3233
- literals: [ast.symbol],
3234
- parameters: []
3235
- }
3236
- case "String":
3237
- case "Number":
3238
- case "Symbol":
3239
- case "TemplateLiteral":
3240
- return {
3241
- literals: [],
3242
- parameters: [ast]
3243
- }
3244
- case "Union": {
3245
- const out: {
3246
- literals: ReadonlyArray<PropertyKey>
3247
- parameters: ReadonlyArray<AST>
3248
- } = { literals: [], parameters: [] }
3249
- for (let i = 0; i < ast.types.length; i++) {
3250
- const parsed = parseParameter(ast.types[i])
3251
- out.literals = out.literals.concat(parsed.literals)
3252
- out.parameters = out.parameters.concat(parsed.parameters)
3253
- }
3254
- return out
3291
+ const literals: Array<PropertyKey> = []
3292
+ const parameters: Array<AST> = []
3293
+ function go(ast: AST) {
3294
+ switch (ast._tag) {
3295
+ case "Literal":
3296
+ if (Predicate.isPropertyKey(ast.literal)) {
3297
+ literals.push(ast.literal)
3298
+ }
3299
+ return
3300
+ case "UniqueSymbol":
3301
+ literals.push(ast.symbol)
3302
+ return
3303
+ case "Never":
3304
+ return
3305
+ case "Union":
3306
+ for (let i = 0; i < ast.types.length; i++) {
3307
+ go(ast.types[i])
3308
+ }
3309
+ return
3310
+ default:
3311
+ parameters.push(ast)
3255
3312
  }
3256
3313
  }
3257
- return { literals: [], parameters: [] }
3314
+ go(ast)
3315
+ return { literals, parameters }
3258
3316
  }
3259
3317
 
3260
3318
  /** @internal */
@@ -3323,9 +3381,13 @@ export const toType = memoize(<A extends AST>(ast: A): A => {
3323
3381
  }
3324
3382
  const out: any = ast
3325
3383
  const type = out.recur?.(toType) ?? out
3326
- if (getEncodingChecks(type)) {
3384
+ const encodingChecks = type.encodingChecks
3385
+ if (encodingChecks) {
3327
3386
  return modifyOwnPropertyDescriptors(type, (d) => {
3328
3387
  d.encodingChecks.value = undefined
3388
+ if (type === ast) {
3389
+ d.checks.value = combineChecks(type.checks, encodingChecks)
3390
+ }
3329
3391
  })
3330
3392
  }
3331
3393
  return type
@@ -3416,45 +3478,6 @@ export function containsUndefined(ast: AST): boolean {
3416
3478
  }
3417
3479
  }
3418
3480
 
3419
- function getTemplateLiteralSource(ast: TemplateLiteral, top: boolean): string {
3420
- return ast.encodedParts.map((part) =>
3421
- handleTemplateLiteralASTPartParens(part, getTemplateLiteralASTPartPattern(part), top)
3422
- ).join("")
3423
- }
3424
-
3425
- /** @internal */
3426
- export const getTemplateLiteralRegExp = memoize((ast: TemplateLiteral): RegExp => {
3427
- return new globalThis.RegExp(`^${getTemplateLiteralSource(ast, true)}$`)
3428
- })
3429
-
3430
- function getTemplateLiteralASTPartPattern(part: TemplateLiteralPart): string {
3431
- switch (part._tag) {
3432
- case "Literal":
3433
- return RegEx.escape(globalThis.String(part.literal))
3434
- case "String":
3435
- return STRING_PATTERN
3436
- case "Number":
3437
- return FINITE_PATTERN
3438
- case "BigInt":
3439
- return BIGINT_PATTERN
3440
- case "TemplateLiteral":
3441
- return getTemplateLiteralSource(part, false)
3442
- case "Union":
3443
- return part.types.map(getTemplateLiteralASTPartPattern).join("|")
3444
- }
3445
- }
3446
-
3447
- function handleTemplateLiteralASTPartParens(part: TemplateLiteralPart, s: string, top: boolean): string {
3448
- if (isUnion(part)) {
3449
- if (!top) {
3450
- return `(?:${s})`
3451
- }
3452
- } else if (!top) {
3453
- return s
3454
- }
3455
- return `(${s})`
3456
- }
3457
-
3458
3481
  function fromConst<const T>(
3459
3482
  ast: AST,
3460
3483
  value: T
@@ -3484,6 +3507,52 @@ function fromRefinement<T>(
3484
3507
  }
3485
3508
  }
3486
3509
 
3510
+ function applyTemplateLiteralPartChecks<A>(ast: AST, value: A, options: ParseOptions): A | undefined {
3511
+ if (options?.disableChecks || ast.checks === undefined) return value
3512
+ const issues: Array<SchemaIssue.Issue> = []
3513
+ collectIssues(ast.checks, value, issues, ast, options)
3514
+ return issues.length === 0 ? value : undefined
3515
+ }
3516
+
3517
+ function segmentTemplateLiteralParts(
3518
+ parts: ReadonlyArray<TemplateLiteralPart>,
3519
+ input: string,
3520
+ options: ParseOptions
3521
+ ): Array<string> | undefined {
3522
+ const out = new Array<string>(parts.length)
3523
+ const failures = new Set<string>()
3524
+ function go(i: number, pos: number): boolean {
3525
+ if (i === parts.length) return pos === input.length
3526
+ const key = `${i}/${pos}`
3527
+ if (failures.has(key)) return false
3528
+ const part = parts[i]
3529
+ if (i === parts.length - 1) {
3530
+ const s = input.slice(pos)
3531
+ if (part.matchPart(s, options) !== undefined) {
3532
+ out[i] = s
3533
+ return true
3534
+ }
3535
+ } else if (part._tag === "Literal") {
3536
+ const s = globalThis.String(part.literal)
3537
+ if (input.startsWith(s, pos) && go(i + 1, pos + s.length)) {
3538
+ out[i] = s
3539
+ return true
3540
+ }
3541
+ } else {
3542
+ for (let end = input.length; end >= pos; end--) {
3543
+ const s = input.slice(pos, end)
3544
+ if (part.matchPart(s, options) !== undefined && go(i + 1, end)) {
3545
+ out[i] = s
3546
+ return true
3547
+ }
3548
+ }
3549
+ }
3550
+ failures.add(key)
3551
+ return false
3552
+ }
3553
+ return go(0, 0) ? out : undefined
3554
+ }
3555
+
3487
3556
  /** @internal */
3488
3557
  export const enumsToLiterals = memoize((ast: Enum): Union<Literal> => {
3489
3558
  return new Union(
@@ -3492,38 +3561,40 @@ export const enumsToLiterals = memoize((ast: Enum): Union<Literal> => {
3492
3561
  )
3493
3562
  })
3494
3563
 
3495
- /** @internal */
3496
- export function toCodec(f: (ast: AST) => AST) {
3497
- function out(ast: AST): AST {
3498
- return ast.encoding ? replaceEncoding(ast, updateLastLink(ast.encoding, out)) : f(ast)
3499
- }
3500
- return memoize(out)
3501
- }
3502
-
3503
- const indexSignatureParameterFromString = toCodec((ast) => {
3564
+ const parameterFromPropertyKey = applyToSelfOrLastLinkEncoding((ast) => {
3504
3565
  switch (ast._tag) {
3505
3566
  default:
3506
3567
  return ast
3507
3568
  case "Number":
3508
3569
  return ast.toCodecStringTree()
3509
3570
  case "Union":
3510
- return ast.recur(indexSignatureParameterFromString)
3571
+ return ast.recur(parameterFromPropertyKey)
3511
3572
  }
3512
3573
  })
3513
3574
 
3514
- const templateLiteralPartFromString = toCodec((ast) => {
3575
+ /** @internal */
3576
+ export const parameterFromString = applyToSelfOrLastLinkEncoding((ast) => {
3515
3577
  switch (ast._tag) {
3516
3578
  default:
3517
3579
  return ast
3518
- case "String":
3519
- case "TemplateLiteral":
3580
+ case "Symbol":
3581
+ case "UniqueSymbol":
3582
+ return ast.toCodecStringTree()
3583
+ case "Union":
3584
+ return ast.recur(parameterFromString)
3585
+ }
3586
+ })
3587
+
3588
+ const partFromString = applyToSelfOrLastLinkEncoding((ast) => {
3589
+ switch (ast._tag) {
3590
+ default:
3520
3591
  return ast
3521
- case "BigInt":
3522
3592
  case "Number":
3523
3593
  case "Literal":
3594
+ case "BigInt":
3524
3595
  return ast.toCodecStringTree()
3525
3596
  case "Union":
3526
- return ast.recur(templateLiteralPartFromString)
3597
+ return ast.recur(partFromString)
3527
3598
  }
3528
3599
  })
3529
3600
 
@@ -3535,6 +3606,8 @@ export const STRING_PATTERN = "[\\s\\S]*?"
3535
3606
 
3536
3607
  const isStringFiniteRegExp = new globalThis.RegExp(`^${FINITE_PATTERN}$`)
3537
3608
 
3609
+ const isStringNumberRegExp = new globalThis.RegExp(`(?:${FINITE_PATTERN}|Infinity|-Infinity|NaN)`)
3610
+
3538
3611
  /** @internal */
3539
3612
  export function isStringFinite(annotations?: Schema.Annotations.Filter) {
3540
3613
  return isPattern(
@@ -3890,10 +3963,7 @@ const StringTree = new Declaration(
3890
3963
  isStringTree(input) ?
3891
3964
  Effect.succeed(input) :
3892
3965
  Effect.fail(new SchemaIssue.InvalidType(ast, Option.some(input))),
3893
- {
3894
- expected: "StringTree",
3895
- toCodecStringTree: () => new Link(unknown, SchemaTransformation.passthrough())
3896
- }
3966
+ { expected: "StringTree" }
3897
3967
  )
3898
3968
 
3899
3969
  /** @internal */