@clayroach/effect 3.19.14-source-capture.6 → 3.19.14-source-capture.8

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 (371) hide show
  1. package/dist/cjs/Utils.js +1 -1
  2. package/dist/cjs/Utils.js.map +1 -1
  3. package/dist/cjs/internal/clock.js +1 -1
  4. package/dist/cjs/internal/clock.js.map +1 -1
  5. package/dist/esm/Utils.js +1 -1
  6. package/dist/esm/Utils.js.map +1 -1
  7. package/dist/esm/internal/clock.js +1 -1
  8. package/dist/esm/internal/clock.js.map +1 -1
  9. package/package.json +1 -1
  10. package/src/Arbitrary.ts +0 -1101
  11. package/src/Array.ts +0 -3589
  12. package/src/BigDecimal.ts +0 -1349
  13. package/src/BigInt.ts +0 -643
  14. package/src/Boolean.ts +0 -287
  15. package/src/Brand.ts +0 -360
  16. package/src/Cache.ts +0 -281
  17. package/src/Cause.ts +0 -1555
  18. package/src/Channel.ts +0 -2355
  19. package/src/ChildExecutorDecision.ts +0 -146
  20. package/src/Chunk.ts +0 -1495
  21. package/src/Clock.ts +0 -111
  22. package/src/Config.ts +0 -542
  23. package/src/ConfigError.ts +0 -270
  24. package/src/ConfigProvider.ts +0 -333
  25. package/src/ConfigProviderPathPatch.ts +0 -100
  26. package/src/Console.ts +0 -226
  27. package/src/Context.ts +0 -585
  28. package/src/Cron.ts +0 -706
  29. package/src/Data.ts +0 -596
  30. package/src/DateTime.ts +0 -1686
  31. package/src/DefaultServices.ts +0 -34
  32. package/src/Deferred.ts +0 -301
  33. package/src/Differ.ts +0 -450
  34. package/src/Duration.ts +0 -1000
  35. package/src/Effect.ts +0 -14839
  36. package/src/Effectable.ts +0 -107
  37. package/src/Either.ts +0 -1040
  38. package/src/Encoding.ts +0 -195
  39. package/src/Equal.ts +0 -98
  40. package/src/Equivalence.ts +0 -235
  41. package/src/ExecutionPlan.ts +0 -308
  42. package/src/ExecutionStrategy.ts +0 -119
  43. package/src/Exit.ts +0 -467
  44. package/src/FastCheck.ts +0 -9
  45. package/src/Fiber.ts +0 -744
  46. package/src/FiberHandle.ts +0 -540
  47. package/src/FiberId.ts +0 -195
  48. package/src/FiberMap.ts +0 -656
  49. package/src/FiberRef.ts +0 -431
  50. package/src/FiberRefs.ts +0 -204
  51. package/src/FiberRefsPatch.ts +0 -105
  52. package/src/FiberSet.ts +0 -491
  53. package/src/FiberStatus.ts +0 -108
  54. package/src/Function.ts +0 -1222
  55. package/src/GlobalValue.ts +0 -53
  56. package/src/Graph.ts +0 -3732
  57. package/src/GroupBy.ts +0 -103
  58. package/src/HKT.ts +0 -45
  59. package/src/Hash.ts +0 -195
  60. package/src/HashMap.ts +0 -519
  61. package/src/HashRing.ts +0 -317
  62. package/src/HashSet.ts +0 -2346
  63. package/src/Inspectable.ts +0 -287
  64. package/src/Iterable.ts +0 -1119
  65. package/src/JSONSchema.ts +0 -1044
  66. package/src/KeyedPool.ts +0 -167
  67. package/src/Layer.ts +0 -1251
  68. package/src/LayerMap.ts +0 -436
  69. package/src/List.ts +0 -977
  70. package/src/LogLevel.ts +0 -285
  71. package/src/LogSpan.ts +0 -25
  72. package/src/Logger.ts +0 -702
  73. package/src/Mailbox.ts +0 -268
  74. package/src/ManagedRuntime.ts +0 -180
  75. package/src/Match.ts +0 -1477
  76. package/src/MergeDecision.ts +0 -95
  77. package/src/MergeState.ts +0 -172
  78. package/src/MergeStrategy.ts +0 -107
  79. package/src/Metric.ts +0 -780
  80. package/src/MetricBoundaries.ts +0 -69
  81. package/src/MetricHook.ts +0 -151
  82. package/src/MetricKey.ts +0 -224
  83. package/src/MetricKeyType.ts +0 -262
  84. package/src/MetricLabel.ts +0 -47
  85. package/src/MetricPair.ts +0 -71
  86. package/src/MetricPolling.ts +0 -148
  87. package/src/MetricRegistry.ts +0 -48
  88. package/src/MetricState.ts +0 -257
  89. package/src/Micro.ts +0 -4405
  90. package/src/ModuleVersion.ts +0 -18
  91. package/src/MutableHashMap.ts +0 -411
  92. package/src/MutableHashSet.ts +0 -706
  93. package/src/MutableList.ts +0 -297
  94. package/src/MutableQueue.ts +0 -227
  95. package/src/MutableRef.ts +0 -202
  96. package/src/NonEmptyIterable.ts +0 -32
  97. package/src/Number.ts +0 -1071
  98. package/src/Option.ts +0 -2170
  99. package/src/Order.ts +0 -373
  100. package/src/Ordering.ts +0 -111
  101. package/src/ParseResult.ts +0 -2031
  102. package/src/PartitionedSemaphore.ts +0 -200
  103. package/src/Pipeable.ts +0 -566
  104. package/src/Pool.ts +0 -204
  105. package/src/Predicate.ts +0 -1405
  106. package/src/Pretty.ts +0 -205
  107. package/src/PrimaryKey.ts +0 -23
  108. package/src/PubSub.ts +0 -182
  109. package/src/Queue.ts +0 -644
  110. package/src/Random.ts +0 -204
  111. package/src/RateLimiter.ts +0 -138
  112. package/src/RcMap.ts +0 -141
  113. package/src/RcRef.ts +0 -122
  114. package/src/Readable.ts +0 -93
  115. package/src/Record.ts +0 -1274
  116. package/src/RedBlackTree.ts +0 -421
  117. package/src/Redacted.ts +0 -144
  118. package/src/Ref.ts +0 -180
  119. package/src/RegExp.ts +0 -38
  120. package/src/Reloadable.ts +0 -127
  121. package/src/Request.ts +0 -347
  122. package/src/RequestBlock.ts +0 -118
  123. package/src/RequestResolver.ts +0 -366
  124. package/src/Resource.ts +0 -119
  125. package/src/Runtime.ts +0 -383
  126. package/src/RuntimeFlags.ts +0 -368
  127. package/src/RuntimeFlagsPatch.ts +0 -183
  128. package/src/STM.ts +0 -2045
  129. package/src/Schedule.ts +0 -2219
  130. package/src/ScheduleDecision.ts +0 -62
  131. package/src/ScheduleInterval.ts +0 -151
  132. package/src/ScheduleIntervals.ts +0 -122
  133. package/src/Scheduler.ts +0 -353
  134. package/src/Schema.ts +0 -10914
  135. package/src/SchemaAST.ts +0 -3043
  136. package/src/Scope.ts +0 -204
  137. package/src/ScopedCache.ts +0 -151
  138. package/src/ScopedRef.ts +0 -117
  139. package/src/Secret.ts +0 -88
  140. package/src/SingleProducerAsyncInput.ts +0 -67
  141. package/src/Sink.ts +0 -1461
  142. package/src/SortedMap.ts +0 -287
  143. package/src/SortedSet.ts +0 -390
  144. package/src/Stream.ts +0 -6468
  145. package/src/StreamEmit.ts +0 -136
  146. package/src/StreamHaltStrategy.ts +0 -123
  147. package/src/Streamable.ts +0 -45
  148. package/src/String.ts +0 -778
  149. package/src/Struct.ts +0 -243
  150. package/src/Subscribable.ts +0 -100
  151. package/src/SubscriptionRef.ts +0 -298
  152. package/src/Supervisor.ts +0 -240
  153. package/src/Symbol.ts +0 -29
  154. package/src/SynchronizedRef.ts +0 -270
  155. package/src/TArray.ts +0 -495
  156. package/src/TDeferred.ts +0 -100
  157. package/src/TMap.ts +0 -515
  158. package/src/TPriorityQueue.ts +0 -223
  159. package/src/TPubSub.ts +0 -200
  160. package/src/TQueue.ts +0 -432
  161. package/src/TRandom.ts +0 -129
  162. package/src/TReentrantLock.ts +0 -224
  163. package/src/TRef.ts +0 -178
  164. package/src/TSemaphore.ts +0 -129
  165. package/src/TSet.ts +0 -365
  166. package/src/TSubscriptionRef.ts +0 -192
  167. package/src/Take.ts +0 -258
  168. package/src/TestAnnotation.ts +0 -158
  169. package/src/TestAnnotationMap.ts +0 -119
  170. package/src/TestAnnotations.ts +0 -117
  171. package/src/TestClock.ts +0 -556
  172. package/src/TestConfig.ts +0 -47
  173. package/src/TestContext.ts +0 -36
  174. package/src/TestLive.ts +0 -53
  175. package/src/TestServices.ts +0 -390
  176. package/src/TestSized.ts +0 -55
  177. package/src/Tracer.ts +0 -198
  178. package/src/Trie.ts +0 -840
  179. package/src/Tuple.ts +0 -305
  180. package/src/Types.ts +0 -353
  181. package/src/Unify.ts +0 -113
  182. package/src/UpstreamPullRequest.ts +0 -117
  183. package/src/UpstreamPullStrategy.ts +0 -121
  184. package/src/Utils.ts +0 -809
  185. package/src/index.ts +0 -1561
  186. package/src/internal/array.ts +0 -8
  187. package/src/internal/blockedRequests.ts +0 -520
  188. package/src/internal/cache.ts +0 -733
  189. package/src/internal/cause.ts +0 -1050
  190. package/src/internal/channel/channelExecutor.ts +0 -1200
  191. package/src/internal/channel/channelState.ts +0 -134
  192. package/src/internal/channel/childExecutorDecision.ts +0 -96
  193. package/src/internal/channel/continuation.ts +0 -200
  194. package/src/internal/channel/mergeDecision.ts +0 -113
  195. package/src/internal/channel/mergeState.ts +0 -120
  196. package/src/internal/channel/mergeStrategy.ts +0 -72
  197. package/src/internal/channel/singleProducerAsyncInput.ts +0 -259
  198. package/src/internal/channel/subexecutor.ts +0 -229
  199. package/src/internal/channel/upstreamPullRequest.ts +0 -84
  200. package/src/internal/channel/upstreamPullStrategy.ts +0 -87
  201. package/src/internal/channel.ts +0 -2603
  202. package/src/internal/clock.ts +0 -95
  203. package/src/internal/completedRequestMap.ts +0 -9
  204. package/src/internal/concurrency.ts +0 -54
  205. package/src/internal/config.ts +0 -716
  206. package/src/internal/configError.ts +0 -304
  207. package/src/internal/configProvider/pathPatch.ts +0 -97
  208. package/src/internal/configProvider.ts +0 -799
  209. package/src/internal/console.ts +0 -153
  210. package/src/internal/context.ts +0 -337
  211. package/src/internal/core-effect.ts +0 -2293
  212. package/src/internal/core-stream.ts +0 -998
  213. package/src/internal/core.ts +0 -3273
  214. package/src/internal/data.ts +0 -36
  215. package/src/internal/dataSource.ts +0 -327
  216. package/src/internal/dateTime.ts +0 -1277
  217. package/src/internal/defaultServices/console.ts +0 -100
  218. package/src/internal/defaultServices.ts +0 -163
  219. package/src/internal/deferred.ts +0 -46
  220. package/src/internal/differ/chunkPatch.ts +0 -211
  221. package/src/internal/differ/contextPatch.ts +0 -232
  222. package/src/internal/differ/hashMapPatch.ts +0 -220
  223. package/src/internal/differ/hashSetPatch.ts +0 -176
  224. package/src/internal/differ/orPatch.ts +0 -311
  225. package/src/internal/differ/readonlyArrayPatch.ts +0 -210
  226. package/src/internal/differ.ts +0 -200
  227. package/src/internal/doNotation.ts +0 -80
  228. package/src/internal/effect/circular.ts +0 -905
  229. package/src/internal/effectable.ts +0 -131
  230. package/src/internal/either.ts +0 -110
  231. package/src/internal/encoding/base64.ts +0 -286
  232. package/src/internal/encoding/base64Url.ts +0 -29
  233. package/src/internal/encoding/common.ts +0 -51
  234. package/src/internal/encoding/hex.ts +0 -315
  235. package/src/internal/errors.ts +0 -7
  236. package/src/internal/executionPlan.ts +0 -114
  237. package/src/internal/executionStrategy.ts +0 -74
  238. package/src/internal/fiber.ts +0 -388
  239. package/src/internal/fiberId.ts +0 -267
  240. package/src/internal/fiberMessage.ts +0 -82
  241. package/src/internal/fiberRefs/patch.ts +0 -144
  242. package/src/internal/fiberRefs.ts +0 -297
  243. package/src/internal/fiberRuntime.ts +0 -3915
  244. package/src/internal/fiberScope.ts +0 -71
  245. package/src/internal/fiberStatus.ts +0 -119
  246. package/src/internal/groupBy.ts +0 -530
  247. package/src/internal/hashMap/array.ts +0 -49
  248. package/src/internal/hashMap/bitwise.ts +0 -32
  249. package/src/internal/hashMap/config.ts +0 -14
  250. package/src/internal/hashMap/keySet.ts +0 -8
  251. package/src/internal/hashMap/node.ts +0 -391
  252. package/src/internal/hashMap.ts +0 -586
  253. package/src/internal/hashSet.ts +0 -323
  254. package/src/internal/keyedPool.ts +0 -244
  255. package/src/internal/layer/circular.ts +0 -228
  256. package/src/internal/layer.ts +0 -1487
  257. package/src/internal/logSpan.ts +0 -20
  258. package/src/internal/logger-circular.ts +0 -24
  259. package/src/internal/logger.ts +0 -485
  260. package/src/internal/mailbox.ts +0 -561
  261. package/src/internal/managedRuntime/circular.ts +0 -6
  262. package/src/internal/managedRuntime.ts +0 -134
  263. package/src/internal/matcher.ts +0 -652
  264. package/src/internal/metric/boundaries.ts +0 -75
  265. package/src/internal/metric/hook.ts +0 -483
  266. package/src/internal/metric/key.ts +0 -167
  267. package/src/internal/metric/keyType.ts +0 -238
  268. package/src/internal/metric/label.ts +0 -41
  269. package/src/internal/metric/pair.ts +0 -48
  270. package/src/internal/metric/polling.ts +0 -149
  271. package/src/internal/metric/registry.ts +0 -187
  272. package/src/internal/metric/state.ts +0 -290
  273. package/src/internal/metric.ts +0 -577
  274. package/src/internal/opCodes/cause.ts +0 -35
  275. package/src/internal/opCodes/channel.ts +0 -83
  276. package/src/internal/opCodes/channelChildExecutorDecision.ts +0 -17
  277. package/src/internal/opCodes/channelMergeDecision.ts +0 -11
  278. package/src/internal/opCodes/channelMergeState.ts +0 -17
  279. package/src/internal/opCodes/channelMergeStrategy.ts +0 -11
  280. package/src/internal/opCodes/channelState.ts +0 -23
  281. package/src/internal/opCodes/channelUpstreamPullRequest.ts +0 -11
  282. package/src/internal/opCodes/channelUpstreamPullStrategy.ts +0 -11
  283. package/src/internal/opCodes/config.ts +0 -65
  284. package/src/internal/opCodes/configError.ts +0 -35
  285. package/src/internal/opCodes/continuation.ts +0 -11
  286. package/src/internal/opCodes/deferred.ts +0 -11
  287. package/src/internal/opCodes/effect.ts +0 -89
  288. package/src/internal/opCodes/layer.ts +0 -59
  289. package/src/internal/opCodes/streamHaltStrategy.ts +0 -23
  290. package/src/internal/option.ts +0 -80
  291. package/src/internal/pool.ts +0 -432
  292. package/src/internal/pubsub.ts +0 -1762
  293. package/src/internal/query.ts +0 -204
  294. package/src/internal/queue.ts +0 -766
  295. package/src/internal/random.ts +0 -161
  296. package/src/internal/rateLimiter.ts +0 -93
  297. package/src/internal/rcMap.ts +0 -285
  298. package/src/internal/rcRef.ts +0 -192
  299. package/src/internal/redBlackTree/iterator.ts +0 -200
  300. package/src/internal/redBlackTree/node.ts +0 -68
  301. package/src/internal/redBlackTree.ts +0 -1245
  302. package/src/internal/redacted.ts +0 -73
  303. package/src/internal/ref.ts +0 -171
  304. package/src/internal/reloadable.ts +0 -140
  305. package/src/internal/request.ts +0 -177
  306. package/src/internal/resource.ts +0 -76
  307. package/src/internal/ringBuffer.ts +0 -68
  308. package/src/internal/runtime.ts +0 -558
  309. package/src/internal/runtimeFlags.ts +0 -188
  310. package/src/internal/runtimeFlagsPatch.ts +0 -103
  311. package/src/internal/schedule/decision.ts +0 -47
  312. package/src/internal/schedule/interval.ts +0 -101
  313. package/src/internal/schedule/intervals.ts +0 -180
  314. package/src/internal/schedule.ts +0 -2199
  315. package/src/internal/schema/errors.ts +0 -191
  316. package/src/internal/schema/schemaId.ts +0 -106
  317. package/src/internal/schema/util.ts +0 -50
  318. package/src/internal/scopedCache.ts +0 -644
  319. package/src/internal/scopedRef.ts +0 -118
  320. package/src/internal/secret.ts +0 -89
  321. package/src/internal/singleShotGen.ts +0 -35
  322. package/src/internal/sink.ts +0 -2120
  323. package/src/internal/stack.ts +0 -10
  324. package/src/internal/stm/core.ts +0 -817
  325. package/src/internal/stm/entry.ts +0 -59
  326. package/src/internal/stm/journal.ts +0 -123
  327. package/src/internal/stm/opCodes/stm.ts +0 -71
  328. package/src/internal/stm/opCodes/stmState.ts +0 -17
  329. package/src/internal/stm/opCodes/strategy.ts +0 -17
  330. package/src/internal/stm/opCodes/tExit.ts +0 -29
  331. package/src/internal/stm/opCodes/tryCommit.ts +0 -11
  332. package/src/internal/stm/stm.ts +0 -1453
  333. package/src/internal/stm/stmState.ts +0 -136
  334. package/src/internal/stm/tArray.ts +0 -550
  335. package/src/internal/stm/tDeferred.ts +0 -81
  336. package/src/internal/stm/tExit.ts +0 -190
  337. package/src/internal/stm/tMap.ts +0 -824
  338. package/src/internal/stm/tPriorityQueue.ts +0 -267
  339. package/src/internal/stm/tPubSub.ts +0 -551
  340. package/src/internal/stm/tQueue.ts +0 -393
  341. package/src/internal/stm/tRandom.ts +0 -140
  342. package/src/internal/stm/tReentrantLock.ts +0 -352
  343. package/src/internal/stm/tRef.ts +0 -195
  344. package/src/internal/stm/tSemaphore.ts +0 -113
  345. package/src/internal/stm/tSet.ts +0 -259
  346. package/src/internal/stm/tSubscriptionRef.ts +0 -286
  347. package/src/internal/stm/tryCommit.ts +0 -34
  348. package/src/internal/stm/txnId.ts +0 -14
  349. package/src/internal/stm/versioned.ts +0 -4
  350. package/src/internal/stream/debounceState.ts +0 -57
  351. package/src/internal/stream/emit.ts +0 -123
  352. package/src/internal/stream/haltStrategy.ts +0 -94
  353. package/src/internal/stream/handoff.ts +0 -187
  354. package/src/internal/stream/handoffSignal.ts +0 -59
  355. package/src/internal/stream/pull.ts +0 -34
  356. package/src/internal/stream/sinkEndReason.ts +0 -30
  357. package/src/internal/stream/zipAllState.ts +0 -88
  358. package/src/internal/stream/zipChunksState.ts +0 -56
  359. package/src/internal/stream.ts +0 -8801
  360. package/src/internal/string-utils.ts +0 -107
  361. package/src/internal/subscriptionRef.ts +0 -138
  362. package/src/internal/supervisor/patch.ts +0 -190
  363. package/src/internal/supervisor.ts +0 -303
  364. package/src/internal/synchronizedRef.ts +0 -114
  365. package/src/internal/take.ts +0 -199
  366. package/src/internal/testing/sleep.ts +0 -27
  367. package/src/internal/testing/suspendedWarningData.ts +0 -85
  368. package/src/internal/testing/warningData.ts +0 -94
  369. package/src/internal/tracer.ts +0 -293
  370. package/src/internal/trie.ts +0 -722
  371. package/src/internal/version.ts +0 -7
