effect 4.0.0-beta.50 → 4.0.0-beta.52

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 (135) hide show
  1. package/dist/BigDecimal.d.ts.map +1 -1
  2. package/dist/BigDecimal.js +18 -14
  3. package/dist/BigDecimal.js.map +1 -1
  4. package/dist/BigInt.d.ts.map +1 -1
  5. package/dist/BigInt.js +4 -4
  6. package/dist/BigInt.js.map +1 -1
  7. package/dist/Brand.d.ts +2 -4
  8. package/dist/Brand.d.ts.map +1 -1
  9. package/dist/Brand.js.map +1 -1
  10. package/dist/Data.js +2 -2
  11. package/dist/Data.js.map +1 -1
  12. package/dist/Duration.js +1 -1
  13. package/dist/Duration.js.map +1 -1
  14. package/dist/Schema.d.ts +77 -10
  15. package/dist/Schema.d.ts.map +1 -1
  16. package/dist/Schema.js +119 -18
  17. package/dist/Schema.js.map +1 -1
  18. package/dist/SchemaAST.d.ts +6 -0
  19. package/dist/SchemaAST.d.ts.map +1 -1
  20. package/dist/SchemaAST.js +216 -229
  21. package/dist/SchemaAST.js.map +1 -1
  22. package/dist/SchemaGetter.d.ts +3 -5
  23. package/dist/SchemaGetter.d.ts.map +1 -1
  24. package/dist/SchemaGetter.js +3 -2
  25. package/dist/SchemaGetter.js.map +1 -1
  26. package/dist/SchemaIssue.d.ts.map +1 -1
  27. package/dist/SchemaIssue.js +29 -11
  28. package/dist/SchemaIssue.js.map +1 -1
  29. package/dist/SchemaParser.js +14 -2
  30. package/dist/SchemaParser.js.map +1 -1
  31. package/dist/index.d.ts +3 -0
  32. package/dist/index.d.ts.map +1 -1
  33. package/dist/index.js +3 -0
  34. package/dist/index.js.map +1 -1
  35. package/dist/internal/effect.js +142 -65
  36. package/dist/internal/effect.js.map +1 -1
  37. package/dist/internal/schema/representation.js +1 -2
  38. package/dist/internal/schema/representation.js.map +1 -1
  39. package/dist/internal/schema/schema.d.ts.map +1 -1
  40. package/dist/internal/schema/schema.js +37 -0
  41. package/dist/internal/schema/schema.js.map +1 -1
  42. package/dist/unstable/ai/McpServer.d.ts.map +1 -1
  43. package/dist/unstable/ai/McpServer.js +7 -0
  44. package/dist/unstable/ai/McpServer.js.map +1 -1
  45. package/dist/unstable/cli/index.d.ts +2 -0
  46. package/dist/unstable/cli/index.d.ts.map +1 -1
  47. package/dist/unstable/cli/index.js +2 -0
  48. package/dist/unstable/cli/index.js.map +1 -1
  49. package/dist/unstable/cluster/Runners.d.ts.map +1 -1
  50. package/dist/unstable/cluster/Runners.js +3 -2
  51. package/dist/unstable/cluster/Runners.js.map +1 -1
  52. package/dist/unstable/cluster/SqlMessageStorage.d.ts.map +1 -1
  53. package/dist/unstable/cluster/SqlMessageStorage.js +1 -0
  54. package/dist/unstable/cluster/SqlMessageStorage.js.map +1 -1
  55. package/dist/unstable/cluster/SqlRunnerStorage.d.ts.map +1 -1
  56. package/dist/unstable/cluster/SqlRunnerStorage.js +6 -6
  57. package/dist/unstable/cluster/SqlRunnerStorage.js.map +1 -1
  58. package/dist/unstable/eventlog/SqlEventJournal.d.ts.map +1 -1
  59. package/dist/unstable/eventlog/SqlEventJournal.js +9 -8
  60. package/dist/unstable/eventlog/SqlEventJournal.js.map +1 -1
  61. package/dist/unstable/eventlog/SqlEventLogServerEncrypted.d.ts.map +1 -1
  62. package/dist/unstable/eventlog/SqlEventLogServerEncrypted.js +6 -5
  63. package/dist/unstable/eventlog/SqlEventLogServerEncrypted.js.map +1 -1
  64. package/dist/unstable/eventlog/SqlEventLogServerUnencrypted.d.ts.map +1 -1
  65. package/dist/unstable/eventlog/SqlEventLogServerUnencrypted.js +6 -5
  66. package/dist/unstable/eventlog/SqlEventLogServerUnencrypted.js.map +1 -1
  67. package/dist/unstable/httpapi/HttpApi.js.map +1 -1
  68. package/dist/unstable/httpapi/HttpApiBuilder.d.ts.map +1 -1
  69. package/dist/unstable/httpapi/HttpApiBuilder.js +11 -6
  70. package/dist/unstable/httpapi/HttpApiBuilder.js.map +1 -1
  71. package/dist/unstable/httpapi/HttpApiClient.d.ts +5 -6
  72. package/dist/unstable/httpapi/HttpApiClient.d.ts.map +1 -1
  73. package/dist/unstable/httpapi/HttpApiClient.js.map +1 -1
  74. package/dist/unstable/httpapi/HttpApiEndpoint.d.ts +17 -18
  75. package/dist/unstable/httpapi/HttpApiEndpoint.d.ts.map +1 -1
  76. package/dist/unstable/httpapi/HttpApiEndpoint.js +1 -2
  77. package/dist/unstable/httpapi/HttpApiEndpoint.js.map +1 -1
  78. package/dist/unstable/httpapi/HttpApiError.d.ts +28 -8
  79. package/dist/unstable/httpapi/HttpApiError.d.ts.map +1 -1
  80. package/dist/unstable/httpapi/HttpApiError.js +28 -15
  81. package/dist/unstable/httpapi/HttpApiError.js.map +1 -1
  82. package/dist/unstable/httpapi/HttpApiMiddleware.d.ts +4 -3
  83. package/dist/unstable/httpapi/HttpApiMiddleware.d.ts.map +1 -1
  84. package/dist/unstable/httpapi/HttpApiMiddleware.js +2 -2
  85. package/dist/unstable/httpapi/HttpApiMiddleware.js.map +1 -1
  86. package/dist/unstable/httpapi/OpenApi.d.ts +1 -10
  87. package/dist/unstable/httpapi/OpenApi.d.ts.map +1 -1
  88. package/dist/unstable/httpapi/OpenApi.js +2 -11
  89. package/dist/unstable/httpapi/OpenApi.js.map +1 -1
  90. package/dist/unstable/observability/OtlpMetrics.js +1 -1
  91. package/dist/unstable/observability/OtlpMetrics.js.map +1 -1
  92. package/dist/unstable/observability/internal/protobuf.js +4 -4
  93. package/dist/unstable/observability/internal/protobuf.js.map +1 -1
  94. package/dist/unstable/rpc/RpcSerialization.d.ts +11 -0
  95. package/dist/unstable/rpc/RpcSerialization.d.ts.map +1 -1
  96. package/dist/unstable/rpc/RpcSerialization.js +14 -9
  97. package/dist/unstable/rpc/RpcSerialization.js.map +1 -1
  98. package/package.json +1 -1
  99. package/src/BigDecimal.ts +20 -16
  100. package/src/BigInt.ts +4 -4
  101. package/src/Brand.ts +2 -4
  102. package/src/Data.ts +1 -1
  103. package/src/Duration.ts +1 -1
  104. package/src/Schema.ts +183 -23
  105. package/src/SchemaAST.ts +315 -267
  106. package/src/SchemaGetter.ts +4 -6
  107. package/src/SchemaIssue.ts +28 -15
  108. package/src/SchemaParser.ts +8 -2
  109. package/src/index.ts +3 -0
  110. package/src/internal/effect.ts +196 -69
  111. package/src/internal/schema/representation.ts +1 -2
  112. package/src/internal/schema/schema.ts +43 -0
  113. package/src/unstable/ai/McpServer.ts +8 -0
  114. package/src/unstable/cli/index.ts +2 -0
  115. package/src/unstable/cluster/Runners.ts +8 -5
  116. package/src/unstable/cluster/SqlMessageStorage.ts +1 -0
  117. package/src/unstable/cluster/SqlRunnerStorage.ts +12 -6
  118. package/src/unstable/eventlog/SqlEventJournal.ts +10 -2
  119. package/src/unstable/eventlog/SqlEventLogServerEncrypted.ts +8 -3
  120. package/src/unstable/eventlog/SqlEventLogServerUnencrypted.ts +9 -3
  121. package/src/unstable/httpapi/HttpApi.ts +1 -1
  122. package/src/unstable/httpapi/HttpApiBuilder.ts +13 -6
  123. package/src/unstable/httpapi/HttpApiClient.ts +5 -6
  124. package/src/unstable/httpapi/HttpApiEndpoint.ts +6 -7
  125. package/src/unstable/httpapi/HttpApiError.ts +43 -22
  126. package/src/unstable/httpapi/HttpApiMiddleware.ts +6 -5
  127. package/src/unstable/httpapi/OpenApi.ts +3 -15
  128. package/src/unstable/observability/OtlpMetrics.ts +1 -1
  129. package/src/unstable/observability/internal/protobuf.ts +4 -4
  130. package/src/unstable/rpc/RpcSerialization.ts +41 -36
  131. package/dist/internal/schema/to-codec.d.ts +0 -2
  132. package/dist/internal/schema/to-codec.d.ts.map +0 -1
  133. package/dist/internal/schema/to-codec.js +0 -126
  134. package/dist/internal/schema/to-codec.js.map +0 -1
  135. package/src/internal/schema/to-codec.ts +0 -138
