effect 4.0.0-beta.75 → 4.0.0-beta.76

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 (95) hide show
  1. package/dist/Schema.d.ts +102 -40
  2. package/dist/Schema.d.ts.map +1 -1
  3. package/dist/Schema.js +149 -78
  4. package/dist/Schema.js.map +1 -1
  5. package/dist/SchemaAST.d.ts.map +1 -1
  6. package/dist/SchemaAST.js +2 -1
  7. package/dist/SchemaAST.js.map +1 -1
  8. package/dist/SchemaRepresentation.d.ts +10 -1
  9. package/dist/SchemaRepresentation.d.ts.map +1 -1
  10. package/dist/SchemaRepresentation.js +12 -5
  11. package/dist/SchemaRepresentation.js.map +1 -1
  12. package/dist/SchemaTransformation.d.ts.map +1 -1
  13. package/dist/SchemaTransformation.js +57 -18
  14. package/dist/SchemaTransformation.js.map +1 -1
  15. package/dist/internal/schema/representation.js +1 -0
  16. package/dist/internal/schema/representation.js.map +1 -1
  17. package/dist/unstable/ai/McpSchema.js +1 -1
  18. package/dist/unstable/ai/McpSchema.js.map +1 -1
  19. package/dist/unstable/cli/CliError.js +1 -1
  20. package/dist/unstable/cli/CliError.js.map +1 -1
  21. package/dist/unstable/cluster/ClusterError.js +2 -2
  22. package/dist/unstable/cluster/ClusterError.js.map +1 -1
  23. package/dist/unstable/cluster/Reply.js +1 -1
  24. package/dist/unstable/cluster/Reply.js.map +1 -1
  25. package/dist/unstable/devtools/DevToolsSchema.js +5 -1
  26. package/dist/unstable/devtools/DevToolsSchema.js.map +1 -1
  27. package/dist/unstable/http/HttpClientError.js +1 -1
  28. package/dist/unstable/http/HttpClientError.js.map +1 -1
  29. package/dist/unstable/httpapi/HttpApiScalar.d.ts +2 -0
  30. package/dist/unstable/httpapi/HttpApiScalar.d.ts.map +1 -1
  31. package/dist/unstable/httpapi/HttpApiScalar.js +12 -6
  32. package/dist/unstable/httpapi/HttpApiScalar.js.map +1 -1
  33. package/dist/unstable/persistence/Persistable.js +1 -1
  34. package/dist/unstable/persistence/Persistable.js.map +1 -1
  35. package/dist/unstable/persistence/PersistedQueue.js +1 -1
  36. package/dist/unstable/persistence/PersistedQueue.js.map +1 -1
  37. package/dist/unstable/persistence/Persistence.js +1 -1
  38. package/dist/unstable/persistence/Persistence.js.map +1 -1
  39. package/dist/unstable/persistence/RateLimiter.js +1 -1
  40. package/dist/unstable/persistence/RateLimiter.js.map +1 -1
  41. package/dist/unstable/persistence/Redis.js +1 -1
  42. package/dist/unstable/persistence/Redis.js.map +1 -1
  43. package/dist/unstable/reactivity/AsyncResult.js +1 -1
  44. package/dist/unstable/reactivity/AsyncResult.js.map +1 -1
  45. package/dist/unstable/rpc/Rpc.d.ts +1 -1
  46. package/dist/unstable/rpc/Rpc.js +2 -2
  47. package/dist/unstable/rpc/Rpc.js.map +1 -1
  48. package/dist/unstable/rpc/RpcClient.js +1 -1
  49. package/dist/unstable/rpc/RpcClient.js.map +1 -1
  50. package/dist/unstable/rpc/RpcClientError.js +1 -1
  51. package/dist/unstable/rpc/RpcClientError.js.map +1 -1
  52. package/dist/unstable/rpc/RpcMessage.d.ts +2 -2
  53. package/dist/unstable/rpc/RpcMessage.js +3 -3
  54. package/dist/unstable/rpc/RpcMessage.js.map +1 -1
  55. package/dist/unstable/socket/Socket.js +3 -3
  56. package/dist/unstable/socket/Socket.js.map +1 -1
  57. package/dist/unstable/sql/SqlError.js +1 -1
  58. package/dist/unstable/sql/SqlError.js.map +1 -1
  59. package/dist/unstable/workers/WorkerError.js +4 -4
  60. package/dist/unstable/workers/WorkerError.js.map +1 -1
  61. package/dist/unstable/workflow/Activity.js +1 -1
  62. package/dist/unstable/workflow/Activity.js.map +1 -1
  63. package/dist/unstable/workflow/DurableDeferred.js +1 -1
  64. package/dist/unstable/workflow/DurableDeferred.js.map +1 -1
  65. package/dist/unstable/workflow/Workflow.js +2 -2
  66. package/dist/unstable/workflow/Workflow.js.map +1 -1
  67. package/package.json +1 -1
  68. package/src/Schema.ts +179 -117
  69. package/src/SchemaAST.ts +3 -1
  70. package/src/SchemaRepresentation.ts +12 -4
  71. package/src/SchemaTransformation.ts +75 -25
  72. package/src/internal/schema/representation.ts +1 -0
  73. package/src/unstable/ai/McpSchema.ts +1 -1
  74. package/src/unstable/cli/CliError.ts +1 -1
  75. package/src/unstable/cluster/ClusterError.ts +2 -2
  76. package/src/unstable/cluster/Reply.ts +1 -1
  77. package/src/unstable/devtools/DevToolsSchema.ts +1 -1
  78. package/src/unstable/http/HttpClientError.ts +1 -1
  79. package/src/unstable/httpapi/HttpApiScalar.ts +14 -7
  80. package/src/unstable/persistence/Persistable.ts +1 -1
  81. package/src/unstable/persistence/PersistedQueue.ts +1 -1
  82. package/src/unstable/persistence/Persistence.ts +1 -1
  83. package/src/unstable/persistence/RateLimiter.ts +1 -1
  84. package/src/unstable/persistence/Redis.ts +1 -1
  85. package/src/unstable/reactivity/AsyncResult.ts +1 -1
  86. package/src/unstable/rpc/Rpc.ts +3 -3
  87. package/src/unstable/rpc/RpcClient.ts +1 -1
  88. package/src/unstable/rpc/RpcClientError.ts +1 -1
  89. package/src/unstable/rpc/RpcMessage.ts +3 -3
  90. package/src/unstable/socket/Socket.ts +3 -3
  91. package/src/unstable/sql/SqlError.ts +1 -1
  92. package/src/unstable/workers/WorkerError.ts +4 -4
  93. package/src/unstable/workflow/Activity.ts +1 -1
  94. package/src/unstable/workflow/DurableDeferred.ts +1 -1
  95. package/src/unstable/workflow/Workflow.ts +2 -2
