@atproto/lex-schema 0.0.11 → 0.0.12

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 (238) hide show
  1. package/CHANGELOG.md +19 -0
  2. package/dist/core/$type.d.ts +149 -0
  3. package/dist/core/$type.d.ts.map +1 -1
  4. package/dist/core/$type.js +44 -0
  5. package/dist/core/$type.js.map +1 -1
  6. package/dist/core/record-key.d.ts +44 -0
  7. package/dist/core/record-key.d.ts.map +1 -1
  8. package/dist/core/record-key.js +30 -0
  9. package/dist/core/record-key.js.map +1 -1
  10. package/dist/core/result.d.ts +85 -4
  11. package/dist/core/result.d.ts.map +1 -1
  12. package/dist/core/result.js +60 -4
  13. package/dist/core/result.js.map +1 -1
  14. package/dist/core/schema.d.ts +229 -2
  15. package/dist/core/schema.d.ts.map +1 -1
  16. package/dist/core/schema.js +197 -4
  17. package/dist/core/schema.js.map +1 -1
  18. package/dist/core/string-format.d.ts +244 -11
  19. package/dist/core/string-format.d.ts.map +1 -1
  20. package/dist/core/string-format.js +150 -0
  21. package/dist/core/string-format.js.map +1 -1
  22. package/dist/core/types.d.ts +90 -3
  23. package/dist/core/types.d.ts.map +1 -1
  24. package/dist/core/types.js.map +1 -1
  25. package/dist/core/validation-error.d.ts +60 -0
  26. package/dist/core/validation-error.d.ts.map +1 -1
  27. package/dist/core/validation-error.js +60 -0
  28. package/dist/core/validation-error.js.map +1 -1
  29. package/dist/core/validation-issue.d.ts +61 -0
  30. package/dist/core/validation-issue.d.ts.map +1 -1
  31. package/dist/core/validation-issue.js +51 -0
  32. package/dist/core/validation-issue.js.map +1 -1
  33. package/dist/core/validator.d.ts +347 -10
  34. package/dist/core/validator.d.ts.map +1 -1
  35. package/dist/core/validator.js +184 -3
  36. package/dist/core/validator.js.map +1 -1
  37. package/dist/helpers.d.ts +9 -24
  38. package/dist/helpers.d.ts.map +1 -1
  39. package/dist/helpers.js.map +1 -1
  40. package/dist/schema/array.d.ts +45 -0
  41. package/dist/schema/array.d.ts.map +1 -1
  42. package/dist/schema/array.js +14 -0
  43. package/dist/schema/array.js.map +1 -1
  44. package/dist/schema/blob.d.ts +46 -0
  45. package/dist/schema/blob.d.ts.map +1 -1
  46. package/dist/schema/blob.js +39 -0
  47. package/dist/schema/blob.js.map +1 -1
  48. package/dist/schema/boolean.d.ts +28 -0
  49. package/dist/schema/boolean.d.ts.map +1 -1
  50. package/dist/schema/boolean.js +28 -0
  51. package/dist/schema/boolean.js.map +1 -1
  52. package/dist/schema/bytes.d.ts +38 -0
  53. package/dist/schema/bytes.d.ts.map +1 -1
  54. package/dist/schema/bytes.js +32 -0
  55. package/dist/schema/bytes.js.map +1 -1
  56. package/dist/schema/cid.d.ts +38 -0
  57. package/dist/schema/cid.d.ts.map +1 -1
  58. package/dist/schema/cid.js +33 -0
  59. package/dist/schema/cid.js.map +1 -1
  60. package/dist/schema/custom.d.ts +66 -1
  61. package/dist/schema/custom.d.ts.map +1 -1
  62. package/dist/schema/custom.js +54 -0
  63. package/dist/schema/custom.js.map +1 -1
  64. package/dist/schema/dict.d.ts +44 -0
  65. package/dist/schema/dict.d.ts.map +1 -1
  66. package/dist/schema/dict.js +44 -0
  67. package/dist/schema/dict.js.map +1 -1
  68. package/dist/schema/discriminated-union.d.ts +58 -0
  69. package/dist/schema/discriminated-union.d.ts.map +1 -1
  70. package/dist/schema/discriminated-union.js +45 -0
  71. package/dist/schema/discriminated-union.js.map +1 -1
  72. package/dist/schema/enum.d.ts +48 -0
  73. package/dist/schema/enum.d.ts.map +1 -1
  74. package/dist/schema/enum.js +48 -0
  75. package/dist/schema/enum.js.map +1 -1
  76. package/dist/schema/integer.d.ts +42 -0
  77. package/dist/schema/integer.d.ts.map +1 -1
  78. package/dist/schema/integer.js +36 -0
  79. package/dist/schema/integer.js.map +1 -1
  80. package/dist/schema/intersection.d.ts +54 -0
  81. package/dist/schema/intersection.d.ts.map +1 -1
  82. package/dist/schema/intersection.js +49 -0
  83. package/dist/schema/intersection.js.map +1 -1
  84. package/dist/schema/literal.d.ts +44 -0
  85. package/dist/schema/literal.d.ts.map +1 -1
  86. package/dist/schema/literal.js +44 -0
  87. package/dist/schema/literal.js.map +1 -1
  88. package/dist/schema/never.d.ts +42 -0
  89. package/dist/schema/never.d.ts.map +1 -1
  90. package/dist/schema/never.js +42 -0
  91. package/dist/schema/never.js.map +1 -1
  92. package/dist/schema/null.d.ts +29 -0
  93. package/dist/schema/null.d.ts.map +1 -1
  94. package/dist/schema/null.js +29 -0
  95. package/dist/schema/null.js.map +1 -1
  96. package/dist/schema/nullable.d.ts +41 -0
  97. package/dist/schema/nullable.d.ts.map +1 -1
  98. package/dist/schema/nullable.js +41 -0
  99. package/dist/schema/nullable.js.map +1 -1
  100. package/dist/schema/object.d.ts +56 -0
  101. package/dist/schema/object.d.ts.map +1 -1
  102. package/dist/schema/object.js +51 -0
  103. package/dist/schema/object.js.map +1 -1
  104. package/dist/schema/optional.d.ts +42 -0
  105. package/dist/schema/optional.d.ts.map +1 -1
  106. package/dist/schema/optional.js +42 -0
  107. package/dist/schema/optional.js.map +1 -1
  108. package/dist/schema/params.d.ts +90 -10
  109. package/dist/schema/params.d.ts.map +1 -1
  110. package/dist/schema/params.js +84 -10
  111. package/dist/schema/params.js.map +1 -1
  112. package/dist/schema/payload.d.ts +111 -15
  113. package/dist/schema/payload.d.ts.map +1 -1
  114. package/dist/schema/payload.js +70 -0
  115. package/dist/schema/payload.js.map +1 -1
  116. package/dist/schema/permission-set.d.ts +58 -0
  117. package/dist/schema/permission-set.d.ts.map +1 -1
  118. package/dist/schema/permission-set.js +50 -0
  119. package/dist/schema/permission-set.js.map +1 -1
  120. package/dist/schema/permission.d.ts +42 -0
  121. package/dist/schema/permission.d.ts.map +1 -1
  122. package/dist/schema/permission.js +39 -0
  123. package/dist/schema/permission.js.map +1 -1
  124. package/dist/schema/procedure.d.ts +64 -0
  125. package/dist/schema/procedure.d.ts.map +1 -1
  126. package/dist/schema/procedure.js +64 -0
  127. package/dist/schema/procedure.js.map +1 -1
  128. package/dist/schema/query.d.ts +55 -0
  129. package/dist/schema/query.d.ts.map +1 -1
  130. package/dist/schema/query.js +55 -0
  131. package/dist/schema/query.js.map +1 -1
  132. package/dist/schema/record.d.ts +63 -8
  133. package/dist/schema/record.d.ts.map +1 -1
  134. package/dist/schema/record.js +20 -0
  135. package/dist/schema/record.js.map +1 -1
  136. package/dist/schema/ref.d.ts +50 -0
  137. package/dist/schema/ref.d.ts.map +1 -1
  138. package/dist/schema/ref.js +17 -0
  139. package/dist/schema/ref.js.map +1 -1
  140. package/dist/schema/refine.d.ts +58 -9
  141. package/dist/schema/refine.d.ts.map +1 -1
  142. package/dist/schema/refine.js.map +1 -1
  143. package/dist/schema/regexp.d.ts +44 -0
  144. package/dist/schema/regexp.d.ts.map +1 -1
  145. package/dist/schema/regexp.js +44 -0
  146. package/dist/schema/regexp.js.map +1 -1
  147. package/dist/schema/string.d.ts +50 -0
  148. package/dist/schema/string.d.ts.map +1 -1
  149. package/dist/schema/string.js +41 -0
  150. package/dist/schema/string.js.map +1 -1
  151. package/dist/schema/subscription.d.ts +72 -2
  152. package/dist/schema/subscription.d.ts.map +1 -1
  153. package/dist/schema/subscription.js +59 -0
  154. package/dist/schema/subscription.js.map +1 -1
  155. package/dist/schema/token.d.ts +47 -0
  156. package/dist/schema/token.d.ts.map +1 -1
  157. package/dist/schema/token.js +47 -0
  158. package/dist/schema/token.js.map +1 -1
  159. package/dist/schema/typed-object.d.ts +62 -8
  160. package/dist/schema/typed-object.d.ts.map +1 -1
  161. package/dist/schema/typed-object.js +18 -0
  162. package/dist/schema/typed-object.js.map +1 -1
  163. package/dist/schema/typed-ref.d.ts +53 -0
  164. package/dist/schema/typed-ref.d.ts.map +1 -1
  165. package/dist/schema/typed-ref.js +15 -0
  166. package/dist/schema/typed-ref.js.map +1 -1
  167. package/dist/schema/typed-union.d.ts +50 -1
  168. package/dist/schema/typed-union.d.ts.map +1 -1
  169. package/dist/schema/typed-union.js +50 -1
  170. package/dist/schema/typed-union.js.map +1 -1
  171. package/dist/schema/union.d.ts +45 -0
  172. package/dist/schema/union.d.ts.map +1 -1
  173. package/dist/schema/union.js +40 -0
  174. package/dist/schema/union.js.map +1 -1
  175. package/dist/schema/unknown-object.d.ts +34 -0
  176. package/dist/schema/unknown-object.d.ts.map +1 -1
  177. package/dist/schema/unknown-object.js +31 -0
  178. package/dist/schema/unknown-object.js.map +1 -1
  179. package/dist/schema/unknown.d.ts +33 -0
  180. package/dist/schema/unknown.d.ts.map +1 -1
  181. package/dist/schema/unknown.js +33 -0
  182. package/dist/schema/unknown.js.map +1 -1
  183. package/dist/schema/with-default.d.ts +44 -0
  184. package/dist/schema/with-default.d.ts.map +1 -1
  185. package/dist/schema/with-default.js +44 -0
  186. package/dist/schema/with-default.js.map +1 -1
  187. package/package.json +3 -3
  188. package/src/core/$type.ts +150 -18
  189. package/src/core/record-key.ts +44 -0
  190. package/src/core/result.ts +86 -4
  191. package/src/core/schema.ts +236 -7
  192. package/src/core/string-format.ts +259 -13
  193. package/src/core/types.ts +91 -3
  194. package/src/core/validation-error.ts +60 -0
  195. package/src/core/validation-issue.ts +65 -0
  196. package/src/core/validator.ts +351 -10
  197. package/src/helpers.test.ts +110 -29
  198. package/src/helpers.ts +9 -14
  199. package/src/schema/array.test.ts +94 -79
  200. package/src/schema/array.ts +45 -0
  201. package/src/schema/blob.ts +46 -0
  202. package/src/schema/boolean.ts +28 -0
  203. package/src/schema/bytes.ts +38 -0
  204. package/src/schema/cid.ts +38 -0
  205. package/src/schema/custom.ts +66 -1
  206. package/src/schema/dict.ts +44 -0
  207. package/src/schema/discriminated-union.ts +58 -0
  208. package/src/schema/enum.ts +48 -0
  209. package/src/schema/integer.ts +42 -0
  210. package/src/schema/intersection.ts +54 -0
  211. package/src/schema/literal.ts +44 -0
  212. package/src/schema/never.ts +42 -0
  213. package/src/schema/null.ts +29 -0
  214. package/src/schema/nullable.ts +41 -0
  215. package/src/schema/object.ts +56 -0
  216. package/src/schema/optional.ts +42 -0
  217. package/src/schema/params.test.ts +58 -2
  218. package/src/schema/params.ts +125 -19
  219. package/src/schema/payload.test.ts +3 -3
  220. package/src/schema/payload.ts +142 -38
  221. package/src/schema/permission-set.ts +58 -0
  222. package/src/schema/permission.ts +42 -0
  223. package/src/schema/procedure.ts +64 -0
  224. package/src/schema/query.ts +55 -0
  225. package/src/schema/record.ts +63 -8
  226. package/src/schema/ref.ts +50 -0
  227. package/src/schema/refine.ts +58 -9
  228. package/src/schema/regexp.ts +44 -0
  229. package/src/schema/string.ts +50 -0
  230. package/src/schema/subscription.ts +72 -2
  231. package/src/schema/token.ts +47 -0
  232. package/src/schema/typed-object.ts +62 -8
  233. package/src/schema/typed-ref.ts +53 -0
  234. package/src/schema/typed-union.ts +55 -2
  235. package/src/schema/union.ts +45 -0
  236. package/src/schema/unknown-object.ts +34 -0
  237. package/src/schema/unknown.ts +33 -0
  238. package/src/schema/with-default.ts +44 -0
