@atproto/lex-schema 0.0.9 → 0.0.11

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 (280) hide show
  1. package/CHANGELOG.md +41 -0
  2. package/LICENSE.txt +1 -1
  3. package/dist/core/$type.d.ts +11 -0
  4. package/dist/core/$type.d.ts.map +1 -1
  5. package/dist/core/$type.js +4 -0
  6. package/dist/core/$type.js.map +1 -1
  7. package/dist/core/schema.d.ts +31 -24
  8. package/dist/core/schema.d.ts.map +1 -1
  9. package/dist/core/schema.js +38 -8
  10. package/dist/core/schema.js.map +1 -1
  11. package/dist/core/string-format.d.ts +35 -35
  12. package/dist/core/string-format.d.ts.map +1 -1
  13. package/dist/core/string-format.js +49 -91
  14. package/dist/core/string-format.js.map +1 -1
  15. package/dist/core/validation-error.d.ts +1 -1
  16. package/dist/core/validation-issue.js +1 -1
  17. package/dist/core/validation-issue.js.map +1 -1
  18. package/dist/core/validator.d.ts +53 -32
  19. package/dist/core/validator.d.ts.map +1 -1
  20. package/dist/core/validator.js +18 -22
  21. package/dist/core/validator.js.map +1 -1
  22. package/dist/external.d.ts +0 -85
  23. package/dist/external.d.ts.map +1 -1
  24. package/dist/external.js +0 -164
  25. package/dist/external.js.map +1 -1
  26. package/dist/helpers.d.ts +13 -5
  27. package/dist/helpers.d.ts.map +1 -1
  28. package/dist/helpers.js +4 -4
  29. package/dist/helpers.js.map +1 -1
  30. package/dist/schema/array.d.ts +9 -5
  31. package/dist/schema/array.d.ts.map +1 -1
  32. package/dist/schema/array.js +14 -5
  33. package/dist/schema/array.js.map +1 -1
  34. package/dist/schema/blob.d.ts +9 -7
  35. package/dist/schema/blob.d.ts.map +1 -1
  36. package/dist/schema/blob.js +9 -5
  37. package/dist/schema/blob.js.map +1 -1
  38. package/dist/schema/boolean.d.ts +3 -7
  39. package/dist/schema/boolean.d.ts.map +1 -1
  40. package/dist/schema/boolean.js +6 -7
  41. package/dist/schema/boolean.js.map +1 -1
  42. package/dist/schema/bytes.d.ts +3 -2
  43. package/dist/schema/bytes.d.ts.map +1 -1
  44. package/dist/schema/bytes.js +7 -3
  45. package/dist/schema/bytes.js.map +1 -1
  46. package/dist/schema/cid.d.ts +7 -7
  47. package/dist/schema/cid.d.ts.map +1 -1
  48. package/dist/schema/cid.js +5 -1
  49. package/dist/schema/cid.js.map +1 -1
  50. package/dist/schema/custom.d.ts +6 -5
  51. package/dist/schema/custom.d.ts.map +1 -1
  52. package/dist/schema/custom.js +10 -4
  53. package/dist/schema/custom.js.map +1 -1
  54. package/dist/schema/dict.d.ts +8 -8
  55. package/dist/schema/dict.d.ts.map +1 -1
  56. package/dist/schema/dict.js +11 -2
  57. package/dist/schema/dict.js.map +1 -1
  58. package/dist/schema/discriminated-union.d.ts +21 -14
  59. package/dist/schema/discriminated-union.d.ts.map +1 -1
  60. package/dist/schema/discriminated-union.js +7 -0
  61. package/dist/schema/discriminated-union.js.map +1 -1
  62. package/dist/schema/enum.d.ts +7 -9
  63. package/dist/schema/enum.d.ts.map +1 -1
  64. package/dist/schema/enum.js +8 -4
  65. package/dist/schema/enum.js.map +1 -1
  66. package/dist/schema/integer.d.ts +5 -5
  67. package/dist/schema/integer.d.ts.map +1 -1
  68. package/dist/schema/integer.js +9 -5
  69. package/dist/schema/integer.js.map +1 -1
  70. package/dist/schema/intersection.d.ts +4 -4
  71. package/dist/schema/intersection.d.ts.map +1 -1
  72. package/dist/schema/intersection.js +5 -0
  73. package/dist/schema/intersection.js.map +1 -1
  74. package/dist/schema/literal.d.ts +6 -9
  75. package/dist/schema/literal.d.ts.map +1 -1
  76. package/dist/schema/literal.js +7 -4
  77. package/dist/schema/literal.js.map +1 -1
  78. package/dist/schema/never.d.ts +3 -2
  79. package/dist/schema/never.d.ts.map +1 -1
  80. package/dist/schema/never.js +5 -1
  81. package/dist/schema/never.js.map +1 -1
  82. package/dist/schema/null.d.ts +4 -3
  83. package/dist/schema/null.d.ts.map +1 -1
  84. package/dist/schema/null.js +6 -4
  85. package/dist/schema/null.js.map +1 -1
  86. package/dist/schema/nullable.d.ts +6 -5
  87. package/dist/schema/nullable.d.ts.map +1 -1
  88. package/dist/schema/nullable.js +9 -5
  89. package/dist/schema/nullable.js.map +1 -1
  90. package/dist/schema/object.d.ts +10 -8
  91. package/dist/schema/object.d.ts.map +1 -1
  92. package/dist/schema/object.js +11 -3
  93. package/dist/schema/object.js.map +1 -1
  94. package/dist/schema/optional.d.ts +7 -5
  95. package/dist/schema/optional.d.ts.map +1 -1
  96. package/dist/schema/optional.js +14 -6
  97. package/dist/schema/optional.js.map +1 -1
  98. package/dist/schema/params.d.ts +26 -13
  99. package/dist/schema/params.d.ts.map +1 -1
  100. package/dist/schema/params.js +47 -25
  101. package/dist/schema/params.js.map +1 -1
  102. package/dist/schema/payload.d.ts +12 -9
  103. package/dist/schema/payload.d.ts.map +1 -1
  104. package/dist/schema/payload.js +11 -0
  105. package/dist/schema/payload.js.map +1 -1
  106. package/dist/schema/permission-set.d.ts +1 -0
  107. package/dist/schema/permission-set.d.ts.map +1 -1
  108. package/dist/schema/permission-set.js +5 -0
  109. package/dist/schema/permission-set.js.map +1 -1
  110. package/dist/schema/permission.d.ts +6 -5
  111. package/dist/schema/permission.d.ts.map +1 -1
  112. package/dist/schema/permission.js +5 -0
  113. package/dist/schema/permission.js.map +1 -1
  114. package/dist/schema/procedure.d.ts +2 -1
  115. package/dist/schema/procedure.d.ts.map +1 -1
  116. package/dist/schema/procedure.js +5 -0
  117. package/dist/schema/procedure.js.map +1 -1
  118. package/dist/schema/query.d.ts +2 -1
  119. package/dist/schema/query.d.ts.map +1 -1
  120. package/dist/schema/query.js +5 -0
  121. package/dist/schema/query.js.map +1 -1
  122. package/dist/schema/record.d.ts +48 -30
  123. package/dist/schema/record.d.ts.map +1 -1
  124. package/dist/schema/record.js +12 -9
  125. package/dist/schema/record.js.map +1 -1
  126. package/dist/schema/ref.d.ts +9 -6
  127. package/dist/schema/ref.d.ts.map +1 -1
  128. package/dist/schema/ref.js +9 -16
  129. package/dist/schema/ref.js.map +1 -1
  130. package/dist/schema/refine.d.ts +4 -4
  131. package/dist/schema/refine.d.ts.map +1 -1
  132. package/dist/schema/refine.js.map +1 -1
  133. package/dist/schema/regexp.d.ts +4 -3
  134. package/dist/schema/regexp.d.ts.map +1 -1
  135. package/dist/schema/regexp.js +5 -0
  136. package/dist/schema/regexp.js.map +1 -1
  137. package/dist/schema/string.d.ts +7 -8
  138. package/dist/schema/string.d.ts.map +1 -1
  139. package/dist/schema/string.js +13 -19
  140. package/dist/schema/string.js.map +1 -1
  141. package/dist/schema/subscription.d.ts +2 -1
  142. package/dist/schema/subscription.d.ts.map +1 -1
  143. package/dist/schema/subscription.js +5 -0
  144. package/dist/schema/subscription.js.map +1 -1
  145. package/dist/schema/token.d.ts +6 -5
  146. package/dist/schema/token.d.ts.map +1 -1
  147. package/dist/schema/token.js +5 -0
  148. package/dist/schema/token.js.map +1 -1
  149. package/dist/schema/typed-object.d.ts +43 -26
  150. package/dist/schema/typed-object.d.ts.map +1 -1
  151. package/dist/schema/typed-object.js +6 -3
  152. package/dist/schema/typed-object.js.map +1 -1
  153. package/dist/schema/typed-ref.d.ts +16 -25
  154. package/dist/schema/typed-ref.d.ts.map +1 -1
  155. package/dist/schema/typed-ref.js +7 -17
  156. package/dist/schema/typed-ref.js.map +1 -1
  157. package/dist/schema/typed-union.d.ts +9 -21
  158. package/dist/schema/typed-union.d.ts.map +1 -1
  159. package/dist/schema/typed-union.js +15 -11
  160. package/dist/schema/typed-union.js.map +1 -1
  161. package/dist/schema/union.d.ts +6 -6
  162. package/dist/schema/union.d.ts.map +1 -1
  163. package/dist/schema/union.js +7 -5
  164. package/dist/schema/union.js.map +1 -1
  165. package/dist/schema/unknown-object.d.ts +5 -4
  166. package/dist/schema/unknown-object.d.ts.map +1 -1
  167. package/dist/schema/unknown-object.js +5 -1
  168. package/dist/schema/unknown-object.js.map +1 -1
  169. package/dist/schema/unknown.d.ts +3 -2
  170. package/dist/schema/unknown.d.ts.map +1 -1
  171. package/dist/schema/unknown.js +5 -1
  172. package/dist/schema/unknown.js.map +1 -1
  173. package/dist/schema/with-default.d.ts +9 -0
  174. package/dist/schema/with-default.d.ts.map +1 -0
  175. package/dist/schema/with-default.js +27 -0
  176. package/dist/schema/with-default.js.map +1 -0
  177. package/dist/schema.d.ts +2 -2
  178. package/dist/schema.d.ts.map +1 -1
  179. package/dist/schema.js +2 -4
  180. package/dist/schema.js.map +1 -1
  181. package/dist/util/assertion-util.d.ts +0 -6
  182. package/dist/util/assertion-util.d.ts.map +1 -1
  183. package/dist/util/assertion-util.js +0 -28
  184. package/dist/util/assertion-util.js.map +1 -1
  185. package/dist/util/memoize.d.ts +2 -2
  186. package/dist/util/memoize.d.ts.map +1 -1
  187. package/dist/util/memoize.js +23 -39
  188. package/dist/util/memoize.js.map +1 -1
  189. package/package.json +3 -3
  190. package/src/core/$type.test.ts +20 -0
  191. package/src/core/$type.ts +30 -0
  192. package/src/core/schema.ts +86 -38
  193. package/src/core/string-format.ts +119 -158
  194. package/src/core/validation-issue.ts +1 -1
  195. package/src/core/validator.ts +93 -53
  196. package/src/external.ts +0 -404
  197. package/src/helpers.test.ts +22 -21
  198. package/src/helpers.ts +19 -14
  199. package/src/schema/array.test.ts +38 -40
  200. package/src/schema/array.ts +35 -13
  201. package/src/schema/blob.test.ts +21 -21
  202. package/src/schema/blob.ts +19 -17
  203. package/src/schema/boolean.test.ts +9 -8
  204. package/src/schema/boolean.ts +7 -13
  205. package/src/schema/bytes.test.ts +13 -13
  206. package/src/schema/bytes.ts +13 -8
  207. package/src/schema/cid.test.ts +3 -3
  208. package/src/schema/cid.ts +13 -12
  209. package/src/schema/custom.test.ts +26 -26
  210. package/src/schema/custom.ts +20 -13
  211. package/src/schema/dict.test.ts +21 -39
  212. package/src/schema/dict.ts +28 -19
  213. package/src/schema/discriminated-union.test.ts +128 -128
  214. package/src/schema/discriminated-union.ts +45 -26
  215. package/src/schema/enum.test.ts +17 -16
  216. package/src/schema/enum.ts +16 -16
  217. package/src/schema/integer.test.ts +22 -21
  218. package/src/schema/integer.ts +12 -9
  219. package/src/schema/intersection.test.ts +10 -10
  220. package/src/schema/intersection.ts +17 -14
  221. package/src/schema/literal.test.ts +35 -34
  222. package/src/schema/literal.ts +12 -15
  223. package/src/schema/never.test.ts +5 -5
  224. package/src/schema/never.ts +7 -2
  225. package/src/schema/null.test.ts +3 -3
  226. package/src/schema/null.ts +9 -9
  227. package/src/schema/nullable.test.ts +31 -42
  228. package/src/schema/nullable.ts +17 -9
  229. package/src/schema/object.test.ts +10 -12
  230. package/src/schema/object.ts +27 -18
  231. package/src/schema/optional.test.ts +21 -28
  232. package/src/schema/optional.ts +27 -10
  233. package/src/schema/params.test.ts +471 -47
  234. package/src/schema/params.ts +74 -38
  235. package/src/schema/payload.test.ts +150 -156
  236. package/src/schema/payload.ts +35 -19
  237. package/src/schema/permission-set.test.ts +206 -273
  238. package/src/schema/permission-set.ts +8 -0
  239. package/src/schema/permission.test.ts +177 -177
  240. package/src/schema/permission.ts +13 -5
  241. package/src/schema/procedure.test.ts +183 -242
  242. package/src/schema/procedure.ts +18 -5
  243. package/src/schema/query.test.ts +186 -200
  244. package/src/schema/query.ts +16 -4
  245. package/src/schema/record.test.ts +121 -101
  246. package/src/schema/record.ts +74 -40
  247. package/src/schema/ref.test.ts +101 -118
  248. package/src/schema/ref.ts +33 -28
  249. package/src/schema/refine.test.ts +28 -28
  250. package/src/schema/refine.ts +23 -20
  251. package/src/schema/regexp.test.ts +29 -33
  252. package/src/schema/regexp.ts +11 -7
  253. package/src/schema/string.test.ts +35 -35
  254. package/src/schema/string.ts +24 -33
  255. package/src/schema/subscription.test.ts +259 -387
  256. package/src/schema/subscription.ts +16 -4
  257. package/src/schema/token.test.ts +47 -324
  258. package/src/schema/token.ts +14 -7
  259. package/src/schema/typed-object.test.ts +98 -81
  260. package/src/schema/typed-object.ts +68 -33
  261. package/src/schema/typed-ref.test.ts +206 -234
  262. package/src/schema/typed-ref.ts +40 -42
  263. package/src/schema/typed-union.test.ts +40 -64
  264. package/src/schema/typed-union.ts +36 -58
  265. package/src/schema/union.test.ts +17 -27
  266. package/src/schema/union.ts +20 -16
  267. package/src/schema/unknown-object.test.ts +8 -8
  268. package/src/schema/unknown-object.ts +9 -7
  269. package/src/schema/unknown.test.ts +4 -4
  270. package/src/schema/unknown.ts +7 -5
  271. package/src/schema/with-default.ts +35 -0
  272. package/src/schema.ts +2 -6
  273. package/src/util/assertion-util.ts +0 -39
  274. package/src/util/memoize.ts +26 -46
  275. package/dist/schema/_parameters.d.ts +0 -17
  276. package/dist/schema/_parameters.d.ts.map +0 -1
  277. package/dist/schema/_parameters.js +0 -20
  278. package/dist/schema/_parameters.js.map +0 -1
  279. package/src/schema/_parameters.test.ts +0 -417
  280. package/src/schema/_parameters.ts +0 -26
