@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.
- package/CHANGELOG.md +41 -0
- package/LICENSE.txt +1 -1
- package/dist/core/$type.d.ts +11 -0
- package/dist/core/$type.d.ts.map +1 -1
- package/dist/core/$type.js +4 -0
- package/dist/core/$type.js.map +1 -1
- package/dist/core/schema.d.ts +31 -24
- package/dist/core/schema.d.ts.map +1 -1
- package/dist/core/schema.js +38 -8
- package/dist/core/schema.js.map +1 -1
- package/dist/core/string-format.d.ts +35 -35
- package/dist/core/string-format.d.ts.map +1 -1
- package/dist/core/string-format.js +49 -91
- package/dist/core/string-format.js.map +1 -1
- package/dist/core/validation-error.d.ts +1 -1
- package/dist/core/validation-issue.js +1 -1
- package/dist/core/validation-issue.js.map +1 -1
- package/dist/core/validator.d.ts +53 -32
- package/dist/core/validator.d.ts.map +1 -1
- package/dist/core/validator.js +18 -22
- package/dist/core/validator.js.map +1 -1
- package/dist/external.d.ts +0 -85
- package/dist/external.d.ts.map +1 -1
- package/dist/external.js +0 -164
- package/dist/external.js.map +1 -1
- package/dist/helpers.d.ts +13 -5
- package/dist/helpers.d.ts.map +1 -1
- package/dist/helpers.js +4 -4
- package/dist/helpers.js.map +1 -1
- package/dist/schema/array.d.ts +9 -5
- package/dist/schema/array.d.ts.map +1 -1
- package/dist/schema/array.js +14 -5
- package/dist/schema/array.js.map +1 -1
- package/dist/schema/blob.d.ts +9 -7
- package/dist/schema/blob.d.ts.map +1 -1
- package/dist/schema/blob.js +9 -5
- package/dist/schema/blob.js.map +1 -1
- package/dist/schema/boolean.d.ts +3 -7
- package/dist/schema/boolean.d.ts.map +1 -1
- package/dist/schema/boolean.js +6 -7
- package/dist/schema/boolean.js.map +1 -1
- package/dist/schema/bytes.d.ts +3 -2
- package/dist/schema/bytes.d.ts.map +1 -1
- package/dist/schema/bytes.js +7 -3
- package/dist/schema/bytes.js.map +1 -1
- package/dist/schema/cid.d.ts +7 -7
- package/dist/schema/cid.d.ts.map +1 -1
- package/dist/schema/cid.js +5 -1
- package/dist/schema/cid.js.map +1 -1
- package/dist/schema/custom.d.ts +6 -5
- package/dist/schema/custom.d.ts.map +1 -1
- package/dist/schema/custom.js +10 -4
- package/dist/schema/custom.js.map +1 -1
- package/dist/schema/dict.d.ts +8 -8
- package/dist/schema/dict.d.ts.map +1 -1
- package/dist/schema/dict.js +11 -2
- package/dist/schema/dict.js.map +1 -1
- package/dist/schema/discriminated-union.d.ts +21 -14
- package/dist/schema/discriminated-union.d.ts.map +1 -1
- package/dist/schema/discriminated-union.js +7 -0
- package/dist/schema/discriminated-union.js.map +1 -1
- package/dist/schema/enum.d.ts +7 -9
- package/dist/schema/enum.d.ts.map +1 -1
- package/dist/schema/enum.js +8 -4
- package/dist/schema/enum.js.map +1 -1
- package/dist/schema/integer.d.ts +5 -5
- package/dist/schema/integer.d.ts.map +1 -1
- package/dist/schema/integer.js +9 -5
- package/dist/schema/integer.js.map +1 -1
- package/dist/schema/intersection.d.ts +4 -4
- package/dist/schema/intersection.d.ts.map +1 -1
- package/dist/schema/intersection.js +5 -0
- package/dist/schema/intersection.js.map +1 -1
- package/dist/schema/literal.d.ts +6 -9
- package/dist/schema/literal.d.ts.map +1 -1
- package/dist/schema/literal.js +7 -4
- package/dist/schema/literal.js.map +1 -1
- package/dist/schema/never.d.ts +3 -2
- package/dist/schema/never.d.ts.map +1 -1
- package/dist/schema/never.js +5 -1
- package/dist/schema/never.js.map +1 -1
- package/dist/schema/null.d.ts +4 -3
- package/dist/schema/null.d.ts.map +1 -1
- package/dist/schema/null.js +6 -4
- package/dist/schema/null.js.map +1 -1
- package/dist/schema/nullable.d.ts +6 -5
- package/dist/schema/nullable.d.ts.map +1 -1
- package/dist/schema/nullable.js +9 -5
- package/dist/schema/nullable.js.map +1 -1
- package/dist/schema/object.d.ts +10 -8
- package/dist/schema/object.d.ts.map +1 -1
- package/dist/schema/object.js +11 -3
- package/dist/schema/object.js.map +1 -1
- package/dist/schema/optional.d.ts +7 -5
- package/dist/schema/optional.d.ts.map +1 -1
- package/dist/schema/optional.js +14 -6
- package/dist/schema/optional.js.map +1 -1
- package/dist/schema/params.d.ts +26 -13
- package/dist/schema/params.d.ts.map +1 -1
- package/dist/schema/params.js +47 -25
- package/dist/schema/params.js.map +1 -1
- package/dist/schema/payload.d.ts +12 -9
- package/dist/schema/payload.d.ts.map +1 -1
- package/dist/schema/payload.js +11 -0
- package/dist/schema/payload.js.map +1 -1
- package/dist/schema/permission-set.d.ts +1 -0
- package/dist/schema/permission-set.d.ts.map +1 -1
- package/dist/schema/permission-set.js +5 -0
- package/dist/schema/permission-set.js.map +1 -1
- package/dist/schema/permission.d.ts +6 -5
- package/dist/schema/permission.d.ts.map +1 -1
- package/dist/schema/permission.js +5 -0
- package/dist/schema/permission.js.map +1 -1
- package/dist/schema/procedure.d.ts +2 -1
- package/dist/schema/procedure.d.ts.map +1 -1
- package/dist/schema/procedure.js +5 -0
- package/dist/schema/procedure.js.map +1 -1
- package/dist/schema/query.d.ts +2 -1
- package/dist/schema/query.d.ts.map +1 -1
- package/dist/schema/query.js +5 -0
- package/dist/schema/query.js.map +1 -1
- package/dist/schema/record.d.ts +48 -30
- package/dist/schema/record.d.ts.map +1 -1
- package/dist/schema/record.js +12 -9
- package/dist/schema/record.js.map +1 -1
- package/dist/schema/ref.d.ts +9 -6
- package/dist/schema/ref.d.ts.map +1 -1
- package/dist/schema/ref.js +9 -16
- package/dist/schema/ref.js.map +1 -1
- package/dist/schema/refine.d.ts +4 -4
- package/dist/schema/refine.d.ts.map +1 -1
- package/dist/schema/refine.js.map +1 -1
- package/dist/schema/regexp.d.ts +4 -3
- package/dist/schema/regexp.d.ts.map +1 -1
- package/dist/schema/regexp.js +5 -0
- package/dist/schema/regexp.js.map +1 -1
- package/dist/schema/string.d.ts +7 -8
- package/dist/schema/string.d.ts.map +1 -1
- package/dist/schema/string.js +13 -19
- package/dist/schema/string.js.map +1 -1
- package/dist/schema/subscription.d.ts +2 -1
- package/dist/schema/subscription.d.ts.map +1 -1
- package/dist/schema/subscription.js +5 -0
- package/dist/schema/subscription.js.map +1 -1
- package/dist/schema/token.d.ts +6 -5
- package/dist/schema/token.d.ts.map +1 -1
- package/dist/schema/token.js +5 -0
- package/dist/schema/token.js.map +1 -1
- package/dist/schema/typed-object.d.ts +43 -26
- package/dist/schema/typed-object.d.ts.map +1 -1
- package/dist/schema/typed-object.js +6 -3
- package/dist/schema/typed-object.js.map +1 -1
- package/dist/schema/typed-ref.d.ts +16 -25
- package/dist/schema/typed-ref.d.ts.map +1 -1
- package/dist/schema/typed-ref.js +7 -17
- package/dist/schema/typed-ref.js.map +1 -1
- package/dist/schema/typed-union.d.ts +9 -21
- package/dist/schema/typed-union.d.ts.map +1 -1
- package/dist/schema/typed-union.js +15 -11
- package/dist/schema/typed-union.js.map +1 -1
- package/dist/schema/union.d.ts +6 -6
- package/dist/schema/union.d.ts.map +1 -1
- package/dist/schema/union.js +7 -5
- package/dist/schema/union.js.map +1 -1
- package/dist/schema/unknown-object.d.ts +5 -4
- package/dist/schema/unknown-object.d.ts.map +1 -1
- package/dist/schema/unknown-object.js +5 -1
- package/dist/schema/unknown-object.js.map +1 -1
- package/dist/schema/unknown.d.ts +3 -2
- package/dist/schema/unknown.d.ts.map +1 -1
- package/dist/schema/unknown.js +5 -1
- package/dist/schema/unknown.js.map +1 -1
- package/dist/schema/with-default.d.ts +9 -0
- package/dist/schema/with-default.d.ts.map +1 -0
- package/dist/schema/with-default.js +27 -0
- package/dist/schema/with-default.js.map +1 -0
- package/dist/schema.d.ts +2 -2
- package/dist/schema.d.ts.map +1 -1
- package/dist/schema.js +2 -4
- package/dist/schema.js.map +1 -1
- package/dist/util/assertion-util.d.ts +0 -6
- package/dist/util/assertion-util.d.ts.map +1 -1
- package/dist/util/assertion-util.js +0 -28
- package/dist/util/assertion-util.js.map +1 -1
- package/dist/util/memoize.d.ts +2 -2
- package/dist/util/memoize.d.ts.map +1 -1
- package/dist/util/memoize.js +23 -39
- package/dist/util/memoize.js.map +1 -1
- package/package.json +3 -3
- package/src/core/$type.test.ts +20 -0
- package/src/core/$type.ts +30 -0
- package/src/core/schema.ts +86 -38
- package/src/core/string-format.ts +119 -158
- package/src/core/validation-issue.ts +1 -1
- package/src/core/validator.ts +93 -53
- package/src/external.ts +0 -404
- package/src/helpers.test.ts +22 -21
- package/src/helpers.ts +19 -14
- package/src/schema/array.test.ts +38 -40
- package/src/schema/array.ts +35 -13
- package/src/schema/blob.test.ts +21 -21
- package/src/schema/blob.ts +19 -17
- package/src/schema/boolean.test.ts +9 -8
- package/src/schema/boolean.ts +7 -13
- package/src/schema/bytes.test.ts +13 -13
- package/src/schema/bytes.ts +13 -8
- package/src/schema/cid.test.ts +3 -3
- package/src/schema/cid.ts +13 -12
- package/src/schema/custom.test.ts +26 -26
- package/src/schema/custom.ts +20 -13
- package/src/schema/dict.test.ts +21 -39
- package/src/schema/dict.ts +28 -19
- package/src/schema/discriminated-union.test.ts +128 -128
- package/src/schema/discriminated-union.ts +45 -26
- package/src/schema/enum.test.ts +17 -16
- package/src/schema/enum.ts +16 -16
- package/src/schema/integer.test.ts +22 -21
- package/src/schema/integer.ts +12 -9
- package/src/schema/intersection.test.ts +10 -10
- package/src/schema/intersection.ts +17 -14
- package/src/schema/literal.test.ts +35 -34
- package/src/schema/literal.ts +12 -15
- package/src/schema/never.test.ts +5 -5
- package/src/schema/never.ts +7 -2
- package/src/schema/null.test.ts +3 -3
- package/src/schema/null.ts +9 -9
- package/src/schema/nullable.test.ts +31 -42
- package/src/schema/nullable.ts +17 -9
- package/src/schema/object.test.ts +10 -12
- package/src/schema/object.ts +27 -18
- package/src/schema/optional.test.ts +21 -28
- package/src/schema/optional.ts +27 -10
- package/src/schema/params.test.ts +471 -47
- package/src/schema/params.ts +74 -38
- package/src/schema/payload.test.ts +150 -156
- package/src/schema/payload.ts +35 -19
- package/src/schema/permission-set.test.ts +206 -273
- package/src/schema/permission-set.ts +8 -0
- package/src/schema/permission.test.ts +177 -177
- package/src/schema/permission.ts +13 -5
- package/src/schema/procedure.test.ts +183 -242
- package/src/schema/procedure.ts +18 -5
- package/src/schema/query.test.ts +186 -200
- package/src/schema/query.ts +16 -4
- package/src/schema/record.test.ts +121 -101
- package/src/schema/record.ts +74 -40
- package/src/schema/ref.test.ts +101 -118
- package/src/schema/ref.ts +33 -28
- package/src/schema/refine.test.ts +28 -28
- package/src/schema/refine.ts +23 -20
- package/src/schema/regexp.test.ts +29 -33
- package/src/schema/regexp.ts +11 -7
- package/src/schema/string.test.ts +35 -35
- package/src/schema/string.ts +24 -33
- package/src/schema/subscription.test.ts +259 -387
- package/src/schema/subscription.ts +16 -4
- package/src/schema/token.test.ts +47 -324
- package/src/schema/token.ts +14 -7
- package/src/schema/typed-object.test.ts +98 -81
- package/src/schema/typed-object.ts +68 -33
- package/src/schema/typed-ref.test.ts +206 -234
- package/src/schema/typed-ref.ts +40 -42
- package/src/schema/typed-union.test.ts +40 -64
- package/src/schema/typed-union.ts +36 -58
- package/src/schema/union.test.ts +17 -27
- package/src/schema/union.ts +20 -16
- package/src/schema/unknown-object.test.ts +8 -8
- package/src/schema/unknown-object.ts +9 -7
- package/src/schema/unknown.test.ts +4 -4
- package/src/schema/unknown.ts +7 -5
- package/src/schema/with-default.ts +35 -0
- package/src/schema.ts +2 -6
- package/src/util/assertion-util.ts +0 -39
- package/src/util/memoize.ts +26 -46
- package/dist/schema/_parameters.d.ts +0 -17
- package/dist/schema/_parameters.d.ts.map +0 -1
- package/dist/schema/_parameters.js +0 -20
- package/dist/schema/_parameters.js.map +0 -1
- package/src/schema/_parameters.test.ts +0 -417
- package/src/schema/_parameters.ts +0 -26
package/src/schema/blob.test.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { describe, expect, it } from 'vitest'
|
|
2
2
|
import { parseCid } from '@atproto/lex-data'
|
|
3
|
-
import {
|
|
3
|
+
import { blob } from './blob.js'
|
|
4
4
|
|
|
5
5
|
// await cidForRawBytes(Buffer.from('Hello, World!'))
|
|
6
6
|
const blobCid = parseCid(
|
|
@@ -13,7 +13,7 @@ const lexCid = parseCid(
|
|
|
13
13
|
|
|
14
14
|
describe('BlobSchema', () => {
|
|
15
15
|
describe('basic validation', () => {
|
|
16
|
-
const schema =
|
|
16
|
+
const schema = blob({})
|
|
17
17
|
|
|
18
18
|
it('validates valid blob references', () => {
|
|
19
19
|
const result = schema.safeParse({
|
|
@@ -82,7 +82,7 @@ describe('BlobSchema', () => {
|
|
|
82
82
|
})
|
|
83
83
|
|
|
84
84
|
describe('BlobRef validation', () => {
|
|
85
|
-
const schema =
|
|
85
|
+
const schema = blob({})
|
|
86
86
|
|
|
87
87
|
it('rejects blob without $type', () => {
|
|
88
88
|
const result = schema.safeParse({
|
|
@@ -214,7 +214,7 @@ describe('BlobSchema', () => {
|
|
|
214
214
|
})
|
|
215
215
|
|
|
216
216
|
describe('strict validation', () => {
|
|
217
|
-
const strictSchema =
|
|
217
|
+
const strictSchema = blob({ strict: true })
|
|
218
218
|
|
|
219
219
|
it('accepts valid raw CID in strict mode', () => {
|
|
220
220
|
const result = strictSchema.safeParse({
|
|
@@ -237,7 +237,7 @@ describe('BlobSchema', () => {
|
|
|
237
237
|
})
|
|
238
238
|
|
|
239
239
|
it('accepts non-raw CID in non-strict mode', () => {
|
|
240
|
-
const nonStrictSchema =
|
|
240
|
+
const nonStrictSchema = blob({ strict: false })
|
|
241
241
|
const result = nonStrictSchema.safeParse({
|
|
242
242
|
$type: 'blob',
|
|
243
243
|
ref: lexCid,
|
|
@@ -250,7 +250,7 @@ describe('BlobSchema', () => {
|
|
|
250
250
|
|
|
251
251
|
describe('legacy blob format', () => {
|
|
252
252
|
it('rejects legacy format by default', () => {
|
|
253
|
-
const schema =
|
|
253
|
+
const schema = blob({})
|
|
254
254
|
const result = schema.safeParse({
|
|
255
255
|
cid: blobCid.toString(),
|
|
256
256
|
mimeType: 'image/jpeg',
|
|
@@ -259,7 +259,7 @@ describe('BlobSchema', () => {
|
|
|
259
259
|
})
|
|
260
260
|
|
|
261
261
|
it('accepts legacy format when allowLegacy is true', () => {
|
|
262
|
-
const schema =
|
|
262
|
+
const schema = blob({ allowLegacy: true })
|
|
263
263
|
const result = schema.safeParse({
|
|
264
264
|
cid: blobCid.toString(),
|
|
265
265
|
mimeType: 'image/jpeg',
|
|
@@ -274,7 +274,7 @@ describe('BlobSchema', () => {
|
|
|
274
274
|
})
|
|
275
275
|
|
|
276
276
|
it('accepts legacy format with lexCid when allowLegacy is true', () => {
|
|
277
|
-
const schema =
|
|
277
|
+
const schema = blob({ allowLegacy: true })
|
|
278
278
|
const result = schema.safeParse({
|
|
279
279
|
cid: lexCid.toString(),
|
|
280
280
|
mimeType: 'image/png',
|
|
@@ -283,7 +283,7 @@ describe('BlobSchema', () => {
|
|
|
283
283
|
})
|
|
284
284
|
|
|
285
285
|
it('rejects legacy format without cid', () => {
|
|
286
|
-
const schema =
|
|
286
|
+
const schema = blob({ allowLegacy: true })
|
|
287
287
|
const result = schema.safeParse({
|
|
288
288
|
mimeType: 'image/jpeg',
|
|
289
289
|
})
|
|
@@ -291,7 +291,7 @@ describe('BlobSchema', () => {
|
|
|
291
291
|
})
|
|
292
292
|
|
|
293
293
|
it('rejects legacy format without mimeType', () => {
|
|
294
|
-
const schema =
|
|
294
|
+
const schema = blob({ allowLegacy: true })
|
|
295
295
|
const result = schema.safeParse({
|
|
296
296
|
cid: blobCid.toString(),
|
|
297
297
|
})
|
|
@@ -299,7 +299,7 @@ describe('BlobSchema', () => {
|
|
|
299
299
|
})
|
|
300
300
|
|
|
301
301
|
it('rejects legacy format with invalid cid', () => {
|
|
302
|
-
const schema =
|
|
302
|
+
const schema = blob({ allowLegacy: true })
|
|
303
303
|
const result = schema.safeParse({
|
|
304
304
|
cid: 'invalid-cid',
|
|
305
305
|
mimeType: 'image/jpeg',
|
|
@@ -308,7 +308,7 @@ describe('BlobSchema', () => {
|
|
|
308
308
|
})
|
|
309
309
|
|
|
310
310
|
it('rejects legacy format with numeric cid', () => {
|
|
311
|
-
const schema =
|
|
311
|
+
const schema = blob({ allowLegacy: true })
|
|
312
312
|
const result = schema.safeParse({
|
|
313
313
|
cid: 123,
|
|
314
314
|
mimeType: 'image/jpeg',
|
|
@@ -317,7 +317,7 @@ describe('BlobSchema', () => {
|
|
|
317
317
|
})
|
|
318
318
|
|
|
319
319
|
it('rejects legacy format with extra properties', () => {
|
|
320
|
-
const schema =
|
|
320
|
+
const schema = blob({ allowLegacy: true })
|
|
321
321
|
const result = schema.safeParse({
|
|
322
322
|
cid: blobCid.toString(),
|
|
323
323
|
mimeType: 'image/jpeg',
|
|
@@ -327,7 +327,7 @@ describe('BlobSchema', () => {
|
|
|
327
327
|
})
|
|
328
328
|
|
|
329
329
|
it('accepts both BlobRef and LegacyBlobRef formats when allowLegacy is true', () => {
|
|
330
|
-
const schema =
|
|
330
|
+
const schema = blob({ allowLegacy: true })
|
|
331
331
|
|
|
332
332
|
const blobRefResult = schema.safeParse({
|
|
333
333
|
$type: 'blob',
|
|
@@ -347,7 +347,7 @@ describe('BlobSchema', () => {
|
|
|
347
347
|
|
|
348
348
|
describe('accept and maxSize options', () => {
|
|
349
349
|
it('accepts blob with accept option (not enforced)', () => {
|
|
350
|
-
const schema =
|
|
350
|
+
const schema = blob({ accept: ['image/jpeg', 'image/png'] })
|
|
351
351
|
const result = schema.safeParse({
|
|
352
352
|
$type: 'blob',
|
|
353
353
|
ref: blobCid,
|
|
@@ -358,7 +358,7 @@ describe('BlobSchema', () => {
|
|
|
358
358
|
})
|
|
359
359
|
|
|
360
360
|
it('accepts blob with maxSize option (not enforced)', () => {
|
|
361
|
-
const schema =
|
|
361
|
+
const schema = blob({ maxSize: 1000 })
|
|
362
362
|
const result = schema.safeParse({
|
|
363
363
|
$type: 'blob',
|
|
364
364
|
ref: blobCid,
|
|
@@ -369,7 +369,7 @@ describe('BlobSchema', () => {
|
|
|
369
369
|
})
|
|
370
370
|
|
|
371
371
|
it('accepts blob matching accept constraint', () => {
|
|
372
|
-
const schema =
|
|
372
|
+
const schema = blob({ accept: ['image/jpeg', 'image/png'] })
|
|
373
373
|
const result = schema.safeParse({
|
|
374
374
|
$type: 'blob',
|
|
375
375
|
ref: blobCid,
|
|
@@ -380,7 +380,7 @@ describe('BlobSchema', () => {
|
|
|
380
380
|
})
|
|
381
381
|
|
|
382
382
|
it('accepts blob matching maxSize constraint', () => {
|
|
383
|
-
const schema =
|
|
383
|
+
const schema = blob({ maxSize: 20000 })
|
|
384
384
|
const result = schema.safeParse({
|
|
385
385
|
$type: 'blob',
|
|
386
386
|
ref: blobCid,
|
|
@@ -392,7 +392,7 @@ describe('BlobSchema', () => {
|
|
|
392
392
|
})
|
|
393
393
|
|
|
394
394
|
describe('edge cases', () => {
|
|
395
|
-
const schema =
|
|
395
|
+
const schema = blob({})
|
|
396
396
|
|
|
397
397
|
it('validates blob with large size', () => {
|
|
398
398
|
const result = schema.safeParse({
|
|
@@ -457,7 +457,7 @@ describe('BlobSchema', () => {
|
|
|
457
457
|
|
|
458
458
|
describe('combined options', () => {
|
|
459
459
|
it('validates with strict and allowLegacy both true', () => {
|
|
460
|
-
const schema =
|
|
460
|
+
const schema = blob({ strict: true, allowLegacy: true })
|
|
461
461
|
|
|
462
462
|
// Should accept strict BlobRef
|
|
463
463
|
const blobRefResult = schema.safeParse({
|
|
@@ -486,7 +486,7 @@ describe('BlobSchema', () => {
|
|
|
486
486
|
})
|
|
487
487
|
|
|
488
488
|
it('validates with all options combined', () => {
|
|
489
|
-
const schema =
|
|
489
|
+
const schema = blob({
|
|
490
490
|
strict: true,
|
|
491
491
|
allowLegacy: true,
|
|
492
492
|
accept: ['image/jpeg'],
|
package/src/schema/blob.ts
CHANGED
|
@@ -5,7 +5,8 @@ import {
|
|
|
5
5
|
isBlobRef,
|
|
6
6
|
isLegacyBlobRef,
|
|
7
7
|
} from '@atproto/lex-data'
|
|
8
|
-
import { Schema,
|
|
8
|
+
import { Schema, ValidationContext } from '../core.js'
|
|
9
|
+
import { memoizedOptions } from '../util/memoize.js'
|
|
9
10
|
|
|
10
11
|
export type BlobSchemaOptions = BlobRefCheckOptions & {
|
|
11
12
|
/**
|
|
@@ -25,27 +26,22 @@ export type BlobSchemaOptions = BlobRefCheckOptions & {
|
|
|
25
26
|
|
|
26
27
|
export type { BlobRef, LegacyBlobRef }
|
|
27
28
|
|
|
28
|
-
export
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
export class BlobSchema<O extends BlobSchemaOptions> extends Schema<
|
|
33
|
-
BlobSchemaOutput<O>
|
|
29
|
+
export class BlobSchema<
|
|
30
|
+
const TOptions extends BlobSchemaOptions = NonNullable<unknown>,
|
|
31
|
+
> extends Schema<
|
|
32
|
+
TOptions extends { allowLegacy: true } ? BlobRef | LegacyBlobRef : BlobRef
|
|
34
33
|
> {
|
|
35
|
-
constructor(readonly options
|
|
34
|
+
constructor(readonly options?: TOptions) {
|
|
36
35
|
super()
|
|
37
36
|
}
|
|
38
37
|
|
|
39
|
-
validateInContext(
|
|
40
|
-
input: unknown,
|
|
41
|
-
ctx: ValidatorContext,
|
|
42
|
-
): ValidationResult<BlobSchemaOutput<O>> {
|
|
38
|
+
validateInContext(input: unknown, ctx: ValidationContext) {
|
|
43
39
|
const blob: null | BlobRef | LegacyBlobRef =
|
|
44
40
|
(input as any)?.$type !== undefined
|
|
45
41
|
? isBlobRef(input, this.options)
|
|
46
42
|
? input
|
|
47
43
|
: null
|
|
48
|
-
: this.options
|
|
44
|
+
: this.options?.allowLegacy === true && isLegacyBlobRef(input)
|
|
49
45
|
? input
|
|
50
46
|
: null
|
|
51
47
|
|
|
@@ -53,21 +49,21 @@ export class BlobSchema<O extends BlobSchemaOptions> extends Schema<
|
|
|
53
49
|
return ctx.issueInvalidType(input, 'blob')
|
|
54
50
|
}
|
|
55
51
|
|
|
56
|
-
const
|
|
52
|
+
const accept = this.options?.accept
|
|
57
53
|
if (accept && !matchesMime(blob.mimeType, accept)) {
|
|
58
54
|
return ctx.issueInvalidPropertyValue(blob, 'mimeType', accept)
|
|
59
55
|
}
|
|
60
56
|
|
|
61
|
-
const
|
|
57
|
+
const maxSize = this.options?.maxSize
|
|
62
58
|
if (maxSize != null && 'size' in blob && blob.size > maxSize) {
|
|
63
59
|
return ctx.issueTooBig(blob, 'blob', maxSize, blob.size)
|
|
64
60
|
}
|
|
65
61
|
|
|
66
|
-
return ctx.success(blob
|
|
62
|
+
return ctx.success(blob)
|
|
67
63
|
}
|
|
68
64
|
|
|
69
65
|
matchesMime(mime: string): boolean {
|
|
70
|
-
const
|
|
66
|
+
const accept = this.options?.accept
|
|
71
67
|
if (!accept) return true
|
|
72
68
|
return matchesMime(mime, accept)
|
|
73
69
|
}
|
|
@@ -83,3 +79,9 @@ function matchesMime(mime: string, accepted: string[]): boolean {
|
|
|
83
79
|
}
|
|
84
80
|
return false
|
|
85
81
|
}
|
|
82
|
+
|
|
83
|
+
export const blob = /*#__PURE__*/ memoizedOptions(function <
|
|
84
|
+
O extends BlobSchemaOptions = { allowLegacy?: false },
|
|
85
|
+
>(options?: O) {
|
|
86
|
+
return new BlobSchema(options)
|
|
87
|
+
})
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { describe, expect, it } from 'vitest'
|
|
2
|
-
import {
|
|
2
|
+
import { boolean } from './boolean.js'
|
|
3
|
+
import { withDefault } from './with-default.js'
|
|
3
4
|
|
|
4
5
|
describe('BooleanSchema', () => {
|
|
5
6
|
describe('basic validation', () => {
|
|
6
|
-
const schema =
|
|
7
|
+
const schema = boolean()
|
|
7
8
|
|
|
8
9
|
it('validates true', () => {
|
|
9
10
|
const result = schema.safeParse(true)
|
|
@@ -54,7 +55,7 @@ describe('BooleanSchema', () => {
|
|
|
54
55
|
|
|
55
56
|
describe('with default value', () => {
|
|
56
57
|
it('uses default value of true when input is undefined', () => {
|
|
57
|
-
const schema =
|
|
58
|
+
const schema = withDefault(boolean(), true)
|
|
58
59
|
const result = schema.safeParse(undefined)
|
|
59
60
|
expect(result.success).toBe(true)
|
|
60
61
|
if (result.success) {
|
|
@@ -63,7 +64,7 @@ describe('BooleanSchema', () => {
|
|
|
63
64
|
})
|
|
64
65
|
|
|
65
66
|
it('uses default value of false when input is undefined', () => {
|
|
66
|
-
const schema =
|
|
67
|
+
const schema = withDefault(boolean(), false)
|
|
67
68
|
const result = schema.safeParse(undefined)
|
|
68
69
|
expect(result.success).toBe(true)
|
|
69
70
|
if (result.success) {
|
|
@@ -72,7 +73,7 @@ describe('BooleanSchema', () => {
|
|
|
72
73
|
})
|
|
73
74
|
|
|
74
75
|
it('overrides default value with explicit true', () => {
|
|
75
|
-
const schema =
|
|
76
|
+
const schema = withDefault(boolean(), false)
|
|
76
77
|
const result = schema.safeParse(true)
|
|
77
78
|
expect(result.success).toBe(true)
|
|
78
79
|
if (result.success) {
|
|
@@ -81,7 +82,7 @@ describe('BooleanSchema', () => {
|
|
|
81
82
|
})
|
|
82
83
|
|
|
83
84
|
it('overrides default value with explicit false', () => {
|
|
84
|
-
const schema =
|
|
85
|
+
const schema = withDefault(boolean(), true)
|
|
85
86
|
const result = schema.safeParse(false)
|
|
86
87
|
expect(result.success).toBe(true)
|
|
87
88
|
if (result.success) {
|
|
@@ -90,14 +91,14 @@ describe('BooleanSchema', () => {
|
|
|
90
91
|
})
|
|
91
92
|
|
|
92
93
|
it('rejects invalid types even with default', () => {
|
|
93
|
-
const schema =
|
|
94
|
+
const schema = withDefault(boolean(), true)
|
|
94
95
|
const result = schema.safeParse('not a boolean')
|
|
95
96
|
expect(result.success).toBe(false)
|
|
96
97
|
})
|
|
97
98
|
})
|
|
98
99
|
|
|
99
100
|
describe('edge cases', () => {
|
|
100
|
-
const schema =
|
|
101
|
+
const schema = boolean()
|
|
101
102
|
|
|
102
103
|
it('rejects Boolean object', () => {
|
|
103
104
|
const result = schema.safeParse(new Boolean(true))
|
package/src/schema/boolean.ts
CHANGED
|
@@ -1,18 +1,8 @@
|
|
|
1
|
-
import { Schema,
|
|
2
|
-
|
|
3
|
-
export type BooleanSchemaOptions = {
|
|
4
|
-
default?: boolean
|
|
5
|
-
}
|
|
1
|
+
import { Schema, ValidationContext } from '../core.js'
|
|
2
|
+
import { memoizedOptions } from '../util/memoize.js'
|
|
6
3
|
|
|
7
4
|
export class BooleanSchema extends Schema<boolean> {
|
|
8
|
-
|
|
9
|
-
super()
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
validateInContext(
|
|
13
|
-
input: unknown = this.options?.default,
|
|
14
|
-
ctx: ValidatorContext,
|
|
15
|
-
): ValidationResult<boolean> {
|
|
5
|
+
validateInContext(input: unknown, ctx: ValidationContext) {
|
|
16
6
|
if (typeof input === 'boolean') {
|
|
17
7
|
return ctx.success(input)
|
|
18
8
|
}
|
|
@@ -20,3 +10,7 @@ export class BooleanSchema extends Schema<boolean> {
|
|
|
20
10
|
return ctx.issueInvalidType(input, 'boolean')
|
|
21
11
|
}
|
|
22
12
|
}
|
|
13
|
+
|
|
14
|
+
export const boolean = /*#__PURE__*/ memoizedOptions(function () {
|
|
15
|
+
return new BooleanSchema()
|
|
16
|
+
})
|
package/src/schema/bytes.test.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { describe, expect, it } from 'vitest'
|
|
2
|
-
import {
|
|
2
|
+
import { bytes } from './bytes.js'
|
|
3
3
|
|
|
4
4
|
describe('BytesSchema', () => {
|
|
5
5
|
describe('basic validation', () => {
|
|
6
|
-
const schema =
|
|
6
|
+
const schema = bytes({})
|
|
7
7
|
|
|
8
8
|
it('validates Uint8Array', () => {
|
|
9
9
|
const result = schema.safeParse(new Uint8Array([0, 1, 2, 3]))
|
|
@@ -72,7 +72,7 @@ describe('BytesSchema', () => {
|
|
|
72
72
|
})
|
|
73
73
|
|
|
74
74
|
describe('minLength constraint', () => {
|
|
75
|
-
const schema =
|
|
75
|
+
const schema = bytes({ minLength: 3 })
|
|
76
76
|
|
|
77
77
|
it('validates bytes at minimum length', () => {
|
|
78
78
|
const result = schema.safeParse(new Uint8Array([0, 1, 2]))
|
|
@@ -96,7 +96,7 @@ describe('BytesSchema', () => {
|
|
|
96
96
|
})
|
|
97
97
|
|
|
98
98
|
describe('maxLength constraint', () => {
|
|
99
|
-
const schema =
|
|
99
|
+
const schema = bytes({ maxLength: 5 })
|
|
100
100
|
|
|
101
101
|
it('validates bytes at maximum length', () => {
|
|
102
102
|
const result = schema.safeParse(new Uint8Array([0, 1, 2, 3, 4]))
|
|
@@ -120,7 +120,7 @@ describe('BytesSchema', () => {
|
|
|
120
120
|
})
|
|
121
121
|
|
|
122
122
|
describe('minLength and maxLength constraints', () => {
|
|
123
|
-
const schema =
|
|
123
|
+
const schema = bytes({ minLength: 2, maxLength: 5 })
|
|
124
124
|
|
|
125
125
|
it('validates bytes within range', () => {
|
|
126
126
|
const result = schema.safeParse(new Uint8Array([0, 1, 2]))
|
|
@@ -150,37 +150,37 @@ describe('BytesSchema', () => {
|
|
|
150
150
|
|
|
151
151
|
describe('edge cases', () => {
|
|
152
152
|
it('validates with minLength of 0', () => {
|
|
153
|
-
const schema =
|
|
153
|
+
const schema = bytes({ minLength: 0 })
|
|
154
154
|
const result = schema.safeParse(new Uint8Array([]))
|
|
155
155
|
expect(result.success).toBe(true)
|
|
156
156
|
})
|
|
157
157
|
|
|
158
158
|
it('validates with maxLength of 0', () => {
|
|
159
|
-
const schema =
|
|
159
|
+
const schema = bytes({ maxLength: 0 })
|
|
160
160
|
const result = schema.safeParse(new Uint8Array([]))
|
|
161
161
|
expect(result.success).toBe(true)
|
|
162
162
|
})
|
|
163
163
|
|
|
164
164
|
it('rejects non-empty bytes with maxLength of 0', () => {
|
|
165
|
-
const schema =
|
|
165
|
+
const schema = bytes({ maxLength: 0 })
|
|
166
166
|
const result = schema.safeParse(new Uint8Array([0]))
|
|
167
167
|
expect(result.success).toBe(false)
|
|
168
168
|
})
|
|
169
169
|
|
|
170
170
|
it('validates bytes with all zeros', () => {
|
|
171
|
-
const schema =
|
|
171
|
+
const schema = bytes({})
|
|
172
172
|
const result = schema.safeParse(new Uint8Array([0, 0, 0, 0]))
|
|
173
173
|
expect(result.success).toBe(true)
|
|
174
174
|
})
|
|
175
175
|
|
|
176
176
|
it('validates bytes with all 255s', () => {
|
|
177
|
-
const schema =
|
|
177
|
+
const schema = bytes({})
|
|
178
178
|
const result = schema.safeParse(new Uint8Array([255, 255, 255, 255]))
|
|
179
179
|
expect(result.success).toBe(true)
|
|
180
180
|
})
|
|
181
181
|
|
|
182
182
|
it('validates large byte arrays', () => {
|
|
183
|
-
const schema =
|
|
183
|
+
const schema = bytes({})
|
|
184
184
|
const largeArray = new Uint8Array(10000)
|
|
185
185
|
const result = schema.safeParse(largeArray)
|
|
186
186
|
expect(result.success).toBe(true)
|
|
@@ -188,7 +188,7 @@ describe('BytesSchema', () => {
|
|
|
188
188
|
})
|
|
189
189
|
|
|
190
190
|
describe('TypedArray coercion', () => {
|
|
191
|
-
const schema =
|
|
191
|
+
const schema = bytes({})
|
|
192
192
|
|
|
193
193
|
it('coerces Int8Array to Uint8Array', () => {
|
|
194
194
|
const int8 = new Int8Array([1, 2, 3])
|
|
@@ -218,7 +218,7 @@ describe('BytesSchema', () => {
|
|
|
218
218
|
})
|
|
219
219
|
|
|
220
220
|
it('validates coerced TypedArray with length constraints', () => {
|
|
221
|
-
const schema =
|
|
221
|
+
const schema = bytes({ minLength: 2, maxLength: 10 })
|
|
222
222
|
const int16 = new Int16Array([1, 2, 3]) // 6 bytes
|
|
223
223
|
const result = schema.safeParse(int16)
|
|
224
224
|
expect(result.success).toBe(true)
|
package/src/schema/bytes.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { asUint8Array } from '@atproto/lex-data'
|
|
2
|
-
import { Schema,
|
|
1
|
+
import { asUint8Array, ifUint8Array } from '@atproto/lex-data'
|
|
2
|
+
import { Schema, ValidationContext } from '../core.js'
|
|
3
|
+
import { memoizedOptions } from '../util/memoize.js'
|
|
3
4
|
|
|
4
5
|
export type BytesSchemaOptions = {
|
|
5
6
|
minLength?: number
|
|
@@ -11,12 +12,10 @@ export class BytesSchema extends Schema<Uint8Array> {
|
|
|
11
12
|
super()
|
|
12
13
|
}
|
|
13
14
|
|
|
14
|
-
validateInContext(
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
// Coerce different binary formats into Uint8Array
|
|
19
|
-
const bytes = asUint8Array(input)
|
|
15
|
+
validateInContext(input: unknown, ctx: ValidationContext) {
|
|
16
|
+
// In "parse" mode, coerce different binary formats into Uint8Array
|
|
17
|
+
const bytes =
|
|
18
|
+
ctx.options.mode === 'parse' ? asUint8Array(input) : ifUint8Array(input)
|
|
20
19
|
if (!bytes) {
|
|
21
20
|
return ctx.issueInvalidType(input, 'bytes')
|
|
22
21
|
}
|
|
@@ -34,3 +33,9 @@ export class BytesSchema extends Schema<Uint8Array> {
|
|
|
34
33
|
return ctx.success(bytes)
|
|
35
34
|
}
|
|
36
35
|
}
|
|
36
|
+
|
|
37
|
+
export const bytes = /*#__PURE__*/ memoizedOptions(function (
|
|
38
|
+
options?: BytesSchemaOptions,
|
|
39
|
+
) {
|
|
40
|
+
return new BytesSchema(options)
|
|
41
|
+
})
|
package/src/schema/cid.test.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { describe, expect, it } from 'vitest'
|
|
2
2
|
import { parseCid } from '@atproto/lex-data'
|
|
3
|
-
import {
|
|
3
|
+
import { cid } from './cid.js'
|
|
4
4
|
|
|
5
5
|
const cborCid = parseCid(
|
|
6
6
|
'bafyreidfayvfuwqa7qlnopdjiqrxzs6blmoeu4rujcjtnci5beludirz2a',
|
|
@@ -26,7 +26,7 @@ const sha512Cid = parseCid(
|
|
|
26
26
|
|
|
27
27
|
describe('CidSchema', () => {
|
|
28
28
|
describe('default mode (non-strict)', () => {
|
|
29
|
-
const schema =
|
|
29
|
+
const schema = cid({})
|
|
30
30
|
|
|
31
31
|
it('validates CID v1 with DAG-CBOR codec and SHA-256', () => {
|
|
32
32
|
const result = schema.safeParse(cborCid)
|
|
@@ -80,7 +80,7 @@ describe('CidSchema', () => {
|
|
|
80
80
|
})
|
|
81
81
|
|
|
82
82
|
describe('strict mode', () => {
|
|
83
|
-
const schema =
|
|
83
|
+
const schema = cid({ flavor: 'dasl' })
|
|
84
84
|
|
|
85
85
|
it('validates CID v1 with DAG-CBOR codec and SHA-256', () => {
|
|
86
86
|
const result = schema.safeParse(cborCid)
|
package/src/schema/cid.ts
CHANGED
|
@@ -1,24 +1,19 @@
|
|
|
1
1
|
import { CheckCidOptions, Cid, InferCheckedCid, isCid } from '@atproto/lex-data'
|
|
2
|
-
import { Schema,
|
|
2
|
+
import { Schema, ValidationContext } from '../core.js'
|
|
3
|
+
import { memoizedOptions } from '../util/memoize.js'
|
|
3
4
|
|
|
4
5
|
export type { Cid }
|
|
5
6
|
|
|
6
7
|
export type CidSchemaOptions = CheckCidOptions
|
|
7
|
-
export type CidSchemaOutput<
|
|
8
|
-
TOptions extends CidSchemaOptions = { flavor: undefined },
|
|
9
|
-
> = InferCheckedCid<TOptions>
|
|
10
8
|
|
|
11
|
-
export class CidSchema<
|
|
12
|
-
|
|
13
|
-
> {
|
|
14
|
-
constructor(readonly options
|
|
9
|
+
export class CidSchema<
|
|
10
|
+
const TOptions extends CidSchemaOptions = { flavor: undefined },
|
|
11
|
+
> extends Schema<InferCheckedCid<TOptions>> {
|
|
12
|
+
constructor(readonly options?: TOptions) {
|
|
15
13
|
super()
|
|
16
14
|
}
|
|
17
15
|
|
|
18
|
-
validateInContext(
|
|
19
|
-
input: unknown,
|
|
20
|
-
ctx: ValidatorContext,
|
|
21
|
-
): ValidationResult<CidSchemaOutput<TOptions>> {
|
|
16
|
+
validateInContext(input: unknown, ctx: ValidationContext) {
|
|
22
17
|
if (!isCid(input, this.options)) {
|
|
23
18
|
return ctx.issueInvalidType(input, 'cid')
|
|
24
19
|
}
|
|
@@ -26,3 +21,9 @@ export class CidSchema<TOptions extends CidSchemaOptions> extends Schema<
|
|
|
26
21
|
return ctx.success(input)
|
|
27
22
|
}
|
|
28
23
|
}
|
|
24
|
+
|
|
25
|
+
export const cid = /*#__PURE__*/ memoizedOptions(function <
|
|
26
|
+
O extends CidSchemaOptions = NonNullable<unknown>,
|
|
27
|
+
>(options?: O) {
|
|
28
|
+
return new CidSchema(options)
|
|
29
|
+
})
|