@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,593 +0,0 @@
1
- import { describe, expect, it } from 'vitest'
2
- import { lexMap } from './lex-map.js'
3
-
4
- describe(lexMap, () => {
5
- describe('basic validation', () => {
6
- const schema = lexMap()
7
-
8
- it('accepts empty plain objects', () => {
9
- const result = schema.safeParse({})
10
- expect(result.success).toBe(true)
11
- if (result.success) {
12
- expect(result.value).toEqual({})
13
- }
14
- })
15
-
16
- it('accepts plain objects with string values', () => {
17
- const obj = { key: 'value', name: 'test' }
18
- const result = schema.safeParse(obj)
19
- expect(result.success).toBe(true)
20
- if (result.success) {
21
- expect(result.value).toEqual(obj)
22
- }
23
- })
24
-
25
- it('accepts plain objects with number values', () => {
26
- const obj = { count: 42, total: 100 }
27
- const result = schema.safeParse(obj)
28
- expect(result.success).toBe(true)
29
- if (result.success) {
30
- expect(result.value).toEqual(obj)
31
- }
32
- })
33
-
34
- it('accepts plain objects with boolean values', () => {
35
- const obj = { enabled: true, visible: false }
36
- const result = schema.safeParse(obj)
37
- expect(result.success).toBe(true)
38
- if (result.success) {
39
- expect(result.value).toEqual(obj)
40
- }
41
- })
42
-
43
- it('accepts plain objects with null values', () => {
44
- const obj = { value: null, optional: null }
45
- const result = schema.safeParse(obj)
46
- expect(result.success).toBe(true)
47
- if (result.success) {
48
- expect(result.value).toEqual(obj)
49
- }
50
- })
51
-
52
- it('accepts nested plain objects', () => {
53
- const obj = {
54
- nested: {
55
- deep: {
56
- value: 'test',
57
- },
58
- },
59
- }
60
- const result = schema.safeParse(obj)
61
- expect(result.success).toBe(true)
62
- if (result.success) {
63
- expect(result.value).toEqual(obj)
64
- }
65
- })
66
-
67
- it('accepts plain objects with array values', () => {
68
- const obj = {
69
- items: [1, 2, 3],
70
- names: ['alice', 'bob'],
71
- }
72
- const result = schema.safeParse(obj)
73
- expect(result.success).toBe(true)
74
- if (result.success) {
75
- expect(result.value).toEqual(obj)
76
- }
77
- })
78
-
79
- it('accepts plain objects with mixed value types', () => {
80
- const obj = {
81
- string: 'value',
82
- number: 42,
83
- boolean: true,
84
- null: null,
85
- array: [1, 'two', 3],
86
- nested: { key: 'value' },
87
- }
88
- const result = schema.safeParse(obj)
89
- expect(result.success).toBe(true)
90
- if (result.success) {
91
- expect(result.value).toEqual(obj)
92
- }
93
- })
94
-
95
- it('accepts plain objects with Uint8Array values', () => {
96
- const obj = {
97
- bytes: new Uint8Array([1, 2, 3, 4]),
98
- data: new Uint8Array([255, 0, 128]),
99
- }
100
- const result = schema.safeParse(obj)
101
- expect(result.success).toBe(true)
102
- if (result.success) {
103
- expect(result.value).toEqual(obj)
104
- }
105
- })
106
- })
107
-
108
- describe('rejects non-plain-objects', () => {
109
- const schema = lexMap()
110
-
111
- it('rejects strings', () => {
112
- const result = schema.safeParse('not an object')
113
- expect(result.success).toBe(false)
114
- })
115
-
116
- it('rejects numbers', () => {
117
- const result = schema.safeParse(42)
118
- expect(result.success).toBe(false)
119
- })
120
-
121
- it('rejects booleans', () => {
122
- const result = schema.safeParse(true)
123
- expect(result.success).toBe(false)
124
- })
125
-
126
- it('rejects null', () => {
127
- const result = schema.safeParse(null)
128
- expect(result.success).toBe(false)
129
- })
130
-
131
- it('rejects undefined', () => {
132
- const result = schema.safeParse(undefined)
133
- expect(result.success).toBe(false)
134
- })
135
-
136
- it('rejects arrays', () => {
137
- const result = schema.safeParse([1, 2, 3])
138
- expect(result.success).toBe(false)
139
- })
140
-
141
- it('rejects empty arrays', () => {
142
- const result = schema.safeParse([])
143
- expect(result.success).toBe(false)
144
- })
145
-
146
- it('rejects Date objects', () => {
147
- const result = schema.safeParse(new Date())
148
- expect(result.success).toBe(false)
149
- })
150
-
151
- it('rejects RegExp objects', () => {
152
- const result = schema.safeParse(/test/gi)
153
- expect(result.success).toBe(false)
154
- })
155
-
156
- it('rejects Map objects', () => {
157
- const result = schema.safeParse(new Map([['key', 'value']]))
158
- expect(result.success).toBe(false)
159
- })
160
-
161
- it('rejects Set objects', () => {
162
- const result = schema.safeParse(new Set([1, 2, 3]))
163
- expect(result.success).toBe(false)
164
- })
165
-
166
- it('rejects WeakMap objects', () => {
167
- const result = schema.safeParse(new WeakMap())
168
- expect(result.success).toBe(false)
169
- })
170
-
171
- it('rejects WeakSet objects', () => {
172
- const result = schema.safeParse(new WeakSet())
173
- expect(result.success).toBe(false)
174
- })
175
-
176
- it('rejects Error objects', () => {
177
- const result = schema.safeParse(new Error('test'))
178
- expect(result.success).toBe(false)
179
- })
180
-
181
- it('rejects Promise objects', () => {
182
- const result = schema.safeParse(Promise.resolve(42))
183
- expect(result.success).toBe(false)
184
- })
185
-
186
- it('rejects functions', () => {
187
- const result = schema.safeParse(() => 'test')
188
- expect(result.success).toBe(false)
189
- })
190
-
191
- it('rejects Symbol', () => {
192
- const result = schema.safeParse(Symbol('test'))
193
- expect(result.success).toBe(false)
194
- })
195
-
196
- it('rejects BigInt', () => {
197
- const result = schema.safeParse(BigInt(123))
198
- expect(result.success).toBe(false)
199
- })
200
-
201
- it('rejects class instances', () => {
202
- class TestClass {
203
- constructor(public value: string) {}
204
- }
205
- const result = schema.safeParse(new TestClass('test'))
206
- expect(result.success).toBe(false)
207
- })
208
- })
209
-
210
- describe('rejects invalid value types', () => {
211
- const schema = lexMap()
212
-
213
- it('rejects objects with floating point numbers', () => {
214
- const result = schema.safeParse({ value: 3.14 })
215
- expect(result.success).toBe(false)
216
- })
217
-
218
- it('rejects objects with NaN values', () => {
219
- const result = schema.safeParse({ value: NaN })
220
- expect(result.success).toBe(false)
221
- })
222
-
223
- it('rejects objects with Infinity values', () => {
224
- const result = schema.safeParse({ value: Infinity })
225
- expect(result.success).toBe(false)
226
- })
227
-
228
- it('rejects objects with -Infinity values', () => {
229
- const result = schema.safeParse({ value: -Infinity })
230
- expect(result.success).toBe(false)
231
- })
232
-
233
- it('rejects objects with undefined values', () => {
234
- const result = schema.safeParse({ value: undefined })
235
- expect(result.success).toBe(false)
236
- })
237
-
238
- it('rejects objects with function values', () => {
239
- const result = schema.safeParse({ fn: () => 'test' })
240
- expect(result.success).toBe(false)
241
- })
242
-
243
- it('rejects objects with Symbol values', () => {
244
- const result = schema.safeParse({ sym: Symbol('test') })
245
- expect(result.success).toBe(false)
246
- })
247
-
248
- it('rejects objects with BigInt values', () => {
249
- const result = schema.safeParse({ big: BigInt(123) })
250
- expect(result.success).toBe(false)
251
- })
252
-
253
- it('rejects objects with Date values', () => {
254
- const result = schema.safeParse({ date: new Date() })
255
- expect(result.success).toBe(false)
256
- })
257
-
258
- it('rejects objects with RegExp values', () => {
259
- const result = schema.safeParse({ pattern: /test/i })
260
- expect(result.success).toBe(false)
261
- })
262
-
263
- it('rejects objects with Map values', () => {
264
- const result = schema.safeParse({ map: new Map() })
265
- expect(result.success).toBe(false)
266
- })
267
-
268
- it('rejects objects with Set values', () => {
269
- const result = schema.safeParse({ set: new Set() })
270
- expect(result.success).toBe(false)
271
- })
272
-
273
- it('rejects objects with Error values', () => {
274
- const result = schema.safeParse({ error: new Error('test') })
275
- expect(result.success).toBe(false)
276
- })
277
-
278
- it('rejects objects with Promise values', () => {
279
- const result = schema.safeParse({ promise: Promise.resolve(1) })
280
- expect(result.success).toBe(false)
281
- })
282
-
283
- it('rejects objects with class instance values', () => {
284
- class TestClass {}
285
- const result = schema.safeParse({ instance: new TestClass() })
286
- expect(result.success).toBe(false)
287
- })
288
- })
289
-
290
- describe('rejects invalid nested values', () => {
291
- const schema = lexMap()
292
-
293
- it('rejects deeply nested invalid values', () => {
294
- const result = schema.safeParse({
295
- nested: {
296
- deep: {
297
- invalid: 3.14,
298
- },
299
- },
300
- })
301
- expect(result.success).toBe(false)
302
- })
303
-
304
- it('rejects arrays with invalid values', () => {
305
- const result = schema.safeParse({
306
- items: [1, 2, 3.14],
307
- })
308
- expect(result.success).toBe(false)
309
- })
310
-
311
- it('rejects arrays with undefined values', () => {
312
- const result = schema.safeParse({
313
- items: [1, undefined, 3],
314
- })
315
- expect(result.success).toBe(false)
316
- })
317
-
318
- it('rejects nested arrays with invalid values', () => {
319
- const result = schema.safeParse({
320
- matrix: [
321
- [1, 2],
322
- [3, 4.5],
323
- ],
324
- })
325
- expect(result.success).toBe(false)
326
- })
327
-
328
- it('rejects mixed valid and invalid keys', () => {
329
- const result = schema.safeParse({
330
- valid: 'string',
331
- invalid: Infinity,
332
- })
333
- expect(result.success).toBe(false)
334
- })
335
- })
336
-
337
- describe('edge cases', () => {
338
- const schema = lexMap()
339
-
340
- it('accepts objects with numeric string keys', () => {
341
- const obj = { '0': 'zero', '1': 'one', '2': 'two' }
342
- const result = schema.safeParse(obj)
343
- expect(result.success).toBe(true)
344
- if (result.success) {
345
- expect(result.value).toEqual(obj)
346
- }
347
- })
348
-
349
- it('accepts objects with special string keys', () => {
350
- const obj = {
351
- 'with spaces': 'value',
352
- 'with-dashes': 'value',
353
- 'with.dots': 'value',
354
- with_underscores: 'value',
355
- }
356
- const result = schema.safeParse(obj)
357
- expect(result.success).toBe(true)
358
- if (result.success) {
359
- expect(result.value).toEqual(obj)
360
- }
361
- })
362
-
363
- it('accepts objects with empty string keys', () => {
364
- const obj = { '': 'empty key value' }
365
- const result = schema.safeParse(obj)
366
- expect(result.success).toBe(true)
367
- if (result.success) {
368
- expect(result.value).toEqual(obj)
369
- }
370
- })
371
-
372
- it('accepts objects with zero values', () => {
373
- const obj = { count: 0, index: 0 }
374
- const result = schema.safeParse(obj)
375
- expect(result.success).toBe(true)
376
- if (result.success) {
377
- expect(result.value).toEqual(obj)
378
- }
379
- })
380
-
381
- it('accepts objects with negative integer values', () => {
382
- const obj = { temperature: -10, balance: -500 }
383
- const result = schema.safeParse(obj)
384
- expect(result.success).toBe(true)
385
- if (result.success) {
386
- expect(result.value).toEqual(obj)
387
- }
388
- })
389
-
390
- it('accepts objects with empty string values', () => {
391
- const obj = { name: '', description: '' }
392
- const result = schema.safeParse(obj)
393
- expect(result.success).toBe(true)
394
- if (result.success) {
395
- expect(result.value).toEqual(obj)
396
- }
397
- })
398
-
399
- it('accepts objects with empty array values', () => {
400
- const obj = { items: [], tags: [] }
401
- const result = schema.safeParse(obj)
402
- expect(result.success).toBe(true)
403
- if (result.success) {
404
- expect(result.value).toEqual(obj)
405
- }
406
- })
407
-
408
- it('accepts objects with empty nested objects', () => {
409
- const obj = { config: {}, metadata: {} }
410
- const result = schema.safeParse(obj)
411
- expect(result.success).toBe(true)
412
- if (result.success) {
413
- expect(result.value).toEqual(obj)
414
- }
415
- })
416
-
417
- it('accepts objects with empty Uint8Array values', () => {
418
- const obj = { data: new Uint8Array([]) }
419
- const result = schema.safeParse(obj)
420
- expect(result.success).toBe(true)
421
- if (result.success) {
422
- expect(result.value).toEqual(obj)
423
- }
424
- })
425
-
426
- it('accepts deeply nested structures', () => {
427
- const obj = {
428
- level1: {
429
- level2: {
430
- level3: {
431
- level4: {
432
- level5: {
433
- value: 'deep',
434
- array: [1, 2, [3, 4, [5]]],
435
- },
436
- },
437
- },
438
- },
439
- },
440
- }
441
- const result = schema.safeParse(obj)
442
- expect(result.success).toBe(true)
443
- if (result.success) {
444
- expect(result.value).toEqual(obj)
445
- }
446
- })
447
-
448
- it('accepts complex nested arrays and objects', () => {
449
- const obj = {
450
- matrix: [
451
- [1, 2],
452
- [3, 4],
453
- [5, 6],
454
- ],
455
- nested: {
456
- arrays: [[['deep']]],
457
- mixed: [{ a: 1 }, { b: 2 }],
458
- },
459
- }
460
- const result = schema.safeParse(obj)
461
- expect(result.success).toBe(true)
462
- if (result.success) {
463
- expect(result.value).toEqual(obj)
464
- }
465
- })
466
-
467
- it('accepts objects with null prototype', () => {
468
- const obj = Object.create(null)
469
- obj.key = 'value'
470
- obj.count = 42
471
- const result = schema.safeParse(obj)
472
- expect(result.success).toBe(true)
473
- if (result.success) {
474
- expect(result.value).toBe(obj)
475
- }
476
- })
477
-
478
- it('accepts objects with $type property', () => {
479
- const obj = { $type: 'app.bsky.feed.post', text: 'Hello' }
480
- const result = schema.safeParse(obj)
481
- expect(result.success).toBe(true)
482
- if (result.success) {
483
- expect(result.value).toEqual(obj)
484
- }
485
- })
486
-
487
- it('accepts objects with various special characters in keys', () => {
488
- const obj = {
489
- '@mention': 'user',
490
- '#hashtag': 'tag',
491
- '!important': 'flag',
492
- 'key:value': 'pair',
493
- }
494
- const result = schema.safeParse(obj)
495
- expect(result.success).toBe(true)
496
- if (result.success) {
497
- expect(result.value).toEqual(obj)
498
- }
499
- })
500
- })
501
-
502
- describe('large objects', () => {
503
- const schema = lexMap()
504
-
505
- it('accepts objects with many keys', () => {
506
- const obj: Record<string, number> = {}
507
- for (let i = 0; i < 100; i++) {
508
- obj[`key${i}`] = i
509
- }
510
- const result = schema.safeParse(obj)
511
- expect(result.success).toBe(true)
512
- if (result.success) {
513
- expect(result.value).toEqual(obj)
514
- }
515
- })
516
-
517
- it('accepts objects with large arrays', () => {
518
- const obj = {
519
- numbers: Array.from({ length: 1000 }, (_, i) => i),
520
- }
521
- const result = schema.safeParse(obj)
522
- expect(result.success).toBe(true)
523
- if (result.success) {
524
- expect(result.value).toEqual(obj)
525
- }
526
- })
527
-
528
- it('accepts objects with large Uint8Array values', () => {
529
- const obj = {
530
- data: new Uint8Array(1000).fill(0),
531
- }
532
- const result = schema.safeParse(obj)
533
- expect(result.success).toBe(true)
534
- if (result.success) {
535
- expect(result.value).toEqual(obj)
536
- }
537
- })
538
- })
539
-
540
- describe('preservation of input', () => {
541
- const schema = lexMap()
542
-
543
- it('preserves the original object reference', () => {
544
- const input = { key: 'value', count: 42 }
545
- const result = schema.safeParse(input)
546
-
547
- if (result.success) {
548
- expect(result.value).toBe(input)
549
- } else {
550
- throw new Error('Expected validation to succeed')
551
- }
552
- })
553
-
554
- it('preserves nested object references', () => {
555
- const nested = { inner: 'value' }
556
- const input = { outer: nested }
557
- const result = schema.safeParse(input)
558
-
559
- if (result.success) {
560
- expect(result.value).toBe(input)
561
- expect(result.value.outer).toBe(nested)
562
- } else {
563
- throw new Error('Expected validation to succeed')
564
- }
565
- })
566
-
567
- it('preserves array references in object values', () => {
568
- const arr = [1, 2, 3]
569
- const input = { items: arr }
570
- const result = schema.safeParse(input)
571
-
572
- if (result.success) {
573
- expect(result.value).toBe(input)
574
- expect(result.value.items).toBe(arr)
575
- } else {
576
- throw new Error('Expected validation to succeed')
577
- }
578
- })
579
-
580
- it('preserves Uint8Array references in object values', () => {
581
- const bytes = new Uint8Array([1, 2, 3])
582
- const input = { data: bytes }
583
- const result = schema.safeParse(input)
584
-
585
- if (result.success) {
586
- expect(result.value).toBe(input)
587
- expect(result.value.data).toBe(bytes)
588
- } else {
589
- throw new Error('Expected validation to succeed')
590
- }
591
- })
592
- })
593
- })
@@ -1,63 +0,0 @@
1
- import { LexMap, isPlainObject } from '@atproto/lex-data'
2
- import { Schema, ValidationContext } from '../core.js'
3
- import { memoizedOptions } from '../util/memoize.js'
4
- import { lexValue } from './lex-value.js'
5
-
6
- const propertyValueSchema = /*#__PURE__*/ lexValue()
7
-
8
- export type { LexMap }
9
-
10
- /**
11
- * AT Protocol lexicon schema definitions with "type": "unknown" are represented
12
- * as plain objects with string keys and values that are valid AT Protocol data
13
- * types (string, integer, boolean, null, bytes, cid, array, or object). This
14
- * type alias corresponds to the expected structure of such "unknown" schema
15
- * values.
16
- */
17
- export class LexMapSchema extends Schema<LexMap> {
18
- readonly type = 'lexMap' as const
19
-
20
- validateInContext(input: unknown, ctx: ValidationContext) {
21
- if (!isPlainObject(input)) {
22
- return ctx.issueUnexpectedType(input, 'object')
23
- }
24
-
25
- for (const key of Object.keys(input)) {
26
- // @NOTE We use a lexValue() schema here to recursively validate all
27
- // nested values, which ensures that the error reporting includes the
28
- // correct path and type information for any invalid nested values. This
29
- // allows for more informative error descriptions than a simple "isLexMap"
30
- // check.
31
- const r = ctx.validateChild(input, key, propertyValueSchema) // recursively validate all properties
32
- if (!r.success) return r
33
- }
34
-
35
- return ctx.success(input)
36
- }
37
- }
38
-
39
- /**
40
- * Creates a schema that accepts any plain object with string keys and values
41
- * that are valid AT Protocol data types (string, integer, boolean, null, bytes,
42
- * cid, array, or object).
43
- *
44
- * @see {@link LexMap} from `@atproto/lex-data` for the type definition of valid AT Protocol data types
45
- * @returns A new {@link LexMapSchema} instance
46
- *
47
- * @example
48
- * ```ts
49
- * // Accept any object shape
50
- * const schema = l.lexMap()
51
- *
52
- * schema.validate({ any: 'props' }) // success
53
- * schema.validate([1, 2, 3]) // fails - only plain objects are accepted
54
- * schema.validate({ foo: new Date() }) // fails - Date is not a valid LexValue
55
- * schema.validate({ foo: 1.2 }) // fails - 1.2 is not a valid LexValue (not an integer)
56
- * ```
57
- */
58
- export const lexMap = /*#__PURE__*/ memoizedOptions(function () {
59
- return new LexMapSchema()
60
- })
61
-
62
- /** @deprecated Use {@link lexMap} instead */
63
- export const unknownObject = lexMap