@@ -1,22 +1,23 @@
1
1
  import { describe, expect, it } from 'vitest'
2
- import { IntegerSchema } from './integer.js'
3
- import { ObjectSchema } from './object.js'
4
- import { StringSchema } from './string.js'
5
- import { TypedObjectSchema } from './typed-object.js'
6
- import { TypedRefSchema } from './typed-ref.js'
2
+ import { integer } from './integer.js'
3
+ import { object } from './object.js'
4
+ import { string } from './string.js'
5
+ import { typedObject } from './typed-object.js'
6
+ import { typedRef } from './typed-ref.js'
7
7
 
8
8
  describe('TypedRefSchema', () => {
9
9
  describe('basic validation', () => {
10
10
  it('validates through a typed object reference with explicit $type', () => {
11
- const typedObject = new TypedObjectSchema(
11
+ const typedObjectSchema = typedObject(
12
12
  'com.example.user',
13
- new ObjectSchema({
14
- name: new StringSchema({}),
15
- age: new IntegerSchema({}),
13
+ 'main',
14
+ object({
15
+ name: string(),
16
+ age: integer(),
16
17
  }),
17
18
  )
18
19
 
19
- const schema = new TypedRefSchema(() => typedObject)
20
+ const schema = typedRef(() => typedObjectSchema)
20
21
 
21
22
  const result = schema.safeParse({
22
23
  $type: 'com.example.user',
@@ -31,15 +32,16 @@ describe('TypedRefSchema', () => {
31
32
  })
32
33
 
33
34
  it('validates through a typed object with explicit $type', () => {
34
- const typedObject = new TypedObjectSchema(
35
+ const typedObjectSchema = typedObject(
35
36
  'com.example.user',
36
- new ObjectSchema({
37
- name: new StringSchema({}),
38
- age: new IntegerSchema({}),
37
+ 'main',
38
+ object({
39
+ name: string(),
40
+ age: integer(),
39
41
  }),
40
42
  )
41
43
 
42
- const schema = new TypedRefSchema(() => typedObject)
44
+ const schema = typedRef(() => typedObjectSchema)
43
45
 
44
46
  const result = schema.safeParse({
45
47
  $type: 'com.example.user',
@@ -54,14 +56,15 @@ describe('TypedRefSchema', () => {
54
56
  })
55
57
 
56
58
  it('rejects input with wrong $type', () => {
57
- const typedObject = new TypedObjectSchema(
59
+ const typedObjectSchema = typedObject(
58
60
  'com.example.user',
59
- new ObjectSchema({
60
- name: new StringSchema({}),
61
+ 'main',
62
+ object({
63
+ name: string(),
61
64
  }),
62
65
  )
63
66
 
64
- const schema = new TypedRefSchema(() => typedObject)
67
+ const schema = typedRef(() => typedObjectSchema)
65
68
 
66
69
  const result = schema.safeParse({
67
70
  $type: 'com.example.wrong',
@@ -72,15 +75,16 @@ describe('TypedRefSchema', () => {
72
75
  })
73
76
 
74
77
  it('rejects invalid input through reference', () => {
75
- const typedObject = new TypedObjectSchema(
78
+ const typedObjectSchema = typedObject(
76
79
  'com.example.user',
77
- new ObjectSchema({
78
- name: new StringSchema({}),
79
- age: new IntegerSchema({}),
80
+ 'main',
81
+ object({
82
+ name: string(),
83
+ age: integer(),
80
84
  }),
81
85
  )
82
86
 
83
- const schema = new TypedRefSchema(() => typedObject)
87
+ const schema = typedRef(() => typedObjectSchema)
84
88
 
85
89
  const result = schema.safeParse({
86
90
  $type: 'com.example.user',
@@ -92,42 +96,45 @@ describe('TypedRefSchema', () => {
92
96
  })
93
97
 
94
98
  it('rejects non-objects through reference', () => {
95
- const typedObject = new TypedObjectSchema(
99
+ const typedObjectSchema = typedObject(
96
100
  'com.example.value',
97
- new ObjectSchema({
98
- value: new StringSchema({}),
101
+ 'main',
102
+ object({
103
+ value: string(),
99
104
  }),
100
105
  )
101
106
 
102
- const schema = new TypedRefSchema(() => typedObject)
107
+ const schema = typedRef(() => typedObjectSchema)
103
108
 
104
109
  const result = schema.safeParse('not an object')
105
110
  expect(result.success).toBe(false)
106
111
  })
107
112
 
108
113
  it('rejects null through reference', () => {
109
- const typedObject = new TypedObjectSchema(
114
+ const typedObjectSchema = typedObject(
110
115
  'com.example.user',
111
- new ObjectSchema({
112
- name: new StringSchema({}),
116
+ 'main',
117
+ object({
118
+ name: string(),
113
119
  }),
114
120
  )
115
121
 
116
- const schema = new TypedRefSchema(() => typedObject)
122
+ const schema = typedRef(() => typedObjectSchema)
117
123
 
118
124
  const result = schema.safeParse(null)
119
125
  expect(result.success).toBe(false)
120
126
  })
121
127
 
122
128
  it('rejects undefined through reference', () => {
123
- const typedObject = new TypedObjectSchema(
129
+ const typedObjectSchema = typedObject(
124
130
  'com.example.user',
125
- new ObjectSchema({
126
- name: new StringSchema({}),
131
+ 'main',
132
+ object({
133
+ name: string(),
127
134
  }),
128
135
  )
129
136
 
130
- const schema = new TypedRefSchema(() => typedObject)
137
+ const schema = typedRef(() => typedObjectSchema)
131
138
 
132
139
  const result = schema.safeParse(undefined)
133
140
  expect(result.success).toBe(false)
@@ -136,27 +143,29 @@ describe('TypedRefSchema', () => {
136
143
 
137
144
  describe('$type property', () => {
138
145
  it('exposes the $type from the referenced schema', () => {
139
- const typedObject = new TypedObjectSchema(
146
+ const typedObjectSchema = typedObject(
140
147
  'com.example.post',
141
- new ObjectSchema({
142
- text: new StringSchema({}),
148
+ 'main',
149
+ object({
150
+ text: string(),
143
151
  }),
144
152
  )
145
153
 
146
- const schema = new TypedRefSchema(() => typedObject)
154
+ const schema = typedRef(() => typedObjectSchema)
147
155
 
148
156
  expect(schema.$type).toBe('com.example.post')
149
157
  })
150
158
 
151
159
  it('validates that output has correct $type', () => {
152
- const typedObject = new TypedObjectSchema(
160
+ const typedObjectSchema = typedObject(
153
161
  'com.example.like',
154
- new ObjectSchema({
155
- subject: new StringSchema({}),
162
+ 'main',
163
+ object({
164
+ subject: string(),
156
165
  }),
157
166
  )
158
167
 
159
- const schema = new TypedRefSchema(() => typedObject)
168
+ const schema = typedRef(() => typedObjectSchema)
160
169
 
161
170
  const result = schema.safeParse({
162
171
  $type: 'com.example.like',
@@ -170,14 +179,15 @@ describe('TypedRefSchema', () => {
170
179
  })
171
180
 
172
181
  it('ensures $type matches expected value', () => {
173
- const typedObject = new TypedObjectSchema(
182
+ const typedObjectSchema = typedObject(
174
183
  'com.example.follow',
175
- new ObjectSchema({
176
- subject: new StringSchema({}),
184
+ 'main',
185
+ object({
186
+ subject: string(),
177
187
  }),
178
188
  )
179
189
 
180
- const schema = new TypedRefSchema(() => typedObject)
190
+ const schema = typedRef(() => typedObjectSchema)
181
191
 
182
192
  // Try to pass wrong $type
183
193
  const result = schema.safeParse({
@@ -193,12 +203,13 @@ describe('TypedRefSchema', () => {
193
203
  it('does not call getter until first validation', () => {
194
204
  let getterCalled = false
195
205
 
196
- const schema = new TypedRefSchema(() => {
206
+ const schema = typedRef(() => {
197
207
  getterCalled = true
198
- return new TypedObjectSchema(
208
+ return typedObject(
199
209
  'com.example.test',
200
- new ObjectSchema({
201
- value: new StringSchema({}),
210
+ 'main',
211
+ object({
212
+ value: string(),
202
213
  }),
203
214
  )
204
215
  })
@@ -212,12 +223,13 @@ describe('TypedRefSchema', () => {
212
223
  it('does not call getter until $type is accessed', () => {
213
224
  let getterCalled = false
214
225
 
215
- const schema = new TypedRefSchema(() => {
226
+ const schema = typedRef(() => {
216
227
  getterCalled = true
217
- return new TypedObjectSchema(
228
+ return typedObject(
218
229
  'com.example.test',
219
- new ObjectSchema({
220
- value: new StringSchema({}),
230
+ 'main',
231
+ object({
232
+ value: string(),
221
233
  }),
222
234
  )
223
235
  })
@@ -230,55 +242,11 @@ describe('TypedRefSchema', () => {
230
242
  expect(type).toBe('com.example.test')
231
243
  })
232
244
 
233
- it('caches the resolved schema', () => {
234
- let callCount = 0
235
-
236
- const schema = new TypedRefSchema(() => {
237
- callCount++
238
- return new TypedObjectSchema(
239
- 'com.example.test',
240
- new ObjectSchema({
241
- value: new StringSchema({}),
242
- }),
243
- )
244
- })
245
-
246
- schema.safeParse({ value: 'first' })
247
- schema.safeParse({ value: 'second' })
248
- schema.safeParse({ value: 'third' })
249
-
250
- expect(callCount).toBe(1)
251
- })
252
-
253
- it('caches schema after $type access', () => {
254
- let callCount = 0
255
-
256
- const schema = new TypedRefSchema(() => {
257
- callCount++
258
- return new TypedObjectSchema(
259
- 'com.example.test',
260
- new ObjectSchema({
261
- value: new StringSchema({}),
262
- }),
263
- )
264
- })
265
-
266
- // Access $type first
267
- schema.$type
268
- expect(callCount).toBe(1)
269
-
270
- // Then validate multiple times
271
- schema.safeParse({ value: 'test1' })
272
- schema.safeParse({ value: 'test2' })
273
-
274
- expect(callCount).toBe(1)
275
- })
276
-
277
245
  it('throws error if getter is called recursively', () => {
278
246
  // @ts-expect-error
279
- const schema = new TypedRefSchema(() => {
247
+ const schema = typedRef(() => {
280
248
  // This would cause infinite recursion if not protected
281
- return schema.schema
249
+ return schema.validator
282
250
  })
283
251
 
284
252
  expect(() => {
@@ -289,14 +257,15 @@ describe('TypedRefSchema', () => {
289
257
 
290
258
  describe('with constrained schemas', () => {
291
259
  it('validates typed object with string constraints', () => {
292
- const typedObject = new TypedObjectSchema(
260
+ const typedObjectSchema = typedObject(
293
261
  'com.example.post',
294
- new ObjectSchema({
295
- text: new StringSchema({ minLength: 1, maxLength: 300 }),
262
+ 'main',
263
+ object({
264
+ text: string({ minLength: 1, maxLength: 300 }),
296
265
  }),
297
266
  )
298
267
 
299
- const schema = new TypedRefSchema(() => typedObject)
268
+ const schema = typedRef(() => typedObjectSchema)
300
269
 
301
270
  const result = schema.safeParse({
302
271
  $type: 'com.example.post',
@@ -307,14 +276,15 @@ describe('TypedRefSchema', () => {
307
276
  })
308
277
 
309
278
  it('rejects typed object violating string constraints', () => {
310
- const typedObject = new TypedObjectSchema(
279
+ const typedObjectSchema = typedObject(
311
280
  'com.example.post',
312
- new ObjectSchema({
313
- text: new StringSchema({ minLength: 1, maxLength: 300 }),
281
+ 'main',
282
+ object({
283
+ text: string({ minLength: 1, maxLength: 300 }),
314
284
  }),
315
285
  )
316
286
 
317
- const schema = new TypedRefSchema(() => typedObject)
287
+ const schema = typedRef(() => typedObjectSchema)
318
288
 
319
289
  const result = schema.safeParse({
320
290
  $type: 'com.example.post',
@@ -325,14 +295,15 @@ describe('TypedRefSchema', () => {
325
295
  })
326
296
 
327
297
  it('validates typed object with integer constraints', () => {
328
- const typedObject = new TypedObjectSchema(
298
+ const typedObjectSchema = typedObject(
329
299
  'com.example.rating',
330
- new ObjectSchema({
331
- score: new IntegerSchema({ minimum: 1, maximum: 5 }),
300
+ 'main',
301
+ object({
302
+ score: integer({ minimum: 1, maximum: 5 }),
332
303
  }),
333
304
  )
334
305
 
335
- const schema = new TypedRefSchema(() => typedObject)
306
+ const schema = typedRef(() => typedObjectSchema)
336
307
 
337
308
  const result = schema.safeParse({
338
309
  $type: 'com.example.rating',
@@ -343,14 +314,15 @@ describe('TypedRefSchema', () => {
343
314
  })
344
315
 
345
316
  it('rejects typed object violating integer constraints', () => {
346
- const typedObject = new TypedObjectSchema(
317
+ const typedObjectSchema = typedObject(
347
318
  'com.example.rating',
348
- new ObjectSchema({
349
- score: new IntegerSchema({ minimum: 1, maximum: 5 }),
319
+ 'main',
320
+ object({
321
+ score: integer({ minimum: 1, maximum: 5 }),
350
322
  }),
351
323
  )
352
324
 
353
- const schema = new TypedRefSchema(() => typedObject)
325
+ const schema = typedRef(() => typedObjectSchema)
354
326
 
355
327
  const result = schema.safeParse({
356
328
  $type: 'com.example.rating',
@@ -363,14 +335,15 @@ describe('TypedRefSchema', () => {
363
335
 
364
336
  describe('multiple validations', () => {
365
337
  it('validates multiple inputs correctly', () => {
366
- const typedObject = new TypedObjectSchema(
338
+ const typedObjectSchema = typedObject(
367
339
  'com.example.user',
368
- new ObjectSchema({
369
- name: new StringSchema({ minLength: 2 }),
340
+ 'main',
341
+ object({
342
+ name: string({ minLength: 2 }),
370
343
  }),
371
344
  )
372
345
 
373
- const schema = new TypedRefSchema(() => typedObject)
346
+ const schema = typedRef(() => typedObjectSchema)
374
347
 
375
348
  const result1 = schema.safeParse({
376
349
  $type: 'com.example.user',
@@ -392,15 +365,16 @@ describe('TypedRefSchema', () => {
392
365
  })
393
366
 
394
367
  it('handles different types of validation failures', () => {
395
- const typedObject = new TypedObjectSchema(
368
+ const typedObjectSchema = typedObject(
396
369
  'com.example.user',
397
- new ObjectSchema({
398
- name: new StringSchema({ minLength: 2 }),
399
- age: new IntegerSchema({ minimum: 0, maximum: 150 }),
370
+ 'main',
371
+ object({
372
+ name: string({ minLength: 2 }),
373
+ age: integer({ minimum: 0, maximum: 150 }),
400
374
  }),
401
375
  )
402
376
 
403
- const schema = new TypedRefSchema(() => typedObject)
377
+ const schema = typedRef(() => typedObjectSchema)
404
378
 
405
379
  const result1 = schema.safeParse({
406
380
  $type: 'com.example.user',
@@ -431,14 +405,15 @@ describe('TypedRefSchema', () => {
431
405
  })
432
406
 
433
407
  it('validates same input multiple times consistently', () => {
434
- const typedObject = new TypedObjectSchema(
408
+ const typedObjectSchema = typedObject(
435
409
  'com.example.post',
436
- new ObjectSchema({
437
- text: new StringSchema({}),
410
+ 'main',
411
+ object({
412
+ text: string(),
438
413
  }),
439
414
  )
440
415
 
441
- const schema = new TypedRefSchema(() => typedObject)
416
+ const schema = typedRef(() => typedObjectSchema)
442
417
 
443
418
  const input = { $type: 'com.example.post', text: 'Hello world' }
444
419
 
@@ -454,12 +429,13 @@ describe('TypedRefSchema', () => {
454
429
 
455
430
  describe('edge cases', () => {
456
431
  it('handles empty object validation', () => {
457
- const typedObject = new TypedObjectSchema(
432
+ const typedObjectSchema = typedObject(
458
433
  'com.example.empty',
459
- new ObjectSchema({}),
434
+ 'main',
435
+ object({}),
460
436
  )
461
437
 
462
- const schema = new TypedRefSchema(() => typedObject)
438
+ const schema = typedRef(() => typedObjectSchema)
463
439
 
464
440
  const result = schema.safeParse({ $type: 'com.example.empty' })
465
441
  expect(result.success).toBe(true)
@@ -469,28 +445,30 @@ describe('TypedRefSchema', () => {
469
445
  })
470
446
 
471
447
  it('rejects arrays', () => {
472
- const typedObject = new TypedObjectSchema(
448
+ const typedObjectSchema = typedObject(
473
449
  'com.example.test',
474
- new ObjectSchema({
475
- value: new StringSchema({}),
450
+ 'main',
451
+ object({
452
+ value: string(),
476
453
  }),
477
454
  )
478
455
 
479
- const schema = new TypedRefSchema(() => typedObject)
456
+ const schema = typedRef(() => typedObjectSchema)
480
457
 
481
458
  const result = schema.safeParse([{ value: 'test' }])
482
459
  expect(result.success).toBe(false)
483
460
  })
484
461
 
485
462
  it('rejects primitive values', () => {
486
- const typedObject = new TypedObjectSchema(
463
+ const typedObjectSchema = typedObject(
487
464
  'com.example.test',
488
- new ObjectSchema({
489
- value: new StringSchema({}),
465
+ 'main',
466
+ object({
467
+ value: string(),
490
468
  }),
491
469
  )
492
470
 
493
- const schema = new TypedRefSchema(() => typedObject)
471
+ const schema = typedRef(() => typedObjectSchema)
494
472
 
495
473
  const result1 = schema.safeParse('string')
496
474
  expect(result1.success).toBe(false)
@@ -503,14 +481,15 @@ describe('TypedRefSchema', () => {
503
481
  })
504
482
 
505
483
  it('handles objects with extra properties', () => {
506
- const typedObject = new TypedObjectSchema(
484
+ const typedObjectSchema = typedObject(
507
485
  'com.example.user',
508
- new ObjectSchema({
509
- name: new StringSchema({}),
486
+ 'main',
487
+ object({
488
+ name: string(),
510
489
  }),
511
490
  )
512
491
 
513
- const schema = new TypedRefSchema(() => typedObject)
492
+ const schema = typedRef(() => typedObjectSchema)
514
493
 
515
494
  const result = schema.safeParse({
516
495
  $type: 'com.example.user',
@@ -523,14 +502,15 @@ describe('TypedRefSchema', () => {
523
502
  })
524
503
 
525
504
  it('validates with zero values', () => {
526
- const typedObject = new TypedObjectSchema(
505
+ const typedObjectSchema = typedObject(
527
506
  'com.example.counter',
528
- new ObjectSchema({
529
- count: new IntegerSchema({}),
507
+ 'main',
508
+ object({
509
+ count: integer(),
530
510
  }),
531
511
  )
532
512
 
533
- const schema = new TypedRefSchema(() => typedObject)
513
+ const schema = typedRef(() => typedObjectSchema)
534
514
 
535
515
  const result = schema.safeParse({
536
516
  $type: 'com.example.counter',
@@ -540,14 +520,15 @@ describe('TypedRefSchema', () => {
540
520
  })
541
521
 
542
522
  it('validates with empty strings', () => {
543
- const typedObject = new TypedObjectSchema(
523
+ const typedObjectSchema = typedObject(
544
524
  'com.example.text',
545
- new ObjectSchema({
546
- content: new StringSchema({}),
525
+ 'main',
526
+ object({
527
+ content: string(),
547
528
  }),
548
529
  )
549
530
 
550
- const schema = new TypedRefSchema(() => typedObject)
531
+ const schema = typedRef(() => typedObjectSchema)
551
532
 
552
533
  const result = schema.safeParse({
553
534
  $type: 'com.example.text',
@@ -557,14 +538,15 @@ describe('TypedRefSchema', () => {
557
538
  })
558
539
 
559
540
  it('rejects NaN in integer fields', () => {
560
- const typedObject = new TypedObjectSchema(
541
+ const typedObjectSchema = typedObject(
561
542
  'com.example.number',
562
- new ObjectSchema({
563
- value: new IntegerSchema({}),
543
+ 'main',
544
+ object({
545
+ value: integer(),
564
546
  }),
565
547
  )
566
548
 
567
- const schema = new TypedRefSchema(() => typedObject)
549
+ const schema = typedRef(() => typedObjectSchema)
568
550
 
569
551
  const result = schema.safeParse({
570
552
  $type: 'com.example.number',
@@ -574,14 +556,15 @@ describe('TypedRefSchema', () => {
574
556
  })
575
557
 
576
558
  it('rejects Infinity in integer fields', () => {
577
- const typedObject = new TypedObjectSchema(
559
+ const typedObjectSchema = typedObject(
578
560
  'com.example.number',
579
- new ObjectSchema({
580
- value: new IntegerSchema({}),
561
+ 'main',
562
+ object({
563
+ value: integer(),
581
564
  }),
582
565
  )
583
566
 
584
- const schema = new TypedRefSchema(() => typedObject)
567
+ const schema = typedRef(() => typedObjectSchema)
585
568
 
586
569
  const result = schema.safeParse({
587
570
  $type: 'com.example.number',
@@ -593,15 +576,16 @@ describe('TypedRefSchema', () => {
593
576
 
594
577
  describe('nested references', () => {
595
578
  it('validates through nested TypedRefSchema', () => {
596
- const typedObject = new TypedObjectSchema(
579
+ const typedObjectSchema = typedObject(
597
580
  'com.example.user',
598
- new ObjectSchema({
599
- name: new StringSchema({ minLength: 2 }),
581
+ 'main',
582
+ object({
583
+ name: string({ minLength: 2 }),
600
584
  }),
601
585
  )
602
586
 
603
- const innerRef = new TypedRefSchema(() => typedObject)
604
- const outerRef = new TypedRefSchema(() => innerRef.schema)
587
+ const innerRef = typedRef(() => typedObjectSchema)
588
+ const outerRef = typedRef(() => innerRef.validator)
605
589
 
606
590
  const result = outerRef.safeParse({
607
591
  $type: 'com.example.user',
@@ -614,22 +598,24 @@ describe('TypedRefSchema', () => {
614
598
  })
615
599
 
616
600
  it('validates with objects containing TypedRef fields', () => {
617
- const innerTyped = new TypedObjectSchema(
601
+ const innerTyped = typedObject(
618
602
  'com.example.profile',
619
- new ObjectSchema({
620
- bio: new StringSchema({}),
603
+ 'main',
604
+ object({
605
+ bio: string(),
621
606
  }),
622
607
  )
623
608
 
624
- const outerTyped = new TypedObjectSchema(
609
+ const outerTyped = typedObject(
625
610
  'com.example.user',
626
- new ObjectSchema({
627
- name: new StringSchema({}),
628
- profile: new TypedRefSchema(() => innerTyped),
611
+ 'main',
612
+ object({
613
+ name: string(),
614
+ profile: typedRef(() => innerTyped),
629
615
  }),
630
616
  )
631
617
 
632
- const schema = new TypedRefSchema(() => outerTyped)
618
+ const schema = typedRef(() => outerTyped)
633
619
 
634
620
  const result = schema.safeParse({
635
621
  $type: 'com.example.user',
@@ -648,22 +634,24 @@ describe('TypedRefSchema', () => {
648
634
  })
649
635
 
650
636
  it('rejects nested objects with wrong $type', () => {
651
- const innerTyped = new TypedObjectSchema(
637
+ const innerTyped = typedObject(
652
638
  'com.example.profile',
653
- new ObjectSchema({
654
- bio: new StringSchema({}),
639
+ 'main',
640
+ object({
641
+ bio: string(),
655
642
  }),
656
643
  )
657
644
 
658
- const outerTyped = new TypedObjectSchema(
645
+ const outerTyped = typedObject(
659
646
  'com.example.user',
660
- new ObjectSchema({
661
- name: new StringSchema({}),
662
- profile: new TypedRefSchema(() => innerTyped),
647
+ 'main',
648
+ object({
649
+ name: string(),
650
+ profile: typedRef(() => innerTyped),
663
651
  }),
664
652
  )
665
653
 
666
- const schema = new TypedRefSchema(() => outerTyped)
654
+ const schema = typedRef(() => outerTyped)
667
655
 
668
656
  const result = schema.safeParse({
669
657
  $type: 'com.example.user',
@@ -680,73 +668,55 @@ describe('TypedRefSchema', () => {
680
668
 
681
669
  describe('schema property access', () => {
682
670
  it('allows direct access to resolved schema', () => {
683
- const typedObject = new TypedObjectSchema(
671
+ const typedObjectSchema = typedObject(
684
672
  'com.example.test',
685
- new ObjectSchema({
686
- value: new StringSchema({}),
673
+ 'main',
674
+ object({
675
+ value: string(),
687
676
  }),
688
677
  )
689
678
 
690
- const refSchema = new TypedRefSchema(() => typedObject)
679
+ const refSchema = typedRef(() => typedObjectSchema)
691
680
 
692
- const resolved = refSchema.schema
693
- expect(resolved).toBe(typedObject)
681
+ const resolved = refSchema.validator
682
+ expect(resolved).toBe(typedObjectSchema)
694
683
  expect(resolved.$type).toBe('com.example.test')
695
684
  })
696
685
 
697
686
  it('returns same instance on multiple schema property accesses', () => {
698
- const typedObject = new TypedObjectSchema(
687
+ const typedObjectSchema = typedObject(
699
688
  'com.example.test',
700
- new ObjectSchema({
701
- value: new StringSchema({}),
689
+ 'main',
690
+ object({
691
+ value: string(),
702
692
  }),
703
693
  )
704
694
 
705
- const refSchema = new TypedRefSchema(() => typedObject)
695
+ const refSchema = typedRef(() => typedObjectSchema)
706
696
 
707
- const first = refSchema.schema
708
- const second = refSchema.schema
709
- const third = refSchema.schema
697
+ const first = refSchema.validator
698
+ const second = refSchema.validator
699
+ const third = refSchema.validator
710
700
 
711
701
  expect(first).toBe(second)
712
702
  expect(second).toBe(third)
713
703
  expect(first.$type).toBe('com.example.test')
714
704
  })
715
-
716
- it('resolves schema before validation', () => {
717
- let resolved = false
718
-
719
- const refSchema = new TypedRefSchema(() => {
720
- resolved = true
721
- return new TypedObjectSchema(
722
- 'com.example.test',
723
- new ObjectSchema({
724
- value: new StringSchema({}),
725
- }),
726
- )
727
- })
728
-
729
- expect(resolved).toBe(false)
730
-
731
- const schemaValue = refSchema.schema
732
- expect(resolved).toBe(true)
733
- expect(schemaValue).toBeDefined()
734
- expect(schemaValue.$type).toBe('com.example.test')
735
- })
736
705
  })
737
706
 
738
707
  describe('complex object structures', () => {
739
708
  it('validates complex nested structure', () => {
740
- const typedObject = new TypedObjectSchema(
709
+ const typedObjectSchema = typedObject(
741
710
  'com.example.post',
742
- new ObjectSchema({
743
- text: new StringSchema({ minLength: 1, maxLength: 300 }),
744
- createdAt: new StringSchema({ format: 'datetime' }),
745
- likeCount: new IntegerSchema({ minimum: 0 }),
711
+ 'main',
712
+ object({
713
+ text: string({ minLength: 1, maxLength: 300 }),
714
+ createdAt: string({ format: 'datetime' }),
715
+ likeCount: integer({ minimum: 0 }),
746
716
  }),
747
717
  )
748
718
 
749
- const schema = new TypedRefSchema(() => typedObject)
719
+ const schema = typedRef(() => typedObjectSchema)
750
720
 
751
721
  const result = schema.safeParse({
752
722
  $type: 'com.example.post',
@@ -764,16 +734,17 @@ describe('TypedRefSchema', () => {
764
734
  })
765
735
 
766
736
  it('validates structure with multiple property types', () => {
767
- const typedObject = new TypedObjectSchema(
737
+ const typedObjectSchema = typedObject(
768
738
  'com.example.record',
769
- new ObjectSchema({
770
- id: new StringSchema({ format: 'nsid' }),
771
- count: new IntegerSchema({ minimum: 0 }),
772
- flag: new StringSchema({}),
739
+ 'main',
740
+ object({
741
+ id: string({ format: 'nsid' }),
742
+ count: integer({ minimum: 0 }),
743
+ flag: string(),
773
744
  }),
774
745
  )
775
746
 
776
- const schema = new TypedRefSchema(() => typedObject)
747
+ const schema = typedRef(() => typedObjectSchema)
777
748
 
778
749
  const result = schema.safeParse({
779
750
  $type: 'com.example.record',
@@ -786,15 +757,16 @@ describe('TypedRefSchema', () => {
786
757
  })
787
758
 
788
759
  it('rejects structure with any invalid property', () => {
789
- const typedObject = new TypedObjectSchema(
760
+ const typedObjectSchema = typedObject(
790
761
  'com.example.record',
791
- new ObjectSchema({
792
- name: new StringSchema({ minLength: 1 }),
793
- count: new IntegerSchema({ minimum: 0 }),
762
+ 'main',
763
+ object({
764
+ name: string({ minLength: 1 }),
765
+ count: integer({ minimum: 0 }),
794
766
  }),
795
767
  )
796
768
 
797
- const schema = new TypedRefSchema(() => typedObject)
769
+ const schema = typedRef(() => typedObjectSchema)
798
770
 
799
771
  // Valid name, invalid count
800
772
  const result1 = schema.safeParse({