package/dist/Schema.js CHANGED
@@ -31,7 +31,7 @@
31
31
  * - Type guard: {@link is}
32
32
  * - Assertion: {@link asserts}
33
33
  * - Add constraints: `.check(...)` with filters like {@link isMinLength},
34
- * {@link isGreaterThan}, {@link isPattern}, {@link isUUID}
34
+ * {@link isGreaterThan}, {@link isPattern}, {@link isUUID}, {@link isGUID}
35
35
  * - Transform between schemas: {@link decodeTo}, {@link encodeTo}
36
36
  * - Add a default for missing keys: {@link withDecodingDefault}, {@link withDecodingDefaultKey}
37
37
  * - Create branded types: {@link brand}
@@ -3221,7 +3221,7 @@ export const isStringBigInt = SchemaAST.isStringBigInt;
3221
3221
  */
3222
3222
  export const isStringSymbol = SchemaAST.isStringSymbol;
3223
3223
  /**
3224
- * Returns a RegExp for validating an RFC 4122 UUID.
3224
+ * Returns a RegExp for validating an RFC 9562 / RFC 4122 UUID.
3225
3225
  *
3226
3226
  * Optionally specify a version 1-8. If no version is specified (`undefined`), all versions are supported.
3227
3227
  */
@@ -3229,15 +3229,24 @@ const getUUIDRegExp = version => {
3229
3229
  if (version) {
3230
3230
  return new globalThis.RegExp(`^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-${version}[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12})$`);
3231
3231
  }
3232
- return /^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-8][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}|00000000-0000-0000-0000-000000000000)$/;
3232
+ return /^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-8][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}|00000000-0000-0000-0000-000000000000|[fF]{8}-[fF]{4}-[fF]{4}-[fF]{4}-[fF]{12})$/;
3233
3233
  };