package/dist/Schema.js CHANGED
@@ -96,7 +96,7 @@ import * as Equal from "./Equal.js";
96
96
  import * as Equivalence from "./Equivalence.js";
97
97
  import * as Exit_ from "./Exit.js";
98
98
  import { format, formatPropertyKey } from "./Formatter.js";
99
- import { identity } from "./Function.js";
99
+ import { identity, memoize } from "./Function.js";
100
100
  import * as HashMap_ from "./HashMap.js";
101
101
  import * as HashSet_ from "./HashSet.js";
102
102
  import * as core from "./internal/core.js";
@@ -106,7 +106,6 @@ import * as InternalEquivalence from "./internal/schema/equivalence.js";
106
106
  import * as InternalStandard from "./internal/schema/representation.js";
107
107
  import * as InternalSchema from "./internal/schema/schema.js";
108
108
  import { SchemaError } from "./internal/schema/schema.js";
109
- import * as InternalToCodec from "./internal/schema/to-codec.js";
110
109
  import * as JsonPatch from "./JsonPatch.js";
111
110
  import * as JsonSchema from "./JsonSchema.js";
112
111
  import { remainder } from "./Number.js";
@@ -2441,7 +2440,6 @@ export function instanceOf(constructor, annotations) {
2441
2440
  * Used when building low-level AST transformations that bridge two schema types.
2442
2441
  *
2443
2442
  * @since 4.0.0
2444
- * @experimental
2445
2443
  */
2446
2444
  export function link() {
2447
2445
  return (encodeTo, transformation) => {
@@ -2454,18 +2452,47 @@ export function link() {
2454
2452
  /**
2455
2453
  * Creates a custom filter check from a predicate function. The predicate
2456
2454
  * receives the input value, the schema's AST, and parse options, and returns
2457
- * `true`/`undefined` on success or a failure description on error.
2455
+ * a value of type {@link FilterOutput}.
2456
+ *
2457
+ * **Example** (Failure at a nested path)
2458
2458
  *
2459
- * **Example** (Custom filter check)
2460
2459
  * ```ts
2461
2460
  * import { Schema } from "effect"
2462
2461
  *
2463
- * // Check that a number is even
2464
- * const isEven = Schema.makeFilter(
2465
- * (n: number) => n % 2 === 0 || "expected an even number"
2462
+ * const schema = Schema.Struct({ password: Schema.String, confirmPassword: Schema.String }).check(
2463
+ * Schema.makeFilter((o) =>
2464
+ * o.password === o.confirmPassword
2465
+ * ? undefined
2466
+ * : { path: ["password"], issue: "password and confirmPassword must match" }
2467
+ * )
2466
2468
  * )
2467
2469
  *
2468
- * const EvenNumber = Schema.Number.check(isEven)
2470
+ * console.log(String(Schema.decodeUnknownExit(schema)({ password: "123456", confirmPassword: "1234567" })))
2471
+ * // Failure(Cause([Fail(SchemaError: password and confirmPassword must match
2472
+ * // at ["password"])]))
2473
+ * ```
2474
+ *
2475
+ * **Example** (Reporting multiple failures at once)
2476
+ *
2477
+ * ```ts
2478
+ * import { Schema } from "effect"
2479
+ *
2480
+ * const schema = Schema.Struct({ a: Schema.Finite, b: Schema.Finite, c: Schema.Finite }).check(
2481
+ * Schema.makeFilter((o) => {
2482
+ * const issues: Array<Schema.FilterIssue> = []
2483
+ * if (o.a > 0) {
2484
+ * if (o.b <= 0) issues.push({ path: ["b"], issue: "b must be greater than 0" })
2485
+ * if (o.c <= 0) issues.push({ path: ["c"], issue: "c must be greater than 0" })
2486
+ * }
2487
+ * return issues
2488
+ * })
2489
+ * )
2490
+ *
2491
+ * console.log(String(Schema.decodeUnknownExit(schema)({ a: 1, b: 0, c: 0 })))
2492
+ * // Failure(Cause([Fail(SchemaError: b must be greater than 0
2493
+ * // at ["b"]
2494
+ * // c must be greater than 0
2495
+ * // at ["c"])]))
2469
2496
  * ```
2470
2497
  *
2471
2498
  * @category Checks Constructors
@@ -5287,6 +5314,7 @@ export const Duration = /*#__PURE__*/declare(Duration_.isDuration, {
5287
5314
  toFormatter: () => globalThis.String,
5288
5315
  toEquivalence: () => Duration_.Equivalence
5289
5316
  });
5317
+ const bigint0 = /*#__PURE__*/globalThis.BigInt(0);
5290
5318
  /**
5291
5319
  * A transformation schema that decodes a non-negative `bigint` into a
5292
5320
  * `Duration`, treating the `bigint` value as the duration in nanoseconds.
@@ -5300,7 +5328,7 @@ export const Duration = /*#__PURE__*/declare(Duration_.isDuration, {
5300
5328
  * @category Duration
5301
5329
  * @since 4.0.0
5302
5330
  */
5303
- export const DurationFromNanos = /*#__PURE__*/BigInt.check(isGreaterThanOrEqualToBigInt(0n)).pipe(/*#__PURE__*/decodeTo(Duration, Transformation.durationFromNanos));
5331
+ export const DurationFromNanos = /*#__PURE__*/BigInt.check(isGreaterThanOrEqualToBigInt(bigint0)).pipe(/*#__PURE__*/decodeTo(Duration, Transformation.durationFromNanos));
5304
5332
  /**
5305
5333
  * A transformation schema that decodes a non-negative (possibly infinite)
5306
5334
  * integer into a `Duration`, treating the integer value as the duration in
@@ -6782,7 +6810,60 @@ export function toJsonSchemaDocument(schema, options) {
6782
6810
  * @since 4.0.0
6783
6811
  */
6784
6812
  export function toCodecJson(schema) {
6785
- return make(InternalToCodec.toCodecJson(schema.ast));
6813
+ return make(toCodecJsonTop(schema.ast));
6814
+ }
6815
+ const toCodecJsonTop = /*#__PURE__*/AST.toCodec(ast => {
6816
+ const out = toCodecJsonBase(ast, toCodecJsonTop);
6817
+ return out !== ast && AST.isOptional(ast) ? AST.optionalKeyLastLink(out) : out;
6818
+ });
6819
+ function toCodecJsonBase(ast, recur) {
6820
+ switch (ast._tag) {
6821
+ case "Declaration":
6822
+ {
6823
+ const getLink = ast.annotations?.toCodecJson ?? ast.annotations?.toCodec;
6824
+ if (Predicate.isFunction(getLink)) {
6825
+ const tps = AST.isDeclaration(ast) ? ast.typeParameters.map(tp => InternalSchema.make(AST.toEncoded(tp))) : [];
6826
+ const link = getLink(tps);
6827
+ const to = recur(link.to);
6828
+ return AST.replaceEncoding(ast, to === link.to ? [link] : [new AST.Link(to, link.transformation)]);
6829
+ }
6830
+ return AST.replaceEncoding(ast, [AST.unknownToNull]);
6831
+ }
6832
+ case "Unknown":
6833
+ case "ObjectKeyword":
6834
+ return AST.replaceEncoding(ast, [AST.unknownToJson]);
6835
+ case "Undefined":
6836
+ case "Void":
6837
+ case "Literal":
6838
+ case "Number":
6839
+ return ast.toCodecJson();
6840
+ case "UniqueSymbol":
6841
+ case "Symbol":
6842
+ case "BigInt":
6843
+ return ast.toCodecStringTree();
6844
+ case "Objects":
6845
+ {
6846
+ if (ast.propertySignatures.some(ps => typeof ps.name !== "string")) {
6847
+ throw new globalThis.Error("Objects property names must be strings", {
6848
+ cause: ast
6849
+ });
6850
+ }
6851
+ return ast.recur(recur);
6852
+ }
6853
+ case "Union":
6854
+ {
6855
+ const sortedTypes = InternalSchema.jsonReorder(ast.types);
6856
+ if (sortedTypes !== ast.types) {
6857
+ return new AST.Union(sortedTypes, ast.mode, ast.annotations, ast.checks, ast.encoding, ast.context).recur(recur);
6858
+ }
6859
+ return ast.recur(recur);
6860
+ }
6861
+ case "Arrays":
6862
+ case "Suspend":
6863
+ return ast.recur(recur);
6864
+ }
6865
+ // `Schema.Any` is used as an escape hatch
6866
+ return ast;
6786
6867
  }
6787
6868
  /**
6788
6869
  * Derives an isomorphism codec from a schema. The encoded form is the
@@ -6792,14 +6873,34 @@ export function toCodecJson(schema) {
6792
6873
  * @since 4.0.0
6793
6874
  */
6794
6875
  export function toCodecIso(schema) {
6795
- return make(InternalToCodec.toCodecIso(AST.toType(schema.ast)));
6876
+ return make(toCodecIsoTop(AST.toType(schema.ast)));
6796
6877
  }
6797
- export function toCodecStringTree(schema, options) {
6798
- if (options?.keepDeclarations === true) {
6799
- return make(toCodecEnsureArray(serializerStringTreeKeepDeclarations(schema.ast)));
6800
- } else {
6801
- return make(toCodecEnsureArray(serializerStringTree(schema.ast)));
6878
+ const toCodecIsoTop = /*#__PURE__*/memoize(ast => {
6879
+ const out = toCodecIsoBase(ast, toCodecIsoTop);
6880
+ return out !== ast && AST.isOptional(ast) ? AST.optionalKeyLastLink(out) : out;
6881
+ });
6882
+ function toCodecIsoBase(ast, recur) {
6883
+ switch (ast._tag) {
6884
+ case "Declaration":
6885
+ {
6886
+ const getLink = ast.annotations?.toCodecIso ?? ast.annotations?.toCodec;
6887
+ if (Predicate.isFunction(getLink)) {
6888
+ const link = getLink(ast.typeParameters.map(tp => InternalSchema.make(tp)));
6889
+ const to = recur(link.to);
6890
+ return AST.replaceEncoding(ast, to === link.to ? [link] : [new AST.Link(to, link.transformation)]);
6891
+ }
6892
+ return ast;
6893
+ }
6894
+ case "Arrays":
6895
+ case "Objects":
6896
+ case "Union":
6897
+ case "Suspend":
6898
+ return ast.recur(recur);
6802
6899
  }
6900
+ return ast;
6901
+ }
6902
+ export function toCodecStringTree(schema, options) {
6903
+ return make(toCodecEnsureArray(options?.keepDeclarations === true ? serializerStringTreeKeepDeclarations(schema.ast) : serializerStringTree(schema.ast)));
6803
6904
  }
6804
6905
  /**
6805
6906
  * Derives an XML encoder from a codec. Encodes a value to an XML string by
@@ -6919,7 +7020,7 @@ function getStringTreePriority(ast) {
6919
7020
  return 1;
6920
7021
  }
6921
7022
  }
6922
- const treeReorder = /*#__PURE__*/InternalToCodec.makeReorder(getStringTreePriority);
7023
+ const treeReorder = /*#__PURE__*/InternalSchema.makeReorder(getStringTreePriority);
6923
7024
  function serializerTree(ast, recur, onMissingAnnotation) {
6924
7025
  switch (ast._tag) {
6925
7026
  case "Declaration":