@@ -38,9 +38,8 @@ describe('InferMethodParams', () => {
38
38
  const procedure = l.procedure(
39
39
  'com.example.list',
40
40
  l.params({ limit: l.string() }),
41
- l.payload(undefined, undefined),
42
- l.payload(undefined, undefined),
43
- undefined,
41
+ l.payload(),
42
+ l.payload(),
44
43
  )
45
44
 
46
45
  type Params = l.InferMethodParams<typeof procedure>
@@ -62,7 +61,7 @@ describe('InferMethodParams', () => {
62
61
  l.params({
63
62
  cursor: l.optional(l.integer()),
64
63
  }),
65
- l.unknown(),
64
+ l.unknownObject(),
66
65
  )
67
66
 
68
67
  type Params = l.InferMethodParams<typeof subscription>
@@ -82,9 +81,8 @@ describe('InferMethodInput', () => {
82
81
  const procedure = l.procedure(
83
82
  'com.example.create',
84
83
  l.params(),
85
- l.payload('application/json', l.object({ text: l.string() })),
86
- l.payload(undefined, undefined),
87
- undefined,
84
+ l.jsonPayload({ text: l.string() }),
85
+ l.payload(),
88
86
  )
89
87
 
90
88
  type Input = l.InferMethodInput<typeof procedure, BinaryValue>
@@ -93,6 +91,8 @@ describe('InferMethodInput', () => {
93
91
  // @ts-expect-error
94
92
  expectType<Input>({ encoding: 'application/json', body: { text: 123 } })
95
93
  // @ts-expect-error
94
+ expectType<Input>({ encoding: 'text/plain', body: { text: 'hi' } })
95
+ // @ts-expect-error
96
96
  expectType<Input>({ encoding: 'text/plain', body: 'hello' })
97
97
  // @ts-expect-error
98
98
  expectType<Input>({ encoding: 'text/plain', body: new Uint8Array() })
@@ -105,8 +105,7 @@ describe('InferMethodInput', () => {
105
105
  'com.example.create',
106
106
  l.params(),
107
107
  l.payload('*/*', undefined),
108
- l.payload(undefined, undefined),
109
- undefined,
108
+ l.payload(),
110
109
  )
111
110
 
112
111
  type Input = l.InferMethodInput<typeof procedure, BinaryValue>
@@ -128,9 +127,8 @@ describe('InferMethodInputBody', () => {
128
127
  const procedure = l.procedure(
129
128
  'com.example.create',
130
129
  l.params(),
131
- l.payload('application/json', l.object({ text: l.string() })),
132
- l.payload(undefined, undefined),
133
- undefined,
130
+ l.jsonPayload({ text: l.string() }),
131
+ l.payload(),
134
132
  )
135
133
 
136
134
  type InputBody = l.InferMethodInputBody<typeof procedure, BinaryValue>
@@ -147,8 +145,7 @@ describe('InferMethodInputBody', () => {
147
145
  'com.example.upload',
148
146
  l.params(),
149
147
  l.payload('*/*', undefined),
150
- l.payload(undefined, undefined),
151
- undefined,
148
+ l.payload(),
152
149
  )
153
150
 
154
151
  type InputBody = l.InferMethodInputBody<typeof procedure, BinaryValue>
@@ -168,9 +165,8 @@ describe('InferMethodInputEncoding', () => {
168
165
  const procedure = l.procedure(
169
166
  'com.example.create',
170
167
  l.params(),
171
- l.payload('application/json', l.object({ text: l.string() })),
172
- l.payload(undefined, undefined),
173
- undefined,
168
+ l.jsonPayload({ text: l.string() }),
169
+ l.payload(),
174
170
  )
175
171
 
176
172
  type InputEncoding = l.InferMethodInputEncoding<typeof procedure>
@@ -187,8 +183,7 @@ describe('InferMethodInputEncoding', () => {
187
183
  'com.example.upload',
188
184
  l.params(),
189
185
  l.payload('*/*', undefined),
190
- l.payload(undefined, undefined),
191
- undefined,
186
+ l.payload(),
192
187
  )
193
188
 
194
189
  type InputEncoding = l.InferMethodInputEncoding<typeof procedure>
@@ -203,11 +198,42 @@ describe('InferMethodInputEncoding', () => {
203
198
 
204
199
  describe('InferMethodOutput', () => {
205
200
  describe('query', () => {
201
+ test('generic output', () => {
202
+ const query = l.query(
203
+ 'com.example.query',
204
+ l.params(),
205
+ l.payload(),
206
+ ) as unknown as l.Query
207
+ const unknownValue = {} as unknown
208
+ const lexValue = {} as l.LexValue
209
+
210
+ type Output = l.InferMethodOutput<typeof query, BinaryValue>
211
+
212
+ expectType<Output>(undefined)
213
+ expectType<Output>({ body: binaryValue, encoding: 'text/plain' })
214
+ expectType<Output>({ body: lexValue, encoding: 'application/json' })
215
+
216
+ expectType<Output>({
217
+ // @ts-expect-error
218
+ body: unknownValue,
219
+ encoding: 'application/octet-stream',
220
+ })
221
+
222
+ class Foo {
223
+ constructor(readonly field: number = 3) {}
224
+ }
225
+ expectType<Output>({
226
+ // @ts-expect-error
227
+ body: new Foo(),
228
+ encoding: 'application/octet-stream',
229
+ })
230
+ })
231
+
206
232
  test('with payload schema', () => {
207
233
  const query = l.query(
208
234
  'com.example.query',
209
235
  l.params(),
210
- l.payload('application/json', l.object({ items: l.array(l.string()) })),
236
+ l.jsonPayload({ items: l.array(l.string()) }),
211
237
  )
212
238
 
213
239
  type Output = l.InferMethodOutput<typeof query, BinaryValue>
@@ -237,11 +263,44 @@ describe('InferMethodOutput', () => {
237
263
  })
238
264
 
239
265
  describe('procedure', () => {
266
+ test('generic output', () => {
267
+ const procedure = l.procedure(
268
+ 'com.example.procedure',
269
+ l.params(),
270
+ l.payload(),
271
+ l.payload(),
272
+ ) as unknown as l.Procedure
273
+ const unknownValue = {} as unknown
274
+ const lexValue = {} as l.LexValue
275
+
276
+ type Output = l.InferMethodOutput<typeof procedure, BinaryValue>
277
+
278
+ expectType<Output>(undefined)
279
+ expectType<Output>({ body: binaryValue, encoding: 'text/plain' })
280
+ expectType<Output>({ body: lexValue, encoding: 'application/json' })
281
+ expectType<Output>({ body: { foo: 'bar' }, encoding: 'application/json' })
282
+
283
+ expectType<Output>({
284
+ // @ts-expect-error
285
+ body: unknownValue,
286
+ encoding: 'application/octet-stream',
287
+ })
288
+
289
+ class Foo {
290
+ constructor(readonly field: number = 3) {}
291
+ }
292
+ expectType<Output>({
293
+ // @ts-expect-error
294
+ body: new Foo(),
295
+ encoding: 'application/octet-stream',
296
+ })
297
+ })
298
+
240
299
  test('with payload schema', () => {
241
300
  const procedure = l.procedure(
242
301
  'com.example.create',
243
302
  l.params(),
244
- l.payload(undefined, undefined),
303
+ l.payload(),
245
304
  l.payload(
246
305
  'application/json',
247
306
  l.object({ uri: l.string({ format: 'at-uri' }) }),
@@ -267,7 +326,7 @@ describe('InferMethodOutput', () => {
267
326
  const procedure = l.procedure(
268
327
  'com.example.export',
269
328
  l.params(),
270
- l.payload(undefined, undefined),
329
+ l.payload(),
271
330
  l.payload('*/*', undefined),
272
331
  undefined,
273
332
  )
@@ -291,6 +350,27 @@ describe('InferMethodOutput', () => {
291
350
 
292
351
  describe('InferMethodOutputBody', () => {
293
352
  describe('query', () => {
353
+ test('generic output', () => {
354
+ const query = l.query(
355
+ 'com.example.query',
356
+ l.params(),
357
+ l.payload(),
358
+ ) as unknown as l.Query
359
+ const lexValue = {} as l.LexValue
360
+
361
+ type OutputBody = l.InferMethodOutputBody<typeof query, BinaryValue>
362
+
363
+ expectType<OutputBody>(undefined)
364
+ expectType<OutputBody>(binaryValue)
365
+ expectType<OutputBody>(lexValue)
366
+
367
+ class Foo {
368
+ constructor(readonly field: number = 3) {}
369
+ }
370
+ // @ts-expect-error
371
+ expectType<OutputBody>(new Foo())
372
+ })
373
+
294
374
  test('with payload schema', () => {
295
375
  const query = l.query(
296
376
  'com.example.query',
@@ -338,7 +418,7 @@ describe('InferMethodOutputBody', () => {
338
418
  const procedure = l.procedure(
339
419
  'com.example.get',
340
420
  l.params(),
341
- l.payload(undefined, undefined),
421
+ l.payload(),
342
422
  l.payload(
343
423
  'application/json',
344
424
  l.object({ uri: l.string({ format: 'at-uri' }) }),
@@ -359,7 +439,7 @@ describe('InferMethodOutputBody', () => {
359
439
  const procedure = l.procedure(
360
440
  'com.example.export',
361
441
  l.params(),
362
- l.payload(undefined, undefined),
442
+ l.payload(),
363
443
  l.payload('*/*', undefined),
364
444
  undefined,
365
445
  )
@@ -381,7 +461,7 @@ describe('InferMethodOutputEncoding', () => {
381
461
  const query = l.query(
382
462
  'com.example.query',
383
463
  l.params(),
384
- l.payload('application/json', l.object({ data: l.string() })),
464
+ l.jsonPayload({ data: l.string() }),
385
465
  )
386
466
 
387
467
  type OutputEncoding = l.InferMethodOutputEncoding<typeof query>
@@ -414,8 +494,8 @@ describe('InferMethodOutputEncoding', () => {
414
494
  const procedure = l.procedure(
415
495
  'com.example.create',
416
496
  l.params(),
417
- l.payload(undefined, undefined),
418
- l.payload('application/json', l.object({ id: l.string() })),
497
+ l.payload(),
498
+ l.jsonPayload({ id: l.string() }),
419
499
  undefined,
420
500
  )
421
501
 
@@ -432,7 +512,7 @@ describe('InferMethodOutputEncoding', () => {
432
512
  const procedure = l.procedure(
433
513
  'com.example.export',
434
514
  l.params(),
435
- l.payload(undefined, undefined),
515
+ l.payload(),
436
516
  l.payload('*/*', undefined),
437
517
  undefined,
438
518
  )
@@ -474,13 +554,14 @@ describe('InferMethodMessage', () => {
474
554
  const subscription = l.subscription(
475
555
  'com.example.subscribe',
476
556
  l.params(),
477
- l.unknown(),
557
+ l.integer(),
478
558
  )
479
559
 
480
560
  type Message = l.InferMethodMessage<typeof subscription>
481
561
 
482
562
  // @ts-expect-error "unknown" is turned into LexValue
483
563
  expectType<Message>(undefined)
564
+ // @ts-expect-error Not a LexValue
484
565
  expectType<Message>({ any: 'value' })
485
566
  expectType<Message>(123)
486
567
  })
package/src/helpers.ts CHANGED
@@ -1,10 +1,9 @@
1
- import { LexErrorData, LexValue } from '@atproto/lex-data'
1
+ import { LexErrorData } from '@atproto/lex-data'
2
2
  import { InferOutput, Restricted, Schema } from './core.js'
3
3
  import {
4
4
  InferPayload,
5
5
  InferPayloadBody,
6
6
  InferPayloadEncoding,
7
- Payload,
8
7
  Procedure,
9
8
  Query,
10
9
  Subscription,
@@ -35,39 +34,35 @@ export type InferMethodParams<M extends Procedure | Query | Subscription> =
35
34
  export type InferMethodInput<
36
35
  M extends Procedure | Query | Subscription,
37
36
  B = BinaryData,
38
- > = M extends { input: Payload } ? InferPayload<M['input'], B> : undefined
37
+ > = M extends Procedure ? InferPayload<M['input'], B> : undefined
39
38
 
40
39
  export type InferMethodInputBody<
41
40
  M extends Procedure | Query | Subscription,
42
41
  B = BinaryData,
43
- > = M extends { input: Payload } ? InferPayloadBody<M['input'], B> : undefined
42
+ > = M extends Procedure ? InferPayloadBody<M['input'], B> : undefined
44
43
 
45
44
  export type InferMethodInputEncoding<
46
45
  M extends Procedure | Query | Subscription,
47
- > = M extends { input: Payload } ? InferPayloadEncoding<M['input']> : undefined
46
+ > = M extends Procedure ? InferPayloadEncoding<M['input']> : undefined
48
47
 
49
48
  export type InferMethodOutput<
50
49
  M extends Procedure | Query | Subscription,
51
50
  B = BinaryData,
52
- > = M extends { output: Payload } ? InferPayload<M['output'], B> : undefined
51
+ > = M extends Procedure | Query ? InferPayload<M['output'], B> : undefined
53
52
 
54
53
  export type InferMethodOutputBody<
55
54
  M extends Procedure | Query | Subscription,
56
55
  B = BinaryData,
57
- > = M extends { output: Payload } ? InferPayloadBody<M['output'], B> : undefined
56
+ > = M extends Procedure | Query ? InferPayloadBody<M['output'], B> : undefined
58
57
 
59
58
  export type InferMethodOutputEncoding<
60
59
  M extends Procedure | Query | Subscription,
61
- > = M extends { output: Payload }
62
- ? InferPayloadEncoding<M['output']>
63
- : undefined
60
+ > = M extends Procedure | Query ? InferPayloadEncoding<M['output']> : undefined
64
61
 
65
62
  export type InferMethodMessage<
66
63
  //
67
- M extends Procedure | Query | Subscription,
68
- > = M extends { message: Schema }
69
- ? LexValue & InferOutput<M['message']>
70
- : undefined
64
+ M extends Subscription,
65
+ > = M extends Subscription ? InferOutput<M['message']> : undefined
71
66
 
72
67
  export type InferMethodError<
73
68
  //
@@ -5,103 +5,118 @@ import { object } from './object.js'
5
5
  import { string } from './string.js'
6
6
 
7
7
  describe('ArraySchema', () => {
8
- it('validates arrays with string items', () => {
9
- const schema = array(string())
10
- const result = schema.safeParse(['hello', 'world'])
11
- expect(result.success).toBe(true)
12
- })
13
-
14
- it('validates arrays with integer items', () => {
15
- const schema = array(integer())
16
- const result = schema.safeParse([1, 2, 3])
17
- expect(result.success).toBe(true)
18
- })
8
+ describe('validation', () => {
9
+ it('validates arrays with string items', () => {
10
+ const schema = array(string())
11
+ const result = schema.safeValidate(['hello', 'world'])
12
+ expect(result).toMatchObject({ success: true })
13
+ })
14
+
15
+ it('validates arrays with integer items', () => {
16
+ const schema = array(integer())
17
+ const result = schema.safeValidate([1, 2, 3])
18
+ expect(result).toMatchObject({ success: true })
19
+ })
20
+
21
+ it('validates arrays with object items', () => {
22
+ const schema = array(
23
+ object({
24
+ name: string(),
25
+ age: integer(),
26
+ }),
27
+ )
28
+ const result = schema.safeValidate([
29
+ { name: 'Alice', age: 30 },
30
+ { name: 'Bob', age: 25 },
31
+ ])
32
+ expect(result).toMatchObject({ success: true })
33
+ })
19
34
 
20
- it('validates arrays with object items', () => {
21
- const schema = array(
22
- object({
23
- name: string(),
24
- age: integer(),
25
- }),
26
- )
27
- const result = schema.safeParse([
28
- { name: 'Alice', age: 30 },
29
- { name: 'Bob', age: 25 },
30
- ])
31
- expect(result.success).toBe(true)
32
- })
35
+ it('validates empty arrays', () => {
36
+ const schema = array(string())
37
+ const result = schema.safeValidate([])
38
+ expect(result).toMatchObject({ success: true })
39
+ })
33
40
 
34
- it('validates empty arrays', () => {
35
- const schema = array(string())
36
- const result = schema.safeParse([])
37
- expect(result.success).toBe(true)
38
- })
41
+ it('rejects non-array values', () => {
42
+ const schema = array(string())
43
+ const result = schema.safeValidate('not an array')
44
+ expect(result).toMatchObject({ success: false })
45
+ })
39
46
 
40
- it('rejects non-array values', () => {
41
- const schema = array(string())
42
- const result = schema.safeParse('not an array')
43
- expect(result.success).toBe(false)
44
- })
47
+ it('rejects null values', () => {
48
+ const schema = array(string())
49
+ const result = schema.safeValidate(null)
50
+ expect(result).toMatchObject({ success: false })
51
+ })
45
52
 
46
- it('rejects null values', () => {
47
- const schema = array(string())
48
- const result = schema.safeParse(null)
49
- expect(result.success).toBe(false)
50
- })
53
+ it('rejects undefined values', () => {
54
+ const schema = array(string())
55
+ const result = schema.safeValidate(undefined)
56
+ expect(result).toMatchObject({ success: false })
57
+ })
51
58
 
52
- it('rejects undefined values', () => {
53
- const schema = array(string())
54
- const result = schema.safeParse(undefined)
55
- expect(result.success).toBe(false)
56
- })
59
+ it('rejects objects that look like arrays', () => {
60
+ const schema = array(string())
61
+ const result = schema.safeValidate({ 0: 'a', 1: 'b', length: 2 })
62
+ expect(result).toMatchObject({ success: false })
63
+ })
57
64
 
58
- it('rejects objects that look like arrays', () => {
59
- const schema = array(string())
60
- const result = schema.safeParse({ 0: 'a', 1: 'b', length: 2 })
61
- expect(result.success).toBe(false)
62
- })
65
+ it('rejects arrays with invalid items', () => {
66
+ const schema = array(integer())
67
+ const result = schema.safeValidate([1, 2, 'three'])
68
+ expect(result).toMatchObject({ success: false })
69
+ })
63
70
 
64
- it('rejects arrays with invalid items', () => {
65
- const schema = array(integer())
66
- const result = schema.safeParse([1, 2, 'three'])
67
- expect(result.success).toBe(false)
68
- })
71
+ it('rejects arrays with some invalid items', () => {
72
+ const schema = array(string())
73
+ const result = schema.safeValidate(['valid', null, 'also valid'])
74
+ expect(result).toMatchObject({ success: false })
75
+ })
69
76
 
70
- it('rejects arrays with some invalid items', () => {
71
- const schema = array(string())
72
- const result = schema.safeParse(['valid', null, 'also valid'])
73
- expect(result.success).toBe(false)
77
+ it('rejects single values', () => {
78
+ const schema = array(string())
79
+ const result = schema.safeValidate(3)
80
+ expect(result).toEqual({
81
+ success: false,
82
+ reason: expect.objectContaining({
83
+ message: expect.stringContaining(
84
+ 'Expected array value type at $ (got integer)',
85
+ ),
86
+ }),
87
+ })
88
+ })
74
89
  })
75
90
 
76
91
  describe('minLength constraint', () => {
77
92
  it('validates arrays meeting minLength', () => {
78
93
  const schema = array(string(), { minLength: 2 })
79
94
  const result = schema.safeParse(['a', 'b'])
80
- expect(result.success).toBe(true)
95
+ expect(result).toMatchObject({ success: true })
81
96
  })
82
97
 
83
98
  it('validates arrays exceeding minLength', () => {
84
99
  const schema = array(string(), { minLength: 2 })
85
100
  const result = schema.safeParse(['a', 'b', 'c'])
86
- expect(result.success).toBe(true)
101
+ expect(result).toMatchObject({ success: true })
87
102
  })
88
103
 
89
104
  it('rejects arrays below minLength', () => {
90
105
  const schema = array(string(), { minLength: 3 })
91
106
  const result = schema.safeParse(['a', 'b'])
92
- expect(result.success).toBe(false)
107
+ expect(result).toMatchObject({ success: false })
93
108
  })
94
109
 
95
110
  it('rejects empty arrays when minLength is set', () => {
96
111
  const schema = array(string(), { minLength: 1 })
97
112
  const result = schema.safeParse([])
98
- expect(result.success).toBe(false)
113
+ expect(result).toMatchObject({ success: false })
99
114
  })
100
115
 
101
116
  it('validates empty arrays when minLength is 0', () => {
102
117
  const schema = array(string(), { minLength: 0 })
103
118
  const result = schema.safeParse([])
104
- expect(result.success).toBe(true)
119
+ expect(result).toMatchObject({ success: true })
105
120
  })
106
121
  })
107
122
 
@@ -109,37 +124,37 @@ describe('ArraySchema', () => {
109
124
  it('validates arrays meeting maxLength', () => {
110
125
  const schema = array(string(), { maxLength: 3 })
111
126
  const result = schema.safeParse(['a', 'b', 'c'])
112
- expect(result.success).toBe(true)
127
+ expect(result).toMatchObject({ success: true })
113
128
  })
114
129
 
115
130
  it('validates arrays below maxLength', () => {
116
131
  const schema = array(string(), { maxLength: 3 })
117
132
  const result = schema.safeParse(['a', 'b'])
118
- expect(result.success).toBe(true)
133
+ expect(result).toMatchObject({ success: true })
119
134
  })
120
135
 
121
136
  it('rejects arrays exceeding maxLength', () => {
122
137
  const schema = array(string(), { maxLength: 2 })
123
138
  const result = schema.safeParse(['a', 'b', 'c'])
124
- expect(result.success).toBe(false)
139
+ expect(result).toMatchObject({ success: false })
125
140
  })
126
141
 
127
142
  it('validates empty arrays with maxLength', () => {
128
143
  const schema = array(string(), { maxLength: 5 })
129
144
  const result = schema.safeParse([])
130
- expect(result.success).toBe(true)
145
+ expect(result).toMatchObject({ success: true })
131
146
  })
132
147
 
133
148
  it('rejects empty arrays when maxLength is 0', () => {
134
149
  const schema = array(string(), { maxLength: 0 })
135
150
  const result = schema.safeParse(['a'])
136
- expect(result.success).toBe(false)
151
+ expect(result).toMatchObject({ success: false })
137
152
  })
138
153
 
139
154
  it('validates empty arrays when maxLength is 0', () => {
140
155
  const schema = array(string(), { maxLength: 0 })
141
156
  const result = schema.safeParse([])
142
- expect(result.success).toBe(true)
157
+ expect(result).toMatchObject({ success: true })
143
158
  })
144
159
  })
145
160
 
@@ -150,7 +165,7 @@ describe('ArraySchema', () => {
150
165
  maxLength: 4,
151
166
  })
152
167
  const result = schema.safeParse(['a', 'b', 'c'])
153
- expect(result.success).toBe(true)
168
+ expect(result).toMatchObject({ success: true })
154
169
  })
155
170
 
156
171
  it('validates arrays at min boundary', () => {
@@ -159,7 +174,7 @@ describe('ArraySchema', () => {
159
174
  maxLength: 4,
160
175
  })
161
176
  const result = schema.safeParse(['a', 'b'])
162
- expect(result.success).toBe(true)
177
+ expect(result).toMatchObject({ success: true })
163
178
  })
164
179
 
165
180
  it('validates arrays at max boundary', () => {
@@ -168,7 +183,7 @@ describe('ArraySchema', () => {
168
183
  maxLength: 4,
169
184
  })
170
185
  const result = schema.safeParse(['a', 'b', 'c', 'd'])
171
- expect(result.success).toBe(true)
186
+ expect(result).toMatchObject({ success: true })
172
187
  })
173
188
 
174
189
  it('rejects arrays below minLength', () => {
@@ -177,7 +192,7 @@ describe('ArraySchema', () => {
177
192
  maxLength: 4,
178
193
  })
179
194
  const result = schema.safeParse(['a'])
180
- expect(result.success).toBe(false)
195
+ expect(result).toMatchObject({ success: false })
181
196
  })
182
197
 
183
198
  it('rejects arrays above maxLength', () => {
@@ -186,7 +201,7 @@ describe('ArraySchema', () => {
186
201
  maxLength: 4,
187
202
  })
188
203
  const result = schema.safeParse(['a', 'b', 'c', 'd', 'e'])
189
- expect(result.success).toBe(false)
204
+ expect(result).toMatchObject({ success: false })
190
205
  })
191
206
 
192
207
  it('validates single-length range', () => {
@@ -195,7 +210,7 @@ describe('ArraySchema', () => {
195
210
  maxLength: 3,
196
211
  })
197
212
  const result = schema.safeParse(['a', 'b', 'c'])
198
- expect(result.success).toBe(true)
213
+ expect(result).toMatchObject({ success: true })
199
214
  })
200
215
 
201
216
  it('rejects arrays not matching exact length', () => {
@@ -204,7 +219,7 @@ describe('ArraySchema', () => {
204
219
  maxLength: 3,
205
220
  })
206
221
  const result = schema.safeParse(['a', 'b'])
207
- expect(result.success).toBe(false)
222
+ expect(result).toMatchObject({ success: false })
208
223
  })
209
224
  })
210
225
 
@@ -215,7 +230,7 @@ describe('ArraySchema', () => {
215
230
  ['a', 'b'],
216
231
  ['c', 'd'],
217
232
  ])
218
- expect(result.success).toBe(true)
233
+ expect(result).toMatchObject({ success: true })
219
234
  })
220
235
 
221
236
  it('rejects invalid nested arrays', () => {
@@ -224,13 +239,13 @@ describe('ArraySchema', () => {
224
239
  [1, 2],
225
240
  [3, 'four'],
226
241
  ])
227
- expect(result.success).toBe(false)
242
+ expect(result).toMatchObject({ success: false })
228
243
  })
229
244
 
230
245
  it('validates deeply nested arrays', () => {
231
246
  const schema = array(array(array(integer())))
232
247
  const result = schema.safeParse([[[1, 2], [3]], [[4, 5, 6]]])
233
- expect(result.success).toBe(true)
248
+ expect(result).toMatchObject({ success: true })
234
249
  })
235
250
  })
236
251
  })