3234
3234
  /**
3235
- * Validates that a string is a valid Universally Unique Identifier (UUID).
3236
- * Optionally specify a version (1-8) to validate against a specific UUID version.
3237
- * If no version is specified (`undefined`), all versions are supported.
3235
+ * Validates that a string is a strict Universally Unique Identifier (UUID).
3236
+ *
3237
+ * **When to use**
3238
+ *
3239
+ * Use when you need UUID semantics, including version and RFC variant bits,
3240
+ * rather than only the dashed hexadecimal shape.
3238
3241
  *
3239
3242
  * **Details**
3240
3243
  *
3244
+ * Without a version argument, this accepts UUID versions 1 through 8, the nil
3245
+ * UUID (`00000000-0000-0000-0000-000000000000`), and the max UUID
3246
+ * (`ffffffff-ffff-ffff-ffff-ffffffffffff`). With a version argument, this
3247
+ * accepts only UUIDs with that version and RFC variant bits; nil and max UUIDs
3248
+ * are not versioned UUIDs and do not match version-specific checks.
3249
+ *
3241
3250
  * JSON Schema:
3242
3251
  *
3243
3252
  * This check corresponds to a `pattern` constraint in JSON Schema that matches
@@ -3248,6 +3257,7 @@ const getUUIDRegExp = version => {
3248
3257
  * When generating test data with fast-check, this applies a `patterns`
3249
3258
  * constraint to ensure generated strings match the UUID pattern.
3250
3259
  *
3260
+ * @see {@link isGUID} for shape-only GUID validation.
3251
3261
  * @category String checks
3252
3262
  * @since 4.0.0
3253
3263
  */
@@ -3263,6 +3273,41 @@ export function isUUID(version, annotations) {
3263
3273
  ...annotations
3264
3274
  });
3265
3275
  }
3276
+ const GUID_REGEXP = /^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12})$/;
3277
+ /**
3278
+ * Validates that a string has the GUID / UUID textual shape.
3279
+ *
3280
+ * **When to use**
3281
+ *
3282
+ * Use when you need to accept dashed hexadecimal identifiers without enforcing
3283
+ * UUID version or variant bits.
3284
+ *
3285
+ * **Details**
3286
+ *
3287
+ * This check accepts strings in the `8-4-4-4-12` hexadecimal form. JSON Schema
3288
+ * output includes the corresponding `pattern` constraint and intentionally does
3289
+ * not include `format: "uuid"` because GUID validation is looser than UUID
3290
+ * validation.
3291
+ *
3292
+ * Arbitrary:
3293
+ *
3294
+ * When generating test data with fast-check, this applies a `patterns`
3295
+ * constraint to ensure generated strings match the GUID pattern.
3296
+ *
3297
+ * @see {@link isUUID} for strict UUID validation.
3298
+ * @category String checks
3299
+ * @since 4.0.0
3300
+ */
3301
+ export function isGUID(annotations) {
3302
+ return isPattern(GUID_REGEXP, {
3303
+ expected: "a GUID",
3304
+ meta: {
3305
+ _tag: "isGUID",
3306
+ regExp: GUID_REGEXP
3307
+ },
3308
+ ...annotations
3309
+ });
3310
+ }
3266
3311
  /**
3267
3312
  * Validates that a string is a valid ULID (Universally Unique Lexicographically
3268
3313
  * Sortable Identifier).
@@ -5370,99 +5415,119 @@ function causeToFormatter(error, defect) {
5370
5415
  const causeReason = causeReasonToFormatter(error, defect);
5371
5416
  return t => `Cause([${t.reasons.map(causeReason).join(", ")}])`;
5372
5417
  }
5373
- const ErrorJsonEncoded = /*#__PURE__*/Struct({
5374
- message: String,
5375
- name: /*#__PURE__*/optionalKey(String),
5376
- stack: /*#__PURE__*/optionalKey(String)
5377
- });
5418
+ const getErrorOptionsKey = options => (options?.includeStack === true ? 1 : 0) | (options?.excludeCause === true ? 2 : 0);
5419
+ const getErrorOptions = key => {
5420
+ switch (key) {
5421
+ case 0:
5422
+ return undefined;
5423
+ case 1:
5424
+ return {
5425
+ includeStack: true
5426
+ };
5427
+ case 2:
5428
+ return {
5429
+ excludeCause: true
5430
+ };
5431
+ case 3:
5432
+ return {
5433
+ includeStack: true,
5434
+ excludeCause: true
5435
+ };
5436
+ }
5437
+ };
5438
+ const errorSchemaCache = [];
5378
5439
  /**
5379
5440
  * Schema for JavaScript `Error` objects.
5380
5441
  *
5381
5442
  * **Details**
5382
5443
  *
5383
5444
  * Default JSON serializer:
5384
- * Encodes an `Error` as an object with `message` and optional `name` properties,
5385
- * and decodes that object back into an `Error`. The stack trace is omitted from
5386
- * the encoded form for security.
5387
5445
  *
5388
- * @category schemas
5446
+ * Encodes an `Error` as an object with `message`, optional `name`, and optional
5447
+ * `cause` properties, and decodes that object back into an `Error`. Stack
5448
+ * traces are omitted by default for security. Pass `{ includeStack: true }` to
5449
+ * include stack traces, or `{ excludeCause: true }` to omit causes.
5450
+ *
5451
+ * @category constructors
5389
5452
  * @since 4.0.0
5390
5453
  */