package/src/JSONSchema.ts DELETED
@@ -1,1044 +0,0 @@
1
- /**
2
- * @since 3.10.0
3
- */
4
-
5
- import * as Arr from "./Array.js"
6
- import * as errors_ from "./internal/schema/errors.js"
7
- import * as schemaId_ from "./internal/schema/schemaId.js"
8
- import * as Option from "./Option.js"
9
- import * as ParseResult from "./ParseResult.js"
10
- import * as Predicate from "./Predicate.js"
11
- import * as Record from "./Record.js"
12
- import type * as Schema from "./Schema.js"
13
- import * as AST from "./SchemaAST.js"
14
-
15
- type JsonValue = string | number | boolean | null | Array<JsonValue> | { [key: string]: JsonValue }
16
-
17
- /**
18
- * @category model
19
- * @since 3.10.0
20
- */
21
- export interface JsonSchemaAnnotations {
22
- title?: string
23
- description?: string
24
- default?: JsonValue
25
- examples?: Array<JsonValue>
26
- }
27
-
28
- /**
29
- * @category model
30
- * @since 3.11.5
31
- */
32
- export interface JsonSchema7Never extends JsonSchemaAnnotations {
33
- $id: "/schemas/never"
34
- not: {}
35
- }
36
-
37
- /**
38
- * @category model
39
- * @since 3.10.0
40
- */
41
- export interface JsonSchema7Any extends JsonSchemaAnnotations {
42
- $id: "/schemas/any"
43
- }
44
-
45
- /**
46
- * @category model
47
- * @since 3.10.0
48
- */
49
- export interface JsonSchema7Unknown extends JsonSchemaAnnotations {
50
- $id: "/schemas/unknown"
51
- }
52
-
53
- /**
54
- * @category model
55
- * @since 3.10.0
56
- */
57
- export interface JsonSchema7Void extends JsonSchemaAnnotations {
58
- $id: "/schemas/void"
59
- }
60
-
61
- /**
62
- * @category model
63
- * @since 3.10.0
64
- */
65
- export interface JsonSchema7object extends JsonSchemaAnnotations {
66
- $id: "/schemas/object"
67
- anyOf: [
68
- { type: "object" },
69
- { type: "array" }
70
- ]
71
- }
72
-
73
- /**
74
- * @category model
75
- * @since 3.10.0
76
- */
77
- export interface JsonSchema7empty extends JsonSchemaAnnotations {
78
- $id: "/schemas/%7B%7D"
79
- anyOf: [
80
- { type: "object" },
81
- { type: "array" }
82
- ]
83
- }
84
-
85
- /**
86
- * @category model
87
- * @since 3.10.0
88
- */
89
- export interface JsonSchema7Ref extends JsonSchemaAnnotations {
90
- $ref: string
91
- }
92
-
93
- /**
94
- * @category model
95
- * @since 3.11.7
96
- */
97
- export interface JsonSchema7Null extends JsonSchemaAnnotations {
98
- type: "null"
99
- }
100
-
101
- /**
102
- * @category model
103
- * @since 3.10.0
104
- */
105
- export interface JsonSchema7String extends JsonSchemaAnnotations {
106
- type: "string"
107
- minLength?: number
108
- maxLength?: number
109
- pattern?: string
110
- format?: string
111
- contentMediaType?: string
112
- allOf?: Array<{
113
- minLength?: number
114
- maxLength?: number
115
- pattern?: string
116
- }>
117
- }
118
-
119
- /**
120
- * @category model
121
- * @since 3.10.0
122
- */
123
- export interface JsonSchema7Numeric extends JsonSchemaAnnotations {
124
- minimum?: number
125
- exclusiveMinimum?: number
126
- maximum?: number
127
- exclusiveMaximum?: number
128
- multipleOf?: number
129
- allOf?: Array<{
130
- minimum?: number
131
- exclusiveMinimum?: number
132
- maximum?: number
133
- exclusiveMaximum?: number
134
- multipleOf?: number
135
- }>
136
- }
137
-
138
- /**
139
- * @category model
140
- * @since 3.10.0
141
- */
142
- export interface JsonSchema7Number extends JsonSchema7Numeric {
143
- type: "number"
144
- }
145
-
146
- /**
147
- * @category model
148
- * @since 3.10.0
149
- */
150
- export interface JsonSchema7Integer extends JsonSchema7Numeric {
151
- type: "integer"
152
- }
153
-
154
- /**
155
- * @category model
156
- * @since 3.10.0
157
- */
158
- export interface JsonSchema7Boolean extends JsonSchemaAnnotations {
159
- type: "boolean"
160
- }
161
-
162
- /**
163
- * @category model
164
- * @since 3.10.0
165
- */
166
- export interface JsonSchema7Array extends JsonSchemaAnnotations {
167
- type: "array"
168
- items?: JsonSchema7 | Array<JsonSchema7> | false
169
- prefixItems?: Array<JsonSchema7>
170
- minItems?: number
171
- maxItems?: number
172
- additionalItems?: JsonSchema7 | boolean
173
- }
174
-
175
- /**
176
- * @category model
177
- * @since 3.10.0
178
- */
179
- export interface JsonSchema7Enum extends JsonSchemaAnnotations {
180
- type?: "string" | "number" | "boolean"
181
- enum: Array<string | number | boolean>
182
- }
183
-
184
- /**
185
- * @category model
186
- * @since 3.10.0
187
- */
188
- export interface JsonSchema7Enums extends JsonSchemaAnnotations {
189
- $comment: "/schemas/enums"
190
- anyOf: Array<{
191
- type: "string" | "number"
192
- title: string
193
- enum: [string | number]
194
- }>
195
- }
196
-
197
- /**
198
- * @category model
199
- * @since 3.10.0
200
- */
201
- export interface JsonSchema7AnyOf extends JsonSchemaAnnotations {
202
- anyOf: Array<JsonSchema7>
203
- }
204
-
205
- /**
206
- * @category model
207
- * @since 3.10.0
208
- */
209
- export interface JsonSchema7Object extends JsonSchemaAnnotations {
210
- type: "object"
211
- required: Array<string>
212
- properties: Record<string, JsonSchema7>
213
- additionalProperties?: boolean | JsonSchema7
214
- patternProperties?: Record<string, JsonSchema7>
215
- propertyNames?: JsonSchema7
216
- }
217
-
218
- /**
219
- * @category model
220
- * @since 3.10.0
221
- */
222
- export type JsonSchema7 =
223
- | JsonSchema7Never
224
- | JsonSchema7Any
225
- | JsonSchema7Unknown
226
- | JsonSchema7Void
227
- | JsonSchema7object
228
- | JsonSchema7empty
229
- | JsonSchema7Ref
230
- | JsonSchema7Null
231
- | JsonSchema7String
232
- | JsonSchema7Number
233
- | JsonSchema7Integer
234
- | JsonSchema7Boolean
235
- | JsonSchema7Array
236
- | JsonSchema7Enum
237
- | JsonSchema7Enums
238
- | JsonSchema7AnyOf
239
- | JsonSchema7Object
240
-
241
- /**
242
- * @category model
243
- * @since 3.10.0
244
- */
245
- export type JsonSchema7Root = JsonSchema7 & {
246
- $schema?: string
247
- $defs?: Record<string, JsonSchema7>
248
- }
249
-
250
- /**
251
- * Generates a JSON Schema from a schema.
252
- *
253
- * **Options**
254
- *
255
- * - `target`: The target JSON Schema version. Possible values are:
256
- * - `"jsonSchema7"`: JSON Schema draft-07 (default behavior).
257
- * - `"jsonSchema2019-09"`: JSON Schema draft-2019-09.
258
- * - `"jsonSchema2020-12"`: JSON Schema draft-2020-12.
259
- * - `"openApi3.1"`: OpenAPI 3.1.
260
- *
261
- * @category encoding
262
- * @since 3.10.0
263
- */
264
- export const make = <A, I, R>(schema: Schema.Schema<A, I, R>, options?: {
265
- readonly target?: Target | undefined
266
- }): JsonSchema7Root => {
267
- const definitions: Record<string, any> = {}
268
- const target = options?.target ?? "jsonSchema7"
269
- const ast = AST.isTransformation(schema.ast) && isParseJsonTransformation(schema.ast.from)
270
- // Special case top level `parseJson` transformations
271
- ? schema.ast.to
272
- : schema.ast
273
- const jsonSchema = fromAST(ast, {
274
- definitions,
275
- target
276
- })
277
- const out: JsonSchema7Root = {
278
- $schema: getMetaSchemaUri(target),
279
- $defs: {},
280
- ...jsonSchema
281
- }
282
- if (Record.isEmptyRecord(definitions)) {
283
- delete out.$defs
284
- } else {
285
- out.$defs = definitions
286
- }
287
- return out
288
- }
289
-
290
- type Target = "jsonSchema7" | "jsonSchema2019-09" | "openApi3.1" | "jsonSchema2020-12"
291
-
292
- type TopLevelReferenceStrategy = "skip" | "keep"
293
-
294
- type AdditionalPropertiesStrategy = "allow" | "strict"
295
-
296
- /** @internal */
297
- export function getMetaSchemaUri(target: Target) {
298
- switch (target) {
299
- case "jsonSchema7":
300
- return "http://json-schema.org/draft-07/schema#"
301
- case "jsonSchema2019-09":
302
- return "https://json-schema.org/draft/2019-09/schema"
303
- case "jsonSchema2020-12":
304
- case "openApi3.1":
305
- return "https://json-schema.org/draft/2020-12/schema"
306
- }
307
- }
308
-
309
- /**
310
- * Returns a JSON Schema with additional options and definitions.
311
- *
312
- * **Warning**
313
- *
314
- * This function is experimental and subject to change.
315
- *
316
- * **Options**
317
- *
318
- * - `definitions`: A record of definitions that are included in the schema.
319
- * - `definitionPath`: The path to the definitions within the schema (defaults
320
- * to "#/$defs/").
321
- * - `target`: Which spec to target. Possible values are:
322
- * - `'jsonSchema7'`: JSON Schema draft-07 (default behavior).
323
- * - `'jsonSchema2019-09'`: JSON Schema draft-2019-09.
324
- * - `'openApi3.1'`: OpenAPI 3.1.
325
- * - `topLevelReferenceStrategy`: Controls the handling of the top-level
326
- * reference. Possible values are:
327
- * - `"keep"`: Keep the top-level reference (default behavior).
328
- * - `"skip"`: Skip the top-level reference.
329
- * - `additionalPropertiesStrategy`: Controls the handling of additional properties. Possible values are:
330
- * - `"strict"`: Disallow additional properties (default behavior).
331
- * - `"allow"`: Allow additional properties.
332
- *
333
- * @category encoding
334
- * @since 3.11.5
335
- * @experimental
336
- */
337
- export const fromAST = (ast: AST.AST, options: {
338
- readonly definitions: Record<string, JsonSchema7>
339
- readonly definitionPath?: string | undefined
340
- readonly target?: Target | undefined
341
- readonly topLevelReferenceStrategy?: TopLevelReferenceStrategy | undefined
342
- readonly additionalPropertiesStrategy?: AdditionalPropertiesStrategy | undefined
343
- }): JsonSchema7 => {
344
- const definitionPath = options.definitionPath ?? "#/$defs/"
345
- const getRef = (id: string) => definitionPath + id
346
- const target = options.target ?? "jsonSchema7"
347
- const topLevelReferenceStrategy = options.topLevelReferenceStrategy ?? "keep"
348
- const additionalPropertiesStrategy = options.additionalPropertiesStrategy ?? "strict"
349
- return go(
350
- ast,
351
- options.definitions,
352
- "handle-identifier",
353
- [],
354
- {
355
- getRef,
356
- target,
357
- topLevelReferenceStrategy,
358
- additionalPropertiesStrategy
359
- },
360
- "handle-annotation",
361
- "handle-errors"
362
- )
363
- }
364
-
365
- const constNever: JsonSchema7Never = {
366
- $id: "/schemas/never",
367
- not: {}
368
- }
369
-
370
- const constAny: JsonSchema7Any = {
371
- $id: "/schemas/any"
372
- }
373
-
374
- const constUnknown: JsonSchema7Unknown = {
375
- $id: "/schemas/unknown"
376
- }
377
-
378
- const constVoid: JsonSchema7Void = {
379
- $id: "/schemas/void"
380
- }
381
-
382
- const constObject: JsonSchema7object = {
383
- $id: "/schemas/object",
384
- "anyOf": [
385
- { "type": "object" },
386
- { "type": "array" }
387
- ]
388
- }
389
-
390
- const constEmptyStruct: JsonSchema7empty = {
391
- $id: "/schemas/%7B%7D",
392
- "anyOf": [
393
- { "type": "object" },
394
- { "type": "array" }
395
- ]
396
- }
397
-
398
- function getRawDescription(annotated: AST.Annotated | undefined): string | undefined {
399
- if (annotated !== undefined) return Option.getOrUndefined(AST.getDescriptionAnnotation(annotated))
400
- }
401
-
402
- function getRawTitle(annotated: AST.Annotated | undefined): string | undefined {
403
- if (annotated !== undefined) return Option.getOrUndefined(AST.getTitleAnnotation(annotated))
404
- }
405
-
406
- function getRawDefault(annotated: AST.Annotated | undefined): Option.Option<unknown> {
407
- if (annotated !== undefined) return AST.getDefaultAnnotation(annotated)
408
- return Option.none()
409
- }
410
-
411
- function encodeDefault(ast: AST.AST, def: unknown): Option.Option<unknown> {
412
- const getOption = ParseResult.getOption(ast, false)
413
- return getOption(def)
414
- }
415
-
416
- function getRawExamples(annotated: AST.Annotated | undefined): ReadonlyArray<unknown> | undefined {
417
- if (annotated !== undefined) return Option.getOrUndefined(AST.getExamplesAnnotation(annotated))
418
- }
419
-
420
- function encodeExamples(ast: AST.AST, examples: ReadonlyArray<unknown>): Array<JsonValue> | undefined {
421
- const getOption = ParseResult.getOption(ast, false)
422
- const out = Arr.filterMap(examples, (e) => getOption(e).pipe(Option.filter(isJsonValue)))
423
- return out.length > 0 ? out : undefined
424
- }
425
-
426
- function filterBuiltIn(ast: AST.AST, annotation: string | undefined, key: symbol): string | undefined {
427
- if (annotation !== undefined) {
428
- switch (ast._tag) {
429
- case "StringKeyword":
430
- return annotation !== AST.stringKeyword.annotations[key] ? annotation : undefined
431
- case "NumberKeyword":
432
- return annotation !== AST.numberKeyword.annotations[key] ? annotation : undefined
433
- case "BooleanKeyword":
434
- return annotation !== AST.booleanKeyword.annotations[key] ? annotation : undefined
435
- }
436
- }
437
- return annotation
438
- }
439
-
440
- function isJsonValue(value: unknown, visited: Set<unknown> = new Set()): value is JsonValue {
441
- if (value === null || typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
442
- return true
443
- }
444
- if (Array.isArray(value) || typeof value === "object") {
445
- // Check for cyclic references
446
- if (visited.has(value)) {
447
- return false
448
- }
449
- visited.add(value)
450
- try {
451
- if (Array.isArray(value)) {
452
- return value.every((item) => isJsonValue(item, visited))
453
- }
454
- // Exclude non-plain objects (Date, RegExp, etc.) by checking constructor
455
- const proto = Object.getPrototypeOf(value)
456
- if (proto !== null && proto !== Object.prototype) {
457
- return false
458
- }
459
- // JSON only allows string keys, so exclude objects with Symbol keys
460
- if (Object.getOwnPropertySymbols(value).length > 0) {
461
- return false
462
- }
463
- // Check all values are JSON values
464
- return Object.values(value).every((v) => isJsonValue(v, visited))
465
- } finally {
466
- visited.delete(value)
467
- }
468
- }
469
- return false
470
- }
471
-
472
- function pruneJsonSchemaAnnotations(
473
- ast: AST.AST,
474
- description: string | undefined,
475
- title: string | undefined,
476
- def: Option.Option<unknown>,
477
- examples: ReadonlyArray<unknown> | undefined
478
- ): JsonSchemaAnnotations | undefined {
479
- const out: JsonSchemaAnnotations = {}
480
- if (description !== undefined) out.description = description
481
- if (title !== undefined) out.title = title
482
- if (Option.isSome(def)) {
483
- const o = encodeDefault(ast, def.value)
484
- if (Option.isSome(o) && isJsonValue(o.value)) {
485
- out.default = o.value
486
- }
487
- }
488
- if (examples !== undefined) {
489
- const encodedExamples = encodeExamples(ast, examples)
490
- if (encodedExamples !== undefined) {
491
- out.examples = encodedExamples
492
- }
493
- }
494
- if (Object.keys(out).length === 0) {
495
- return undefined
496
- }
497
- return out
498
- }
499
-
500
- function getContextJsonSchemaAnnotations(ast: AST.AST, annotated: AST.Annotated): JsonSchemaAnnotations | undefined {
501
- return pruneJsonSchemaAnnotations(
502
- ast,
503
- getRawDescription(annotated),
504
- getRawTitle(annotated),
505
- getRawDefault(annotated),
506
- getRawExamples(annotated)
507
- )
508
- }
509
-
510
- function getJsonSchemaAnnotations(ast: AST.AST): JsonSchemaAnnotations | undefined {
511
- return pruneJsonSchemaAnnotations(
512
- ast,
513
- filterBuiltIn(ast, getRawDescription(ast), AST.DescriptionAnnotationId),
514
- filterBuiltIn(ast, getRawTitle(ast), AST.TitleAnnotationId),
515
- getRawDefault(ast),
516
- getRawExamples(ast)
517
- )
518
- }
519
-
520
- function mergeJsonSchemaAnnotations(
521
- jsonSchema: JsonSchema7,
522
- jsonSchemaAnnotations: JsonSchemaAnnotations | undefined
523
- ): JsonSchema7 {
524
- if (jsonSchemaAnnotations) {
525
- if ("$ref" in jsonSchema) {
526
- return { allOf: [jsonSchema], ...jsonSchemaAnnotations } as any
527
- }
528
- return { ...jsonSchema, ...jsonSchemaAnnotations }
529
- }
530
- return jsonSchema
531
- }
532
-
533
- const pruneUndefined = (ast: AST.AST): AST.AST | undefined => {
534
- if (Option.isNone(AST.getJSONSchemaAnnotation(ast))) {
535
- return AST.pruneUndefined(ast, pruneUndefined, (ast) => pruneUndefined(ast.from))
536
- }
537
- }
538
-
539
- const isParseJsonTransformation = (ast: AST.AST): boolean =>
540
- ast.annotations[AST.SchemaIdAnnotationId] === AST.ParseJsonSchemaId
541
-
542
- const isOverrideAnnotation = (ast: AST.AST, jsonSchema: JsonSchema7): boolean => {
543
- if (AST.isRefinement(ast)) {
544
- const schemaId = ast.annotations[AST.SchemaIdAnnotationId]
545
- if (schemaId === schemaId_.IntSchemaId) {
546
- return "type" in jsonSchema && jsonSchema.type !== "integer"
547
- }
548
- }
549
- return ("type" in jsonSchema) || ("oneOf" in jsonSchema) || ("anyOf" in jsonSchema) || ("$ref" in jsonSchema)
550
- }
551
-
552
- const mergeRefinements = (from: any, jsonSchema: any, ast: AST.AST): any => {
553
- const out: any = { ...from, ...getJsonSchemaAnnotations(ast), ...jsonSchema }
554
- out.allOf ??= []
555
-
556
- const handle = (name: string, filter: (i: any) => boolean) => {
557
- if (name in jsonSchema && name in from) {
558
- out.allOf.unshift({ [name]: from[name] })
559
- out.allOf = out.allOf.filter(filter)
560
- }
561
- }
562
-
563
- handle("minLength", (i) => i.minLength > jsonSchema.minLength)
564
- handle("maxLength", (i) => i.maxLength < jsonSchema.maxLength)
565
- handle("pattern", (i) => i.pattern !== jsonSchema.pattern)
566
- handle("minItems", (i) => i.minItems > jsonSchema.minItems)
567
- handle("maxItems", (i) => i.maxItems < jsonSchema.maxItems)
568
- handle("minimum", (i) => i.minimum > jsonSchema.minimum)
569
- handle("maximum", (i) => i.maximum < jsonSchema.maximum)
570
- handle("exclusiveMinimum", (i) => i.exclusiveMinimum > jsonSchema.exclusiveMinimum)
571
- handle("exclusiveMaximum", (i) => i.exclusiveMaximum < jsonSchema.exclusiveMaximum)
572
- handle("multipleOf", (i) => i.multipleOf !== jsonSchema.multipleOf)
573
-
574
- if (out.allOf.length === 0) {
575
- delete out.allOf
576
- }
577
- return out
578
- }
579
-
580
- type GoOptions = {
581
- readonly getRef: (id: string) => string
582
- readonly target: Target
583
- readonly topLevelReferenceStrategy: TopLevelReferenceStrategy
584
- readonly additionalPropertiesStrategy: AdditionalPropertiesStrategy
585
- }
586
-
587
- function isContentSchemaSupported(options: GoOptions): boolean {
588
- switch (options.target) {
589
- case "jsonSchema7":
590
- return false
591
- case "jsonSchema2019-09":
592
- case "jsonSchema2020-12":
593
- case "openApi3.1":
594
- return true
595
- }
596
- }
597
-
598
- function getAdditionalProperties(options: GoOptions): boolean {
599
- switch (options.additionalPropertiesStrategy) {
600
- case "allow":
601
- return true
602
- case "strict":
603
- return false
604
- }
605
- }
606
-
607
- function addASTAnnotations(jsonSchema: JsonSchema7, ast: AST.AST): JsonSchema7 {
608
- return addAnnotations(jsonSchema, getJsonSchemaAnnotations(ast))
609
- }
610
-
611
- function addAnnotations(jsonSchema: JsonSchema7, annotations: JsonSchemaAnnotations | undefined): JsonSchema7 {
612
- if (annotations === undefined || Object.keys(annotations).length === 0) {
613
- return jsonSchema
614
- }
615
- if ("$ref" in jsonSchema) {
616
- return { allOf: [jsonSchema], ...annotations } as any
617
- }
618
- return { ...jsonSchema, ...annotations }
619
- }
620
-
621
- function getIdentifierAnnotation(ast: AST.AST): string | undefined {
622
- const identifier = Option.getOrUndefined(AST.getJSONIdentifier(ast))
623
- if (identifier === undefined) {
624
- if (AST.isSuspend(ast)) {
625
- return getIdentifierAnnotation(ast.f())
626
- }
627
- if (AST.isTransformation(ast) && AST.isTypeLiteral(ast.from) && AST.isDeclaration(ast.to)) {
628
- const to = ast.to
629
- const surrogate = AST.getSurrogateAnnotation(to)
630
- if (Option.isSome(surrogate)) {
631
- return getIdentifierAnnotation(to)
632
- }
633
- }
634
- }
635
- return identifier
636
- }
637
-
638
- function go(
639
- ast: AST.AST,
640
- $defs: Record<string, JsonSchema7>,
641
- identifier: "handle-identifier" | "ignore-identifier",
642
- path: ReadonlyArray<PropertyKey>,
643
- options: GoOptions,
644
- annotation: "handle-annotation" | "ignore-annotation",
645
- errors: "handle-errors" | "ignore-errors"
646
- ): JsonSchema7 {
647
- if (
648
- identifier === "handle-identifier" &&
649
- (options.topLevelReferenceStrategy !== "skip" || AST.isSuspend(ast))
650
- ) {
651
- const id = getIdentifierAnnotation(ast)
652
- if (id !== undefined) {
653
- const escapedId = id.replace(/~/ig, "~0").replace(/\//ig, "~1")
654
- const out = { $ref: options.getRef(escapedId) }
655
- if (!Record.has($defs, id)) {
656
- $defs[id] = out
657
- $defs[id] = go(ast, $defs, "ignore-identifier", path, options, "handle-annotation", errors)
658
- }
659
- return out
660
- }
661
- }
662
- if (annotation === "handle-annotation") {
663
- const hook = AST.getJSONSchemaAnnotation(ast)
664
- if (Option.isSome(hook)) {
665
- const handler = hook.value as JsonSchema7
666
- if (isOverrideAnnotation(ast, handler)) {
667
- switch (ast._tag) {
668
- case "Declaration":
669
- return addASTAnnotations(handler, ast)
670
- default:
671
- return handler
672
- }
673
- } else {
674
- switch (ast._tag) {
675
- case "Refinement": {
676
- const t = AST.getTransformationFrom(ast)
677
- if (t === undefined) {
678
- return mergeRefinements(
679
- go(ast.from, $defs, identifier, path, options, "handle-annotation", errors),
680
- handler,
681
- ast
682
- )
683
- } else {
684
- return go(t, $defs, identifier, path, options, "handle-annotation", errors)
685
- }
686
- }
687
- default:
688
- return {
689
- ...go(ast, $defs, identifier, path, options, "ignore-annotation", errors),
690
- ...handler
691
- } as any
692
- }
693
- }
694
- }
695
- }
696
- const surrogate = AST.getSurrogateAnnotation(ast)
697
- if (Option.isSome(surrogate)) {
698
- return go(surrogate.value, $defs, identifier, path, options, "handle-annotation", errors)
699
- }
700
- switch (ast._tag) {
701
- // Unsupported
702
- case "Declaration":
703
- case "UndefinedKeyword":
704
- case "BigIntKeyword":
705
- case "UniqueSymbol":
706
- case "SymbolKeyword": {
707
- if (errors === "ignore-errors") return addASTAnnotations(constAny, ast)
708
- throw new Error(errors_.getJSONSchemaMissingAnnotationErrorMessage(path, ast))
709
- }
710
- case "Suspend": {
711
- if (identifier === "handle-identifier") {
712
- if (errors === "ignore-errors") return addASTAnnotations(constAny, ast)
713
- throw new Error(errors_.getJSONSchemaMissingIdentifierAnnotationErrorMessage(path, ast))
714
- }
715
- return go(ast.f(), $defs, "ignore-identifier", path, options, "handle-annotation", errors)
716
- }
717
- // Primitives
718
- case "NeverKeyword":
719
- return addASTAnnotations(constNever, ast)
720
- case "VoidKeyword":
721
- return addASTAnnotations(constVoid, ast)
722
- case "UnknownKeyword":
723
- return addASTAnnotations(constUnknown, ast)
724
- case "AnyKeyword":
725
- return addASTAnnotations(constAny, ast)
726
- case "ObjectKeyword":
727
- return addASTAnnotations(constObject, ast)
728
- case "StringKeyword":
729
- return addASTAnnotations({ type: "string" }, ast)
730
- case "NumberKeyword":
731
- return addASTAnnotations({ type: "number" }, ast)
732
- case "BooleanKeyword":
733
- return addASTAnnotations({ type: "boolean" }, ast)
734
- case "Literal": {
735
- const literal = ast.literal
736
- if (literal === null) {
737
- return addASTAnnotations({ type: "null" }, ast)
738
- } else if (Predicate.isString(literal)) {
739
- return addASTAnnotations({ type: "string", enum: [literal] }, ast)
740
- } else if (Predicate.isNumber(literal)) {
741
- return addASTAnnotations({ type: "number", enum: [literal] }, ast)
742
- } else if (Predicate.isBoolean(literal)) {
743
- return addASTAnnotations({ type: "boolean", enum: [literal] }, ast)
744
- }
745
- if (errors === "ignore-errors") return addASTAnnotations(constAny, ast)
746
- throw new Error(errors_.getJSONSchemaMissingAnnotationErrorMessage(path, ast))
747
- }
748
- case "Enums": {
749
- const anyOf = ast.enums.map((e) => {
750
- const type: "string" | "number" = Predicate.isNumber(e[1]) ? "number" : "string"
751
- return { type, title: e[0], enum: [e[1]] }
752
- })
753
- return anyOf.length >= 1 ?
754
- addASTAnnotations({
755
- $comment: "/schemas/enums",
756
- anyOf
757
- }, ast) :
758
- addASTAnnotations(constNever, ast)
759
- }
760
- case "TupleType": {
761
- const elements = ast.elements.map((e, i) =>
762
- mergeJsonSchemaAnnotations(
763
- go(e.type, $defs, "handle-identifier", path.concat(i), options, "handle-annotation", errors),
764
- getContextJsonSchemaAnnotations(e.type, e)
765
- )
766
- )
767
- const rest = ast.rest.map((type) =>
768
- mergeJsonSchemaAnnotations(
769
- go(type.type, $defs, "handle-identifier", path, options, "handle-annotation", errors),
770
- getContextJsonSchemaAnnotations(type.type, type)
771
- )
772
- )
773
- const output: JsonSchema7Array = { type: "array" }
774
- // ---------------------------------------------
775
- // handle elements
776
- // ---------------------------------------------
777
- const len = ast.elements.length
778
- if (len > 0) {
779
- output.minItems = len - ast.elements.filter((element) => element.isOptional).length
780
- if (options.target === "jsonSchema7") {
781
- output.items = elements
782
- } else {
783
- output.prefixItems = elements
784
- }
785
- }
786
- // ---------------------------------------------
787
- // handle rest element
788
- // ---------------------------------------------
789
- const restLength = rest.length
790
- if (restLength > 0) {
791
- const head = rest[0]
792
- const isHomogeneous = restLength === 1 && ast.elements.every((e) => e.type === ast.rest[0].type)
793
- if (isHomogeneous) {
794
- if (options.target === "jsonSchema7") {
795
- output.items = head
796
- } else {
797
- output.items = head
798
- delete output.prefixItems
799
- }
800
- } else {
801
- if (options.target === "jsonSchema7") {
802
- output.additionalItems = head
803
- } else {
804
- output.items = head
805
- }
806
- }
807
-
808
- // ---------------------------------------------
809
- // handle post rest elements
810
- // ---------------------------------------------
811
- if (restLength > 1) {
812
- if (errors === "ignore-errors") return addASTAnnotations(constAny, ast)
813
- throw new Error(errors_.getJSONSchemaUnsupportedPostRestElementsErrorMessage(path))
814
- }
815
- } else {
816
- if (len > 0) {
817
- if (options.target === "jsonSchema7") {
818
- output.additionalItems = false
819
- } else {
820
- output.items = false
821
- }
822
- } else {
823
- output.maxItems = 0
824
- }
825
- }
826
-
827
- return addASTAnnotations(output, ast)
828
- }
829
- case "TypeLiteral": {
830
- if (ast.propertySignatures.length === 0 && ast.indexSignatures.length === 0) {
831
- return addASTAnnotations(constEmptyStruct, ast)
832
- }
833
- const output: JsonSchema7Object = {
834
- type: "object",
835
- required: [],
836
- properties: {},
837
- additionalProperties: getAdditionalProperties(options)
838
- }
839
- let patternProperties: JsonSchema7 | undefined = undefined
840
- let propertyNames: JsonSchema7 | undefined = undefined
841
- for (const is of ast.indexSignatures) {
842
- const pruned = pruneUndefined(is.type) ?? is.type
843
- const parameter = is.parameter
844
- switch (parameter._tag) {
845
- case "StringKeyword": {
846
- output.additionalProperties = go(
847
- pruned,
848
- $defs,
849
- "handle-identifier",
850
- path,
851
- options,
852
- "handle-annotation",
853
- errors
854
- )
855
- break
856
- }
857
- case "TemplateLiteral": {
858
- patternProperties = go(pruned, $defs, "handle-identifier", path, options, "handle-annotation", errors)
859
- propertyNames = {
860
- type: "string",
861
- pattern: AST.getTemplateLiteralRegExp(parameter).source
862
- }
863
- break
864
- }
865
- case "Refinement": {
866
- patternProperties = go(pruned, $defs, "handle-identifier", path, options, "handle-annotation", errors)
867
- propertyNames = go(parameter, $defs, "handle-identifier", path, options, "handle-annotation", errors)
868
- break
869
- }
870
- case "SymbolKeyword": {
871
- const indexSignaturePath = path.concat("[symbol]")
872
- output.additionalProperties = go(
873
- pruned,
874
- $defs,
875
- "handle-identifier",
876
- indexSignaturePath,
877
- options,
878
- "handle-annotation",
879
- errors
880
- )
881
- propertyNames = go(
882
- parameter,
883
- $defs,
884
- "handle-identifier",
885
- indexSignaturePath,
886
- options,
887
- "handle-annotation",
888
- errors
889
- )
890
- break
891
- }
892
- }
893
- }
894
- // ---------------------------------------------
895
- // handle property signatures
896
- // ---------------------------------------------
897
- for (let i = 0; i < ast.propertySignatures.length; i++) {
898
- const ps = ast.propertySignatures[i]
899
- const name = ps.name
900
- if (Predicate.isString(name)) {
901
- const pruned = pruneUndefined(ps.type)
902
- const type = pruned ?? ps.type
903
- output.properties[name] = mergeJsonSchemaAnnotations(
904
- go(type, $defs, "handle-identifier", path.concat(ps.name), options, "handle-annotation", errors),
905
- getContextJsonSchemaAnnotations(type, ps)
906
- )
907
- // ---------------------------------------------
908
- // handle optional property signatures
909
- // ---------------------------------------------
910
- if (!ps.isOptional && pruned === undefined) {
911
- output.required.push(name)
912
- }
913
- } else {
914
- if (errors === "ignore-errors") return addASTAnnotations(constAny, ast)
915
- throw new Error(errors_.getJSONSchemaUnsupportedKeyErrorMessage(name, path))
916
- }
917
- }
918
- // ---------------------------------------------
919
- // handle index signatures
920
- // ---------------------------------------------
921
- if (patternProperties !== undefined) {
922
- delete output.additionalProperties
923
- output.patternProperties = { "": patternProperties }
924
- }
925
- if (propertyNames !== undefined) {
926
- output.propertyNames = propertyNames
927
- }
928
-
929
- return addASTAnnotations(output, ast)
930
- }
931
- case "Union": {
932
- const members: Array<JsonSchema7> = ast.types.map((t) =>
933
- go(t, $defs, "handle-identifier", path, options, "handle-annotation", errors)
934
- )
935
- const anyOf = compactUnion(members)
936
- switch (anyOf.length) {
937
- case 0:
938
- return constNever
939
- case 1:
940
- return addASTAnnotations(anyOf[0], ast)
941
- default:
942
- return addASTAnnotations({ anyOf }, ast)
943
- }
944
- }
945
- case "Refinement":
946
- return go(ast.from, $defs, identifier, path, options, "handle-annotation", errors)
947
- case "TemplateLiteral": {
948
- const regex = AST.getTemplateLiteralRegExp(ast)
949
- return addASTAnnotations({
950
- type: "string",
951
- title: String(ast),
952
- description: "a template literal",
953
- pattern: regex.source
954
- }, ast)
955
- }
956
- case "Transformation": {
957
- if (isParseJsonTransformation(ast.from)) {
958
- const out: JsonSchema7String & { contentSchema?: JsonSchema7 } = {
959
- "type": "string",
960
- "contentMediaType": "application/json"
961
- }
962
- if (isContentSchemaSupported(options)) {
963
- out["contentSchema"] = go(ast.to, $defs, identifier, path, options, "handle-annotation", errors)
964
- }
965
- return out
966
- }
967
- const from = go(ast.from, $defs, identifier, path, options, "handle-annotation", errors)
968
- if (
969
- ast.transformation._tag === "TypeLiteralTransformation" &&
970
- isJsonSchema7Object(from)
971
- ) {
972
- const to = go(ast.to, {}, "ignore-identifier", path, options, "handle-annotation", "ignore-errors")
973
- if (isJsonSchema7Object(to)) {
974
- for (const t of ast.transformation.propertySignatureTransformations) {
975
- const toKey = t.to
976
- const fromKey = t.from
977
- if (Predicate.isString(toKey) && Predicate.isString(fromKey)) {
978
- const toProperty = to.properties[toKey]
979
- if (Predicate.isRecord(toProperty)) {
980
- const fromProperty = from.properties[fromKey]
981
- if (Predicate.isRecord(fromProperty)) {
982
- const annotations: JsonSchemaAnnotations = {}
983
- if (Predicate.isString(toProperty.title)) annotations.title = toProperty.title
984
- if (Predicate.isString(toProperty.description)) annotations.description = toProperty.description
985
- if (Array.isArray(toProperty.examples)) annotations.examples = toProperty.examples
986
- if (Object.hasOwn(toProperty, "default") && toProperty.default !== undefined) {
987
- annotations.default = toProperty.default
988
- }
989
- from.properties[fromKey] = addAnnotations(fromProperty, annotations)
990
- }
991
- }
992
- }
993
- }
994
- }
995
- }
996
- return addASTAnnotations(from, ast)
997
- }
998
- }
999
- }
1000
-
1001
- function isJsonSchema7Object(jsonSchema: unknown): jsonSchema is JsonSchema7Object {
1002
- return Predicate.isRecord(jsonSchema) && jsonSchema.type === "object" && Predicate.isRecord(jsonSchema.properties)
1003
- }
1004
-
1005
- function isNeverWithoutCustomAnnotations(jsonSchema: JsonSchema7): boolean {
1006
- return jsonSchema === constNever || (Predicate.hasProperty(jsonSchema, "$id") && jsonSchema.$id === constNever.$id &&
1007
- Object.keys(jsonSchema).length === 3 && jsonSchema.title === AST.neverKeyword.annotations[AST.TitleAnnotationId])
1008
- }
1009
-
1010
- function isAny(jsonSchema: JsonSchema7): jsonSchema is JsonSchema7Any {
1011
- return "$id" in jsonSchema && jsonSchema.$id === constAny.$id
1012
- }
1013
-
1014
- function isUnknown(jsonSchema: JsonSchema7): jsonSchema is JsonSchema7Unknown {
1015
- return "$id" in jsonSchema && jsonSchema.$id === constUnknown.$id
1016
- }
1017
-
1018
- function isVoid(jsonSchema: JsonSchema7): jsonSchema is JsonSchema7Void {
1019
- return "$id" in jsonSchema && jsonSchema.$id === constVoid.$id
1020
- }
1021
-
1022
- function isCompactableLiteral(jsonSchema: JsonSchema7 | undefined): jsonSchema is JsonSchema7Enum {
1023
- return Predicate.hasProperty(jsonSchema, "enum") && "type" in jsonSchema && Object.keys(jsonSchema).length === 2
1024
- }
1025
-
1026
- function compactUnion(members: Array<JsonSchema7>): Array<JsonSchema7> {
1027
- const out: Array<JsonSchema7> = []
1028
- for (const m of members) {
1029
- if (isNeverWithoutCustomAnnotations(m)) continue
1030
- if (isAny(m) || isUnknown(m) || isVoid(m)) return [m]
1031
- if (isCompactableLiteral(m) && out.length > 0) {
1032
- const last = out[out.length - 1]
1033
- if (isCompactableLiteral(last) && last.type === m.type) {
1034
- out[out.length - 1] = {
1035
- type: last.type,
1036
- enum: [...last.enum, ...m.enum]
1037
- } as JsonSchema7Enum
1038
- continue
1039
- }
1040
- }
1041
- out.push(m)
1042
- }
1043
- return out
1044
- }