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