5391
- export const Error = /*#__PURE__*/instanceOf(globalThis.Error, {
5392
- typeConstructor: {
5393
- _tag: "Error"
5394
- },
5395
- generation: {
5396
- runtime: `Schema.Error`,
5397
- Type: `globalThis.Error`
5398
- },
5399
- expected: "Error",
5400
- toCodecJson: () => link()(ErrorJsonEncoded, SchemaTransformation.errorFromErrorJsonEncoded()),
5401
- toArbitrary: () => fc => fc.string().map(message => new globalThis.Error(message))
5402
- });
5454
+ export function Error(options) {
5455
+ const key = getErrorOptionsKey(options);
5456
+ const cached = errorSchemaCache[key];
5457
+ if (cached !== undefined) {
5458
+ return cached;
5459
+ }
5460
+ const normalizedOptions = getErrorOptions(key);
5461
+ const schema = instanceOf(globalThis.Error, {
5462
+ typeConstructor: {
5463
+ _tag: "Error",
5464
+ ...(normalizedOptions === undefined ? {} : {
5465
+ options: normalizedOptions
5466
+ })
5467
+ },
5468
+ generation: {
5469
+ runtime: normalizedOptions !== undefined ? `Schema.Error(${format(normalizedOptions)})` : `Schema.Error()`,
5470
+ Type: `globalThis.Error`
5471
+ },
5472
+ expected: "Error",
5473
+ toCodecJson: () => link()(JsonError, SchemaTransformation.errorFromJsonError(normalizedOptions)),
5474
+ toArbitrary: () => fc => fc.string().map(message => new globalThis.Error(message))
5475
+ });
5476
+ errorSchemaCache[key] = schema;
5477
+ return schema;
5478
+ }
5479
+ const defectSchemaCache = [];
5403
5480
  /**
5404
- * Schema for JavaScript `Error` objects that preserves stack traces in the JSON
5481
+ * Schema for unexpected defect values represented as `unknown` with a JSON
5405
5482
  * encoded form.
5406
5483
  *
5484
+ * **When to use**
5485
+ *
5486
+ * Use when you need a schema for `Cause` defects or other unexpected failures
5487
+ * whose runtime value may be any value.
5488
+ *
5407
5489
  * **Details**
5408
5490
  *
5409
- * Default JSON serializer:
5410
- * Encodes an `Error` as an object with `message`, optional `name`, and optional
5411
- * `stack` properties, and decodes that object back into an `Error`.
5491
+ * The encoded side is {@link Json}. During decoding, JSON objects with a string
5492
+ * `message` property are decoded into JavaScript `Error` values, preserving a
5493
+ * non-default `name` and any string `stack`. Other JSON values decode
5494
+ * unchanged.
5412
5495
  *
5413
- * @category schemas
5414
- * @since 4.0.0
5415
- */
5416
- export const ErrorWithStack = /*#__PURE__*/instanceOf(globalThis.Error, {
5417
- typeConstructor: {
5418
- _tag: "ErrorWithStack"
5419
- },
5420
- generation: {
5421
- runtime: `Schema.ErrorWithStack`,
5422
- Type: `globalThis.Error`
5423
- },
5424
- expected: "Error",
5425
- toCodecJson: () => link()(ErrorJsonEncoded, SchemaTransformation.errorFromErrorJsonEncoded({
5426
- includeStack: true
5427
- })),
5428
- toArbitrary: () => fc => fc.string().map(message => new globalThis.Error(message))
5429
- });
5430
- const defectTransformation = /*#__PURE__*/new SchemaTransformation.Transformation(/*#__PURE__*/SchemaGetter.passthrough(), /*#__PURE__*/SchemaGetter.transform(u => {
5431
- try {
5432
- return JSON.parse(JSON.stringify(u));
5433
- } catch {
5434
- return format(u);
5435
- }
5436
- }));
5437
- /**
5438
- * Schema for defect values, accepting either JavaScript `Error` values encoded
5439
- * with `message` and optional `name`, or arbitrary unknown defect values.
5496
+ * During encoding, JavaScript `Error` values encode to JSON objects with
5497
+ * `name`, `message`, and optional `cause` properties. Pass
5498
+ * `{ includeStack: true }` to include string stack traces in encoded `Error`
5499
+ * defects, or `{ excludeCause: true }` to omit causes. Other values are
5500
+ * serialized through Effect's JSON formatter and then parsed back into JSON
5501
+ * when possible.
5440
5502
  *
5441
- * **Details**
5503
+ * **Gotchas**
5442
5504
  *
5443
- * Default JSON serializer:
5444
- * Unknown defects are serialized with `JSON.stringify` when possible and fall
5445
- * back to Effect's formatted representation when JSON serialization fails.
5505
+ * This schema is for carrying defects across JSON boundaries, not for
5506
+ * preserving every JavaScript value exactly. Some values cannot round-trip
5507
+ * unchanged:
5446
5508
  *
5447
- * @category constructors
5448
- * @since 3.10.0
5449
- */
5450
- export const Defect = /*#__PURE__*/Union([/*#__PURE__*/ErrorJsonEncoded.pipe(/*#__PURE__*/decodeTo(Error, /*#__PURE__*/SchemaTransformation.errorFromErrorJsonEncoded())), /*#__PURE__*/Any.pipe(/*#__PURE__*/decodeTo(/*#__PURE__*/Unknown.annotate({
5451
- toCodecJson: () => link()(Any, defectTransformation),
5452
- toArbitrary: () => fc => fc.json()
5453
- }), defectTransformation))]);
5454
- /**
5455
- * Schema for defects that also includes stack traces in the encoded form.
5509
+ * - A non-`Error` object such as `{ message: "boom" }` encodes as an
5510
+ * error-shaped JSON object and decodes back as an `Error`.
5511
+ * - JSON serialization normalizes unsupported values. For example,
5512
+ * `undefined` array elements encode as `null`, unsupported object properties
5513
+ * are omitted, and circular references are dropped.
5514
+ * - Values that cannot be represented as JSON fall back to Effect's formatted
5515
+ * string representation.
5456
5516
  *
5457
- * @category Defect
5517
+ * @see {@link Error} for a schema that only accepts JavaScript `Error` values.
5518
+ * @category constructors
5458
5519
  * @since 4.0.0
5459
5520
  */
