@atproto/lex-schema 0.1.5 → 0.1.6

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 (263) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/dist/core/$type.d.ts +2 -2
  3. package/dist/core/$type.d.ts.map +1 -1
  4. package/dist/core/$type.js.map +1 -1
  5. package/dist/core/record-key.d.ts +1 -1
  6. package/dist/core/record-key.d.ts.map +1 -1
  7. package/dist/core/record-key.js.map +1 -1
  8. package/dist/core/schema.d.ts +3 -2
  9. package/dist/core/schema.d.ts.map +1 -1
  10. package/dist/core/schema.js +1 -1
  11. package/dist/core/schema.js.map +1 -1
  12. package/dist/core/standard-schema.d.ts +2 -2
  13. package/dist/core/standard-schema.d.ts.map +1 -1
  14. package/dist/core/standard-schema.js.map +1 -1
  15. package/dist/core/string-format.d.ts +2 -2
  16. package/dist/core/string-format.d.ts.map +1 -1
  17. package/dist/core/string-format.js.map +1 -1
  18. package/dist/core/validation-error.d.ts +1 -1
  19. package/dist/core/validation-error.d.ts.map +1 -1
  20. package/dist/core/validation-error.js +1 -1
  21. package/dist/core/validation-error.js.map +1 -1
  22. package/dist/core/validator.d.ts +1 -1
  23. package/dist/core/validator.d.ts.map +1 -1
  24. package/dist/core/validator.js +1 -1
  25. package/dist/core/validator.js.map +1 -1
  26. package/dist/helpers.d.ts +2 -2
  27. package/dist/helpers.d.ts.map +1 -1
  28. package/dist/helpers.js +2 -2
  29. package/dist/helpers.js.map +1 -1
  30. package/dist/schema/array.d.ts +1 -1
  31. package/dist/schema/array.d.ts.map +1 -1
  32. package/dist/schema/array.js +1 -1
  33. package/dist/schema/array.js.map +1 -1
  34. package/dist/schema/blob.d.ts +1 -1
  35. package/dist/schema/blob.d.ts.map +1 -1
  36. package/dist/schema/blob.js +2 -2
  37. package/dist/schema/blob.js.map +1 -1
  38. package/dist/schema/boolean.js +1 -1
  39. package/dist/schema/boolean.js.map +1 -1
  40. package/dist/schema/bytes.js +1 -1
  41. package/dist/schema/bytes.js.map +1 -1
  42. package/dist/schema/cid.d.ts +1 -1
  43. package/dist/schema/cid.d.ts.map +1 -1
  44. package/dist/schema/cid.js +3 -3
  45. package/dist/schema/cid.js.map +1 -1
  46. package/dist/schema/custom.js +1 -1
  47. package/dist/schema/custom.js.map +1 -1
  48. package/dist/schema/dict.d.ts +1 -1
  49. package/dist/schema/dict.d.ts.map +1 -1
  50. package/dist/schema/dict.js +1 -1
  51. package/dist/schema/dict.js.map +1 -1
  52. package/dist/schema/discriminated-union.d.ts +1 -1
  53. package/dist/schema/discriminated-union.d.ts.map +1 -1
  54. package/dist/schema/discriminated-union.js +2 -1
  55. package/dist/schema/discriminated-union.js.map +1 -1
  56. package/dist/schema/enum.js +1 -1
  57. package/dist/schema/enum.js.map +1 -1
  58. package/dist/schema/integer.js +1 -1
  59. package/dist/schema/integer.js.map +1 -1
  60. package/dist/schema/intersection.d.ts +1 -1
  61. package/dist/schema/intersection.d.ts.map +1 -1
  62. package/dist/schema/intersection.js +3 -1
  63. package/dist/schema/intersection.js.map +1 -1
  64. package/dist/schema/lex-map.d.ts +1 -1
  65. package/dist/schema/lex-map.d.ts.map +1 -1
  66. package/dist/schema/lex-map.js +1 -1
  67. package/dist/schema/lex-map.js.map +1 -1
  68. package/dist/schema/lex-value.d.ts +1 -1
  69. package/dist/schema/lex-value.d.ts.map +1 -1
  70. package/dist/schema/lex-value.js +1 -1
  71. package/dist/schema/lex-value.js.map +1 -1
  72. package/dist/schema/literal.js +1 -1
  73. package/dist/schema/literal.js.map +1 -1
  74. package/dist/schema/never.js +1 -1
  75. package/dist/schema/never.js.map +1 -1
  76. package/dist/schema/null.js +1 -1
  77. package/dist/schema/null.js.map +1 -1
  78. package/dist/schema/nullable.d.ts +1 -1
  79. package/dist/schema/nullable.d.ts.map +1 -1
  80. package/dist/schema/nullable.js +1 -1
  81. package/dist/schema/nullable.js.map +1 -1
  82. package/dist/schema/object.d.ts +2 -1
  83. package/dist/schema/object.d.ts.map +1 -1
  84. package/dist/schema/object.js +1 -1
  85. package/dist/schema/object.js.map +1 -1
  86. package/dist/schema/optional.d.ts +2 -1
  87. package/dist/schema/optional.d.ts.map +1 -1
  88. package/dist/schema/optional.js +2 -1
  89. package/dist/schema/optional.js.map +1 -1
  90. package/dist/schema/params.d.ts +1 -1
  91. package/dist/schema/params.d.ts.map +1 -1
  92. package/dist/schema/params.js +1 -1
  93. package/dist/schema/params.js.map +1 -1
  94. package/dist/schema/payload.d.ts +3 -2
  95. package/dist/schema/payload.d.ts.map +1 -1
  96. package/dist/schema/payload.js +2 -1
  97. package/dist/schema/payload.js.map +1 -1
  98. package/dist/schema/permission-set.d.ts +1 -1
  99. package/dist/schema/permission-set.d.ts.map +1 -1
  100. package/dist/schema/permission-set.js +1 -0
  101. package/dist/schema/permission-set.js.map +1 -1
  102. package/dist/schema/permission.d.ts +1 -1
  103. package/dist/schema/permission.d.ts.map +1 -1
  104. package/dist/schema/permission.js.map +1 -1
  105. package/dist/schema/procedure.d.ts +1 -1
  106. package/dist/schema/procedure.d.ts.map +1 -1
  107. package/dist/schema/procedure.js +2 -0
  108. package/dist/schema/procedure.js.map +1 -1
  109. package/dist/schema/query.d.ts +1 -1
  110. package/dist/schema/query.d.ts.map +1 -1
  111. package/dist/schema/query.js +2 -0
  112. package/dist/schema/query.js.map +1 -1
  113. package/dist/schema/record.d.ts +2 -2
  114. package/dist/schema/record.d.ts.map +1 -1
  115. package/dist/schema/record.js +1 -1
  116. package/dist/schema/record.js.map +1 -1
  117. package/dist/schema/ref.d.ts +1 -1
  118. package/dist/schema/ref.d.ts.map +1 -1
  119. package/dist/schema/ref.js +1 -1
  120. package/dist/schema/ref.js.map +1 -1
  121. package/dist/schema/refine.d.ts +2 -2
  122. package/dist/schema/refine.d.ts.map +1 -1
  123. package/dist/schema/refine.js +1 -1
  124. package/dist/schema/refine.js.map +1 -1
  125. package/dist/schema/regexp.js +1 -1
  126. package/dist/schema/regexp.js.map +1 -1
  127. package/dist/schema/string.d.ts +2 -2
  128. package/dist/schema/string.d.ts.map +1 -1
  129. package/dist/schema/string.js +1 -1
  130. package/dist/schema/string.js.map +1 -1
  131. package/dist/schema/subscription.d.ts +3 -2
  132. package/dist/schema/subscription.d.ts.map +1 -1
  133. package/dist/schema/subscription.js +2 -0
  134. package/dist/schema/subscription.js.map +1 -1
  135. package/dist/schema/token.d.ts +1 -1
  136. package/dist/schema/token.d.ts.map +1 -1
  137. package/dist/schema/token.js +1 -1
  138. package/dist/schema/token.js.map +1 -1
  139. package/dist/schema/typed-object.d.ts +2 -2
  140. package/dist/schema/typed-object.d.ts.map +1 -1
  141. package/dist/schema/typed-object.js +1 -1
  142. package/dist/schema/typed-object.js.map +1 -1
  143. package/dist/schema/typed-ref.d.ts +1 -1
  144. package/dist/schema/typed-ref.d.ts.map +1 -1
  145. package/dist/schema/typed-ref.js +1 -1
  146. package/dist/schema/typed-ref.js.map +1 -1
  147. package/dist/schema/typed-union.d.ts +1 -1
  148. package/dist/schema/typed-union.d.ts.map +1 -1
  149. package/dist/schema/typed-union.js +3 -1
  150. package/dist/schema/typed-union.js.map +1 -1
  151. package/dist/schema/union.d.ts +1 -1
  152. package/dist/schema/union.d.ts.map +1 -1
  153. package/dist/schema/union.js +1 -1
  154. package/dist/schema/union.js.map +1 -1
  155. package/dist/schema/unknown.js +1 -1
  156. package/dist/schema/unknown.js.map +1 -1
  157. package/dist/schema/with-default.d.ts +1 -1
  158. package/dist/schema/with-default.d.ts.map +1 -1
  159. package/dist/schema/with-default.js +1 -1
  160. package/dist/schema/with-default.js.map +1 -1
  161. package/package.json +6 -10
  162. package/src/core/$type.test.ts +0 -24
  163. package/src/core/$type.ts +0 -199
  164. package/src/core/record-key.ts +0 -85
  165. package/src/core/result.ts +0 -15
  166. package/src/core/schema.ts +0 -412
  167. package/src/core/standard-schema.test.ts +0 -124
  168. package/src/core/standard-schema.ts +0 -31
  169. package/src/core/string-format.ts +0 -411
  170. package/src/core/types.ts +0 -120
  171. package/src/core/validation-error.ts +0 -134
  172. package/src/core/validation-issue.ts +0 -340
  173. package/src/core/validator.ts +0 -636
  174. package/src/core.ts +0 -9
  175. package/src/external.ts +0 -3
  176. package/src/helpers.test.ts +0 -694
  177. package/src/helpers.ts +0 -222
  178. package/src/index.ts +0 -3
  179. package/src/schema/array.test.ts +0 -251
  180. package/src/schema/array.ts +0 -126
  181. package/src/schema/blob.test.ts +0 -733
  182. package/src/schema/blob.ts +0 -150
  183. package/src/schema/boolean.test.ts +0 -118
  184. package/src/schema/boolean.ts +0 -46
  185. package/src/schema/bytes.test.ts +0 -227
  186. package/src/schema/bytes.ts +0 -81
  187. package/src/schema/cid.test.ts +0 -125
  188. package/src/schema/cid.ts +0 -69
  189. package/src/schema/custom.test.ts +0 -414
  190. package/src/schema/custom.ts +0 -106
  191. package/src/schema/dict.test.ts +0 -181
  192. package/src/schema/dict.ts +0 -122
  193. package/src/schema/discriminated-union.test.ts +0 -676
  194. package/src/schema/discriminated-union.ts +0 -196
  195. package/src/schema/enum.test.ts +0 -398
  196. package/src/schema/enum.ts +0 -77
  197. package/src/schema/integer.test.ts +0 -314
  198. package/src/schema/integer.ts +0 -86
  199. package/src/schema/intersection.test.ts +0 -33
  200. package/src/schema/intersection.ts +0 -113
  201. package/src/schema/lex-map.test.ts +0 -593
  202. package/src/schema/lex-map.ts +0 -63
  203. package/src/schema/lex-value.test.ts +0 -81
  204. package/src/schema/lex-value.ts +0 -86
  205. package/src/schema/literal.test.ts +0 -533
  206. package/src/schema/literal.ts +0 -70
  207. package/src/schema/never.test.ts +0 -175
  208. package/src/schema/never.ts +0 -56
  209. package/src/schema/null.test.ts +0 -80
  210. package/src/schema/null.ts +0 -49
  211. package/src/schema/nullable.test.ts +0 -470
  212. package/src/schema/nullable.ts +0 -74
  213. package/src/schema/object.test.ts +0 -69
  214. package/src/schema/object.ts +0 -136
  215. package/src/schema/optional.test.ts +0 -479
  216. package/src/schema/optional.ts +0 -92
  217. package/src/schema/params.test.ts +0 -1118
  218. package/src/schema/params.ts +0 -371
  219. package/src/schema/payload.test.ts +0 -340
  220. package/src/schema/payload.ts +0 -204
  221. package/src/schema/permission-set.test.ts +0 -613
  222. package/src/schema/permission-set.ts +0 -86
  223. package/src/schema/permission.test.ts +0 -537
  224. package/src/schema/permission.ts +0 -63
  225. package/src/schema/procedure.test.ts +0 -324
  226. package/src/schema/procedure.ts +0 -98
  227. package/src/schema/query.test.ts +0 -348
  228. package/src/schema/query.ts +0 -86
  229. package/src/schema/record.test.ts +0 -812
  230. package/src/schema/record.ts +0 -217
  231. package/src/schema/ref.test.ts +0 -349
  232. package/src/schema/ref.ts +0 -103
  233. package/src/schema/refine.test.ts +0 -579
  234. package/src/schema/refine.ts +0 -153
  235. package/src/schema/regexp.test.ts +0 -577
  236. package/src/schema/regexp.ts +0 -82
  237. package/src/schema/string.test.ts +0 -773
  238. package/src/schema/string.ts +0 -229
  239. package/src/schema/subscription.test.ts +0 -499
  240. package/src/schema/subscription.ts +0 -108
  241. package/src/schema/token.test.ts +0 -152
  242. package/src/schema/token.ts +0 -103
  243. package/src/schema/typed-object.test.ts +0 -745
  244. package/src/schema/typed-object.ts +0 -181
  245. package/src/schema/typed-ref.test.ts +0 -796
  246. package/src/schema/typed-ref.ts +0 -126
  247. package/src/schema/typed-union.test.ts +0 -355
  248. package/src/schema/typed-union.ts +0 -130
  249. package/src/schema/union.test.ts +0 -191
  250. package/src/schema/union.ts +0 -89
  251. package/src/schema/unknown.test.ts +0 -313
  252. package/src/schema/unknown.ts +0 -47
  253. package/src/schema/with-default.ts +0 -81
  254. package/src/schema.ts +0 -43
  255. package/src/util/array-agg.test.ts +0 -42
  256. package/src/util/array-agg.ts +0 -44
  257. package/src/util/assertion-util.ts +0 -1
  258. package/src/util/if-any.ts +0 -3
  259. package/src/util/lazy-property.ts +0 -14
  260. package/src/util/memoize.ts +0 -37
  261. package/tsconfig.build.json +0 -12
  262. package/tsconfig.json +0 -7
  263. package/tsconfig.tests.json +0 -8
