@atproto/lex-schema 0.0.4 → 0.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +58 -0
- package/dist/core/$type.d.ts +7 -0
- package/dist/core/$type.d.ts.map +1 -1
- package/dist/core/$type.js.map +1 -1
- package/dist/core/property-key.d.ts.map +1 -0
- package/dist/core/property-key.js.map +1 -0
- package/dist/core/result.d.ts +7 -6
- package/dist/core/result.d.ts.map +1 -1
- package/dist/core/result.js +9 -8
- package/dist/core/result.js.map +1 -1
- package/dist/{validation → core}/schema.d.ts +21 -2
- package/dist/core/schema.d.ts.map +1 -0
- package/dist/{validation → core}/schema.js +25 -2
- package/dist/core/schema.js.map +1 -0
- package/dist/core/string-format.d.ts +37 -26
- package/dist/core/string-format.d.ts.map +1 -1
- package/dist/core/string-format.js +66 -59
- package/dist/core/string-format.js.map +1 -1
- package/dist/core/types.d.ts +3 -0
- package/dist/core/types.d.ts.map +1 -1
- package/dist/core/types.js.map +1 -1
- package/dist/core/validation-error.d.ts +19 -0
- package/dist/core/validation-error.d.ts.map +1 -0
- package/dist/{validation → core}/validation-error.js +13 -6
- package/dist/core/validation-error.js.map +1 -0
- package/dist/{validation → core}/validation-issue.d.ts +43 -0
- package/dist/core/validation-issue.d.ts.map +1 -0
- package/dist/{validation → core}/validation-issue.js +55 -2
- package/dist/core/validation-issue.js.map +1 -0
- package/dist/{validation → core}/validator.d.ts +5 -4
- package/dist/core/validator.d.ts.map +1 -0
- package/dist/{validation → core}/validator.js +16 -13
- package/dist/core/validator.js.map +1 -0
- package/dist/core.d.ts +5 -0
- package/dist/core.d.ts.map +1 -1
- package/dist/core.js +5 -0
- package/dist/core.js.map +1 -1
- package/dist/external.d.ts +21 -21
- package/dist/external.d.ts.map +1 -1
- package/dist/external.js +39 -55
- package/dist/external.js.map +1 -1
- package/dist/helpers.d.ts +41 -0
- package/dist/helpers.d.ts.map +1 -0
- package/dist/helpers.js +13 -0
- package/dist/helpers.js.map +1 -0
- package/dist/schema/_parameters.d.ts +1 -1
- package/dist/schema/_parameters.d.ts.map +1 -1
- package/dist/schema/_parameters.js.map +1 -1
- package/dist/schema/array.d.ts +1 -1
- package/dist/schema/array.d.ts.map +1 -1
- package/dist/schema/array.js +2 -2
- package/dist/schema/array.js.map +1 -1
- package/dist/schema/blob.d.ts +2 -1
- package/dist/schema/blob.d.ts.map +1 -1
- package/dist/schema/blob.js +34 -20
- package/dist/schema/blob.js.map +1 -1
- package/dist/schema/boolean.d.ts +1 -1
- package/dist/schema/boolean.d.ts.map +1 -1
- package/dist/schema/boolean.js +2 -2
- package/dist/schema/boolean.js.map +1 -1
- package/dist/schema/bytes.d.ts +1 -1
- package/dist/schema/bytes.d.ts.map +1 -1
- package/dist/schema/bytes.js +2 -2
- package/dist/schema/bytes.js.map +1 -1
- package/dist/schema/cid.d.ts +1 -1
- package/dist/schema/cid.d.ts.map +1 -1
- package/dist/schema/cid.js +2 -2
- package/dist/schema/cid.js.map +1 -1
- package/dist/schema/custom.d.ts +1 -1
- package/dist/schema/custom.d.ts.map +1 -1
- package/dist/schema/custom.js +3 -3
- package/dist/schema/custom.js.map +1 -1
- package/dist/schema/dict.d.ts +1 -1
- package/dist/schema/dict.d.ts.map +1 -1
- package/dist/schema/dict.js +2 -2
- package/dist/schema/dict.js.map +1 -1
- package/dist/schema/discriminated-union.d.ts +1 -1
- package/dist/schema/discriminated-union.d.ts.map +1 -1
- package/dist/schema/discriminated-union.js +2 -2
- package/dist/schema/discriminated-union.js.map +1 -1
- package/dist/schema/enum.d.ts +1 -1
- package/dist/schema/enum.d.ts.map +1 -1
- package/dist/schema/enum.js +2 -2
- package/dist/schema/enum.js.map +1 -1
- package/dist/schema/integer.d.ts +1 -1
- package/dist/schema/integer.d.ts.map +1 -1
- package/dist/schema/integer.js +4 -4
- package/dist/schema/integer.js.map +1 -1
- package/dist/schema/intersection.d.ts +1 -2
- package/dist/schema/intersection.d.ts.map +1 -1
- package/dist/schema/intersection.js +2 -2
- package/dist/schema/intersection.js.map +1 -1
- package/dist/schema/literal.d.ts +1 -1
- package/dist/schema/literal.d.ts.map +1 -1
- package/dist/schema/literal.js +2 -2
- package/dist/schema/literal.js.map +1 -1
- package/dist/schema/never.d.ts +1 -1
- package/dist/schema/never.d.ts.map +1 -1
- package/dist/schema/never.js +2 -2
- package/dist/schema/never.js.map +1 -1
- package/dist/schema/null.d.ts +1 -1
- package/dist/schema/null.d.ts.map +1 -1
- package/dist/schema/null.js +2 -2
- package/dist/schema/null.js.map +1 -1
- package/dist/schema/nullable.d.ts +1 -1
- package/dist/schema/nullable.d.ts.map +1 -1
- package/dist/schema/nullable.js +2 -2
- package/dist/schema/nullable.js.map +1 -1
- package/dist/schema/object.d.ts +1 -2
- package/dist/schema/object.d.ts.map +1 -1
- package/dist/schema/object.js +2 -2
- package/dist/schema/object.js.map +1 -1
- package/dist/schema/optional.d.ts +1 -1
- package/dist/schema/optional.d.ts.map +1 -1
- package/dist/schema/optional.js +2 -2
- package/dist/schema/optional.js.map +1 -1
- package/dist/schema/params.d.ts +1 -3
- package/dist/schema/params.d.ts.map +1 -1
- package/dist/schema/params.js +2 -2
- package/dist/schema/params.js.map +1 -1
- package/dist/schema/payload.d.ts +17 -15
- package/dist/schema/payload.d.ts.map +1 -1
- package/dist/schema/payload.js +28 -0
- package/dist/schema/payload.js.map +1 -1
- package/dist/schema/procedure.d.ts +3 -6
- package/dist/schema/procedure.d.ts.map +1 -1
- package/dist/schema/procedure.js +1 -0
- package/dist/schema/procedure.js.map +1 -1
- package/dist/schema/query.d.ts +3 -5
- package/dist/schema/query.d.ts.map +1 -1
- package/dist/schema/query.js +1 -0
- package/dist/schema/query.js.map +1 -1
- package/dist/schema/record.d.ts +14 -14
- package/dist/schema/record.d.ts.map +1 -1
- package/dist/schema/record.js +2 -2
- package/dist/schema/record.js.map +1 -1
- package/dist/schema/ref.d.ts +1 -1
- package/dist/schema/ref.d.ts.map +1 -1
- package/dist/schema/ref.js +2 -2
- package/dist/schema/ref.js.map +1 -1
- package/dist/schema/refine.d.ts +18 -1
- package/dist/schema/refine.d.ts.map +1 -1
- package/dist/schema/refine.js +2 -2
- package/dist/schema/refine.js.map +1 -1
- package/dist/schema/regexp.d.ts +1 -1
- package/dist/schema/regexp.d.ts.map +1 -1
- package/dist/schema/regexp.js +2 -2
- package/dist/schema/regexp.js.map +1 -1
- package/dist/schema/string.d.ts +1 -2
- package/dist/schema/string.d.ts.map +1 -1
- package/dist/schema/string.js +1 -2
- package/dist/schema/string.js.map +1 -1
- package/dist/schema/subscription.d.ts +4 -8
- package/dist/schema/subscription.d.ts.map +1 -1
- package/dist/schema/subscription.js.map +1 -1
- package/dist/schema/token.d.ts +1 -1
- package/dist/schema/token.d.ts.map +1 -1
- package/dist/schema/token.js +2 -2
- package/dist/schema/token.js.map +1 -1
- package/dist/schema/typed-object.d.ts +8 -8
- package/dist/schema/typed-object.d.ts.map +1 -1
- package/dist/schema/typed-object.js +2 -2
- package/dist/schema/typed-object.js.map +1 -1
- package/dist/schema/typed-ref.d.ts +1 -1
- package/dist/schema/typed-ref.d.ts.map +1 -1
- package/dist/schema/typed-ref.js +2 -2
- package/dist/schema/typed-ref.js.map +1 -1
- package/dist/schema/typed-union.d.ts +2 -3
- package/dist/schema/typed-union.d.ts.map +1 -1
- package/dist/schema/typed-union.js +2 -2
- package/dist/schema/typed-union.js.map +1 -1
- package/dist/schema/union.d.ts +1 -1
- package/dist/schema/union.d.ts.map +1 -1
- package/dist/schema/union.js +3 -6
- package/dist/schema/union.js.map +1 -1
- package/dist/schema/unknown-object.d.ts +1 -2
- package/dist/schema/unknown-object.d.ts.map +1 -1
- package/dist/schema/unknown-object.js +2 -2
- package/dist/schema/unknown-object.js.map +1 -1
- package/dist/schema/unknown.d.ts +1 -1
- package/dist/schema/unknown.d.ts.map +1 -1
- package/dist/schema/unknown.js +2 -2
- package/dist/schema/unknown.js.map +1 -1
- package/dist/util/assertion-util.d.ts +8 -0
- package/dist/util/assertion-util.d.ts.map +1 -0
- package/dist/util/assertion-util.js +31 -0
- package/dist/util/assertion-util.js.map +1 -0
- package/dist/util/memoize.d.ts +3 -0
- package/dist/util/memoize.d.ts.map +1 -0
- package/dist/util/memoize.js +52 -0
- package/dist/util/memoize.js.map +1 -0
- package/package.json +6 -6
- package/src/core/$type.ts +4 -0
- package/src/core/result.ts +9 -8
- package/src/{validation → core}/schema.ts +29 -4
- package/src/core/string-format.ts +88 -68
- package/src/core/types.ts +4 -0
- package/src/{validation → core}/validation-error.ts +14 -6
- package/src/{validation → core}/validation-issue.ts +64 -2
- package/src/{validation → core}/validator.ts +17 -13
- package/src/core.ts +5 -0
- package/src/external.ts +75 -55
- package/src/helpers.test.ts +487 -0
- package/src/helpers.ts +75 -0
- package/src/schema/_parameters.test.ts +1 -0
- package/src/schema/_parameters.ts +1 -1
- package/src/schema/array.test.ts +1 -0
- package/src/schema/array.ts +1 -1
- package/src/schema/blob.test.ts +3 -4
- package/src/schema/blob.ts +32 -24
- package/src/schema/boolean.test.ts +1 -0
- package/src/schema/boolean.ts +1 -1
- package/src/schema/bytes.test.ts +1 -0
- package/src/schema/bytes.ts +1 -1
- package/src/schema/cid.test.ts +1 -0
- package/src/schema/cid.ts +1 -1
- package/src/schema/custom.test.ts +8 -7
- package/src/schema/custom.ts +2 -2
- package/src/schema/dict.test.ts +1 -0
- package/src/schema/dict.ts +1 -1
- package/src/schema/discriminated-union.test.ts +1 -0
- package/src/schema/discriminated-union.ts +1 -1
- package/src/schema/enum.test.ts +1 -0
- package/src/schema/enum.ts +1 -1
- package/src/schema/integer.test.ts +1 -0
- package/src/schema/integer.ts +3 -3
- package/src/schema/intersection.test.ts +1 -0
- package/src/schema/intersection.ts +2 -2
- package/src/schema/literal.test.ts +1 -0
- package/src/schema/literal.ts +1 -1
- package/src/schema/never.test.ts +1 -0
- package/src/schema/never.ts +1 -1
- package/src/schema/null.test.ts +1 -0
- package/src/schema/null.ts +1 -1
- package/src/schema/nullable.test.ts +1 -0
- package/src/schema/nullable.ts +1 -1
- package/src/schema/object.test.ts +1 -0
- package/src/schema/object.ts +3 -3
- package/src/schema/optional.test.ts +1 -0
- package/src/schema/optional.ts +1 -1
- package/src/schema/params.test.ts +1 -0
- package/src/schema/params.ts +3 -10
- package/src/schema/payload.test.ts +1 -0
- package/src/schema/payload.ts +67 -34
- package/src/schema/permission-set.test.ts +37 -36
- package/src/schema/permission.test.ts +1 -0
- package/src/schema/procedure.test.ts +2 -62
- package/src/schema/procedure.ts +8 -20
- package/src/schema/query.test.ts +23 -69
- package/src/schema/query.ts +7 -14
- package/src/schema/record.test.ts +1 -0
- package/src/schema/record.ts +13 -6
- package/src/schema/ref.test.ts +2 -1
- package/src/schema/ref.ts +1 -1
- package/src/schema/refine.test.ts +1 -0
- package/src/schema/refine.ts +19 -2
- package/src/schema/regexp.test.ts +1 -0
- package/src/schema/regexp.ts +1 -1
- package/src/schema/string.test.ts +1 -0
- package/src/schema/string.ts +8 -2
- package/src/schema/subscription.test.ts +31 -93
- package/src/schema/subscription.ts +11 -25
- package/src/schema/token.test.ts +1 -0
- package/src/schema/token.ts +1 -1
- package/src/schema/typed-object.test.ts +1 -0
- package/src/schema/typed-object.ts +10 -5
- package/src/schema/typed-ref.test.ts +1 -0
- package/src/schema/typed-ref.ts +1 -1
- package/src/schema/typed-union.test.ts +1 -0
- package/src/schema/typed-union.ts +4 -4
- package/src/schema/union.test.ts +1 -0
- package/src/schema/union.ts +2 -5
- package/src/schema/unknown-object.test.ts +1 -0
- package/src/schema/unknown-object.ts +1 -2
- package/src/schema/unknown.test.ts +1 -0
- package/src/schema/unknown.ts +1 -1
- package/src/util/array-agg.test.ts +1 -0
- package/src/util/assertion-util.ts +40 -0
- package/src/util/memoize.ts +57 -0
- package/tsconfig.tests.json +2 -2
- package/dist/validation/property-key.d.ts.map +0 -1
- package/dist/validation/property-key.js.map +0 -1
- package/dist/validation/schema.d.ts.map +0 -1
- package/dist/validation/schema.js.map +0 -1
- package/dist/validation/validation-error.d.ts +0 -9
- package/dist/validation/validation-error.d.ts.map +0 -1
- package/dist/validation/validation-error.js.map +0 -1
- package/dist/validation/validation-issue.d.ts.map +0 -1
- package/dist/validation/validation-issue.js.map +0 -1
- package/dist/validation/validator.d.ts.map +0 -1
- package/dist/validation/validator.js.map +0 -1
- package/dist/validation.d.ts +0 -6
- package/dist/validation.d.ts.map +0 -1
- package/dist/validation.js +0 -9
- package/dist/validation.js.map +0 -1
- package/src/validation.ts +0 -5
- /package/dist/{validation → core}/property-key.d.ts +0 -0
- /package/dist/{validation → core}/property-key.js +0 -0
- /package/src/{validation → core}/property-key.ts +0 -0
package/src/schema/query.ts
CHANGED
|
@@ -1,22 +1,15 @@
|
|
|
1
1
|
import { NsidString } from '../core.js'
|
|
2
|
-
import { Infer } from '../validation.js'
|
|
3
2
|
import { ParamsSchema } from './params.js'
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
export type InferQueryParameters<Q extends Query> =
|
|
7
|
-
Q extends Query<any, infer P extends ParamsSchema, any> ? Infer<P> : never
|
|
8
|
-
|
|
9
|
-
export type InferQueryOutputBody<Q extends Query> =
|
|
10
|
-
Q extends Query<any, any, infer O extends Payload>
|
|
11
|
-
? InferPayloadBody<O>
|
|
12
|
-
: never
|
|
3
|
+
import { Payload } from './payload.js'
|
|
13
4
|
|
|
14
5
|
export class Query<
|
|
15
|
-
TNsid extends NsidString =
|
|
16
|
-
TParameters extends ParamsSchema =
|
|
17
|
-
TOutputPayload extends Payload =
|
|
18
|
-
TErrors extends undefined | readonly string[] =
|
|
6
|
+
TNsid extends NsidString = NsidString,
|
|
7
|
+
TParameters extends ParamsSchema = ParamsSchema,
|
|
8
|
+
TOutputPayload extends Payload = Payload,
|
|
9
|
+
TErrors extends undefined | readonly string[] = undefined | readonly string[],
|
|
19
10
|
> {
|
|
11
|
+
readonly type = 'query' as const
|
|
12
|
+
|
|
20
13
|
constructor(
|
|
21
14
|
readonly nsid: TNsid,
|
|
22
15
|
readonly parameters: TParameters,
|
package/src/schema/record.ts
CHANGED
|
@@ -1,26 +1,30 @@
|
|
|
1
|
-
import { LexiconRecordKey, NsidString, Simplify, TidString } from '../core.js'
|
|
2
1
|
import {
|
|
3
2
|
Infer,
|
|
3
|
+
LexiconRecordKey,
|
|
4
|
+
NsidString,
|
|
4
5
|
Schema,
|
|
6
|
+
Simplify,
|
|
7
|
+
TidString,
|
|
5
8
|
ValidationResult,
|
|
6
9
|
Validator,
|
|
7
10
|
ValidatorContext,
|
|
8
|
-
} from '../
|
|
11
|
+
} from '../core.js'
|
|
9
12
|
import { LiteralSchema } from './literal.js'
|
|
10
13
|
import { StringSchema } from './string.js'
|
|
14
|
+
import { TypedObject } from './typed-union.js'
|
|
11
15
|
|
|
12
16
|
export type InferRecordKey<R extends RecordSchema> =
|
|
13
17
|
R extends RecordSchema<infer K> ? RecordKeySchemaOutput<K> : never
|
|
14
18
|
|
|
15
19
|
export type RecordSchemaOutput<
|
|
16
20
|
T extends NsidString,
|
|
17
|
-
S extends Validator<{ [
|
|
18
|
-
> = Simplify<Omit<Infer<S>, '$type'
|
|
21
|
+
S extends Validator<{ [k: string]: unknown }>,
|
|
22
|
+
> = Simplify<{ $type: T } & Omit<Infer<S>, '$type'>>
|
|
19
23
|
|
|
20
24
|
export class RecordSchema<
|
|
21
25
|
K extends LexiconRecordKey = any,
|
|
22
26
|
T extends NsidString = any,
|
|
23
|
-
S extends Validator<{ [
|
|
27
|
+
S extends Validator<{ [k: string]: unknown }> = any,
|
|
24
28
|
> extends Schema<RecordSchemaOutput<T, S>> {
|
|
25
29
|
keySchema: RecordKeySchema<K>
|
|
26
30
|
|
|
@@ -35,7 +39,10 @@ export class RecordSchema<
|
|
|
35
39
|
|
|
36
40
|
isTypeOf<X extends { $type?: unknown }>(
|
|
37
41
|
value: X,
|
|
38
|
-
): value is
|
|
42
|
+
): value is Exclude<
|
|
43
|
+
X extends { $type?: T } ? X : X & { $type?: T },
|
|
44
|
+
TypedObject
|
|
45
|
+
> {
|
|
39
46
|
return value.$type === this.$type
|
|
40
47
|
}
|
|
41
48
|
|
package/src/schema/ref.test.ts
CHANGED
package/src/schema/ref.ts
CHANGED
package/src/schema/refine.ts
CHANGED
|
@@ -5,7 +5,7 @@ import {
|
|
|
5
5
|
ValidationResult,
|
|
6
6
|
Validator,
|
|
7
7
|
ValidatorContext,
|
|
8
|
-
} from '../
|
|
8
|
+
} from '../core.js'
|
|
9
9
|
import { CustomAssertionContext } from './custom.js'
|
|
10
10
|
|
|
11
11
|
export type RefinementCheck<T> = {
|
|
@@ -31,6 +31,23 @@ export type Refinement<T = any, Out extends T = T> =
|
|
|
31
31
|
| RefinementCheck<T>
|
|
32
32
|
| RefinementAssertion<T, Out>
|
|
33
33
|
|
|
34
|
+
/**
|
|
35
|
+
* Create a refined schema based on an existing schema and a refinement check.
|
|
36
|
+
*
|
|
37
|
+
* @param schema - The base schema to refine.
|
|
38
|
+
* @param refinement - The refinement check to apply.
|
|
39
|
+
* @returns A new schema that includes the refinement.
|
|
40
|
+
* @example
|
|
41
|
+
*
|
|
42
|
+
* ```ts
|
|
43
|
+
* const PositiveInt = refine(l.integer(), {
|
|
44
|
+
* check: (value) => value > 0,
|
|
45
|
+
* message: 'Value must be a positive integer',
|
|
46
|
+
* })
|
|
47
|
+
* const result = PositiveInt.validate(-5)
|
|
48
|
+
* // result.success === false
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
34
51
|
export function refine<S extends Validator, Out extends Infer<S>>(
|
|
35
52
|
schema: S,
|
|
36
53
|
refinement: RefinementAssertion<Infer<S>, Out>,
|
|
@@ -78,7 +95,7 @@ function validateInContextUnbound<S extends Validator>(
|
|
|
78
95
|
const checkResult = this.refinement.check.call(null, result.value, ctx)
|
|
79
96
|
if (!checkResult) {
|
|
80
97
|
const path = ctx.concatPath(this.refinement.path)
|
|
81
|
-
return ctx.
|
|
98
|
+
return ctx.issue(new IssueCustom(path, input, this.refinement.message))
|
|
82
99
|
}
|
|
83
100
|
|
|
84
101
|
return result
|
package/src/schema/regexp.ts
CHANGED
package/src/schema/string.ts
CHANGED
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
import { asCid, graphemeLen, utf8Len } from '@atproto/lex-data'
|
|
2
|
-
import {
|
|
3
|
-
|
|
2
|
+
import {
|
|
3
|
+
InferStringFormat,
|
|
4
|
+
Schema,
|
|
5
|
+
StringFormat,
|
|
6
|
+
ValidationResult,
|
|
7
|
+
ValidatorContext,
|
|
8
|
+
assertStringFormat,
|
|
9
|
+
} from '../core.js'
|
|
4
10
|
import { TokenSchema } from './token.js'
|
|
5
11
|
|
|
6
12
|
export type StringSchemaOptions = {
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
import { asNsidString } from '../core.js'
|
|
2
3
|
import { IntegerSchema } from './integer.js'
|
|
3
4
|
import { ObjectSchema } from './object.js'
|
|
4
5
|
import { OptionalSchema } from './optional.js'
|
|
@@ -14,7 +15,7 @@ import {
|
|
|
14
15
|
describe('Subscription', () => {
|
|
15
16
|
describe('constructor', () => {
|
|
16
17
|
it('creates a Subscription instance with all parameters', () => {
|
|
17
|
-
const nsid =
|
|
18
|
+
const nsid = asNsidString('com.atproto.sync.subscribeRepos')
|
|
18
19
|
const parameters = new ParamsSchema({
|
|
19
20
|
cursor: new OptionalSchema(new IntegerSchema({})),
|
|
20
21
|
})
|
|
@@ -34,7 +35,7 @@ describe('Subscription', () => {
|
|
|
34
35
|
})
|
|
35
36
|
|
|
36
37
|
it('creates a Subscription instance without errors', () => {
|
|
37
|
-
const nsid =
|
|
38
|
+
const nsid = asNsidString('com.atproto.sync.subscribeRepos')
|
|
38
39
|
const parameters = new ParamsSchema({
|
|
39
40
|
cursor: new OptionalSchema(new IntegerSchema({})),
|
|
40
41
|
})
|
|
@@ -57,7 +58,7 @@ describe('Subscription', () => {
|
|
|
57
58
|
})
|
|
58
59
|
|
|
59
60
|
it('creates a Subscription instance with empty parameters', () => {
|
|
60
|
-
const nsid =
|
|
61
|
+
const nsid = asNsidString('app.bsky.notification.subscribe')
|
|
61
62
|
const parameters = new ParamsSchema({})
|
|
62
63
|
const message = new ObjectSchema({
|
|
63
64
|
type: new StringSchema({}),
|
|
@@ -73,26 +74,11 @@ describe('Subscription', () => {
|
|
|
73
74
|
expect(subscription).toBeInstanceOf(Subscription)
|
|
74
75
|
expect(subscription.parameters).toBe(parameters)
|
|
75
76
|
})
|
|
76
|
-
|
|
77
|
-
it('creates a Subscription instance with undefined message', () => {
|
|
78
|
-
const nsid = asNsid('app.bsky.feed.subscribe')
|
|
79
|
-
const parameters = new ParamsSchema({})
|
|
80
|
-
|
|
81
|
-
const subscription = new Subscription(
|
|
82
|
-
nsid,
|
|
83
|
-
parameters,
|
|
84
|
-
undefined,
|
|
85
|
-
undefined,
|
|
86
|
-
)
|
|
87
|
-
|
|
88
|
-
expect(subscription).toBeInstanceOf(Subscription)
|
|
89
|
-
expect(subscription.message).toBeUndefined()
|
|
90
|
-
})
|
|
91
77
|
})
|
|
92
78
|
|
|
93
79
|
describe('type property', () => {
|
|
94
80
|
it('has type set to "subscription"', () => {
|
|
95
|
-
const nsid =
|
|
81
|
+
const nsid = asNsidString('com.atproto.sync.subscribeRepos')
|
|
96
82
|
const parameters = new ParamsSchema({})
|
|
97
83
|
const message = new ObjectSchema({
|
|
98
84
|
seq: new IntegerSchema({}),
|
|
@@ -109,7 +95,7 @@ describe('Subscription', () => {
|
|
|
109
95
|
})
|
|
110
96
|
|
|
111
97
|
it('type is a constant value', () => {
|
|
112
|
-
const nsid =
|
|
98
|
+
const nsid = asNsidString('com.atproto.sync.subscribeRepos')
|
|
113
99
|
const parameters = new ParamsSchema({})
|
|
114
100
|
const message = new ObjectSchema({
|
|
115
101
|
seq: new IntegerSchema({}),
|
|
@@ -130,7 +116,7 @@ describe('Subscription', () => {
|
|
|
130
116
|
|
|
131
117
|
describe('properties', () => {
|
|
132
118
|
it('has nsid property', () => {
|
|
133
|
-
const nsid =
|
|
119
|
+
const nsid = asNsidString('com.atproto.sync.subscribeRepos')
|
|
134
120
|
const parameters = new ParamsSchema({})
|
|
135
121
|
const message = new ObjectSchema({
|
|
136
122
|
seq: new IntegerSchema({}),
|
|
@@ -148,7 +134,7 @@ describe('Subscription', () => {
|
|
|
148
134
|
})
|
|
149
135
|
|
|
150
136
|
it('has parameters property', () => {
|
|
151
|
-
const nsid =
|
|
137
|
+
const nsid = asNsidString('com.atproto.sync.subscribeRepos')
|
|
152
138
|
const parameters = new ParamsSchema({})
|
|
153
139
|
const message = new ObjectSchema({
|
|
154
140
|
seq: new IntegerSchema({}),
|
|
@@ -166,7 +152,7 @@ describe('Subscription', () => {
|
|
|
166
152
|
})
|
|
167
153
|
|
|
168
154
|
it('has message property', () => {
|
|
169
|
-
const nsid =
|
|
155
|
+
const nsid = asNsidString('com.atproto.sync.subscribeRepos')
|
|
170
156
|
const parameters = new ParamsSchema({})
|
|
171
157
|
const message = new ObjectSchema({
|
|
172
158
|
seq: new IntegerSchema({}),
|
|
@@ -184,7 +170,7 @@ describe('Subscription', () => {
|
|
|
184
170
|
})
|
|
185
171
|
|
|
186
172
|
it('has errors property', () => {
|
|
187
|
-
const nsid =
|
|
173
|
+
const nsid = asNsidString('com.atproto.sync.subscribeRepos')
|
|
188
174
|
const parameters = new ParamsSchema({})
|
|
189
175
|
const message = new ObjectSchema({
|
|
190
176
|
seq: new IntegerSchema({}),
|
|
@@ -200,7 +186,7 @@ describe('Subscription', () => {
|
|
|
200
186
|
|
|
201
187
|
describe('with complex parameters', () => {
|
|
202
188
|
it('creates a Subscription with multiple parameter types', () => {
|
|
203
|
-
const nsid =
|
|
189
|
+
const nsid = asNsidString('com.atproto.sync.subscribeRepos')
|
|
204
190
|
const parameters = new ParamsSchema({
|
|
205
191
|
cursor: new OptionalSchema(new IntegerSchema({ minimum: 0 })),
|
|
206
192
|
includeDeletes: new OptionalSchema(
|
|
@@ -223,7 +209,7 @@ describe('Subscription', () => {
|
|
|
223
209
|
|
|
224
210
|
describe('with various message types', () => {
|
|
225
211
|
it('creates a Subscription with ObjectSchema message', () => {
|
|
226
|
-
const nsid =
|
|
212
|
+
const nsid = asNsidString('com.atproto.sync.subscribeRepos')
|
|
227
213
|
const parameters = new ParamsSchema({})
|
|
228
214
|
const message = new ObjectSchema({
|
|
229
215
|
seq: new IntegerSchema({}),
|
|
@@ -242,7 +228,7 @@ describe('Subscription', () => {
|
|
|
242
228
|
})
|
|
243
229
|
|
|
244
230
|
it('creates a Subscription with RefSchema message', () => {
|
|
245
|
-
const nsid =
|
|
231
|
+
const nsid = asNsidString('app.bsky.feed.subscribe')
|
|
246
232
|
const parameters = new ParamsSchema({})
|
|
247
233
|
const message = new RefSchema(() => new StringSchema({}))
|
|
248
234
|
|
|
@@ -256,26 +242,11 @@ describe('Subscription', () => {
|
|
|
256
242
|
expect(subscription).toBeInstanceOf(Subscription)
|
|
257
243
|
expect(subscription.message).toBeInstanceOf(RefSchema)
|
|
258
244
|
})
|
|
259
|
-
|
|
260
|
-
it('creates a Subscription with undefined message', () => {
|
|
261
|
-
const nsid = asNsid('app.bsky.feed.subscribe')
|
|
262
|
-
const parameters = new ParamsSchema({})
|
|
263
|
-
|
|
264
|
-
const subscription = new Subscription(
|
|
265
|
-
nsid,
|
|
266
|
-
parameters,
|
|
267
|
-
undefined,
|
|
268
|
-
undefined,
|
|
269
|
-
)
|
|
270
|
-
|
|
271
|
-
expect(subscription).toBeInstanceOf(Subscription)
|
|
272
|
-
expect(subscription.message).toBeUndefined()
|
|
273
|
-
})
|
|
274
245
|
})
|
|
275
246
|
|
|
276
247
|
describe('with different error configurations', () => {
|
|
277
248
|
it('creates a Subscription with a single error', () => {
|
|
278
|
-
const nsid =
|
|
249
|
+
const nsid = asNsidString('com.atproto.sync.subscribeRepos')
|
|
279
250
|
const parameters = new ParamsSchema({})
|
|
280
251
|
const message = new ObjectSchema({})
|
|
281
252
|
const errors = ['ConsumerTooSlow'] as const
|
|
@@ -286,7 +257,7 @@ describe('Subscription', () => {
|
|
|
286
257
|
})
|
|
287
258
|
|
|
288
259
|
it('creates a Subscription with multiple errors', () => {
|
|
289
|
-
const nsid =
|
|
260
|
+
const nsid = asNsidString('com.atproto.sync.subscribeRepos')
|
|
290
261
|
const parameters = new ParamsSchema({})
|
|
291
262
|
const message = new ObjectSchema({})
|
|
292
263
|
const errors = [
|
|
@@ -305,7 +276,7 @@ describe('Subscription', () => {
|
|
|
305
276
|
})
|
|
306
277
|
|
|
307
278
|
it('creates a Subscription with empty errors array', () => {
|
|
308
|
-
const nsid =
|
|
279
|
+
const nsid = asNsidString('com.atproto.sync.subscribeRepos')
|
|
309
280
|
const parameters = new ParamsSchema({})
|
|
310
281
|
const message = new ObjectSchema({})
|
|
311
282
|
const errors = [] as const
|
|
@@ -318,7 +289,7 @@ describe('Subscription', () => {
|
|
|
318
289
|
|
|
319
290
|
describe('type inference', () => {
|
|
320
291
|
it('InferSubscriptionParameters correctly infers parameter types', () => {
|
|
321
|
-
const nsid =
|
|
292
|
+
const nsid = asNsidString('com.atproto.sync.subscribeRepos')
|
|
322
293
|
const parameters = new ParamsSchema({
|
|
323
294
|
cursor: new OptionalSchema(new IntegerSchema({})),
|
|
324
295
|
})
|
|
@@ -344,7 +315,7 @@ describe('Subscription', () => {
|
|
|
344
315
|
})
|
|
345
316
|
|
|
346
317
|
it('InferSubscriptionMessage correctly infers message type', () => {
|
|
347
|
-
const nsid =
|
|
318
|
+
const nsid = asNsidString('com.atproto.sync.subscribeRepos')
|
|
348
319
|
const parameters = new ParamsSchema({})
|
|
349
320
|
const message = new ObjectSchema({
|
|
350
321
|
seq: new IntegerSchema({}),
|
|
@@ -369,30 +340,11 @@ describe('Subscription', () => {
|
|
|
369
340
|
expect(msg.seq).toBeDefined()
|
|
370
341
|
expect(msg.data).toBeDefined()
|
|
371
342
|
})
|
|
372
|
-
|
|
373
|
-
it('InferSubscriptionMessage returns unknown for undefined message', () => {
|
|
374
|
-
const nsid = asNsid('app.bsky.feed.subscribe')
|
|
375
|
-
const parameters = new ParamsSchema({})
|
|
376
|
-
|
|
377
|
-
const subscription = new Subscription(
|
|
378
|
-
nsid,
|
|
379
|
-
parameters,
|
|
380
|
-
undefined,
|
|
381
|
-
undefined,
|
|
382
|
-
)
|
|
383
|
-
|
|
384
|
-
type Message = InferSubscriptionMessage<typeof subscription>
|
|
385
|
-
|
|
386
|
-
// Type-level test - unknown should accept any value
|
|
387
|
-
const msg: Message = { anything: 'value' }
|
|
388
|
-
|
|
389
|
-
expect(msg).toBeDefined()
|
|
390
|
-
})
|
|
391
343
|
})
|
|
392
344
|
|
|
393
345
|
describe('edge cases', () => {
|
|
394
346
|
it('handles very long NSID', () => {
|
|
395
|
-
const nsid =
|
|
347
|
+
const nsid = asNsidString(
|
|
396
348
|
'com.example.very.long.namespace.identifier.subscription.name',
|
|
397
349
|
)
|
|
398
350
|
const parameters = new ParamsSchema({})
|
|
@@ -409,7 +361,7 @@ describe('Subscription', () => {
|
|
|
409
361
|
})
|
|
410
362
|
|
|
411
363
|
it('handles subscription with all optional parameters', () => {
|
|
412
|
-
const nsid =
|
|
364
|
+
const nsid = asNsidString('app.bsky.feed.subscribe')
|
|
413
365
|
const parameters = new ParamsSchema({
|
|
414
366
|
cursor: new OptionalSchema(new IntegerSchema({})),
|
|
415
367
|
includeDeletes: new OptionalSchema(new IntegerSchema({})),
|
|
@@ -428,7 +380,7 @@ describe('Subscription', () => {
|
|
|
428
380
|
})
|
|
429
381
|
|
|
430
382
|
it('handles subscription with complex nested message schema', () => {
|
|
431
|
-
const nsid =
|
|
383
|
+
const nsid = asNsidString('com.atproto.sync.subscribeRepos')
|
|
432
384
|
const parameters = new ParamsSchema({})
|
|
433
385
|
const message = new ObjectSchema({
|
|
434
386
|
seq: new IntegerSchema({}),
|
|
@@ -454,7 +406,7 @@ describe('Subscription', () => {
|
|
|
454
406
|
|
|
455
407
|
describe('real-world subscription examples', () => {
|
|
456
408
|
it('creates a subscribeLabels subscription', () => {
|
|
457
|
-
const nsid =
|
|
409
|
+
const nsid = asNsidString('com.atproto.label.subscribeLabels')
|
|
458
410
|
const parameters = new ParamsSchema({
|
|
459
411
|
cursor: new OptionalSchema(new IntegerSchema({ minimum: 0 })),
|
|
460
412
|
})
|
|
@@ -492,7 +444,7 @@ describe('Subscription', () => {
|
|
|
492
444
|
})
|
|
493
445
|
|
|
494
446
|
it('creates a notification subscription', () => {
|
|
495
|
-
const nsid =
|
|
447
|
+
const nsid = asNsidString('app.bsky.notification.subscribe')
|
|
496
448
|
const parameters = new ParamsSchema({
|
|
497
449
|
cursor: new OptionalSchema(new StringSchema({})),
|
|
498
450
|
})
|
|
@@ -516,7 +468,7 @@ describe('Subscription', () => {
|
|
|
516
468
|
|
|
517
469
|
describe('with mixed parameter and message types', () => {
|
|
518
470
|
it('handles required and optional parameters with complex message', () => {
|
|
519
|
-
const nsid =
|
|
471
|
+
const nsid = asNsidString('com.atproto.sync.subscribeRepos')
|
|
520
472
|
const parameters = new ParamsSchema({
|
|
521
473
|
cursor: new OptionalSchema(new IntegerSchema({ minimum: 0 })),
|
|
522
474
|
includeDeletes: new OptionalSchema(
|
|
@@ -552,7 +504,7 @@ describe('Subscription', () => {
|
|
|
552
504
|
|
|
553
505
|
describe('validation through nested schemas', () => {
|
|
554
506
|
it('parameters can validate input through ParamsSchema', () => {
|
|
555
|
-
const nsid =
|
|
507
|
+
const nsid = asNsidString('com.atproto.sync.subscribeRepos')
|
|
556
508
|
const parameters = new ParamsSchema({
|
|
557
509
|
cursor: new IntegerSchema({ minimum: 0 }),
|
|
558
510
|
})
|
|
@@ -574,7 +526,7 @@ describe('Subscription', () => {
|
|
|
574
526
|
})
|
|
575
527
|
|
|
576
528
|
it('message can validate input through ObjectSchema', () => {
|
|
577
|
-
const nsid =
|
|
529
|
+
const nsid = asNsidString('com.atproto.sync.subscribeRepos')
|
|
578
530
|
const parameters = new ParamsSchema({})
|
|
579
531
|
const message = new ObjectSchema({
|
|
580
532
|
seq: new IntegerSchema({ minimum: 0 }),
|
|
@@ -605,7 +557,7 @@ describe('Subscription', () => {
|
|
|
605
557
|
|
|
606
558
|
describe('property access', () => {
|
|
607
559
|
it('can access nsid after construction', () => {
|
|
608
|
-
const nsid =
|
|
560
|
+
const nsid = asNsidString('com.atproto.sync.subscribeRepos')
|
|
609
561
|
const parameters = new ParamsSchema({})
|
|
610
562
|
const message = new ObjectSchema({})
|
|
611
563
|
|
|
@@ -621,7 +573,7 @@ describe('Subscription', () => {
|
|
|
621
573
|
})
|
|
622
574
|
|
|
623
575
|
it('can access type after construction', () => {
|
|
624
|
-
const nsid =
|
|
576
|
+
const nsid = asNsidString('com.atproto.sync.subscribeRepos')
|
|
625
577
|
const parameters = new ParamsSchema({})
|
|
626
578
|
const message = new ObjectSchema({})
|
|
627
579
|
|
|
@@ -639,7 +591,7 @@ describe('Subscription', () => {
|
|
|
639
591
|
|
|
640
592
|
describe('different message schema types', () => {
|
|
641
593
|
it('constructs with ObjectSchema message', () => {
|
|
642
|
-
const nsid =
|
|
594
|
+
const nsid = asNsidString('app.bsky.test')
|
|
643
595
|
const parameters = new ParamsSchema({})
|
|
644
596
|
const message = new ObjectSchema({
|
|
645
597
|
field: new StringSchema({}),
|
|
@@ -657,7 +609,7 @@ describe('Subscription', () => {
|
|
|
657
609
|
})
|
|
658
610
|
|
|
659
611
|
it('constructs with RefSchema message', () => {
|
|
660
|
-
const nsid =
|
|
612
|
+
const nsid = asNsidString('app.bsky.test')
|
|
661
613
|
const parameters = new ParamsSchema({})
|
|
662
614
|
const message = new RefSchema(() => new ObjectSchema({}))
|
|
663
615
|
|
|
@@ -671,19 +623,5 @@ describe('Subscription', () => {
|
|
|
671
623
|
expect(subscription.message).toBeInstanceOf(RefSchema)
|
|
672
624
|
expect(subscription.message).toBe(message)
|
|
673
625
|
})
|
|
674
|
-
|
|
675
|
-
it('constructs with undefined message', () => {
|
|
676
|
-
const nsid = asNsid('app.bsky.test')
|
|
677
|
-
const parameters = new ParamsSchema({})
|
|
678
|
-
|
|
679
|
-
const subscription = new Subscription(
|
|
680
|
-
nsid,
|
|
681
|
-
parameters,
|
|
682
|
-
undefined,
|
|
683
|
-
undefined,
|
|
684
|
-
)
|
|
685
|
-
|
|
686
|
-
expect(subscription.message).toBeUndefined()
|
|
687
|
-
})
|
|
688
626
|
})
|
|
689
627
|
})
|
|
@@ -1,33 +1,19 @@
|
|
|
1
|
-
import { NsidString } from '../core.js'
|
|
2
|
-
import { Infer } from '../validation.js'
|
|
3
|
-
import { ObjectSchema } from './object.js'
|
|
1
|
+
import { Infer, NsidString, Schema } from '../core.js'
|
|
4
2
|
import { ParamsSchema } from './params.js'
|
|
5
|
-
import { RefSchema } from './ref.js'
|
|
6
|
-
import { TypedUnionSchema } from './typed-union.js'
|
|
7
3
|
|
|
8
|
-
export type InferSubscriptionParameters<S extends Subscription> =
|
|
9
|
-
S
|
|
10
|
-
|
|
11
|
-
: never
|
|
4
|
+
export type InferSubscriptionParameters<S extends Subscription> = Infer<
|
|
5
|
+
S['parameters']
|
|
6
|
+
>
|
|
12
7
|
|
|
13
|
-
export type InferSubscriptionMessage<S extends Subscription> =
|
|
14
|
-
S
|
|
15
|
-
|
|
16
|
-
any,
|
|
17
|
-
infer M extends RefSchema | TypedUnionSchema | ObjectSchema
|
|
18
|
-
>
|
|
19
|
-
? Infer<M>
|
|
20
|
-
: unknown
|
|
8
|
+
export type InferSubscriptionMessage<S extends Subscription> = Infer<
|
|
9
|
+
S['message']
|
|
10
|
+
>
|
|
21
11
|
|
|
22
12
|
export class Subscription<
|
|
23
|
-
TNsid extends NsidString =
|
|
24
|
-
TParameters extends ParamsSchema =
|
|
25
|
-
TMessage extends
|
|
26
|
-
|
|
27
|
-
| RefSchema
|
|
28
|
-
| TypedUnionSchema
|
|
29
|
-
| ObjectSchema = any,
|
|
30
|
-
TErrors extends undefined | readonly string[] = any,
|
|
13
|
+
TNsid extends NsidString = NsidString,
|
|
14
|
+
TParameters extends ParamsSchema = ParamsSchema,
|
|
15
|
+
TMessage extends Schema = Schema,
|
|
16
|
+
TErrors extends undefined | readonly string[] = undefined | readonly string[],
|
|
31
17
|
> {
|
|
32
18
|
readonly type = 'subscription' as const
|
|
33
19
|
|
package/src/schema/token.test.ts
CHANGED
package/src/schema/token.ts
CHANGED
|
@@ -1,21 +1,23 @@
|
|
|
1
1
|
import { isPlainObject } from '@atproto/lex-data'
|
|
2
|
-
import { $Type, Simplify } from '../core.js'
|
|
3
2
|
import {
|
|
3
|
+
$Type,
|
|
4
4
|
Infer,
|
|
5
5
|
Schema,
|
|
6
|
+
Simplify,
|
|
6
7
|
ValidationResult,
|
|
7
8
|
Validator,
|
|
8
9
|
ValidatorContext,
|
|
9
|
-
} from '../
|
|
10
|
+
} from '../core.js'
|
|
11
|
+
import { TypedObject } from './typed-union.js'
|
|
10
12
|
|
|
11
13
|
export type TypedObjectSchemaOutput<
|
|
12
14
|
T extends $Type,
|
|
13
|
-
S extends Validator<{ [
|
|
15
|
+
S extends Validator<{ [k: string]: unknown }>,
|
|
14
16
|
> = Simplify<Infer<S> & { $type?: T }>
|
|
15
17
|
|
|
16
18
|
export class TypedObjectSchema<
|
|
17
19
|
const T extends $Type = any,
|
|
18
|
-
const S extends Validator<{ [
|
|
20
|
+
const S extends Validator<{ [k: string]: unknown }> = any,
|
|
19
21
|
> extends Schema<TypedObjectSchemaOutput<T, S>> {
|
|
20
22
|
constructor(
|
|
21
23
|
readonly $type: T,
|
|
@@ -26,7 +28,10 @@ export class TypedObjectSchema<
|
|
|
26
28
|
|
|
27
29
|
isTypeOf<X extends Record<string, unknown>>(
|
|
28
30
|
value: X,
|
|
29
|
-
): value is
|
|
31
|
+
): value is Exclude<
|
|
32
|
+
X extends { $type?: T } ? X : X & { $type?: T },
|
|
33
|
+
TypedObject
|
|
34
|
+
> {
|
|
30
35
|
return value.$type === undefined || value.$type === this.$type
|
|
31
36
|
}
|
|
32
37
|
|
package/src/schema/typed-ref.ts
CHANGED