5460
- export const DefectWithStack = /*#__PURE__*/Union([/*#__PURE__*/ErrorJsonEncoded.pipe(/*#__PURE__*/decodeTo(ErrorWithStack, /*#__PURE__*/SchemaTransformation.errorFromErrorJsonEncoded({
5461
- includeStack: true
5462
- }))), /*#__PURE__*/Any.pipe(/*#__PURE__*/decodeTo(/*#__PURE__*/Unknown.annotate({
5463
- toCodecJson: () => link()(Any, defectTransformation),
5464
- toArbitrary: () => fc => fc.json()
5465
- }), defectTransformation))]);
5521
+ export function Defect(options) {
5522
+ const key = getErrorOptionsKey(options);
5523
+ const cached = defectSchemaCache[key];
5524
+ if (cached !== undefined) {
5525
+ return cached;
5526
+ }
5527
+ const schema = Json.pipe(decodeTo(Unknown, SchemaTransformation.defectFromJson(getErrorOptions(key))));
5528
+ defectSchemaCache[key] = schema;
5529
+ return schema;
5530
+ }
5466
5531
  /**
5467
5532
  * Creates a schema for `Exit` values using schemas for the success value, typed
5468
5533
  * failure, and unexpected defect channels.
@@ -8197,6 +8262,12 @@ export function Tree(node) {
8197
8262
  * @since 4.0.0
8198
8263
  */
8199
8264
  export const Json = /*#__PURE__*/make(SchemaAST.Json);
8265
+ const JsonError = /*#__PURE__*/Struct({
8266
+ message: String,
8267
+ name: /*#__PURE__*/optionalKey(String),
8268
+ stack: /*#__PURE__*/optionalKey(String),
8269
+ cause: /*#__PURE__*/optionalKey(Json)
8270
+ });
8200
8271
  /**
8201
8272
  * Schema that accepts any mutable JSON-compatible value. See {@link Json} for
8202
8273
  * the immutable variant.