effect-app 4.0.0-beta.21 → 4.0.0-beta.210

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