effect-app 4.0.0-beta.15 → 4.0.0-beta.151

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 (198) hide show
  1. package/CHANGELOG.md +595 -0
  2. package/dist/Array.d.ts +1 -1
  3. package/dist/Chunk.d.ts +1 -1
  4. package/dist/Chunk.d.ts.map +1 -1
  5. package/dist/Config/SecretURL.d.ts +1 -1
  6. package/dist/Config/SecretURL.d.ts.map +1 -1
  7. package/dist/Config/SecretURL.js +2 -2
  8. package/dist/Config/internal/configSecretURL.d.ts +1 -1
  9. package/dist/Config/internal/configSecretURL.d.ts.map +1 -1
  10. package/dist/Config.d.ts +7 -0
  11. package/dist/Config.d.ts.map +1 -0
  12. package/dist/Config.js +6 -0
  13. package/dist/ConfigProvider.d.ts +39 -0
  14. package/dist/ConfigProvider.d.ts.map +1 -0
  15. package/dist/ConfigProvider.js +42 -0
  16. package/dist/Context.d.ts +40 -0
  17. package/dist/Context.d.ts.map +1 -0
  18. package/dist/Context.js +67 -0
  19. package/dist/Effect.d.ts +9 -10
  20. package/dist/Effect.d.ts.map +1 -1
  21. package/dist/Effect.js +3 -6
  22. package/dist/Function.d.ts +1 -1
  23. package/dist/Function.d.ts.map +1 -1
  24. package/dist/Inputify.type.d.ts +1 -1
  25. package/dist/Layer.d.ts +6 -5
  26. package/dist/Layer.d.ts.map +1 -1
  27. package/dist/Layer.js +1 -1
  28. package/dist/NonEmptySet.d.ts +1 -1
  29. package/dist/NonEmptySet.d.ts.map +1 -1
  30. package/dist/Operations.d.ts +372 -50
  31. package/dist/Operations.d.ts.map +1 -1
  32. package/dist/Operations.js +9 -9
  33. package/dist/Option.d.ts +1 -1
  34. package/dist/Option.d.ts.map +1 -1
  35. package/dist/Pure.d.ts +5 -5
  36. package/dist/Pure.d.ts.map +1 -1
  37. package/dist/Pure.js +13 -13
  38. package/dist/Schema/Class.d.ts +69 -20
  39. package/dist/Schema/Class.d.ts.map +1 -1
  40. package/dist/Schema/Class.js +193 -22
  41. package/dist/Schema/FastCheck.d.ts +1 -1
  42. package/dist/Schema/FastCheck.d.ts.map +1 -1
  43. package/dist/Schema/Methods.d.ts +1 -1
  44. package/dist/Schema/SpecialJsonSchema.d.ts +33 -0
  45. package/dist/Schema/SpecialJsonSchema.d.ts.map +1 -0
  46. package/dist/Schema/SpecialJsonSchema.js +122 -0
  47. package/dist/Schema/SpecialOpenApi.d.ts +32 -0
  48. package/dist/Schema/SpecialOpenApi.d.ts.map +1 -0
  49. package/dist/Schema/SpecialOpenApi.js +123 -0
  50. package/dist/Schema/brand.d.ts +7 -2
  51. package/dist/Schema/brand.d.ts.map +1 -1
  52. package/dist/Schema/brand.js +1 -1
  53. package/dist/Schema/email.d.ts +1 -1
  54. package/dist/Schema/email.d.ts.map +1 -1
  55. package/dist/Schema/email.js +7 -4
  56. package/dist/Schema/ext.d.ts +113 -48
  57. package/dist/Schema/ext.d.ts.map +1 -1
  58. package/dist/Schema/ext.js +115 -53
  59. package/dist/Schema/moreStrings.d.ts +111 -11
  60. package/dist/Schema/moreStrings.d.ts.map +1 -1
  61. package/dist/Schema/moreStrings.js +14 -15
  62. package/dist/Schema/numbers.d.ts +127 -15
  63. package/dist/Schema/numbers.d.ts.map +1 -1
  64. package/dist/Schema/numbers.js +10 -12
  65. package/dist/Schema/phoneNumber.d.ts +1 -1
  66. package/dist/Schema/phoneNumber.d.ts.map +1 -1
  67. package/dist/Schema/phoneNumber.js +6 -3
  68. package/dist/Schema/schema.d.ts +1 -1
  69. package/dist/Schema/strings.d.ts +37 -5
  70. package/dist/Schema/strings.d.ts.map +1 -1
  71. package/dist/Schema/strings.js +1 -5
  72. package/dist/Schema.d.ts +88 -53
  73. package/dist/Schema.d.ts.map +1 -1
  74. package/dist/Schema.js +125 -63
  75. package/dist/Set.d.ts +1 -1
  76. package/dist/Set.d.ts.map +1 -1
  77. package/dist/TypeTest.d.ts +1 -1
  78. package/dist/Types.d.ts +1 -1
  79. package/dist/Widen.type.d.ts +1 -1
  80. package/dist/_ext/Array.d.ts +1 -1
  81. package/dist/_ext/Array.d.ts.map +1 -1
  82. package/dist/_ext/date.d.ts +1 -1
  83. package/dist/_ext/misc.d.ts +1 -1
  84. package/dist/_ext/ord.ext.d.ts +1 -1
  85. package/dist/_ext/ord.ext.d.ts.map +1 -1
  86. package/dist/builtin.d.ts +1 -1
  87. package/dist/builtin.d.ts.map +1 -1
  88. package/dist/client/apiClientFactory.d.ts +14 -30
  89. package/dist/client/apiClientFactory.d.ts.map +1 -1
  90. package/dist/client/apiClientFactory.js +18 -19
  91. package/dist/client/clientFor.d.ts +7 -6
  92. package/dist/client/clientFor.d.ts.map +1 -1
  93. package/dist/client/errors.d.ts +44 -19
  94. package/dist/client/errors.d.ts.map +1 -1
  95. package/dist/client/errors.js +35 -10
  96. package/dist/client/makeClient.d.ts +77 -29
  97. package/dist/client/makeClient.d.ts.map +1 -1
  98. package/dist/client/makeClient.js +49 -23
  99. package/dist/client.d.ts +1 -1
  100. package/dist/faker.d.ts +1 -1
  101. package/dist/faker.d.ts.map +1 -1
  102. package/dist/http/Request.d.ts +2 -2
  103. package/dist/http/Request.d.ts.map +1 -1
  104. package/dist/http/Request.js +5 -5
  105. package/dist/http/internal/lib.d.ts +1 -1
  106. package/dist/http.d.ts +1 -1
  107. package/dist/ids.d.ts +3 -3
  108. package/dist/ids.d.ts.map +1 -1
  109. package/dist/ids.js +3 -2
  110. package/dist/index.d.ts +5 -8
  111. package/dist/index.d.ts.map +1 -1
  112. package/dist/index.js +6 -8
  113. package/dist/logger.d.ts +1 -1
  114. package/dist/middleware.d.ts +8 -8
  115. package/dist/middleware.d.ts.map +1 -1
  116. package/dist/middleware.js +8 -8
  117. package/dist/rpc/MiddlewareMaker.d.ts +5 -4
  118. package/dist/rpc/MiddlewareMaker.d.ts.map +1 -1
  119. package/dist/rpc/MiddlewareMaker.js +26 -27
  120. package/dist/rpc/RpcContextMap.d.ts +3 -3
  121. package/dist/rpc/RpcContextMap.d.ts.map +1 -1
  122. package/dist/rpc/RpcContextMap.js +4 -4
  123. package/dist/rpc/RpcMiddleware.d.ts +5 -4
  124. package/dist/rpc/RpcMiddleware.d.ts.map +1 -1
  125. package/dist/rpc/RpcMiddleware.js +1 -1
  126. package/dist/rpc.d.ts +1 -2
  127. package/dist/rpc.d.ts.map +1 -1
  128. package/dist/rpc.js +1 -2
  129. package/dist/transform.d.ts +1 -1
  130. package/dist/transform.d.ts.map +1 -1
  131. package/dist/transform.js +3 -3
  132. package/dist/utils/effectify.d.ts +1 -1
  133. package/dist/utils/extend.d.ts +1 -1
  134. package/dist/utils/extend.d.ts.map +1 -1
  135. package/dist/utils/gen.d.ts +2 -2
  136. package/dist/utils/gen.d.ts.map +1 -1
  137. package/dist/utils/logLevel.d.ts +2 -2
  138. package/dist/utils/logLevel.d.ts.map +1 -1
  139. package/dist/utils/logger.d.ts +3 -3
  140. package/dist/utils/logger.d.ts.map +1 -1
  141. package/dist/utils/logger.js +3 -3
  142. package/dist/utils.d.ts +30 -10
  143. package/dist/utils.d.ts.map +1 -1
  144. package/dist/utils.js +10 -4
  145. package/dist/validation/validators.d.ts +1 -1
  146. package/dist/validation/validators.d.ts.map +1 -1
  147. package/dist/validation.d.ts +1 -1
  148. package/dist/validation.d.ts.map +1 -1
  149. package/eslint.config.mjs +1 -1
  150. package/package.json +35 -19
  151. package/src/Config/SecretURL.ts +2 -1
  152. package/src/Config.ts +14 -0
  153. package/src/ConfigProvider.ts +48 -0
  154. package/src/{ServiceMap.ts → Context.ts} +52 -59
  155. package/src/Effect.ts +12 -14
  156. package/src/Layer.ts +5 -4
  157. package/src/Operations.ts +9 -9
  158. package/src/Pure.ts +17 -18
  159. package/src/Schema/Class.ts +281 -62
  160. package/src/Schema/SpecialJsonSchema.ts +137 -0
  161. package/src/Schema/SpecialOpenApi.ts +130 -0
  162. package/src/Schema/brand.ts +9 -1
  163. package/src/Schema/email.ts +7 -2
  164. package/src/Schema/ext.ts +196 -87
  165. package/src/Schema/moreStrings.ts +22 -20
  166. package/src/Schema/numbers.ts +14 -16
  167. package/src/Schema/phoneNumber.ts +5 -1
  168. package/src/Schema/strings.ts +4 -8
  169. package/src/Schema.ts +256 -98
  170. package/src/client/apiClientFactory.ts +107 -113
  171. package/src/client/clientFor.ts +6 -1
  172. package/src/client/errors.ts +42 -17
  173. package/src/client/makeClient.ts +156 -63
  174. package/src/http/Request.ts +7 -4
  175. package/src/ids.ts +2 -1
  176. package/src/index.ts +5 -10
  177. package/src/middleware.ts +7 -9
  178. package/src/rpc/MiddlewareMaker.ts +36 -47
  179. package/src/rpc/RpcContextMap.ts +6 -5
  180. package/src/rpc/RpcMiddleware.ts +5 -4
  181. package/src/rpc.ts +0 -1
  182. package/src/transform.ts +2 -2
  183. package/src/utils/gen.ts +1 -1
  184. package/src/utils/logger.ts +2 -2
  185. package/src/utils.ts +47 -11
  186. package/test/dist/rpc.test.d.ts.map +1 -1
  187. package/test/dist/secretURL.test.d.ts.map +1 -0
  188. package/test/dist/special.test.d.ts.map +1 -0
  189. package/test/rpc.test.ts +38 -6
  190. package/test/schema.test.ts +594 -4
  191. package/test/secretURL.test.ts +157 -0
  192. package/test/special.test.ts +1005 -0
  193. package/test/utils.test.ts +6 -6
  194. package/tsconfig.base.json +0 -1
  195. package/tsconfig.json +0 -1
  196. package/dist/ServiceMap.d.ts +0 -44
  197. package/dist/ServiceMap.d.ts.map +0 -1
  198. package/dist/ServiceMap.js +0 -91