@@ -1,733 +0,0 @@
1
- import { assert, describe, expect, it } from 'vitest'
2
- import { isLegacyBlobRef, isTypedBlobRef, parseCid } from '@atproto/lex-data'
3
- import { blob } from './blob.js'
4
-
5
- // await cidForRawBytes(Buffer.from('Hello, World!'))
6
- const blobCid = parseCid(
7
- 'bafkreig77vqcdozl2wyk6z3cscaj5q5fggi53aoh64fewkdiri3cdauyn4',
8
- )
9
- // await cidForLex(Buffer.from('Hello, World!'))
10
- const lexCid = parseCid(
11
- 'bafyreic52vzks7wdklat4evp3vimohl55i2unzqpshz2ytka5omzr7exdy',
12
- )
13
-
14
- describe('BlobSchema', () => {
15
- describe('basic validation', () => {
16
- const schema = blob({})
17
-
18
- it('validates valid blob references', () => {
19
- const result = schema.safeParse({
20
- $type: 'blob',
21
- ref: blobCid,
22
- mimeType: 'image/jpeg',
23
- size: 10000,
24
- })
25
- assert(result.success)
26
- assert(isTypedBlobRef(result.value))
27
- expect(result.value.$type).toBe('blob')
28
- expect(result.value.mimeType).toBe('image/jpeg')
29
- expect(result.value.size).toBe(10000)
30
- })
31
-
32
- it('validates blob with different mime types', () => {
33
- const result = schema.safeParse({
34
- $type: 'blob',
35
- ref: blobCid,
36
- mimeType: 'image/png',
37
- size: 5000,
38
- })
39
- assert(result.success)
40
- })
41
-
42
- it('validates blob with size 0', () => {
43
- const result = schema.safeParse({
44
- $type: 'blob',
45
- ref: blobCid,
46
- mimeType: 'text/plain',
47
- size: 0,
48
- })
49
- assert(result.success)
50
- })
51
-
52
- it('rejects non-objects', () => {
53
- const result = schema.safeParse('not an object')
54
- assert(!result.success)
55
- })
56
-
57
- it('rejects null', () => {
58
- const result = schema.safeParse(null)
59
- assert(!result.success)
60
- })
61
-
62
- it('rejects undefined', () => {
63
- const result = schema.safeParse(undefined)
64
- assert(!result.success)
65
- })
66
-
67
- it('rejects arrays', () => {
68
- const result = schema.safeParse([])
69
- assert(!result.success)
70
- })
71
-
72
- it('rejects numbers', () => {
73
- const result = schema.safeParse(123)
74
- assert(!result.success)
75
- })
76
-
77
- it('rejects booleans', () => {
78
- const result = schema.safeParse(true)
79
- assert(!result.success)
80
- })
81
- })
82
-
83
- describe('BlobRef validation', () => {
84
- const schema = blob({})
85
-
86
- it('rejects blob without $type', () => {
87
- const result = schema.safeParse({
88
- ref: blobCid,
89
- mimeType: 'image/jpeg',
90
- size: 10000,
91
- })
92
- assert(!result.success)
93
- })
94
-
95
- it('rejects blob with wrong $type', () => {
96
- const result = schema.safeParse({
97
- $type: 'notblob',
98
- ref: blobCid,
99
- mimeType: 'image/jpeg',
100
- size: 10000,
101
- })
102
- assert(!result.success)
103
- })
104
-
105
- it('rejects blob without ref', () => {
106
- const result = schema.safeParse({
107
- $type: 'blob',
108
- mimeType: 'image/jpeg',
109
- size: 10000,
110
- })
111
- assert(!result.success)
112
- })
113
-
114
- it('rejects blob without mimeType', () => {
115
- const result = schema.safeParse({
116
- $type: 'blob',
117
- ref: blobCid,
118
- size: 10000,
119
- })
120
- assert(!result.success)
121
- })
122
-
123
- it('rejects blob without size', () => {
124
- const result = schema.safeParse({
125
- $type: 'blob',
126
- ref: blobCid,
127
- mimeType: 'image/jpeg',
128
- })
129
- assert(!result.success)
130
- })
131
-
132
- it('rejects blob with invalid ref type', () => {
133
- const result = schema.safeParse({
134
- $type: 'blob',
135
- ref: 'not a cid',
136
- mimeType: 'image/jpeg',
137
- size: 10000,
138
- })
139
- assert(!result.success)
140
- })
141
-
142
- it('rejects blob with invalid mimeType type', () => {
143
- const result = schema.safeParse({
144
- $type: 'blob',
145
- ref: blobCid,
146
- mimeType: 123,
147
- size: 10000,
148
- })
149
- assert(!result.success)
150
- })
151
-
152
- it('rejects blob with invalid size type', () => {
153
- const result = schema.safeParse({
154
- $type: 'blob',
155
- ref: blobCid,
156
- mimeType: 'image/jpeg',
157
- size: '10000',
158
- })
159
- assert(!result.success)
160
- })
161
-
162
- it('rejects blob with negative size', () => {
163
- const result = schema.safeParse({
164
- $type: 'blob',
165
- ref: blobCid,
166
- mimeType: 'image/jpeg',
167
- size: -1,
168
- })
169
- assert(!result.success)
170
- })
171
-
172
- it('rejects blob with decimal size', () => {
173
- const result = schema.safeParse({
174
- $type: 'blob',
175
- ref: blobCid,
176
- mimeType: 'image/jpeg',
177
- size: 10000.5,
178
- })
179
- assert(!result.success)
180
- })
181
-
182
- it('rejects blob with extra properties', () => {
183
- const result = schema.safeParse({
184
- $type: 'blob',
185
- ref: blobCid,
186
- mimeType: 'image/jpeg',
187
- size: 10000,
188
- extra: 'not allowed',
189
- })
190
- assert(!result.success)
191
- })
192
-
193
- it('rejects blob with $link format for ref', () => {
194
- const result = schema.safeParse({
195
- $type: 'blob',
196
- ref: { $link: blobCid.toString() },
197
- mimeType: 'image/jpeg',
198
- size: 10000,
199
- })
200
- assert(!result.success)
201
- })
202
-
203
- it('rejects blob with unknown properties', () => {
204
- const result = schema.safeParse({
205
- $type: 'blob',
206
- ref: blobCid,
207
- mimeType: 'image/jpeg',
208
- size: 10000,
209
- unknownProp: 42,
210
- })
211
- assert(!result.success)
212
- })
213
- })
214
-
215
- describe('strict validation', () => {
216
- const schema = blob()
217
-
218
- it('accepts valid raw CID in strict mode', () => {
219
- const result = schema.safeParse(
220
- {
221
- $type: 'blob',
222
- ref: blobCid,
223
- mimeType: 'image/jpeg',
224
- size: 10000,
225
- },
226
- { strict: true },
227
- )
228
- assert(result.success)
229
- })
230
-
231
- it('rejects non-raw CID in strict mode', () => {
232
- const result = schema.safeParse(
233
- {
234
- $type: 'blob',
235
- ref: lexCid,
236
- mimeType: 'image/jpeg',
237
- size: 10000,
238
- },
239
- { strict: true },
240
- )
241
- assert(!result.success)
242
- })
243
-
244
- it('accepts non-raw CID in non-strict mode', () => {
245
- const result = schema.safeParse(
246
- {
247
- $type: 'blob',
248
- ref: lexCid,
249
- mimeType: 'image/jpeg',
250
- size: 10000,
251
- },
252
- { strict: false },
253
- )
254
- assert(result.success)
255
- })
256
- })
257
-
258
- describe('legacy blob format', () => {
259
- it('rejects legacy format by default (strict mode)', () => {
260
- const schema = blob({})
261
- const parseResult = schema.safeParse({
262
- cid: blobCid.toString(),
263
- mimeType: 'image/jpeg',
264
- })
265
- assert(!parseResult.success)
266
-
267
- const validateResult = schema.safeValidate({
268
- cid: blobCid.toString(),
269
- mimeType: 'image/jpeg',
270
- })
271
- assert(!validateResult.success)
272
- })
273
-
274
- it('rejects legacy format when strict: true is explicit', () => {
275
- const schema = blob({})
276
- const parseResult = schema.safeParse(
277
- {
278
- cid: blobCid.toString(),
279
- mimeType: 'image/jpeg',
280
- },
281
- { strict: true },
282
- )
283
- assert(!parseResult.success)
284
-
285
- const validateResult = schema.safeValidate(
286
- {
287
- cid: blobCid.toString(),
288
- mimeType: 'image/jpeg',
289
- },
290
- { strict: true },
291
- )
292
- assert(!validateResult.success)
293
- })
294
-
295
- it('accepts legacy format with strict: false in both parse and validate', () => {
296
- const schema = blob({})
297
- const parseResult = schema.safeParse(
298
- {
299
- cid: blobCid.toString(),
300
- mimeType: 'image/jpeg',
301
- },
302
- { strict: false },
303
- )
304
- assert(parseResult.success)
305
- assert(isLegacyBlobRef(parseResult.value))
306
- expect(parseResult.value).toMatchObject({
307
- cid: blobCid.toString(),
308
- mimeType: 'image/jpeg',
309
- })
310
- expect(parseResult.value.cid).toBe(blobCid.toString())
311
-
312
- const validateResult = schema.safeValidate(
313
- {
314
- cid: blobCid.toString(),
315
- mimeType: 'image/jpeg',
316
- },
317
- { strict: false },
318
- )
319
- assert(validateResult.success)
320
- assert(isLegacyBlobRef(validateResult.value))
321
- })
322
-
323
- it('accepts legacy format with lexCid in non-strict mode', () => {
324
- const schema = blob({})
325
- const result = schema.safeParse(
326
- {
327
- cid: lexCid.toString(),
328
- mimeType: 'image/png',
329
- },
330
- { strict: false },
331
- )
332
- assert(result.success)
333
- })
334
-
335
- it('rejects legacy format without cid', () => {
336
- const schema = blob({})
337
- const result = schema.safeParse(
338
- {
339
- mimeType: 'image/jpeg',
340
- },
341
- { strict: false },
342
- )
343
- assert(!result.success)
344
- })
345
-
346
- it('rejects legacy format without mimeType', () => {
347
- const schema = blob({})
348
- const result = schema.safeParse(
349
- {
350
- cid: blobCid.toString(),
351
- },
352
- { strict: false },
353
- )
354
- assert(!result.success)
355
- })
356
-
357
- it('rejects legacy format with invalid cid', () => {
358
- const schema = blob({})
359
- const result = schema.safeParse(
360
- {
361
- cid: 'invalid-cid',
362
- mimeType: 'image/jpeg',
363
- },
364
- { strict: false },
365
- )
366
- assert(!result.success)
367
- })
368
-
369
- it('rejects legacy format with numeric cid', () => {
370
- const schema = blob({})
371
- const result = schema.safeParse(
372
- {
373
- cid: 123,
374
- mimeType: 'image/jpeg',
375
- },
376
- { strict: false },
377
- )
378
- assert(!result.success)
379
- })
380
-
381
- it('rejects legacy format with extra properties', () => {
382
- const schema = blob({})
383
- const result = schema.safeParse(
384
- {
385
- cid: blobCid.toString(),
386
- mimeType: 'image/jpeg',
387
- extra: 'not allowed',
388
- },
389
- { strict: false },
390
- )
391
- assert(!result.success)
392
- })
393
-
394
- it('accepts standard BlobRef always, LegacyBlobRef only with strict: false', () => {
395
- const schema = blob({})
396
-
397
- const blobRefResult = schema.safeParse({
398
- $type: 'blob',
399
- ref: blobCid,
400
- mimeType: 'image/jpeg',
401
- size: 10000,
402
- })
403
- assert(blobRefResult.success)
404
-
405
- const legacyResultStrict = schema.safeParse({
406
- cid: blobCid.toString(),
407
- mimeType: 'image/jpeg',
408
- })
409
- assert(!legacyResultStrict.success)
410
-
411
- const legacyResultNonStrict = schema.safeParse(
412
- {
413
- cid: blobCid.toString(),
414
- mimeType: 'image/jpeg',
415
- },
416
- { strict: false },
417
- )
418
- assert(legacyResultNonStrict.success)
419
- })
420
- })
421
-
422
- describe('accept and maxSize options', () => {
423
- it('accepts blob with accept option (not enforced)', () => {
424
- const schema = blob({ accept: ['image/jpeg', 'image/png'] })
425
- const result = schema.safeParse({
426
- $type: 'blob',
427
- ref: blobCid,
428
- mimeType: 'image/gif',
429
- size: 10000,
430
- })
431
- assert(!result.success)
432
- })
433
-
434
- it('accepts blob with maxSize option (not enforced)', () => {
435
- const schema = blob({ maxSize: 1000 })
436
- const result = schema.safeParse({
437
- $type: 'blob',
438
- ref: blobCid,
439
- mimeType: 'image/jpeg',
440
- size: 10000,
441
- })
442
- assert(!result.success)
443
- })
444
-
445
- it('accepts blob matching accept constraint', () => {
446
- const schema = blob({ accept: ['image/jpeg', 'image/png'] })
447
- const result = schema.safeParse({
448
- $type: 'blob',
449
- ref: blobCid,
450
- mimeType: 'image/jpeg',
451
- size: 10000,
452
- })
453
- assert(result.success)
454
- })
455
-
456
- it('accepts blob matching maxSize constraint', () => {
457
- const schema = blob({ maxSize: 20000 })
458
- const result = schema.safeParse({
459
- $type: 'blob',
460
- ref: blobCid,
461
- mimeType: 'image/jpeg',
462
- size: 10000,
463
- })
464
- assert(result.success)
465
- })
466
- })
467
-
468
- describe('edge cases', () => {
469
- const schema = blob({})
470
-
471
- it('validates blob with large size', () => {
472
- const result = schema.safeParse({
473
- $type: 'blob',
474
- ref: blobCid,
475
- mimeType: 'video/mp4',
476
- size: Number.MAX_SAFE_INTEGER,
477
- })
478
- assert(result.success)
479
- })
480
-
481
- it('rejects empty object', () => {
482
- const result = schema.safeParse({})
483
- assert(!result.success)
484
- })
485
-
486
- it('rejects object with only $type', () => {
487
- const result = schema.safeParse({ $type: 'blob' })
488
- assert(!result.success)
489
- })
490
-
491
- it('rejects blob with empty mimeType', () => {
492
- const result = schema.safeParse({
493
- $type: 'blob',
494
- ref: blobCid,
495
- mimeType: '',
496
- size: 10000,
497
- })
498
- assert(!result.success)
499
- })
500
-
501
- it('rejects blob with null ref', () => {
502
- const result = schema.safeParse({
503
- $type: 'blob',
504
- ref: null,
505
- mimeType: 'image/jpeg',
506
- size: 10000,
507
- })
508
- assert(!result.success)
509
- })
510
-
511
- it('rejects blob with null mimeType', () => {
512
- const result = schema.safeParse({
513
- $type: 'blob',
514
- ref: blobCid,
515
- mimeType: null,
516
- size: 10000,
517
- })
518
- assert(!result.success)
519
- })
520
-
521
- it('rejects blob with null size', () => {
522
- const result = schema.safeParse({
523
- $type: 'blob',
524
- ref: blobCid,
525
- mimeType: 'image/jpeg',
526
- size: null,
527
- })
528
- assert(!result.success)
529
- })
530
- })
531
-
532
- describe('legacy blob format with strict mode combinations', () => {
533
- const schema = blob()
534
-
535
- describe('strict: true (default)', () => {
536
- it('rejects legacy blob format by default', () => {
537
- const parseResult = schema.safeParse({
538
- cid: blobCid.toString(),
539
- mimeType: 'image/jpeg',
540
- })
541
- assert(!parseResult.success)
542
-
543
- const validateResult = schema.safeValidate({
544
- cid: blobCid.toString(),
545
- mimeType: 'image/jpeg',
546
- })
547
- assert(!validateResult.success)
548
- })
549
-
550
- it('accepts standard BlobRef', () => {
551
- const result = schema.safeParse({
552
- $type: 'blob',
553
- ref: blobCid,
554
- mimeType: 'image/jpeg',
555
- size: 10000,
556
- })
557
- assert(result.success)
558
- })
559
- })
560
-
561
- describe('strict: false', () => {
562
- it('accepts legacy blob format in both parse and validate', () => {
563
- const parseResult = schema.safeParse(
564
- {
565
- cid: blobCid.toString(),
566
- mimeType: 'image/jpeg',
567
- },
568
- { strict: false },
569
- )
570
- assert(parseResult.success)
571
-
572
- const validateResult = schema.safeValidate(
573
- {
574
- cid: blobCid.toString(),
575
- mimeType: 'image/jpeg',
576
- },
577
- { strict: false },
578
- )
579
- assert(validateResult.success)
580
- })
581
-
582
- it('accepts legacy blob format with lexCid', () => {
583
- const result = schema.safeParse(
584
- {
585
- cid: lexCid.toString(),
586
- mimeType: 'image/png',
587
- },
588
- { strict: false },
589
- )
590
- assert(result.success)
591
- })
592
-
593
- it('accepts standard BlobRef with non-raw CID', () => {
594
- const result = schema.safeParse(
595
- {
596
- $type: 'blob',
597
- ref: lexCid,
598
- mimeType: 'image/jpeg',
599
- size: 10000,
600
- },
601
- { strict: false },
602
- )
603
- assert(result.success)
604
- })
605
- })
606
- })
607
-
608
- describe('mime and size checks depend on strict mode', () => {
609
- describe('accept constraint', () => {
610
- const schema = blob({ accept: ['image/jpeg', 'image/png'] })
611
-
612
- it('rejects non-matching mime type in strict mode (default)', () => {
613
- const result = schema.safeParse({
614
- $type: 'blob',
615
- ref: blobCid,
616
- mimeType: 'image/gif',
617
- size: 10000,
618
- })
619
- assert(!result.success)
620
- })
621
-
622
- it('accepts non-matching mime type in non-strict mode', () => {
623
- const result = schema.safeParse(
624
- {
625
- $type: 'blob',
626
- ref: blobCid,
627
- mimeType: 'image/gif',
628
- size: 10000,
629
- },
630
- { strict: false },
631
- )
632
- assert(result.success)
633
- })
634
-
635
- it('accepts matching mime type in strict mode', () => {
636
- const result = schema.safeParse({
637
- $type: 'blob',
638
- ref: blobCid,
639
- mimeType: 'image/jpeg',
640
- size: 10000,
641
- })
642
- assert(result.success)
643
- })
644
- })
645
-
646
- describe('maxSize constraint', () => {
647
- const schema = blob({ maxSize: 1000 })
648
-
649
- it('rejects oversized blob in strict mode (default)', () => {
650
- const result = schema.safeParse({
651
- $type: 'blob',
652
- ref: blobCid,
653
- mimeType: 'image/jpeg',
654
- size: 5000,
655
- })
656
- assert(!result.success)
657
- })
658
-
659
- it('accepts oversized blob in non-strict mode', () => {
660
- const result = schema.safeParse(
661
- {
662
- $type: 'blob',
663
- ref: blobCid,
664
- mimeType: 'image/jpeg',
665
- size: 5000,
666
- },
667
- { strict: false },
668
- )
669
- assert(result.success)
670
- })
671
-
672
- it('accepts correctly sized blob in strict mode', () => {
673
- const result = schema.safeParse({
674
- $type: 'blob',
675
- ref: blobCid,
676
- mimeType: 'image/jpeg',
677
- size: 500,
678
- })
679
- assert(result.success)
680
- })
681
- })
682
-
683
- describe('combined accept and maxSize constraints', () => {
684
- const schema = blob({
685
- accept: ['image/jpeg'],
686
- maxSize: 20000,
687
- })
688
-
689
- it('accepts valid blob in strict mode', () => {
690
- const result = schema.safeParse({
691
- $type: 'blob',
692
- ref: blobCid,
693
- mimeType: 'image/jpeg',
694
- size: 10000,
695
- })
696
- assert(result.success)
697
- })
698
-
699
- it('rejects wrong mime in strict mode', () => {
700
- const result = schema.safeParse({
701
- $type: 'blob',
702
- ref: blobCid,
703
- mimeType: 'image/png',
704
- size: 10000,
705
- })
706
- assert(!result.success)
707
- })
708
-
709
- it('rejects oversized in strict mode', () => {
710
- const result = schema.safeParse({
711
- $type: 'blob',
712
- ref: blobCid,
713
- mimeType: 'image/jpeg',
714
- size: 30000,
715
- })
716
- assert(!result.success)
717
- })
718
-
719
- it('accepts wrong mime and oversized in non-strict mode', () => {
720
- const result = schema.safeParse(
721
- {
722
- $type: 'blob',
723
- ref: blobCid,
724
- mimeType: 'video/mp4',
725
- size: 99999,
726
- },
727
- { strict: false },
728
- )
729
- assert(result.success)
730
- })
731
- })
732
- })
733
- })