effect 3.9.2 → 3.10.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (207) hide show
  1. package/Arbitrary/package.json +6 -0
  2. package/FastCheck/package.json +6 -0
  3. package/JSONSchema/package.json +6 -0
  4. package/ParseResult/package.json +6 -0
  5. package/Pretty/package.json +6 -0
  6. package/Schema/package.json +6 -0
  7. package/SchemaAST/package.json +6 -0
  8. package/TSubscriptionRef/package.json +6 -0
  9. package/dist/cjs/Arbitrary.js +490 -0
  10. package/dist/cjs/Arbitrary.js.map +1 -0
  11. package/dist/cjs/Effect.js +247 -16
  12. package/dist/cjs/Effect.js.map +1 -1
  13. package/dist/cjs/FastCheck.js +17 -0
  14. package/dist/cjs/FastCheck.js.map +1 -0
  15. package/dist/cjs/Inspectable.js +43 -3
  16. package/dist/cjs/Inspectable.js.map +1 -1
  17. package/dist/cjs/JSONSchema.js +418 -0
  18. package/dist/cjs/JSONSchema.js.map +1 -0
  19. package/dist/cjs/ParseResult.js +1539 -0
  20. package/dist/cjs/ParseResult.js.map +1 -0
  21. package/dist/cjs/Predicate.js +3 -1
  22. package/dist/cjs/Predicate.js.map +1 -1
  23. package/dist/cjs/Pretty.js +183 -0
  24. package/dist/cjs/Pretty.js.map +1 -0
  25. package/dist/cjs/Schema.js +5529 -0
  26. package/dist/cjs/Schema.js.map +1 -0
  27. package/dist/cjs/SchemaAST.js +2365 -0
  28. package/dist/cjs/SchemaAST.js.map +1 -0
  29. package/dist/cjs/Stream.js +17 -3
  30. package/dist/cjs/Stream.js.map +1 -1
  31. package/dist/cjs/TPubSub.js +9 -1
  32. package/dist/cjs/TPubSub.js.map +1 -1
  33. package/dist/cjs/TQueue.js.map +1 -1
  34. package/dist/cjs/TRef.js.map +1 -1
  35. package/dist/cjs/TSubscriptionRef.js +96 -0
  36. package/dist/cjs/TSubscriptionRef.js.map +1 -0
  37. package/dist/cjs/index.js +18 -2
  38. package/dist/cjs/index.js.map +1 -1
  39. package/dist/cjs/internal/cause.js +2 -2
  40. package/dist/cjs/internal/cause.js.map +1 -1
  41. package/dist/cjs/internal/channel/channelExecutor.js +160 -161
  42. package/dist/cjs/internal/channel/channelExecutor.js.map +1 -1
  43. package/dist/cjs/internal/core.js +4 -4
  44. package/dist/cjs/internal/core.js.map +1 -1
  45. package/dist/cjs/internal/fiberRuntime.js +14 -12
  46. package/dist/cjs/internal/fiberRuntime.js.map +1 -1
  47. package/dist/cjs/internal/logger.js +7 -6
  48. package/dist/cjs/internal/logger.js.map +1 -1
  49. package/dist/cjs/internal/redacted.js +4 -0
  50. package/dist/cjs/internal/redacted.js.map +1 -1
  51. package/dist/cjs/internal/schema/errors.js +116 -0
  52. package/dist/cjs/internal/schema/errors.js.map +1 -0
  53. package/dist/cjs/internal/schema/filters.js +41 -0
  54. package/dist/cjs/internal/schema/filters.js.map +1 -0
  55. package/dist/cjs/internal/schema/util.js +96 -0
  56. package/dist/cjs/internal/schema/util.js.map +1 -0
  57. package/dist/cjs/internal/stm/core.js +2 -3
  58. package/dist/cjs/internal/stm/core.js.map +1 -1
  59. package/dist/cjs/internal/stm/tPubSub.js.map +1 -1
  60. package/dist/cjs/internal/stm/tQueue.js +1 -2
  61. package/dist/cjs/internal/stm/tQueue.js.map +1 -1
  62. package/dist/cjs/internal/stm/tRef.js +6 -2
  63. package/dist/cjs/internal/stm/tRef.js.map +1 -1
  64. package/dist/cjs/internal/stm/tSubscriptionRef.js +178 -0
  65. package/dist/cjs/internal/stm/tSubscriptionRef.js.map +1 -0
  66. package/dist/cjs/internal/stream.js +36 -16
  67. package/dist/cjs/internal/stream.js.map +1 -1
  68. package/dist/cjs/internal/version.js +1 -1
  69. package/dist/cjs/internal/version.js.map +1 -1
  70. package/dist/dts/Arbitrary.d.ts +45 -0
  71. package/dist/dts/Arbitrary.d.ts.map +1 -0
  72. package/dist/dts/Effect.d.ts +298 -30
  73. package/dist/dts/Effect.d.ts.map +1 -1
  74. package/dist/dts/FastCheck.d.ts +9 -0
  75. package/dist/dts/FastCheck.d.ts.map +1 -0
  76. package/dist/dts/Inspectable.d.ts +28 -0
  77. package/dist/dts/Inspectable.d.ts.map +1 -1
  78. package/dist/dts/JSONSchema.d.ts +181 -0
  79. package/dist/dts/JSONSchema.d.ts.map +1 -0
  80. package/dist/dts/ParseResult.d.ts +551 -0
  81. package/dist/dts/ParseResult.d.ts.map +1 -0
  82. package/dist/dts/Predicate.d.ts.map +1 -1
  83. package/dist/dts/Pretty.d.ts +26 -0
  84. package/dist/dts/Pretty.d.ts.map +1 -0
  85. package/dist/dts/Schema.d.ts +4562 -0
  86. package/dist/dts/Schema.d.ts.map +1 -0
  87. package/dist/dts/SchemaAST.d.ts +1321 -0
  88. package/dist/dts/SchemaAST.d.ts.map +1 -0
  89. package/dist/dts/Stream.d.ts +67 -2
  90. package/dist/dts/Stream.d.ts.map +1 -1
  91. package/dist/dts/TPubSub.d.ts +8 -0
  92. package/dist/dts/TPubSub.d.ts.map +1 -1
  93. package/dist/dts/TQueue.d.ts +7 -7
  94. package/dist/dts/TQueue.d.ts.map +1 -1
  95. package/dist/dts/TRef.d.ts +2 -1
  96. package/dist/dts/TRef.d.ts.map +1 -1
  97. package/dist/dts/TSubscriptionRef.d.ts +251 -0
  98. package/dist/dts/TSubscriptionRef.d.ts.map +1 -0
  99. package/dist/dts/index.d.ts +32 -0
  100. package/dist/dts/index.d.ts.map +1 -1
  101. package/dist/dts/internal/fiberRuntime.d.ts.map +1 -1
  102. package/dist/dts/internal/schema/errors.d.ts +2 -0
  103. package/dist/dts/internal/schema/errors.d.ts.map +1 -0
  104. package/dist/dts/internal/schema/filters.d.ts +2 -0
  105. package/dist/dts/internal/schema/filters.d.ts.map +1 -0
  106. package/dist/dts/internal/schema/util.d.ts +2 -0
  107. package/dist/dts/internal/schema/util.d.ts.map +1 -0
  108. package/dist/dts/internal/stm/tRef.d.ts +3 -1
  109. package/dist/dts/internal/stm/tRef.d.ts.map +1 -1
  110. package/dist/dts/internal/stm/tSubscriptionRef.d.ts +2 -0
  111. package/dist/dts/internal/stm/tSubscriptionRef.d.ts.map +1 -0
  112. package/dist/dts/internal/stream.d.ts.map +1 -1
  113. package/dist/esm/Arbitrary.js +472 -0
  114. package/dist/esm/Arbitrary.js.map +1 -0
  115. package/dist/esm/Effect.js +256 -18
  116. package/dist/esm/Effect.js.map +1 -1
  117. package/dist/esm/FastCheck.js +9 -0
  118. package/dist/esm/FastCheck.js.map +1 -0
  119. package/dist/esm/Inspectable.js +39 -2
  120. package/dist/esm/Inspectable.js.map +1 -1
  121. package/dist/esm/JSONSchema.js +408 -0
  122. package/dist/esm/JSONSchema.js.map +1 -0
  123. package/dist/esm/ParseResult.js +1503 -0
  124. package/dist/esm/ParseResult.js.map +1 -0
  125. package/dist/esm/Predicate.js +2 -1
  126. package/dist/esm/Predicate.js.map +1 -1
  127. package/dist/esm/Pretty.js +173 -0
  128. package/dist/esm/Pretty.js.map +1 -0
  129. package/dist/esm/Schema.js +5328 -0
  130. package/dist/esm/Schema.js.map +1 -0
  131. package/dist/esm/SchemaAST.js +2300 -0
  132. package/dist/esm/SchemaAST.js.map +1 -0
  133. package/dist/esm/Stream.js +14 -0
  134. package/dist/esm/Stream.js.map +1 -1
  135. package/dist/esm/TPubSub.js +8 -0
  136. package/dist/esm/TPubSub.js.map +1 -1
  137. package/dist/esm/TQueue.js.map +1 -1
  138. package/dist/esm/TRef.js.map +1 -1
  139. package/dist/esm/TSubscriptionRef.js +87 -0
  140. package/dist/esm/TSubscriptionRef.js.map +1 -0
  141. package/dist/esm/index.js +32 -0
  142. package/dist/esm/index.js.map +1 -1
  143. package/dist/esm/internal/cause.js +3 -3
  144. package/dist/esm/internal/cause.js.map +1 -1
  145. package/dist/esm/internal/channel/channelExecutor.js +160 -161
  146. package/dist/esm/internal/channel/channelExecutor.js.map +1 -1
  147. package/dist/esm/internal/core.js +4 -4
  148. package/dist/esm/internal/core.js.map +1 -1
  149. package/dist/esm/internal/fiberRuntime.js +14 -12
  150. package/dist/esm/internal/fiberRuntime.js.map +1 -1
  151. package/dist/esm/internal/logger.js +7 -6
  152. package/dist/esm/internal/logger.js.map +1 -1
  153. package/dist/esm/internal/redacted.js +4 -0
  154. package/dist/esm/internal/redacted.js.map +1 -1
  155. package/dist/esm/internal/schema/errors.js +87 -0
  156. package/dist/esm/internal/schema/errors.js.map +1 -0
  157. package/dist/esm/internal/schema/filters.js +35 -0
  158. package/dist/esm/internal/schema/filters.js.map +1 -0
  159. package/dist/esm/internal/schema/util.js +78 -0
  160. package/dist/esm/internal/schema/util.js.map +1 -0
  161. package/dist/esm/internal/stm/core.js +1 -2
  162. package/dist/esm/internal/stm/core.js.map +1 -1
  163. package/dist/esm/internal/stm/tPubSub.js.map +1 -1
  164. package/dist/esm/internal/stm/tQueue.js +1 -2
  165. package/dist/esm/internal/stm/tQueue.js.map +1 -1
  166. package/dist/esm/internal/stm/tRef.js +5 -1
  167. package/dist/esm/internal/stm/tRef.js.map +1 -1
  168. package/dist/esm/internal/stm/tSubscriptionRef.js +166 -0
  169. package/dist/esm/internal/stm/tSubscriptionRef.js.map +1 -0
  170. package/dist/esm/internal/stream.js +33 -15
  171. package/dist/esm/internal/stream.js.map +1 -1
  172. package/dist/esm/internal/version.js +1 -1
  173. package/dist/esm/internal/version.js.map +1 -1
  174. package/package.json +68 -1
  175. package/src/Arbitrary.ts +563 -0
  176. package/src/Effect.ts +302 -31
  177. package/src/FastCheck.ts +9 -0
  178. package/src/Inspectable.ts +56 -2
  179. package/src/JSONSchema.ts +601 -0
  180. package/src/ParseResult.ts +2027 -0
  181. package/src/Predicate.ts +3 -1
  182. package/src/Pretty.ts +204 -0
  183. package/src/Schema.ts +10388 -0
  184. package/src/SchemaAST.ts +2827 -0
  185. package/src/Stream.ts +77 -9
  186. package/src/TPubSub.ts +9 -0
  187. package/src/TQueue.ts +7 -7
  188. package/src/TRef.ts +2 -1
  189. package/src/TSubscriptionRef.ts +284 -0
  190. package/src/index.ts +40 -0
  191. package/src/internal/cause.ts +3 -3
  192. package/src/internal/channel/channelExecutor.ts +213 -214
  193. package/src/internal/core-effect.ts +2 -2
  194. package/src/internal/core.ts +4 -4
  195. package/src/internal/fiberRuntime.ts +14 -12
  196. package/src/internal/logger.ts +7 -6
  197. package/src/internal/redacted.ts +4 -0
  198. package/src/internal/schema/errors.ts +189 -0
  199. package/src/internal/schema/filters.ts +86 -0
  200. package/src/internal/schema/util.ts +113 -0
  201. package/src/internal/stm/core.ts +1 -2
  202. package/src/internal/stm/tPubSub.ts +1 -0
  203. package/src/internal/stm/tQueue.ts +2 -2
  204. package/src/internal/stm/tRef.ts +7 -2
  205. package/src/internal/stm/tSubscriptionRef.ts +286 -0
  206. package/src/internal/stream.ts +97 -20
  207. package/src/internal/version.ts +1 -1