@@ -0,0 +1,1005 @@
1
+ import { Option, Predicate, Schema, SchemaGetter } from "effect"
2
+ import { InvalidStateError, LoginError, NotFoundError, NotLoggedInError, OptimisticConcurrencyException, ServiceUnavailableError, UnauthorizedError, ValidationError } from "effect-app/client/errors"
3
+ import * as AppSchema from "effect-app/Schema"
4
+ import { Class, TaggedClass } from "effect-app/Schema/Class"
5
+ import { flattenNestedAnyOf, flattenSimpleAllOf, specialJsonSchemaDocument } from "effect-app/Schema/SpecialJsonSchema"
6
+ import { deduplicateOpenApiSchemas } from "effect-app/Schema/SpecialOpenApi"
7
+ import * as S from "effect/Schema"
8
+ import { describe, expect, it } from "vitest"
9
+
10
+ describe("Class", () => {
11
+ it("encoding accepts plain objects matching the struct (Fields argument)", () => {
12
+ class A extends Class<A>("A")({ a: S.String }) {}
13
+
14
+ // Encoding a class instance still works
15
+ expect(S.encodeUnknownSync(A)(new A({ a: "hello" }))).toStrictEqual({ a: "hello" })
16
+
17
+ // Encoding a plain object matching the struct now succeeds
18
+ expect(S.encodeUnknownSync(A)({ a: "world" })).toStrictEqual({ a: "world" })
19
+
20
+ // Encoding null still fails
21
+ expect(() => S.encodeUnknownSync(A)(null)).toThrow()
22
+ })
23
+
24
+ it("encoding accepts plain objects matching the struct (Struct argument)", () => {
25
+ class A extends Class<A>("A")(S.Struct({ a: S.String })) {}
26
+
27
+ expect(S.encodeUnknownSync(A)(new A({ a: "hello" }))).toStrictEqual({ a: "hello" })
28
+ expect(S.encodeUnknownSync(A)({ a: "world" })).toStrictEqual({ a: "world" })
29
+ expect(() => S.encodeUnknownSync(A)(null)).toThrow()
30
+ })
31
+
32
+ it("decoding still works normally", () => {
33
+ class A extends Class<A>("A")({ a: S.String }) {}
34
+
35
+ const decoded = S.decodeUnknownSync(A)({ a: "hello" })
36
+ expect(decoded).toBeInstanceOf(A)
37
+ expect((decoded as A).a).toBe("hello")
38
+
39
+ expect(() => S.decodeUnknownSync(A)(null)).toThrow()
40
+ expect(() => S.decodeUnknownSync(A)({ a: 1 })).toThrow()
41
+ })
42
+
43
+ it("S.is accepts class instances and matching plain objects", () => {
44
+ class A extends Class<A>("A")({ a: S.String }) {}
45
+
46
+ expect(S.is(A)(new A({ a: "hello" }))).toBe(true)
47
+ expect(S.is(A)({ a: "world" })).toBe(true)
48
+ expect(S.is(A)({ a: 1 })).toBe(false)
49
+ expect(S.is(A)(null)).toBe(false)
50
+ })
51
+
52
+ it("rejects values that don't match the struct", () => {
53
+ class A extends Class<A>("A")({ a: S.String }) {}
54
+
55
+ expect(() => S.encodeUnknownSync(A)({ a: 123 })).toThrow()
56
+ expect(() => S.encodeUnknownSync(A)("not an object")).toThrow()
57
+ })
58
+
59
+ it("returns a class constructor — new and instanceof work", () => {
60
+ class A extends Class<A>("A")({ a: S.String }) {}
61
+
62
+ const instance = new A({ a: "hello" })
63
+ expect(instance).toBeInstanceOf(A)
64
+ expect(instance.a).toBe("hello")
65
+ })
66
+
67
+ it("preserves fields and identifier", () => {
68
+ class A extends Class<A>("A")({ a: S.String, b: S.Number }) {}
69
+
70
+ expect(A.identifier).toBe("A")
71
+ expect(Object.keys(A.fields)).toStrictEqual(["a", "b"])
72
+ })
73
+ })
74
+
75
+ describe("Class constructor", () => {
76
+ it("works as a base class — new, instanceof, encoding plain objects", () => {
77
+ class A extends Class<A>("A")({ a: S.String }) {}
78
+
79
+ // Construction
80
+ const instance = new A({ a: "hello" })
81
+ expect(instance).toBeInstanceOf(A)
82
+ expect(instance.a).toBe("hello")
83
+
84
+ // Encoding a class instance
85
+ expect(S.encodeUnknownSync(A)(instance)).toStrictEqual({ a: "hello" })
86
+
87
+ // Encoding a plain object
88
+ expect(S.encodeUnknownSync(A)({ a: "world" })).toStrictEqual({ a: "world" })
89
+
90
+ // Encoding invalid input fails
91
+ expect(() => S.encodeUnknownSync(A)(null)).toThrow()
92
+ expect(() => S.encodeUnknownSync(A)({ a: 123 })).toThrow()
93
+ })
94
+
95
+ it("decoding works normally", () => {
96
+ class A extends Class<A>("A")({ a: S.String }) {}
97
+
98
+ const decoded = S.decodeUnknownSync(A)({ a: "hello" })
99
+ expect(decoded).toBeInstanceOf(A)
100
+ expect((decoded as A).a).toBe("hello")
101
+
102
+ expect(() => S.decodeUnknownSync(A)({ a: 1 })).toThrow()
103
+ })
104
+
105
+ it("exposes fields, identifier", () => {
106
+ class A extends Class<A>("A")({ a: S.String, b: S.Number }) {}
107
+
108
+ expect(A.identifier).toBe("A")
109
+ expect(Object.keys(A.fields)).toStrictEqual(["a", "b"])
110
+ })
111
+ })
112
+
113
+ describe("TaggedClass constructor", () => {
114
+ it("works as a base class with _tag — new, instanceof, encoding plain objects", () => {
115
+ class Circle extends TaggedClass<Circle>()("Circle", { radius: S.Number }) {}
116
+
117
+ // Construction
118
+ const instance = new Circle({ radius: 5 })
119
+ expect(instance).toBeInstanceOf(Circle)
120
+ expect(instance._tag).toBe("Circle")
121
+ expect(instance.radius).toBe(5)
122
+
123
+ // Encoding a class instance
124
+ expect(S.encodeUnknownSync(Circle)(instance)).toStrictEqual({ _tag: "Circle", radius: 5 })
125
+
126
+ // Encoding a plain object
127
+ expect(S.encodeUnknownSync(Circle)({ _tag: "Circle", radius: 10 })).toStrictEqual({ _tag: "Circle", radius: 10 })
128
+
129
+ // Encoding invalid input fails
130
+ expect(() => S.encodeUnknownSync(Circle)(null)).toThrow()
131
+ expect(() => S.encodeUnknownSync(Circle)({ _tag: "Circle", radius: "nope" })).toThrow()
132
+ })
133
+
134
+ it("decoding works normally", () => {
135
+ class Circle extends TaggedClass<Circle>()("Circle", { radius: S.Number }) {}
136
+
137
+ const decoded = S.decodeUnknownSync(Circle)({ _tag: "Circle", radius: 5 })
138
+ expect(decoded).toBeInstanceOf(Circle)
139
+ expect((decoded as Circle).radius).toBe(5)
140
+ expect((decoded as Circle)._tag).toBe("Circle")
141
+ })
142
+
143
+ it("S.decodeSync(S.toType(X)) should report n length schema error", () => {
144
+ class X extends TaggedClass<X>()("X", { n: S.String.pipe(S.check(S.isMinLength(3))) }) {}
145
+
146
+ try {
147
+ S.decodeSync(S.toType(X))({ _tag: "X", n: "a" /* not length 3 */ })
148
+ expect.fail("expected decode to fail with a SchemaError")
149
+ } catch (error) {
150
+ expect(error).toBeInstanceOf(Error)
151
+ if (error instanceof Error) {
152
+ expect(error.message).toContain("n")
153
+ expect(error.message.toLowerCase()).toContain("length")
154
+ }
155
+ }
156
+ })
157
+
158
+ it("exposes fields, identifier", () => {
159
+ class Circle extends TaggedClass<Circle>()("Circle", { radius: S.Number }) {}
160
+
161
+ expect(Circle.identifier).toBe("Circle")
162
+ expect(Object.keys(Circle.fields)).toContain("_tag")
163
+ expect(Object.keys(Circle.fields)).toContain("radius")
164
+ })
165
+ })
166
+
167
+ describe("strict declaration option", () => {
168
+ it("Class strict: true keeps class-level expected errors", () => {
169
+ class X extends Class<X>("X")({ n: S.String.pipe(S.check(S.isMinLength(3))) }, undefined, { strict: true }) {}
170
+
171
+ expect(() => S.decodeSync(S.toType(X))({ n: "a" })).toThrow("Expected X")
172
+ })
173
+
174
+ it("TaggedClass strict: true keeps class-level expected errors", () => {
175
+ class X extends TaggedClass<X>()("X", { n: S.String.pipe(S.check(S.isMinLength(3))) }, undefined, {
176
+ strict: true
177
+ }) {}
178
+
179
+ expect(() => S.decodeSync(S.toType(X))({ _tag: "X", n: "a" })).toThrow("Expected X")
180
+ })
181
+
182
+ it("Class with encoded override strict: true keeps class-level expected errors", () => {
183
+ class X extends Class<X, never>("X")({ n: S.String.pipe(S.check(S.isMinLength(3))) }, undefined, {
184
+ strict: true
185
+ }) {}
186
+
187
+ expect(() => S.decodeSync(S.toType(X))({ n: "a" })).toThrow("Expected X")
188
+ })
189
+
190
+ it("TaggedClass with encoded override strict: true keeps class-level expected errors", () => {
191
+ class X extends TaggedClass<X, never>()("X", { n: S.String.pipe(S.check(S.isMinLength(3))) }, undefined, {
192
+ strict: true
193
+ }) {}
194
+
195
+ expect(() => S.decodeSync(S.toType(X))({ _tag: "X", n: "a" })).toThrow("Expected X")
196
+ })
197
+ })
198
+
199
+ describe("Class.copy", () => {
200
+ it("creates a new instance with updated fields", () => {
201
+ class A extends Class<A>("A")({ a: S.String, b: S.Number }) {}
202
+
203
+ const instance = new A({ a: "hello", b: 1 })
204
+ const copied: A = A.copy(instance, { b: 2 })
205
+ expect(copied).toBeInstanceOf(A)
206
+ expect(copied.a).toBe("hello")
207
+ expect(copied.b).toBe(2)
208
+ })
209
+
210
+ it("accepts a function for updates", () => {
211
+ class A extends Class<A>("A")({ a: S.String, b: S.Number }) {}
212
+
213
+ const instance = new A({ a: "hello", b: 1 })
214
+ const copied: A = A.copy(instance, (a) => ({ b: a.b + 1 }))
215
+ expect(copied).toBeInstanceOf(A)
216
+ expect(copied.b).toBe(2)
217
+ })
218
+
219
+ it("is pipeable", () => {
220
+ class A extends Class<A>("A")({ a: S.String, b: S.Number }) {}
221
+
222
+ const instance = new A({ a: "hello", b: 1 })
223
+ const copied: A = A.copy({ b: 2 })(instance)
224
+ expect(copied).toBeInstanceOf(A)
225
+ expect(copied.b).toBe(2)
226
+ })
227
+ })
228
+
229
+ describe("TaggedClass.copy", () => {
230
+ it("creates a new instance with updated fields", () => {
231
+ class Circle extends TaggedClass<Circle>()("Circle", { radius: S.Number }) {}
232
+
233
+ const instance = new Circle({ radius: 5 })
234
+ const copied: Circle = Circle.copy(instance, { radius: 10 })
235
+ expect(copied).toBeInstanceOf(Circle)
236
+ expect(copied._tag).toBe("Circle")
237
+ expect(copied.radius).toBe(10)
238
+ })
239
+
240
+ it("accepts a function for updates", () => {
241
+ class Circle extends TaggedClass<Circle>()("Circle", { radius: S.Number }) {}
242
+
243
+ const instance = new Circle({ radius: 5 })
244
+ const copied: Circle = Circle.copy(instance, (c) => ({ radius: c.radius * 2 }))
245
+ expect(copied).toBeInstanceOf(Circle)
246
+ expect(copied.radius).toBe(10)
247
+ })
248
+ })
249
+
250
+ describe("Struct.copy", () => {
251
+ it("creates a new value with updated fields", () => {
252
+ const A = AppSchema.Struct({ a: S.String, b: S.Number })
253
+
254
+ const instance = A.make({ a: "hello", b: 1 })
255
+ const copied = A.copy(instance, { b: 2 })
256
+
257
+ expect(copied).toEqual({ a: "hello", b: 2 })
258
+ expect(copied).not.toBe(instance)
259
+ })
260
+
261
+ it("accepts a function for updates", () => {
262
+ const A = AppSchema.Struct({ a: S.String, b: S.Number })
263
+
264
+ const instance = A.make({ a: "hello", b: 1 })
265
+ const copied = A.copy(instance, (a) => ({ b: a.b + 1 }))
266
+
267
+ expect(copied).toEqual({ a: "hello", b: 2 })
268
+ })
269
+
270
+ it("allows widening updates against the full struct type", () => {
271
+ const A = AppSchema.Struct({
272
+ name: S.String,
273
+ state: S.Union([
274
+ S.Struct({ _tag: S.tag("a"), a: S.String }),
275
+ S.Struct({ _tag: S.tag("b"), b: S.Number })
276
+ ])
277
+ })
278
+
279
+ const instance = A.make({ name: "x", state: { _tag: "a", a: "a" } })
280
+ const copied = A.copy(instance, { state: { _tag: "b", b: 1 } })
281
+
282
+ expect(copied).toEqual({ name: "x", state: { _tag: "b", b: 1 } })
283
+ })
284
+
285
+ it("copy function is preserved when using .annotate()", () => {
286
+ const A = AppSchema.Struct({ a: S.String, b: S.Number }).annotate({ title: "A" })
287
+
288
+ const instance = A.make({ a: "hello", b: 1 })
289
+ const copied = A.copy(instance, { b: 2 })
290
+
291
+ expect(copied).toEqual({ a: "hello", b: 2 })
292
+ expect(copied).not.toBe(instance)
293
+ })
294
+
295
+ it("copy function is preserved when using .annotateKey()", () => {
296
+ const A = AppSchema.Struct({ a: S.String, b: S.Number }).annotateKey({ title: "A" })
297
+
298
+ const instance = A.make({ a: "hello", b: 1 })
299
+ const copied = A.copy(instance, { b: 2 })
300
+
301
+ expect(copied).toEqual({ a: "hello", b: 2 })
302
+ expect(copied).not.toBe(instance)
303
+ })
304
+
305
+ it("copy function is preserved when using .mapFields()", () => {
306
+ const A = AppSchema.Struct({ a: S.String, b: S.Number }).mapFields((f) => ({ ...f }))
307
+
308
+ const instance = A.make({ a: "hello", b: 1 })
309
+ const copied = A.copy(instance, { b: 2 })
310
+
311
+ expect(copied).toEqual({ a: "hello", b: 2 })
312
+ expect(copied).not.toBe(instance)
313
+ })
314
+
315
+ it("copy function is preserved through chained calls", () => {
316
+ const A = AppSchema
317
+ .Struct({ a: S.String, b: S.Number })
318
+ .annotate({ title: "A" })
319
+ .annotateKey({ description: "test" })
320
+
321
+ const instance = A.make({ a: "hello", b: 1 })
322
+ const copied = A.copy(instance, { b: 2 })
323
+
324
+ expect(copied).toEqual({ a: "hello", b: 2 })
325
+ expect(copied).not.toBe(instance)
326
+ })
327
+ })
328
+
329
+ describe("TaggedStruct.copy", () => {
330
+ it("creates a new tagged value with updated fields", () => {
331
+ const Circle = AppSchema.TaggedStruct("Circle", { radius: S.Number })
332
+
333
+ const instance = Circle.make({ radius: 5 })
334
+ const copied = Circle.copy(instance, { radius: 10 })
335
+
336
+ expect(copied).toEqual({ _tag: "Circle", radius: 10 })
337
+ expect(copied).not.toBe(instance)
338
+ })
339
+ })
340
+
341
+ describe("TaggedError", () => {
342
+ it("InvalidStateError toString includes the message", () => {
343
+ const error = new InvalidStateError("something went wrong")
344
+ expect(error.toString()).toContain("something went wrong")
345
+ })
346
+
347
+ it("NotFoundError toString includes the message", () => {
348
+ const error = new NotFoundError({ type: "User", id: "123" })
349
+ expect(error.toString()).toContain("Didn't find User")
350
+ expect(error.toString()).toContain("123")
351
+ })
352
+
353
+ it("ServiceUnavailableError toString includes the message", () => {
354
+ const error = new ServiceUnavailableError("service down")
355
+ expect(error.toString()).toContain("service down")
356
+ })
357
+
358
+ it("ValidationError toString includes the message", () => {
359
+ const error = new ValidationError({ errors: ["field required"] })
360
+ expect(error.toString()).toContain("Validation failed")
361
+ expect(error.toString()).toContain("field required")
362
+ })
363
+
364
+ it("NotLoggedInError toString includes the message", () => {
365
+ const error = new NotLoggedInError("not logged in")
366
+ expect(error.toString()).toContain("not logged in")
367
+ })
368
+
369
+ it("LoginError toString includes the message", () => {
370
+ const error = new LoginError("login failed")
371
+ expect(error.toString()).toContain("login failed")
372
+ })
373
+
374
+ it("UnauthorizedError toString includes the message", () => {
375
+ const error = new UnauthorizedError("forbidden")
376
+ expect(error.toString()).toContain("forbidden")
377
+ })
378
+
379
+ it("OptimisticConcurrencyException toString includes the message", () => {
380
+ const error = new OptimisticConcurrencyException({ message: "conflict" })
381
+ expect(error.toString()).toContain("conflict")
382
+ })
383
+
384
+ it("OptimisticConcurrencyException from details toString includes the message", () => {
385
+ const error = new OptimisticConcurrencyException({ type: "User", id: "123", code: 409 })
386
+ expect(error.toString()).toContain("Existing User 123 record changed")
387
+ })
388
+ })
389
+
390
+ describe("SpecialJsonSchema", () => {
391
+ it("nullable to optional — from NullOr", () => {
392
+ const nullableDecodedUndefinedEncoded = (schema: Schema.Top) => {
393
+ const isNullableSchema = "members" in schema
394
+ && globalThis.Array.isArray((schema as any).members)
395
+ && (schema as any).members.length === 2
396
+ && (schema as any).members.some((member: any) => member.ast._tag === "Null")
397
+
398
+ const nullableMembers = isNullableSchema ? (schema as any).members as ReadonlyArray<Schema.Top> : undefined
399
+ const innerSchema = nullableMembers
400
+ ? nullableMembers.find((member: any) => member.ast._tag !== "Null")!
401
+ : schema
402
+
403
+ const nullableSchema = isNullableSchema ? schema : Schema.NullOr(schema)
404
+
405
+ return nullableSchema.pipe(
406
+ Schema.encodeTo(Schema.optionalKey(innerSchema), {
407
+ decode: SchemaGetter.transformOptional(Option.orElseSome(() => null)),
408
+ encode: SchemaGetter.transformOptional(Option.filter(Predicate.isNotNull))
409
+ })
410
+ )
411
+ }
412
+
413
+ const fromNullOr = nullableDecodedUndefinedEncoded(Schema.NullOr(Schema.String))
414
+ const structFromNullOr = Schema.Struct({ status: fromNullOr })
415
+
416
+ const encode = Schema.encodeUnknownSync(structFromNullOr as any)
417
+ const encodedNull = encode({ status: null }) as any
418
+ expect("status" in encodedNull).toBe(false)
419
+ expect(encode({ status: "test" })).toStrictEqual({ status: "test" })
420
+
421
+ const decode = Schema.decodeUnknownSync(structFromNullOr as any)
422
+ expect(decode({})).toStrictEqual({ status: null })
423
+ expect(decode({ status: "test" })).toStrictEqual({ status: "test" })
424
+
425
+ const doc = specialJsonSchemaDocument(structFromNullOr)
426
+ expect(doc).toStrictEqual({
427
+ dialect: "draft-2020-12",
428
+ schema: {
429
+ "type": "object",
430
+ "properties": {
431
+ "status": { "type": "string" }
432
+ }
433
+ },
434
+ definitions: {}
435
+ })
436
+ })
437
+
438
+ it("identifies X universally — deduplicates same-fingerprint references", () => {
439
+ const X = Schema.String.annotate({ title: "X", identifier: "X" })
440
+
441
+ const s = Schema.Struct({
442
+ a: Schema.NullOr(X).pipe(
443
+ Schema.encodeTo(Schema.optionalKey(X), {
444
+ decode: SchemaGetter.transformOptional(Option.orElseSome(() => null)),
445
+ encode: SchemaGetter.transformOptional(Option.filter(Predicate.isNotNull))
446
+ })
447
+ ),
448
+ b: Schema.NullOr(X).pipe(
449
+ Schema.encodeTo(Schema.optionalKey(X), {
450
+ decode: SchemaGetter.transformOptional(Option.orElseSome(() => null)),
451
+ encode: SchemaGetter.transformOptional(Option.filter(Predicate.isNotNull))
452
+ })
453
+ ),
454
+ c: Schema.NullOr(X),
455
+ d: X,
456
+ e: X.pipe(Schema.optionalKey)
457
+ })
458
+
459
+ const doc = specialJsonSchemaDocument(s)
460
+ expect(doc).toStrictEqual({
461
+ dialect: "draft-2020-12",
462
+ schema: {
463
+ "type": "object",
464
+ "properties": {
465
+ "a": { "$ref": "#/$defs/X" },
466
+ "b": { "$ref": "#/$defs/X" },
467
+ "c": {
468
+ "anyOf": [
469
+ { "$ref": "#/$defs/X" },
470
+ { "type": "null" }
471
+ ]
472
+ },
473
+ "d": { "$ref": "#/$defs/X" },
474
+ "e": { "$ref": "#/$defs/X" }
475
+ },
476
+ "required": ["c", "d"]
477
+ },
478
+ definitions: {
479
+ X: {
480
+ "type": "string",
481
+ "title": "X"
482
+ }
483
+ }
484
+ })
485
+ })
486
+
487
+ it("shared annotated schema via helper — deduplicates", () => {
488
+ const X = Schema.String.annotate({ title: "X", identifier: "X" })
489
+
490
+ const cache = new WeakMap()
491
+ const nullableDecodedUndefinedEncoded = (schema: Schema.Top) => {
492
+ const isNullableSchema = "members" in schema
493
+ && globalThis.Array.isArray((schema as any).members)
494
+ && (schema as any).members.length === 2
495
+ && (schema as any).members.some((member: any) => member.ast._tag === "Null")
496
+
497
+ const nullableMembers = isNullableSchema ? (schema as any).members as ReadonlyArray<Schema.Top> : undefined
498
+ const innerSchema = nullableMembers
499
+ ? nullableMembers.find((member: any) => member.ast._tag !== "Null")!
500
+ : schema
501
+
502
+ const cached = cache.get(innerSchema.ast)
503
+ if (cached !== undefined) return cached
504
+
505
+ const nullableSchema = isNullableSchema ? schema : Schema.NullOr(schema)
506
+ const out = nullableSchema.pipe(
507
+ Schema.encodeTo(Schema.optionalKey(innerSchema), {
508
+ decode: SchemaGetter.transformOptional(Option.orElseSome(() => null)),
509
+ encode: SchemaGetter.transformOptional(Option.filter(Predicate.isNotNull))
510
+ })
511
+ )
512
+
513
+ cache.set(innerSchema.ast, out)
514
+ return out
515
+ }
516
+
517
+ const structWithShared = Schema.Struct({
518
+ a: nullableDecodedUndefinedEncoded(X),
519
+ b: nullableDecodedUndefinedEncoded(Schema.NullOr(X)),
520
+ c: Schema.NullOr(X),
521
+ d: X,
522
+ e: X.pipe(Schema.optionalKey)
523
+ })
524
+
525
+ const doc = specialJsonSchemaDocument(structWithShared)
526
+ expect(doc).toStrictEqual({
527
+ dialect: "draft-2020-12",
528
+ schema: {
529
+ "type": "object",
530
+ "properties": {
531
+ "a": { "$ref": "#/$defs/X" },
532
+ "b": { "$ref": "#/$defs/X" },
533
+ "c": {
534
+ "anyOf": [
535
+ { "$ref": "#/$defs/X" },
536
+ { "type": "null" }
537
+ ]
538
+ },
539
+ "d": { "$ref": "#/$defs/X" },
540
+ "e": { "$ref": "#/$defs/X" }
541
+ },
542
+ "required": ["c", "d"]
543
+ },
544
+ definitions: {
545
+ X: {
546
+ "type": "string",
547
+ "title": "X"
548
+ }
549
+ }
550
+ })
551
+ })
552
+ })
553
+
554
+ describe("SpecialOpenApi", () => {
555
+ it("deduplicates identical components.schemas entries with same base identifier", () => {
556
+ const spec = {
557
+ openapi: "3.1.0",
558
+ info: { title: "Test", version: "1.0" },
559
+ paths: {
560
+ "/foo": {
561
+ get: {
562
+ responses: {
563
+ 200: {
564
+ content: {
565
+ "application/json": {
566
+ schema: { $ref: "#/components/schemas/X" }
567
+ }
568
+ }
569
+ }
570
+ }
571
+ }
572
+ },
573
+ "/bar": {
574
+ get: {
575
+ responses: {
576
+ 200: {
577
+ content: {
578
+ "application/json": {
579
+ schema: { $ref: "#/components/schemas/X1" }
580
+ }
581
+ }
582
+ }
583
+ }
584
+ }
585
+ }
586
+ },
587
+ components: {
588
+ schemas: {
589
+ X: { type: "string", title: "X" },
590
+ X1: { type: "string", title: "X" }
591
+ }
592
+ }
593
+ }
594
+
595
+ const result = deduplicateOpenApiSchemas(spec) as any
596
+
597
+ // X1 should be removed, and $ref to X1 rewritten to X
598
+ expect(result.components.schemas).toStrictEqual({
599
+ X: { type: "string", title: "X" }
600
+ })
601
+ expect(
602
+ result.paths["/bar"].get.responses[200].content["application/json"].schema
603
+ )
604
+ .toStrictEqual({ $ref: "#/components/schemas/X" })
605
+ })
606
+
607
+ it("does not deduplicate entries with different representations", () => {
608
+ const spec = {
609
+ openapi: "3.1.0",
610
+ info: { title: "Test", version: "1.0" },
611
+ paths: {},
612
+ components: {
613
+ schemas: {
614
+ X: { type: "string", title: "X" },
615
+ X1: { type: "number", title: "X" }
616
+ }
617
+ }
618
+ }
619
+
620
+ const result = deduplicateOpenApiSchemas(spec) as any
621
+
622
+ // Both should remain since they have different representations
623
+ expect(result.components.schemas).toStrictEqual({
624
+ X: { type: "string", title: "X" },
625
+ X1: { type: "number", title: "X" }
626
+ })
627
+ })
628
+
629
+ it("returns spec unchanged when no duplicates exist", () => {
630
+ const spec = {
631
+ openapi: "3.1.0",
632
+ info: { title: "Test", version: "1.0" },
633
+ paths: {},
634
+ components: {
635
+ schemas: {
636
+ Foo: { type: "string" },
637
+ Bar: { type: "number" }
638
+ }
639
+ }
640
+ }
641
+
642
+ const result = deduplicateOpenApiSchemas(spec)
643
+ expect(result).toStrictEqual(spec)
644
+ })
645
+
646
+ it("rewrites nested $ref pointers in allOf/anyOf/oneOf", () => {
647
+ const spec = {
648
+ openapi: "3.1.0",
649
+ info: { title: "Test", version: "1.0" },
650
+ paths: {
651
+ "/baz": {
652
+ post: {
653
+ requestBody: {
654
+ content: {
655
+ "application/json": {
656
+ schema: {
657
+ anyOf: [
658
+ { $ref: "#/components/schemas/Y1" },
659
+ { type: "null" }
660
+ ]
661
+ }
662
+ }
663
+ }
664
+ }
665
+ }
666
+ }
667
+ },
668
+ components: {
669
+ schemas: {
670
+ Y: { type: "object", properties: { name: { type: "string" } } },
671
+ Y1: { type: "object", properties: { name: { type: "string" } } }
672
+ }
673
+ }
674
+ }
675
+
676
+ const result = deduplicateOpenApiSchemas(spec) as any
677
+
678
+ expect(result.components.schemas).toStrictEqual({
679
+ Y: { type: "object", properties: { name: { type: "string" } } }
680
+ })
681
+ expect(
682
+ result.paths["/baz"].post.requestBody.content["application/json"].schema.anyOf[0]
683
+ )
684
+ .toStrictEqual({ $ref: "#/components/schemas/Y" })
685
+ })
686
+
687
+ it("rewrites $ref pointers inside definitions themselves", () => {
688
+ const spec = {
689
+ openapi: "3.1.0",
690
+ info: { title: "Test", version: "1.0" },
691
+ paths: {},
692
+ components: {
693
+ schemas: {
694
+ Inner: { type: "string" },
695
+ Inner1: { type: "string" },
696
+ Outer: {
697
+ type: "object",
698
+ properties: {
699
+ field: { $ref: "#/components/schemas/Inner1" }
700
+ }
701
+ }
702
+ }
703
+ }
704
+ }
705
+
706
+ const result = deduplicateOpenApiSchemas(spec) as any
707
+
708
+ expect(Object.keys(result.components.schemas)).toStrictEqual(["Inner", "Outer"])
709
+ expect(result.components.schemas.Outer.properties.field).toStrictEqual({
710
+ $ref: "#/components/schemas/Inner"
711
+ })
712
+ })
713
+
714
+ it("handles spec without components gracefully", () => {
715
+ const spec = { openapi: "3.1.0", info: { title: "Test", version: "1.0" }, paths: {} }
716
+ const result = deduplicateOpenApiSchemas(spec)
717
+ expect(result).toStrictEqual(spec)
718
+ })
719
+
720
+ it("flattens allOf in components.schemas", () => {
721
+ const spec = {
722
+ openapi: "3.1.0",
723
+ info: { title: "Test", version: "1.0" },
724
+ paths: {},
725
+ components: {
726
+ schemas: {
727
+ PositiveInt: {
728
+ type: "integer",
729
+ allOf: [{ exclusiveMinimum: 0, title: "PositiveInt" }]
730
+ }
731
+ }
732
+ }
733
+ }
734
+
735
+ const result = deduplicateOpenApiSchemas(spec) as any
736
+
737
+ expect(result.components.schemas.PositiveInt).toStrictEqual({
738
+ type: "integer",
739
+ exclusiveMinimum: 0,
740
+ title: "PositiveInt"
741
+ })
742
+ })
743
+
744
+ it("does not flatten allOf containing $ref entries", () => {
745
+ const spec = {
746
+ openapi: "3.1.0",
747
+ info: { title: "Test", version: "1.0" },
748
+ paths: {},
749
+ components: {
750
+ schemas: {
751
+ Composed: {
752
+ type: "object",
753
+ allOf: [{ $ref: "#/components/schemas/Base" }]
754
+ },
755
+ Base: { type: "object", properties: { id: { type: "string" } } }
756
+ }
757
+ }
758
+ }
759
+
760
+ const result = deduplicateOpenApiSchemas(spec) as any
761
+
762
+ expect(result.components.schemas.Composed).toStrictEqual({
763
+ type: "object",
764
+ allOf: [{ $ref: "#/components/schemas/Base" }]
765
+ })
766
+ })
767
+
768
+ it("does not flatten allOf entries that define their own type", () => {
769
+ const spec = {
770
+ openapi: "3.1.0",
771
+ info: { title: "Test", version: "1.0" },
772
+ paths: {},
773
+ components: {
774
+ schemas: {
775
+ Mixed: {
776
+ type: "object",
777
+ allOf: [{ type: "string", minLength: 1 }]
778
+ }
779
+ }
780
+ }
781
+ }
782
+
783
+ const result = deduplicateOpenApiSchemas(spec) as any
784
+
785
+ expect(result.components.schemas.Mixed).toStrictEqual({
786
+ type: "object",
787
+ allOf: [{ type: "string", minLength: 1 }]
788
+ })
789
+ })
790
+ })
791
+
792
+ describe("flattenSimpleAllOf", () => {
793
+ it("flattens constraint-only allOf into parent with type", () => {
794
+ const input = {
795
+ type: "integer",
796
+ allOf: [{ exclusiveMinimum: 0, title: "PositiveInt" }]
797
+ }
798
+ expect(flattenSimpleAllOf(input)).toStrictEqual({
799
+ type: "integer",
800
+ exclusiveMinimum: 0,
801
+ title: "PositiveInt"
802
+ })
803
+ })
804
+
805
+ it("flattens string type with multiple constraints", () => {
806
+ const input = {
807
+ type: "string",
808
+ allOf: [
809
+ { minLength: 1, maxLength: 255 },
810
+ { title: "NonEmptyString255" }
811
+ ]
812
+ }
813
+ expect(flattenSimpleAllOf(input)).toStrictEqual({
814
+ type: "string",
815
+ minLength: 1,
816
+ maxLength: 255,
817
+ title: "NonEmptyString255"
818
+ })
819
+ })
820
+
821
+ it("does not flatten allOf with $ref", () => {
822
+ const input = {
823
+ type: "object",
824
+ allOf: [{ $ref: "#/components/schemas/Base" }]
825
+ }
826
+ expect(flattenSimpleAllOf(input)).toStrictEqual(input)
827
+ })
828
+
829
+ it("does not flatten allOf entries with their own type", () => {
830
+ const input = {
831
+ type: "object",
832
+ allOf: [{ type: "string", minLength: 1 }]
833
+ }
834
+ expect(flattenSimpleAllOf(input)).toStrictEqual(input)
835
+ })
836
+
837
+ it("allOf entry wins on property conflict", () => {
838
+ const input = {
839
+ type: "integer",
840
+ title: "OldTitle",
841
+ allOf: [{ title: "NewTitle", minimum: 0 }]
842
+ }
843
+ expect(flattenSimpleAllOf(input)).toStrictEqual({
844
+ type: "integer",
845
+ title: "NewTitle",
846
+ minimum: 0
847
+ })
848
+ })
849
+ })
850
+
851
+ describe("Post-processing integration — real Effect Schema types", () => {
852
+ it("PositiveInt — allOf flattened, no wrapping", () => {
853
+ const doc = specialJsonSchemaDocument(AppSchema.PositiveInt)
854
+ expect(doc.definitions["PositiveInt"]).toStrictEqual({
855
+ type: "integer",
856
+ exclusiveMinimum: 0
857
+ })
858
+ })
859
+
860
+ it("NonEmptyString255 — multiple allOf constraints merged", () => {
861
+ const doc = specialJsonSchemaDocument(AppSchema.NonEmptyString255)
862
+ expect(doc.definitions["NonEmptyString255"]).toStrictEqual({
863
+ type: "string",
864
+ minLength: 1,
865
+ maxLength: 255
866
+ })
867
+ })
868
+
869
+ it("NullOr(NonEmptyString64k) — null preserved in anyOf, allOf flattened in definition", () => {
870
+ const schema = S.Struct({ note: S.NullOr(AppSchema.NonEmptyString64k) })
871
+ const doc = specialJsonSchemaDocument(schema)
872
+
873
+ // null variant preserved (correct JSON Schema for NullOr)
874
+ expect(doc.schema).toStrictEqual({
875
+ type: "object",
876
+ properties: {
877
+ note: {
878
+ anyOf: [
879
+ { $ref: "#/$defs/NonEmptyString64k" },
880
+ { type: "null" }
881
+ ]
882
+ }
883
+ },
884
+ required: ["note"]
885
+ })
886
+
887
+ // allOf flattened in the referenced definition
888
+ expect(doc.definitions["NonEmptyString64k"]).toStrictEqual({
889
+ type: "string",
890
+ minLength: 1,
891
+ maxLength: 65536
892
+ })
893
+ })
894
+
895
+ it("NonNegativeInt — allOf flattened", () => {
896
+ const doc = specialJsonSchemaDocument(AppSchema.NonNegativeInt)
897
+ expect(doc.definitions["NonNegativeInt"]).toStrictEqual({
898
+ type: "integer",
899
+ minimum: 0
900
+ })
901
+ })
902
+
903
+ it("NullOr union flattens nested anyOf members", () => {
904
+ const A = S.String.annotate({ identifier: "A" })
905
+ const B = S.Boolean.annotate({ identifier: "B" })
906
+ const schema = S.Struct({
907
+ value: S.NullOr(S.Union([A, B]))
908
+ })
909
+ const doc = specialJsonSchemaDocument(schema)
910
+ const valueProp = (doc.schema as Record<string, any>)["properties"]["value"]
911
+
912
+ expect(valueProp).toStrictEqual({
913
+ anyOf: [
914
+ { $ref: "#/$defs/A" },
915
+ { $ref: "#/$defs/B" },
916
+ { type: "null" }
917
+ ]
918
+ })
919
+ })
920
+ })
921
+
922
+ describe("flattenNestedAnyOf", () => {
923
+ it("flattens nested anyOf with no sibling keys", () => {
924
+ const input = {
925
+ anyOf: [
926
+ { anyOf: [{ type: "string" }, { type: "number" }] },
927
+ { type: "null" }
928
+ ]
929
+ }
930
+ expect(flattenNestedAnyOf(input)).toStrictEqual({
931
+ anyOf: [
932
+ { type: "string" },
933
+ { type: "number" },
934
+ { type: "null" }
935
+ ]
936
+ })
937
+ })
938
+
939
+ it("does not flatten anyOf entry with sibling keys", () => {
940
+ const input = {
941
+ anyOf: [
942
+ { anyOf: [{ type: "string" }], title: "X" },
943
+ { type: "null" }
944
+ ]
945
+ }
946
+ // The inner anyOf is not flattened into the outer (sibling "title" prevents it),
947
+ // but the single-element inner anyOf is unwrapped within the entry itself
948
+ expect(flattenNestedAnyOf(input)).toStrictEqual({
949
+ anyOf: [
950
+ { type: "string", title: "X" },
951
+ { type: "null" }
952
+ ]
953
+ })
954
+ })
955
+
956
+ it("unwraps anyOf with single item after flattening", () => {
957
+ const input = {
958
+ anyOf: [
959
+ { anyOf: [{ type: "string" }] }
960
+ ]
961
+ }
962
+ expect(flattenNestedAnyOf(input)).toStrictEqual({ type: "string" })
963
+ })
964
+
965
+ it("unwraps anyOf with single item, merging sibling properties", () => {
966
+ const input = {
967
+ title: "MyField",
968
+ anyOf: [{ type: "string" }]
969
+ }
970
+ expect(flattenNestedAnyOf(input)).toStrictEqual({
971
+ title: "MyField",
972
+ type: "string"
973
+ })
974
+ })
975
+
976
+ it("recurses into nested objects", () => {
977
+ const input = {
978
+ properties: {
979
+ field: {
980
+ anyOf: [
981
+ { anyOf: [{ $ref: "#/defs/A" }, { $ref: "#/defs/B" }] },
982
+ { type: "null" }
983
+ ]
984
+ }
985
+ }
986
+ }
987
+ expect(flattenNestedAnyOf(input)).toStrictEqual({
988
+ properties: {
989
+ field: {
990
+ anyOf: [
991
+ { $ref: "#/defs/A" },
992
+ { $ref: "#/defs/B" },
993
+ { type: "null" }
994
+ ]
995
+ }
996
+ }
997
+ })
998
+ })
999
+
1000
+ it("passes through non-objects unchanged", () => {
1001
+ expect(flattenNestedAnyOf(null)).toBe(null)
1002
+ expect(flattenNestedAnyOf(42)).toBe(42)
1003
+ expect(flattenNestedAnyOf("hello")).toBe("hello")
1004
+ })
1005
+ })