@@ -0,0 +1,1503 @@
1
+ /**
2
+ * @since 3.10.0
3
+ */
4
+ import * as array_ from "./Array.js";
5
+ import { TaggedError } from "./Data.js";
6
+ import * as Effect from "./Effect.js";
7
+ import * as Either from "./Either.js";
8
+ import { dual } from "./Function.js";
9
+ import { globalValue } from "./GlobalValue.js";
10
+ import * as Inspectable from "./Inspectable.js";
11
+ import * as util_ from "./internal/schema/util.js";
12
+ import * as Option from "./Option.js";
13
+ import * as Predicate from "./Predicate.js";
14
+ import * as AST from "./SchemaAST.js";
15
+ /**
16
+ * @category model
17
+ * @since 3.10.0
18
+ */
19
+ export class Pointer {
20
+ path;
21
+ actual;
22
+ issue;
23
+ /**
24
+ * @since 3.10.0
25
+ */
26
+ _tag = "Pointer";
27
+ constructor(path, actual, issue) {
28
+ this.path = path;
29
+ this.actual = actual;
30
+ this.issue = issue;
31
+ }
32
+ }
33
+ /**
34
+ * Error that occurs when an unexpected key or index is present.
35
+ *
36
+ * @category model
37
+ * @since 3.10.0
38
+ */
39
+ export class Unexpected {
40
+ actual;
41
+ message;
42
+ /**
43
+ * @since 3.10.0
44
+ */
45
+ _tag = "Unexpected";
46
+ constructor(actual,
47
+ /**
48
+ * @since 3.10.0
49
+ */
50
+ message) {
51
+ this.actual = actual;
52
+ this.message = message;
53
+ }
54
+ }
55
+ /**
56
+ * Error that occurs when a required key or index is missing.
57
+ *
58
+ * @category model
59
+ * @since 3.10.0
60
+ */
61
+ export class Missing {
62
+ ast;
63
+ message;
64
+ /**
65
+ * @since 3.10.0
66
+ */
67
+ _tag = "Missing";
68
+ /**
69
+ * @since 3.10.0
70
+ */
71
+ actual = undefined;
72
+ constructor(
73
+ /**
74
+ * @since 3.10.0
75
+ */
76
+ ast,
77
+ /**
78
+ * @since 3.10.0
79
+ */
80
+ message) {
81
+ this.ast = ast;
82
+ this.message = message;
83
+ }
84
+ }
85
+ /**
86
+ * Error that contains multiple issues.
87
+ *
88
+ * @category model
89
+ * @since 3.10.0
90
+ */
91
+ export class Composite {
92
+ ast;
93
+ actual;
94
+ issues;
95
+ output;
96
+ /**
97
+ * @since 3.10.0
98
+ */
99
+ _tag = "Composite";
100
+ constructor(ast, actual, issues, output) {
101
+ this.ast = ast;
102
+ this.actual = actual;
103
+ this.issues = issues;
104
+ this.output = output;
105
+ }
106
+ }
107
+ /**
108
+ * Error that occurs when a refinement has an error.
109
+ *
110
+ * @category model
111
+ * @since 3.10.0
112
+ */
113
+ export class Refinement {
114
+ ast;
115
+ actual;
116
+ kind;
117
+ issue;
118
+ /**
119
+ * @since 3.10.0
120
+ */
121
+ _tag = "Refinement";
122
+ constructor(ast, actual, kind, issue) {
123
+ this.ast = ast;
124
+ this.actual = actual;
125
+ this.kind = kind;
126
+ this.issue = issue;
127
+ }
128
+ }
129
+ /**
130
+ * Error that occurs when a transformation has an error.
131
+ *
132
+ * @category model
133
+ * @since 3.10.0
134
+ */
135
+ export class Transformation {
136
+ ast;
137
+ actual;
138
+ kind;
139
+ issue;
140
+ /**
141
+ * @since 3.10.0
142
+ */
143
+ _tag = "Transformation";
144
+ constructor(ast, actual, kind, issue) {
145
+ this.ast = ast;
146
+ this.actual = actual;
147
+ this.kind = kind;
148
+ this.issue = issue;
149
+ }
150
+ }
151
+ /**
152
+ * The `Type` variant of the `ParseIssue` type represents an error that occurs when the `actual` value is not of the expected type.
153
+ * The `ast` field specifies the expected type, and the `actual` field contains the value that caused the error.
154
+ *
155
+ * @category model
156
+ * @since 3.10.0
157
+ */
158
+ export class Type {
159
+ ast;
160
+ actual;
161
+ message;
162
+ /**
163
+ * @since 3.10.0
164
+ */
165
+ _tag = "Type";
166
+ constructor(ast, actual, message) {
167
+ this.ast = ast;
168
+ this.actual = actual;
169
+ this.message = message;
170
+ }
171
+ }
172
+ /**
173
+ * The `Forbidden` variant of the `ParseIssue` type represents a forbidden operation, such as when encountering an Effect that is not allowed to execute (e.g., using `runSync`).
174
+ *
175
+ * @category model
176
+ * @since 3.10.0
177
+ */
178
+ export class Forbidden {
179
+ ast;
180
+ actual;
181
+ message;
182
+ /**
183
+ * @since 3.10.0
184
+ */
185
+ _tag = "Forbidden";
186
+ constructor(ast, actual, message) {
187
+ this.ast = ast;
188
+ this.actual = actual;
189
+ this.message = message;
190
+ }
191
+ }
192
+ /**
193
+ * @category type id
194
+ * @since 3.10.0
195
+ */
196
+ export const ParseErrorTypeId = /*#__PURE__*/Symbol.for("effect/Schema/ParseErrorTypeId");
197
+ /**
198
+ * @since 3.10.0
199
+ */
200
+ export const isParseError = u => Predicate.hasProperty(u, ParseErrorTypeId);
201
+ /**
202
+ * @since 3.10.0
203
+ */
204
+ export class ParseError extends /*#__PURE__*/TaggedError("ParseError") {
205
+ /**
206
+ * @since 3.10.0
207
+ */
208
+ [ParseErrorTypeId] = ParseErrorTypeId;
209
+ get message() {
210
+ return this.toString();
211
+ }
212
+ /**
213
+ * @since 3.10.0
214
+ */
215
+ toString() {
216
+ return TreeFormatter.formatIssueSync(this.issue);
217
+ }
218
+ /**
219
+ * @since 3.10.0
220
+ */
221
+ toJSON() {
222
+ return {
223
+ _id: "ParseError",
224
+ message: this.toString()
225
+ };
226
+ }
227
+ /**
228
+ * @since 3.10.0
229
+ */
230
+ [Inspectable.NodeInspectSymbol]() {
231
+ return this.toJSON();
232
+ }
233
+ }
234
+ /**
235
+ * @category constructors
236
+ * @since 3.10.0
237
+ */
238
+ export const parseError = issue => new ParseError({
239
+ issue
240
+ });
241
+ /**
242
+ * @category constructors
243
+ * @since 3.10.0
244
+ */
245
+ export const succeed = Either.right;
246
+ /**
247
+ * @category constructors
248
+ * @since 3.10.0
249
+ */
250
+ export const fail = Either.left;
251
+ const _try = Either.try;
252
+ export {
253
+ /**
254
+ * @category constructors
255
+ * @since 3.10.0
256
+ */
257
+ _try as try };
258
+ /**
259
+ * @category constructors
260
+ * @since 3.10.0
261
+ */
262
+ export const fromOption = Either.fromOption;
263
+ /**
264
+ * @category optimisation
265
+ * @since 3.10.0
266
+ */
267
+ export const flatMap = /*#__PURE__*/dual(2, (self, f) => {
268
+ const s = self;
269
+ if (s["_tag"] === "Left") {
270
+ return s;
271
+ }
272
+ if (s["_tag"] === "Right") {
273
+ return f(s.right);
274
+ }
275
+ return Effect.flatMap(self, f);
276
+ });
277
+ /**
278
+ * @category optimisation
279
+ * @since 3.10.0
280
+ */
281
+ export const map = /*#__PURE__*/dual(2, (self, f) => {
282
+ const s = self;
283
+ if (s["_tag"] === "Left") {
284
+ return s;
285
+ }
286
+ if (s["_tag"] === "Right") {
287
+ return Either.right(f(s.right));
288
+ }
289
+ return Effect.map(self, f);
290
+ });
291
+ /**
292
+ * @category optimisation
293
+ * @since 3.10.0
294
+ */
295
+ export const mapError = /*#__PURE__*/dual(2, (self, f) => {
296
+ const s = self;
297
+ if (s["_tag"] === "Left") {
298
+ return Either.left(f(s.left));
299
+ }
300
+ if (s["_tag"] === "Right") {
301
+ return s;
302
+ }
303
+ return Effect.mapError(self, f);
304
+ });
305
+ /**
306
+ * @category optimisation
307
+ * @since 3.10.0
308
+ */
309
+ export const eitherOrUndefined = self => {
310
+ const s = self;
311
+ if (s["_tag"] === "Left" || s["_tag"] === "Right") {
312
+ return s;
313
+ }
314
+ };
315
+ /**
316
+ * @category optimisation
317
+ * @since 3.10.0
318
+ */
319
+ export const mapBoth = /*#__PURE__*/dual(2, (self, options) => {
320
+ const s = self;
321
+ if (s["_tag"] === "Left") {
322
+ return Either.left(options.onFailure(s.left));
323
+ }
324
+ if (s["_tag"] === "Right") {
325
+ return Either.right(options.onSuccess(s.right));
326
+ }
327
+ return Effect.mapBoth(self, options);
328
+ });
329
+ /**
330
+ * @category optimisation
331
+ * @since 3.10.0
332
+ */
333
+ export const orElse = /*#__PURE__*/dual(2, (self, f) => {
334
+ const s = self;
335
+ if (s["_tag"] === "Left") {
336
+ return f(s.left);
337
+ }
338
+ if (s["_tag"] === "Right") {
339
+ return s;
340
+ }
341
+ return Effect.catchAll(self, f);
342
+ });
343
+ /** @internal */
344
+ export const mergeInternalOptions = (options, overrideOptions) => {
345
+ if (overrideOptions === undefined || Predicate.isNumber(overrideOptions)) {
346
+ return options;
347
+ }
348
+ if (options === undefined) {
349
+ return overrideOptions;
350
+ }
351
+ return {
352
+ ...options,
353
+ ...overrideOptions
354
+ };
355
+ };
356
+ const getEither = (ast, isDecoding, options) => {
357
+ const parser = goMemo(ast, isDecoding);
358
+ return (u, overrideOptions) => parser(u, mergeInternalOptions(options, overrideOptions));
359
+ };
360
+ const getSync = (ast, isDecoding, options) => {
361
+ const parser = getEither(ast, isDecoding, options);
362
+ return (input, overrideOptions) => Either.getOrThrowWith(parser(input, overrideOptions), parseError);
363
+ };
364
+ const getOption = (ast, isDecoding, options) => {
365
+ const parser = getEither(ast, isDecoding, options);
366
+ return (input, overrideOptions) => Option.getRight(parser(input, overrideOptions));
367
+ };
368
+ const getEffect = (ast, isDecoding, options) => {
369
+ const parser = goMemo(ast, isDecoding);
370
+ return (input, overrideOptions) => parser(input, {
371
+ ...mergeInternalOptions(options, overrideOptions),
372
+ isEffectAllowed: true
373
+ });
374
+ };
375
+ /**
376
+ * @throws `ParseError`
377
+ * @category decoding
378
+ * @since 3.10.0
379
+ */
380
+ export const decodeUnknownSync = (schema, options) => getSync(schema.ast, true, options);
381
+ /**
382
+ * @category decoding
383
+ * @since 3.10.0
384
+ */
385
+ export const decodeUnknownOption = (schema, options) => getOption(schema.ast, true, options);
386
+ /**
387
+ * @category decoding
388
+ * @since 3.10.0
389
+ */
390
+ export const decodeUnknownEither = (schema, options) => getEither(schema.ast, true, options);
391
+ /**
392
+ * @category decoding
393
+ * @since 3.10.0
394
+ */
395
+ export const decodeUnknownPromise = (schema, options) => {
396
+ const parser = decodeUnknown(schema, options);
397
+ return (u, overrideOptions) => Effect.runPromise(parser(u, overrideOptions));
398
+ };
399
+ /**
400
+ * @category decoding
401
+ * @since 3.10.0
402
+ */
403
+ export const decodeUnknown = (schema, options) => getEffect(schema.ast, true, options);
404
+ /**
405
+ * @throws `ParseError`
406
+ * @category encoding
407
+ * @since 3.10.0
408
+ */
409
+ export const encodeUnknownSync = (schema, options) => getSync(schema.ast, false, options);
410
+ /**
411
+ * @category encoding
412
+ * @since 3.10.0
413
+ */
414
+ export const encodeUnknownOption = (schema, options) => getOption(schema.ast, false, options);
415
+ /**
416
+ * @category encoding
417
+ * @since 3.10.0
418
+ */
419
+ export const encodeUnknownEither = (schema, options) => getEither(schema.ast, false, options);
420
+ /**
421
+ * @category encoding
422
+ * @since 3.10.0
423
+ */
424
+ export const encodeUnknownPromise = (schema, options) => {
425
+ const parser = encodeUnknown(schema, options);
426
+ return (u, overrideOptions) => Effect.runPromise(parser(u, overrideOptions));
427
+ };
428
+ /**
429
+ * @category encoding
430
+ * @since 3.10.0
431
+ */
432
+ export const encodeUnknown = (schema, options) => getEffect(schema.ast, false, options);
433
+ /**
434
+ * @category decoding
435
+ * @since 3.10.0
436
+ */
437
+ export const decodeSync = decodeUnknownSync;
438
+ /**
439
+ * @category decoding
440
+ * @since 3.10.0
441
+ */
442
+ export const decodeOption = decodeUnknownOption;
443
+ /**
444
+ * @category decoding
445
+ * @since 3.10.0
446
+ */
447
+ export const decodeEither = decodeUnknownEither;
448
+ /**
449
+ * @category decoding
450
+ * @since 3.10.0
451
+ */
452
+ export const decodePromise = decodeUnknownPromise;
453
+ /**
454
+ * @category decoding
455
+ * @since 3.10.0
456
+ */
457
+ export const decode = decodeUnknown;
458
+ /**
459
+ * @throws `ParseError`
460
+ * @category validation
461
+ * @since 3.10.0
462
+ */
463
+ export const validateSync = (schema, options) => getSync(AST.typeAST(schema.ast), true, options);
464
+ /**
465
+ * @category validation
466
+ * @since 3.10.0
467
+ */
468
+ export const validateOption = (schema, options) => getOption(AST.typeAST(schema.ast), true, options);
469
+ /**
470
+ * @category validation
471
+ * @since 3.10.0
472
+ */
473
+ export const validateEither = (schema, options) => getEither(AST.typeAST(schema.ast), true, options);
474
+ /**
475
+ * @category validation
476
+ * @since 3.10.0
477
+ */
478
+ export const validatePromise = (schema, options) => {
479
+ const parser = validate(schema, options);
480
+ return (u, overrideOptions) => Effect.runPromise(parser(u, overrideOptions));
481
+ };
482
+ /**
483
+ * @category validation
484
+ * @since 3.10.0
485
+ */
486
+ export const validate = (schema, options) => getEffect(AST.typeAST(schema.ast), true, options);
487
+ /**
488
+ * By default the option `exact` is set to `true`.
489
+ *
490
+ * @category validation
491
+ * @since 3.10.0
492
+ */
493
+ export const is = (schema, options) => {
494
+ const parser = goMemo(AST.typeAST(schema.ast), true);
495
+ return (u, overrideOptions) => Either.isRight(parser(u, {
496
+ exact: true,
497
+ ...mergeInternalOptions(options, overrideOptions)
498
+ }));
499
+ };
500
+ /**
501
+ * By default the option `exact` is set to `true`.
502
+ *
503
+ * @throws `ParseError`
504
+ * @category validation
505
+ * @since 3.10.0
506
+ */
507
+ export const asserts = (schema, options) => {
508
+ const parser = goMemo(AST.typeAST(schema.ast), true);
509
+ return (u, overrideOptions) => {
510
+ const result = parser(u, {
511
+ exact: true,
512
+ ...mergeInternalOptions(options, overrideOptions)
513
+ });
514
+ if (Either.isLeft(result)) {
515
+ throw parseError(result.left);
516
+ }
517
+ };
518
+ };
519
+ /**
520
+ * @category encoding
521
+ * @since 3.10.0
522
+ */
523
+ export const encodeSync = encodeUnknownSync;
524
+ /**
525
+ * @category encoding
526
+ * @since 3.10.0
527
+ */
528
+ export const encodeOption = encodeUnknownOption;
529
+ /**
530
+ * @category encoding
531
+ * @since 3.10.0
532
+ */
533
+ export const encodeEither = encodeUnknownEither;
534
+ /**
535
+ * @category encoding
536
+ * @since 3.10.0
537
+ */
538
+ export const encodePromise = encodeUnknownPromise;
539
+ /**
540
+ * @category encoding
541
+ * @since 3.10.0
542
+ */
543
+ export const encode = encodeUnknown;
544
+ const decodeMemoMap = /*#__PURE__*/globalValue( /*#__PURE__*/Symbol.for("effect/Schema/Parser/decodeMemoMap"), () => new WeakMap());
545
+ const encodeMemoMap = /*#__PURE__*/globalValue( /*#__PURE__*/Symbol.for("effect/Schema/Parser/encodeMemoMap"), () => new WeakMap());
546
+ const goMemo = (ast, isDecoding) => {
547
+ const memoMap = isDecoding ? decodeMemoMap : encodeMemoMap;
548
+ const memo = memoMap.get(ast);
549
+ if (memo) {
550
+ return memo;
551
+ }
552
+ const raw = go(ast, isDecoding);
553
+ const parseOptionsAnnotation = AST.getParseOptionsAnnotation(ast);
554
+ const parserWithOptions = Option.isSome(parseOptionsAnnotation) ? (i, options) => raw(i, mergeInternalOptions(options, parseOptionsAnnotation.value)) : raw;
555
+ const decodingFallbackAnnotation = AST.getDecodingFallbackAnnotation(ast);
556
+ const parser = isDecoding && Option.isSome(decodingFallbackAnnotation) ? (i, options) => handleForbidden(orElse(parserWithOptions(i, options), decodingFallbackAnnotation.value), ast, i, options) : parserWithOptions;
557
+ memoMap.set(ast, parser);
558
+ return parser;
559
+ };
560
+ const getConcurrency = ast => Option.getOrUndefined(AST.getConcurrencyAnnotation(ast));
561
+ const getBatching = ast => Option.getOrUndefined(AST.getBatchingAnnotation(ast));
562
+ const go = (ast, isDecoding) => {
563
+ switch (ast._tag) {
564
+ case "Refinement":
565
+ {
566
+ if (isDecoding) {
567
+ const from = goMemo(ast.from, true);
568
+ return (i, options) => {
569
+ options = options ?? AST.defaultParseOption;
570
+ const allErrors = options?.errors === "all";
571
+ const result = flatMap(orElse(from(i, options), ef => {
572
+ const issue = new Refinement(ast, i, "From", ef);
573
+ if (allErrors && AST.hasStableFilter(ast)) {
574
+ return Option.match(ast.filter(i, options, ast), {
575
+ onNone: () => Either.left(issue),
576
+ onSome: ep => Either.left(new Composite(ast, i, [issue, new Refinement(ast, i, "Predicate", ep)]))
577
+ });
578
+ }
579
+ return Either.left(issue);
580
+ }), a => Option.match(ast.filter(a, options, ast), {
581
+ onNone: () => Either.right(a),
582
+ onSome: ep => Either.left(new Refinement(ast, i, "Predicate", ep))
583
+ }));
584
+ return handleForbidden(result, ast, i, options);
585
+ };
586
+ } else {
587
+ const from = goMemo(AST.typeAST(ast), true);
588
+ const to = goMemo(dropRightRefinement(ast.from), false);
589
+ return (i, options) => handleForbidden(flatMap(from(i, options), a => to(a, options)), ast, i, options);
590
+ }
591
+ }
592
+ case "Transformation":
593
+ {
594
+ const transform = getFinalTransformation(ast.transformation, isDecoding);
595
+ const from = isDecoding ? goMemo(ast.from, true) : goMemo(ast.to, false);
596
+ const to = isDecoding ? goMemo(ast.to, true) : goMemo(ast.from, false);
597
+ return (i, options) => handleForbidden(flatMap(mapError(from(i, options), e => new Transformation(ast, i, isDecoding ? "Encoded" : "Type", e)), a => flatMap(mapError(transform(a, options ?? AST.defaultParseOption, ast, i), e => new Transformation(ast, i, "Transformation", e)), i2 => mapError(to(i2, options), e => new Transformation(ast, i, isDecoding ? "Type" : "Encoded", e)))), ast, i, options);
598
+ }
599
+ case "Declaration":
600
+ {
601
+ const parse = isDecoding ? ast.decodeUnknown(...ast.typeParameters) : ast.encodeUnknown(...ast.typeParameters);
602
+ return (i, options) => handleForbidden(parse(i, options ?? AST.defaultParseOption, ast), ast, i, options);
603
+ }
604
+ case "Literal":
605
+ return fromRefinement(ast, u => u === ast.literal);
606
+ case "UniqueSymbol":
607
+ return fromRefinement(ast, u => u === ast.symbol);
608
+ case "UndefinedKeyword":
609
+ return fromRefinement(ast, Predicate.isUndefined);
610
+ case "NeverKeyword":
611
+ return fromRefinement(ast, Predicate.isNever);
612
+ case "UnknownKeyword":
613
+ case "AnyKeyword":
614
+ case "VoidKeyword":
615
+ return Either.right;
616
+ case "StringKeyword":
617
+ return fromRefinement(ast, Predicate.isString);
618
+ case "NumberKeyword":
619
+ return fromRefinement(ast, Predicate.isNumber);
620
+ case "BooleanKeyword":
621
+ return fromRefinement(ast, Predicate.isBoolean);
622
+ case "BigIntKeyword":
623
+ return fromRefinement(ast, Predicate.isBigInt);
624
+ case "SymbolKeyword":
625
+ return fromRefinement(ast, Predicate.isSymbol);
626
+ case "ObjectKeyword":
627
+ return fromRefinement(ast, Predicate.isObject);
628
+ case "Enums":
629
+ return fromRefinement(ast, u => ast.enums.some(([_, value]) => value === u));
630
+ case "TemplateLiteral":
631
+ {
632
+ const regex = AST.getTemplateLiteralRegExp(ast);
633
+ return fromRefinement(ast, u => Predicate.isString(u) && regex.test(u));
634
+ }
635
+ case "TupleType":
636
+ {
637
+ const elements = ast.elements.map(e => goMemo(e.type, isDecoding));
638
+ const rest = ast.rest.map(annotatedAST => goMemo(annotatedAST.type, isDecoding));
639
+ let requiredTypes = ast.elements.filter(e => !e.isOptional);
640
+ if (ast.rest.length > 0) {
641
+ requiredTypes = requiredTypes.concat(ast.rest.slice(1));
642
+ }
643
+ const requiredLen = requiredTypes.length;
644
+ const expectedIndexes = ast.elements.length > 0 ? ast.elements.map((_, i) => i).join(" | ") : "never";
645
+ const concurrency = getConcurrency(ast);
646
+ const batching = getBatching(ast);
647
+ return (input, options) => {
648
+ if (!array_.isArray(input)) {
649
+ return Either.left(new Type(ast, input));
650
+ }
651
+ const allErrors = options?.errors === "all";
652
+ const es = [];
653
+ let stepKey = 0;
654
+ const output = [];
655
+ // ---------------------------------------------
656
+ // handle missing indexes
657
+ // ---------------------------------------------
658
+ const len = input.length;
659
+ for (let i = len; i <= requiredLen - 1; i++) {
660
+ const e = new Pointer(i, input, new Missing(requiredTypes[i - len]));
661
+ if (allErrors) {
662
+ es.push([stepKey++, e]);
663
+ continue;
664
+ } else {
665
+ return Either.left(new Composite(ast, input, e, output));
666
+ }
667
+ }
668
+ // ---------------------------------------------
669
+ // handle excess indexes
670
+ // ---------------------------------------------
671
+ if (ast.rest.length === 0) {
672
+ for (let i = ast.elements.length; i <= len - 1; i++) {
673
+ const e = new Pointer(i, input, new Unexpected(input[i], `is unexpected, expected: ${expectedIndexes}`));
674
+ if (allErrors) {
675
+ es.push([stepKey++, e]);
676
+ continue;
677
+ } else {
678
+ return Either.left(new Composite(ast, input, e, output));
679
+ }
680
+ }
681
+ }
682
+ let i = 0;
683
+ let queue = undefined;
684
+ // ---------------------------------------------
685
+ // handle elements
686
+ // ---------------------------------------------
687
+ for (; i < elements.length; i++) {
688
+ if (len < i + 1) {
689
+ if (ast.elements[i].isOptional) {
690
+ // the input element is missing
691
+ continue;
692
+ }
693
+ } else {
694
+ const parser = elements[i];
695
+ const te = parser(input[i], options);
696
+ const eu = eitherOrUndefined(te);
697
+ if (eu) {
698
+ if (Either.isLeft(eu)) {
699
+ // the input element is present but is not valid
700
+ const e = new Pointer(i, input, eu.left);
701
+ if (allErrors) {
702
+ es.push([stepKey++, e]);
703
+ continue;
704
+ } else {
705
+ return Either.left(new Composite(ast, input, e, sortByIndex(output)));
706
+ }
707
+ }
708
+ output.push([stepKey++, eu.right]);
709
+ } else {
710
+ const nk = stepKey++;
711
+ const index = i;
712
+ if (!queue) {
713
+ queue = [];
714
+ }
715
+ queue.push(({
716
+ es,
717
+ output
718
+ }) => Effect.flatMap(Effect.either(te), t => {
719
+ if (Either.isLeft(t)) {
720
+ // the input element is present but is not valid
721
+ const e = new Pointer(index, input, t.left);
722
+ if (allErrors) {
723
+ es.push([nk, e]);
724
+ return Effect.void;
725
+ } else {
726
+ return Either.left(new Composite(ast, input, e, sortByIndex(output)));
727
+ }
728
+ }
729
+ output.push([nk, t.right]);
730
+ return Effect.void;
731
+ }));
732
+ }
733
+ }
734
+ }
735
+ // ---------------------------------------------
736
+ // handle rest element
737
+ // ---------------------------------------------
738
+ if (array_.isNonEmptyReadonlyArray(rest)) {
739
+ const [head, ...tail] = rest;
740
+ for (; i < len - tail.length; i++) {
741
+ const te = head(input[i], options);
742
+ const eu = eitherOrUndefined(te);
743
+ if (eu) {
744
+ if (Either.isLeft(eu)) {
745
+ const e = new Pointer(i, input, eu.left);
746
+ if (allErrors) {
747
+ es.push([stepKey++, e]);
748
+ continue;
749
+ } else {
750
+ return Either.left(new Composite(ast, input, e, sortByIndex(output)));
751
+ }
752
+ } else {
753
+ output.push([stepKey++, eu.right]);
754
+ }
755
+ } else {
756
+ const nk = stepKey++;
757
+ const index = i;
758
+ if (!queue) {
759
+ queue = [];
760
+ }
761
+ queue.push(({
762
+ es,
763
+ output
764
+ }) => Effect.flatMap(Effect.either(te), t => {
765
+ if (Either.isLeft(t)) {
766
+ const e = new Pointer(index, input, t.left);
767
+ if (allErrors) {
768
+ es.push([nk, e]);
769
+ return Effect.void;
770
+ } else {
771
+ return Either.left(new Composite(ast, input, e, sortByIndex(output)));
772
+ }
773
+ } else {
774
+ output.push([nk, t.right]);
775
+ return Effect.void;
776
+ }
777
+ }));
778
+ }
779
+ }
780
+ // ---------------------------------------------
781
+ // handle post rest elements
782
+ // ---------------------------------------------
783
+ for (let j = 0; j < tail.length; j++) {
784
+ i += j;
785
+ if (len < i + 1) {
786
+ continue;
787
+ } else {
788
+ const te = tail[j](input[i], options);
789
+ const eu = eitherOrUndefined(te);
790
+ if (eu) {
791
+ if (Either.isLeft(eu)) {
792
+ // the input element is present but is not valid
793
+ const e = new Pointer(i, input, eu.left);
794
+ if (allErrors) {
795
+ es.push([stepKey++, e]);
796
+ continue;
797
+ } else {
798
+ return Either.left(new Composite(ast, input, e, sortByIndex(output)));
799
+ }
800
+ }
801
+ output.push([stepKey++, eu.right]);
802
+ } else {
803
+ const nk = stepKey++;
804
+ const index = i;
805
+ if (!queue) {
806
+ queue = [];
807
+ }
808
+ queue.push(({
809
+ es,
810
+ output
811
+ }) => Effect.flatMap(Effect.either(te), t => {
812
+ if (Either.isLeft(t)) {
813
+ // the input element is present but is not valid
814
+ const e = new Pointer(index, input, t.left);
815
+ if (allErrors) {
816
+ es.push([nk, e]);
817
+ return Effect.void;
818
+ } else {
819
+ return Either.left(new Composite(ast, input, e, sortByIndex(output)));
820
+ }
821
+ }
822
+ output.push([nk, t.right]);
823
+ return Effect.void;
824
+ }));
825
+ }
826
+ }
827
+ }
828
+ }
829
+ // ---------------------------------------------
830
+ // compute result
831
+ // ---------------------------------------------
832
+ const computeResult = ({
833
+ es,
834
+ output
835
+ }) => array_.isNonEmptyArray(es) ? Either.left(new Composite(ast, input, sortByIndex(es), sortByIndex(output))) : Either.right(sortByIndex(output));
836
+ if (queue && queue.length > 0) {
837
+ const cqueue = queue;
838
+ return Effect.suspend(() => {
839
+ const state = {
840
+ es: array_.copy(es),
841
+ output: array_.copy(output)
842
+ };
843
+ return Effect.flatMap(Effect.forEach(cqueue, f => f(state), {
844
+ concurrency,
845
+ batching,
846
+ discard: true
847
+ }), () => computeResult(state));
848
+ });
849
+ }
850
+ return computeResult({
851
+ output,
852
+ es
853
+ });
854
+ };
855
+ }
856
+ case "TypeLiteral":
857
+ {
858
+ if (ast.propertySignatures.length === 0 && ast.indexSignatures.length === 0) {
859
+ return fromRefinement(ast, Predicate.isNotNullable);
860
+ }
861
+ const propertySignatures = [];
862
+ const expectedKeysMap = {};
863
+ const expectedKeys = [];
864
+ for (const ps of ast.propertySignatures) {
865
+ propertySignatures.push([goMemo(ps.type, isDecoding), ps]);
866
+ expectedKeysMap[ps.name] = null;
867
+ expectedKeys.push(ps.name);
868
+ }
869
+ const indexSignatures = ast.indexSignatures.map(is => [goMemo(is.parameter, isDecoding), goMemo(is.type, isDecoding), is.parameter]);
870
+ const expectedAST = AST.Union.make(ast.indexSignatures.map(is => is.parameter).concat(expectedKeys.map(key => Predicate.isSymbol(key) ? new AST.UniqueSymbol(key) : new AST.Literal(key))));
871
+ const expected = goMemo(expectedAST, isDecoding);
872
+ const concurrency = getConcurrency(ast);
873
+ const batching = getBatching(ast);
874
+ return (input, options) => {
875
+ if (!Predicate.isRecord(input)) {
876
+ return Either.left(new Type(ast, input));
877
+ }
878
+ const allErrors = options?.errors === "all";
879
+ const es = [];
880
+ let stepKey = 0;
881
+ // ---------------------------------------------
882
+ // handle excess properties
883
+ // ---------------------------------------------
884
+ const onExcessPropertyError = options?.onExcessProperty === "error";
885
+ const onExcessPropertyPreserve = options?.onExcessProperty === "preserve";
886
+ const output = {};
887
+ let inputKeys;
888
+ if (onExcessPropertyError || onExcessPropertyPreserve) {
889
+ inputKeys = util_.ownKeys(input);
890
+ for (const key of inputKeys) {
891
+ const eu = eitherOrUndefined(expected(key, options));
892
+ if (Either.isLeft(eu)) {
893
+ // key is unexpected
894
+ if (onExcessPropertyError) {
895
+ const e = new Pointer(key, input, new Unexpected(input[key], `is unexpected, expected: ${String(expectedAST)}`));
896
+ if (allErrors) {
897
+ es.push([stepKey++, e]);
898
+ continue;
899
+ } else {
900
+ return Either.left(new Composite(ast, input, e, output));
901
+ }
902
+ } else {
903
+ // preserve key
904
+ output[key] = input[key];
905
+ }
906
+ }
907
+ }
908
+ }
909
+ let queue = undefined;
910
+ const isExact = options?.exact === true;
911
+ for (let i = 0; i < propertySignatures.length; i++) {
912
+ const ps = propertySignatures[i][1];
913
+ const name = ps.name;
914
+ const hasKey = Object.prototype.hasOwnProperty.call(input, name);
915
+ if (!hasKey) {
916
+ if (ps.isOptional) {
917
+ continue;
918
+ } else if (isExact) {
919
+ const e = new Pointer(name, input, new Missing(ps));
920
+ if (allErrors) {
921
+ es.push([stepKey++, e]);
922
+ continue;
923
+ } else {
924
+ return Either.left(new Composite(ast, input, e, output));
925
+ }
926
+ }
927
+ }
928
+ const parser = propertySignatures[i][0];
929
+ const te = parser(input[name], options);
930
+ const eu = eitherOrUndefined(te);
931
+ if (eu) {
932
+ if (Either.isLeft(eu)) {
933
+ const e = new Pointer(name, input, hasKey ? eu.left : new Missing(ps));
934
+ if (allErrors) {
935
+ es.push([stepKey++, e]);
936
+ continue;
937
+ } else {
938
+ return Either.left(new Composite(ast, input, e, output));
939
+ }
940
+ }
941
+ output[name] = eu.right;
942
+ } else {
943
+ const nk = stepKey++;
944
+ const index = name;
945
+ if (!queue) {
946
+ queue = [];
947
+ }
948
+ queue.push(({
949
+ es,
950
+ output
951
+ }) => Effect.flatMap(Effect.either(te), t => {
952
+ if (Either.isLeft(t)) {
953
+ const e = new Pointer(index, input, hasKey ? t.left : new Missing(ps));
954
+ if (allErrors) {
955
+ es.push([nk, e]);
956
+ return Effect.void;
957
+ } else {
958
+ return Either.left(new Composite(ast, input, e, output));
959
+ }
960
+ }
961
+ output[index] = t.right;
962
+ return Effect.void;
963
+ }));
964
+ }
965
+ }
966
+ // ---------------------------------------------
967
+ // handle index signatures
968
+ // ---------------------------------------------
969
+ for (let i = 0; i < indexSignatures.length; i++) {
970
+ const indexSignature = indexSignatures[i];
971
+ const parameter = indexSignature[0];
972
+ const type = indexSignature[1];
973
+ const keys = util_.getKeysForIndexSignature(input, indexSignature[2]);
974
+ for (const key of keys) {
975
+ // ---------------------------------------------
976
+ // handle keys
977
+ // ---------------------------------------------
978
+ const keu = eitherOrUndefined(parameter(key, options));
979
+ if (keu && Either.isRight(keu)) {
980
+ // ---------------------------------------------
981
+ // handle values
982
+ // ---------------------------------------------
983
+ const vpr = type(input[key], options);
984
+ const veu = eitherOrUndefined(vpr);
985
+ if (veu) {
986
+ if (Either.isLeft(veu)) {
987
+ const e = new Pointer(key, input, veu.left);
988
+ if (allErrors) {
989
+ es.push([stepKey++, e]);
990
+ continue;
991
+ } else {
992
+ return Either.left(new Composite(ast, input, e, output));
993
+ }
994
+ } else {
995
+ if (!Object.prototype.hasOwnProperty.call(expectedKeysMap, key)) {
996
+ output[key] = veu.right;
997
+ }
998
+ }
999
+ } else {
1000
+ const nk = stepKey++;
1001
+ const index = key;
1002
+ if (!queue) {
1003
+ queue = [];
1004
+ }
1005
+ queue.push(({
1006
+ es,
1007
+ output
1008
+ }) => Effect.flatMap(Effect.either(vpr), tv => {
1009
+ if (Either.isLeft(tv)) {
1010
+ const e = new Pointer(index, input, tv.left);
1011
+ if (allErrors) {
1012
+ es.push([nk, e]);
1013
+ return Effect.void;
1014
+ } else {
1015
+ return Either.left(new Composite(ast, input, e, output));
1016
+ }
1017
+ } else {
1018
+ if (!Object.prototype.hasOwnProperty.call(expectedKeysMap, key)) {
1019
+ output[key] = tv.right;
1020
+ }
1021
+ return Effect.void;
1022
+ }
1023
+ }));
1024
+ }
1025
+ }
1026
+ }
1027
+ }
1028
+ // ---------------------------------------------
1029
+ // compute result
1030
+ // ---------------------------------------------
1031
+ const computeResult = ({
1032
+ es,
1033
+ output
1034
+ }) => {
1035
+ if (array_.isNonEmptyArray(es)) {
1036
+ return Either.left(new Composite(ast, input, sortByIndex(es), output));
1037
+ }
1038
+ if (options?.propertyOrder === "original") {
1039
+ // preserve input keys order
1040
+ const keys = inputKeys || util_.ownKeys(input);
1041
+ for (const name of expectedKeys) {
1042
+ if (keys.indexOf(name) === -1) {
1043
+ keys.push(name);
1044
+ }
1045
+ }
1046
+ const out = {};
1047
+ for (const key of keys) {
1048
+ if (Object.prototype.hasOwnProperty.call(output, key)) {
1049
+ out[key] = output[key];
1050
+ }
1051
+ }
1052
+ return Either.right(out);
1053
+ }
1054
+ return Either.right(output);
1055
+ };
1056
+ if (queue && queue.length > 0) {
1057
+ const cqueue = queue;
1058
+ return Effect.suspend(() => {
1059
+ const state = {
1060
+ es: array_.copy(es),
1061
+ output: Object.assign({}, output)
1062
+ };
1063
+ return Effect.flatMap(Effect.forEach(cqueue, f => f(state), {
1064
+ concurrency,
1065
+ batching,
1066
+ discard: true
1067
+ }), () => computeResult(state));
1068
+ });
1069
+ }
1070
+ return computeResult({
1071
+ es,
1072
+ output
1073
+ });
1074
+ };
1075
+ }
1076
+ case "Union":
1077
+ {
1078
+ const searchTree = getSearchTree(ast.types, isDecoding);
1079
+ const ownKeys = util_.ownKeys(searchTree.keys);
1080
+ const len = ownKeys.length;
1081
+ const map = new Map();
1082
+ for (let i = 0; i < ast.types.length; i++) {
1083
+ map.set(ast.types[i], goMemo(ast.types[i], isDecoding));
1084
+ }
1085
+ const concurrency = getConcurrency(ast) ?? 1;
1086
+ const batching = getBatching(ast);
1087
+ return (input, options) => {
1088
+ const es = [];
1089
+ let stepKey = 0;
1090
+ let candidates = [];
1091
+ if (len > 0) {
1092
+ if (Predicate.isRecordOrArray(input)) {
1093
+ for (let i = 0; i < len; i++) {
1094
+ const name = ownKeys[i];
1095
+ const buckets = searchTree.keys[name].buckets;
1096
+ // for each property that should contain a literal, check if the input contains that property
1097
+ if (Object.prototype.hasOwnProperty.call(input, name)) {
1098
+ const literal = String(input[name]);
1099
+ // check that the value obtained from the input for the property corresponds to an existing bucket
1100
+ if (Object.prototype.hasOwnProperty.call(buckets, literal)) {
1101
+ // retrive the minimal set of candidates for decoding
1102
+ candidates = candidates.concat(buckets[literal]);
1103
+ } else {
1104
+ const literals = AST.Union.make(searchTree.keys[name].literals);
1105
+ es.push([stepKey++, new Composite(new AST.TypeLiteral([new AST.PropertySignature(name, literals, false, true)], []), input, new Pointer(name, input, new Type(literals, input[name])))]);
1106
+ }
1107
+ } else {
1108
+ const literals = AST.Union.make(searchTree.keys[name].literals);
1109
+ const fakeps = new AST.PropertySignature(name, literals, false, true);
1110
+ es.push([stepKey++, new Composite(new AST.TypeLiteral([fakeps], []), input, new Pointer(name, input, new Missing(fakeps)))]);
1111
+ }
1112
+ }
1113
+ } else {
1114
+ es.push([stepKey++, new Type(ast, input)]);
1115
+ }
1116
+ }
1117
+ if (searchTree.otherwise.length > 0) {
1118
+ candidates = candidates.concat(searchTree.otherwise);
1119
+ }
1120
+ let queue = undefined;
1121
+ for (let i = 0; i < candidates.length; i++) {
1122
+ const candidate = candidates[i];
1123
+ const pr = map.get(candidate)(input, options);
1124
+ // the members of a union are ordered based on which one should be decoded first,
1125
+ // therefore if one member has added a task, all subsequent members must
1126
+ // also add a task to the queue even if they are synchronous
1127
+ const eu = !queue || queue.length === 0 ? eitherOrUndefined(pr) : undefined;
1128
+ if (eu) {
1129
+ if (Either.isRight(eu)) {
1130
+ return eu;
1131
+ } else {
1132
+ es.push([stepKey++, eu.left]);
1133
+ }
1134
+ } else {
1135
+ const nk = stepKey++;
1136
+ if (!queue) {
1137
+ queue = [];
1138
+ }
1139
+ queue.push(state => Effect.suspend(() => {
1140
+ if ("finalResult" in state) {
1141
+ return Effect.void;
1142
+ } else {
1143
+ return Effect.flatMap(Effect.either(pr), t => {
1144
+ if (Either.isRight(t)) {
1145
+ state.finalResult = t;
1146
+ } else {
1147
+ state.es.push([nk, t.left]);
1148
+ }
1149
+ return Effect.void;
1150
+ });
1151
+ }
1152
+ }));
1153
+ }
1154
+ }
1155
+ // ---------------------------------------------
1156
+ // compute result
1157
+ // ---------------------------------------------
1158
+ const computeResult = es => array_.isNonEmptyArray(es) ? es.length === 1 && es[0][1]._tag === "Type" ? Either.left(es[0][1]) : Either.left(new Composite(ast, input, sortByIndex(es))) :
1159
+ // this should never happen
1160
+ Either.left(new Type(ast, input));
1161
+ if (queue && queue.length > 0) {
1162
+ const cqueue = queue;
1163
+ return Effect.suspend(() => {
1164
+ const state = {
1165
+ es: array_.copy(es)
1166
+ };
1167
+ return Effect.flatMap(Effect.forEach(cqueue, f => f(state), {
1168
+ concurrency,
1169
+ batching,
1170
+ discard: true
1171
+ }), () => {
1172
+ if ("finalResult" in state) {
1173
+ return state.finalResult;
1174
+ }
1175
+ return computeResult(state.es);
1176
+ });
1177
+ });
1178
+ }
1179
+ return computeResult(es);
1180
+ };
1181
+ }
1182
+ case "Suspend":
1183
+ {
1184
+ const get = util_.memoizeThunk(() => goMemo(AST.annotations(ast.f(), ast.annotations), isDecoding));
1185
+ return (a, options) => get()(a, options);
1186
+ }
1187
+ }
1188
+ };
1189
+ const fromRefinement = (ast, refinement) => u => refinement(u) ? Either.right(u) : Either.left(new Type(ast, u));
1190
+ /** @internal */
1191
+ export const getLiterals = (ast, isDecoding) => {
1192
+ switch (ast._tag) {
1193
+ case "Declaration":
1194
+ {
1195
+ const annotation = AST.getSurrogateAnnotation(ast);
1196
+ if (Option.isSome(annotation)) {
1197
+ return getLiterals(annotation.value, isDecoding);
1198
+ }
1199
+ break;
1200
+ }
1201
+ case "TypeLiteral":
1202
+ {
1203
+ const out = [];
1204
+ for (let i = 0; i < ast.propertySignatures.length; i++) {
1205
+ const propertySignature = ast.propertySignatures[i];
1206
+ const type = isDecoding ? AST.encodedAST(propertySignature.type) : AST.typeAST(propertySignature.type);
1207
+ if (AST.isLiteral(type) && !propertySignature.isOptional) {
1208
+ out.push([propertySignature.name, type]);
1209
+ }
1210
+ }
1211
+ return out;
1212
+ }
1213
+ case "TupleType":
1214
+ {
1215
+ const out = [];
1216
+ for (let i = 0; i < ast.elements.length; i++) {
1217
+ const element = ast.elements[i];
1218
+ const type = isDecoding ? AST.encodedAST(element.type) : AST.typeAST(element.type);
1219
+ if (AST.isLiteral(type) && !element.isOptional) {
1220
+ out.push([i, type]);
1221
+ }
1222
+ }
1223
+ return out;
1224
+ }
1225
+ case "Refinement":
1226
+ return getLiterals(ast.from, isDecoding);
1227
+ case "Suspend":
1228
+ return getLiterals(ast.f(), isDecoding);
1229
+ case "Transformation":
1230
+ return getLiterals(isDecoding ? ast.from : ast.to, isDecoding);
1231
+ }
1232
+ return [];
1233
+ };
1234
+ /**
1235
+ * The purpose of the algorithm is to narrow down the pool of possible candidates for decoding as much as possible.
1236
+ *
1237
+ * This function separates the schemas into two groups, `keys` and `otherwise`:
1238
+ *
1239
+ * - `keys`: the schema has at least one property with a literal value
1240
+ * - `otherwise`: the schema has no properties with a literal value
1241
+ *
1242
+ * If a schema has at least one property with a literal value, so it ends up in `keys`, first a namespace is created for
1243
+ * the name of the property containing the literal, and then within this namespace a "bucket" is created for the literal
1244
+ * value in which to store all the schemas that have the same property and literal value.
1245
+ *
1246
+ * @internal
1247
+ */
1248
+ export const getSearchTree = (members, isDecoding) => {
1249
+ const keys = {};
1250
+ const otherwise = [];
1251
+ for (let i = 0; i < members.length; i++) {
1252
+ const member = members[i];
1253
+ const tags = getLiterals(member, isDecoding);
1254
+ if (tags.length > 0) {
1255
+ for (let j = 0; j < tags.length; j++) {
1256
+ const [key, literal] = tags[j];
1257
+ const hash = String(literal.literal);
1258
+ keys[key] = keys[key] || {
1259
+ buckets: {},
1260
+ literals: []
1261
+ };
1262
+ const buckets = keys[key].buckets;
1263
+ if (Object.prototype.hasOwnProperty.call(buckets, hash)) {
1264
+ if (j < tags.length - 1) {
1265
+ continue;
1266
+ }
1267
+ buckets[hash].push(member);
1268
+ keys[key].literals.push(literal);
1269
+ } else {
1270
+ buckets[hash] = [member];
1271
+ keys[key].literals.push(literal);
1272
+ break;
1273
+ }
1274
+ }
1275
+ } else {
1276
+ otherwise.push(member);
1277
+ }
1278
+ }
1279
+ return {
1280
+ keys,
1281
+ otherwise
1282
+ };
1283
+ };
1284
+ const dropRightRefinement = ast => AST.isRefinement(ast) ? dropRightRefinement(ast.from) : ast;
1285
+ const handleForbidden = (effect, ast, actual, options) => {
1286
+ const eu = eitherOrUndefined(effect);
1287
+ if (eu) {
1288
+ return eu;
1289
+ }
1290
+ if (options?.isEffectAllowed === true) {
1291
+ return effect;
1292
+ }
1293
+ try {
1294
+ return Effect.runSync(Effect.either(effect));
1295
+ } catch (e) {
1296
+ return Either.left(new Forbidden(ast, actual, "cannot be be resolved synchronously, this is caused by using runSync on an effect that performs async work"));
1297
+ }
1298
+ };
1299
+ const compare = ([a], [b]) => a > b ? 1 : a < b ? -1 : 0;
1300
+ function sortByIndex(es) {
1301
+ return es.sort(compare).map(t => t[1]);
1302
+ }
1303
+ // -------------------------------------------------------------------------------------
1304
+ // transformations interpreter
1305
+ // -------------------------------------------------------------------------------------
1306
+ /** @internal */
1307
+ export const getFinalTransformation = (transformation, isDecoding) => {
1308
+ switch (transformation._tag) {
1309
+ case "FinalTransformation":
1310
+ return isDecoding ? transformation.decode : transformation.encode;
1311
+ case "ComposeTransformation":
1312
+ return Either.right;
1313
+ case "TypeLiteralTransformation":
1314
+ return input => {
1315
+ let out = Either.right(input);
1316
+ // ---------------------------------------------
1317
+ // handle property signature transformations
1318
+ // ---------------------------------------------
1319
+ for (const pst of transformation.propertySignatureTransformations) {
1320
+ const [from, to] = isDecoding ? [pst.from, pst.to] : [pst.to, pst.from];
1321
+ const transformation = isDecoding ? pst.decode : pst.encode;
1322
+ const f = input => {
1323
+ const o = transformation(Object.prototype.hasOwnProperty.call(input, from) ? Option.some(input[from]) : Option.none());
1324
+ delete input[from];
1325
+ if (Option.isSome(o)) {
1326
+ input[to] = o.value;
1327
+ }
1328
+ return input;
1329
+ };
1330
+ out = map(out, f);
1331
+ }
1332
+ return out;
1333
+ };
1334
+ }
1335
+ };
1336
+ const makeTree = (value, forest = []) => ({
1337
+ value,
1338
+ forest
1339
+ });
1340
+ /**
1341
+ * @category formatting
1342
+ * @since 3.10.0
1343
+ */
1344
+ export const TreeFormatter = {
1345
+ formatIssue: issue => Effect.map(formatTree(issue), drawTree),
1346
+ formatIssueSync: issue => Effect.runSync(TreeFormatter.formatIssue(issue)),
1347
+ formatError: error => TreeFormatter.formatIssue(error.issue),
1348
+ formatErrorSync: error => TreeFormatter.formatIssueSync(error.issue)
1349
+ };
1350
+ const drawTree = tree => tree.value + draw("\n", tree.forest);
1351
+ const draw = (indentation, forest) => {
1352
+ let r = "";
1353
+ const len = forest.length;
1354
+ let tree;
1355
+ for (let i = 0; i < len; i++) {
1356
+ tree = forest[i];
1357
+ const isLast = i === len - 1;
1358
+ r += indentation + (isLast ? "└" : "├") + "─ " + tree.value;
1359
+ r += draw(indentation + (len > 1 && !isLast ? "│ " : " "), tree.forest);
1360
+ }
1361
+ return r;
1362
+ };
1363
+ const formatTransformationKind = kind => {
1364
+ switch (kind) {
1365
+ case "Encoded":
1366
+ return "Encoded side transformation failure";
1367
+ case "Transformation":
1368
+ return "Transformation process failure";
1369
+ case "Type":
1370
+ return "Type side transformation failure";
1371
+ }
1372
+ };
1373
+ const formatRefinementKind = kind => {
1374
+ switch (kind) {
1375
+ case "From":
1376
+ return "From side refinement failure";
1377
+ case "Predicate":
1378
+ return "Predicate refinement failure";
1379
+ }
1380
+ };
1381
+ const getAnnotated = issue => "ast" in issue ? Option.some(issue.ast) : Option.none();
1382
+ const getCurrentMessage = issue => getAnnotated(issue).pipe(Option.flatMap(AST.getMessageAnnotation), Effect.flatMap(annotation => {
1383
+ const out = annotation(issue);
1384
+ return Predicate.isString(out) ? Effect.succeed({
1385
+ message: out,
1386
+ override: false
1387
+ }) : Effect.isEffect(out) ? Effect.map(out, message => ({
1388
+ message,
1389
+ override: false
1390
+ })) : Predicate.isString(out.message) ? Effect.succeed({
1391
+ message: out.message,
1392
+ override: out.override
1393
+ }) : Effect.map(out.message, message => ({
1394
+ message,
1395
+ override: out.override
1396
+ }));
1397
+ }));
1398
+ const createParseIssueGuard = tag => issue => issue._tag === tag;
1399
+ /**
1400
+ * Returns `true` if the value is a `Composite`.
1401
+ *
1402
+ * @category guards
1403
+ * @since 3.10.0
1404
+ */
1405
+ export const isComposite = /*#__PURE__*/createParseIssueGuard("Composite");
1406
+ const isRefinement = /*#__PURE__*/createParseIssueGuard("Refinement");
1407
+ const isTransformation = /*#__PURE__*/createParseIssueGuard("Transformation");
1408
+ const getMessage = issue => getCurrentMessage(issue).pipe(Effect.flatMap(currentMessage => {
1409
+ const useInnerMessage = !currentMessage.override && (isComposite(issue) || isRefinement(issue) && issue.kind === "From" || isTransformation(issue) && issue.kind !== "Transformation");
1410
+ return useInnerMessage ? isTransformation(issue) || isRefinement(issue) ? getMessage(issue.issue) : Option.none() : Effect.succeed(currentMessage.message);
1411
+ }));
1412
+ const getParseIssueTitleAnnotation = issue => getAnnotated(issue).pipe(Option.flatMap(AST.getParseIssueTitleAnnotation), Option.filterMap(annotation => Option.fromNullable(annotation(issue))));
1413
+ const formatTypeMessage = e => getMessage(e).pipe(Effect.orElse(() => getParseIssueTitleAnnotation(e)), Effect.catchAll(() => Effect.succeed(e.message ?? `Expected ${String(e.ast)}, actual ${util_.formatUnknown(e.actual)}`)));
1414
+ const getParseIssueTitle = issue => Option.getOrElse(getParseIssueTitleAnnotation(issue), () => String(issue.ast));
1415
+ const formatForbiddenMessage = e => e.message ?? "is forbidden";
1416
+ const formatUnexpectedMessage = e => e.message ?? "is unexpected";
1417
+ const formatMissingMessage = e => AST.getMissingMessageAnnotation(e.ast).pipe(Effect.flatMap(annotation => {
1418
+ const out = annotation();
1419
+ return Predicate.isString(out) ? Effect.succeed(out) : out;
1420
+ }), Effect.catchAll(() => Effect.succeed(e.message ?? "is missing")));
1421
+ const getTree = (issue, onFailure) => Effect.matchEffect(getMessage(issue), {
1422
+ onFailure,
1423
+ onSuccess: message => Effect.succeed(makeTree(message))
1424
+ });
1425
+ const formatTree = e => {
1426
+ switch (e._tag) {
1427
+ case "Type":
1428
+ return Effect.map(formatTypeMessage(e), makeTree);
1429
+ case "Forbidden":
1430
+ return Effect.succeed(makeTree(getParseIssueTitle(e), [makeTree(formatForbiddenMessage(e))]));
1431
+ case "Unexpected":
1432
+ return Effect.succeed(makeTree(formatUnexpectedMessage(e)));
1433
+ case "Missing":
1434
+ return Effect.map(formatMissingMessage(e), makeTree);
1435
+ case "Transformation":
1436
+ return getTree(e, () => Effect.map(formatTree(e.issue), tree => makeTree(getParseIssueTitle(e), [makeTree(formatTransformationKind(e.kind), [tree])])));
1437
+ case "Refinement":
1438
+ return getTree(e, () => Effect.map(formatTree(e.issue), tree => makeTree(getParseIssueTitle(e), [makeTree(formatRefinementKind(e.kind), [tree])])));
1439
+ case "Pointer":
1440
+ return Effect.map(formatTree(e.issue), tree => makeTree(util_.formatPath(e.path), [tree]));
1441
+ case "Composite":
1442
+ {
1443
+ const parseIssueTitle = getParseIssueTitle(e);
1444
+ return getTree(e, () => util_.isNonEmpty(e.issues) ? Effect.map(Effect.forEach(e.issues, formatTree), forest => makeTree(parseIssueTitle, forest)) : Effect.map(formatTree(e.issues), tree => makeTree(parseIssueTitle, [tree])));
1445
+ }
1446
+ }
1447
+ };
1448
+ /**
1449
+ * @category formatting
1450
+ * @since 3.10.0
1451
+ */
1452
+ export const ArrayFormatter = {
1453
+ formatIssue: issue => formatArray(issue),
1454
+ formatIssueSync: issue => Effect.runSync(ArrayFormatter.formatIssue(issue)),
1455
+ formatError: error => ArrayFormatter.formatIssue(error.issue),
1456
+ formatErrorSync: error => ArrayFormatter.formatIssueSync(error.issue)
1457
+ };
1458
+ const succeedArrayFormatterIssue = issue => Effect.succeed([issue]);
1459
+ const getArray = (issue, path, onFailure) => Effect.matchEffect(getMessage(issue), {
1460
+ onFailure,
1461
+ onSuccess: message => succeedArrayFormatterIssue({
1462
+ _tag: issue._tag,
1463
+ path,
1464
+ message
1465
+ })
1466
+ });
1467
+ const formatArray = (e, path = []) => {
1468
+ const _tag = e._tag;
1469
+ switch (_tag) {
1470
+ case "Type":
1471
+ return Effect.map(formatTypeMessage(e), message => [{
1472
+ _tag,
1473
+ path,
1474
+ message
1475
+ }]);
1476
+ case "Forbidden":
1477
+ return succeedArrayFormatterIssue({
1478
+ _tag,
1479
+ path,
1480
+ message: formatForbiddenMessage(e)
1481
+ });
1482
+ case "Unexpected":
1483
+ return succeedArrayFormatterIssue({
1484
+ _tag,
1485
+ path,
1486
+ message: formatUnexpectedMessage(e)
1487
+ });
1488
+ case "Missing":
1489
+ return Effect.map(formatMissingMessage(e), message => [{
1490
+ _tag,
1491
+ path,
1492
+ message
1493
+ }]);
1494
+ case "Pointer":
1495
+ return formatArray(e.issue, path.concat(e.path));
1496
+ case "Composite":
1497
+ return getArray(e, path, () => util_.isNonEmpty(e.issues) ? Effect.map(Effect.forEach(e.issues, issue => formatArray(issue, path)), array_.flatten) : formatArray(e.issues, path));
1498
+ case "Refinement":
1499
+ case "Transformation":
1500
+ return getArray(e, path, () => formatArray(e.issue, path));
1501
+ }
1502
+ };
1503
+ //# sourceMappingURL=ParseResult.js.map