@atproto/lex-schema 0.0.9 → 0.0.10
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 +34 -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-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 +10 -5
- package/dist/helpers.d.ts.map +1 -1
- package/dist/helpers.js +3 -3
- 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 +24 -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 +14 -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 +72 -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/dist/schema/ref.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.RefSchema = void 0;
|
|
4
|
+
exports.ref = ref;
|
|
4
5
|
const core_js_1 = require("../core.js");
|
|
5
6
|
class RefSchema extends core_js_1.Schema {
|
|
6
7
|
#getter;
|
|
@@ -10,26 +11,18 @@ class RefSchema extends core_js_1.Schema {
|
|
|
10
11
|
super();
|
|
11
12
|
this.#getter = getter;
|
|
12
13
|
}
|
|
13
|
-
get
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
this
|
|
18
|
-
// Disable the getter and cache the resolved schema on the instance
|
|
19
|
-
Object.defineProperty(this, 'schema', {
|
|
20
|
-
value,
|
|
21
|
-
writable: false,
|
|
22
|
-
enumerable: false,
|
|
23
|
-
configurable: true,
|
|
24
|
-
});
|
|
25
|
-
return value;
|
|
14
|
+
get validator() {
|
|
15
|
+
return this.#getter.call(null);
|
|
16
|
+
}
|
|
17
|
+
unwrap() {
|
|
18
|
+
return this.validator;
|
|
26
19
|
}
|
|
27
20
|
validateInContext(input, ctx) {
|
|
28
|
-
return ctx.validate(input, this.
|
|
21
|
+
return ctx.validate(input, this.validator);
|
|
29
22
|
}
|
|
30
23
|
}
|
|
31
24
|
exports.RefSchema = RefSchema;
|
|
32
|
-
function
|
|
33
|
-
|
|
25
|
+
function ref(get) {
|
|
26
|
+
return new RefSchema(get);
|
|
34
27
|
}
|
|
35
28
|
//# sourceMappingURL=ref.js.map
|
package/dist/schema/ref.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ref.js","sourceRoot":"","sources":["../../src/schema/ref.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"ref.js","sourceRoot":"","sources":["../../src/schema/ref.ts"],"names":[],"mappings":";;;AAkDA,kBAIC;AAtDD,wCAOmB;AAInB,MAAa,SACX,SAAQ,gBAIP;IAGD,OAAO,CAA6B;IAEpC,YAAY,MAAmC;QAC7C,uEAAuE;QACvE,sEAAsE;QAEtE,KAAK,EAAE,CAAA;QAEP,IAAI,CAAC,OAAO,GAAG,MAAM,CAAA;IACvB,CAAC;IAED,IAAI,SAAS;QACX,OAAO,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;IAChC,CAAC;IAED,MAAM;QACJ,OAAO,IAAI,CAAC,SAAS,CAAA;IACvB,CAAC;IAED,iBAAiB,CAAC,KAAc,EAAE,GAAsB;QACtD,OAAO,GAAG,CAAC,QAAQ,CAAC,KAAK,EAAE,IAAI,CAAC,SAAS,CAAC,CAAA;IAC5C,CAAC;CACF;AA9BD,8BA8BC;AASD,SAAgB,GAAG,CACjB,GAAgC;IAEhC,OAAO,IAAI,SAAS,CAAa,GAAG,CAAC,CAAA;AACvC,CAAC","sourcesContent":["import {\n InferInput,\n InferOutput,\n Schema,\n ValidationContext,\n Validator,\n WrappedValidator,\n} from '../core.js'\n\nexport type RefSchemaGetter<out TValidator extends Validator> = () => TValidator\n\nexport class RefSchema<const TValidator extends Validator>\n extends Schema<\n InferInput<TValidator>,\n InferOutput<TValidator>,\n TValidator['__lex']\n >\n implements WrappedValidator<TValidator>\n{\n #getter: RefSchemaGetter<TValidator>\n\n constructor(getter: RefSchemaGetter<TValidator>) {\n // @NOTE In order to avoid circular dependency issues, we don't resolve\n // the schema here. Instead, we resolve it lazily when first accessed.\n\n super()\n\n this.#getter = getter\n }\n\n get validator(): TValidator {\n return this.#getter.call(null)\n }\n\n unwrap(): TValidator {\n return this.validator\n }\n\n validateInContext(input: unknown, ctx: ValidationContext) {\n return ctx.validate(input, this.validator)\n }\n}\n\n/*@__NO_SIDE_EFFECTS__*/\nexport function ref<const TValidator extends Validator>(\n get: RefSchemaGetter<TValidator>,\n): RefSchema<TValidator>\nexport function ref<TInput, TOutput extends TInput = TInput>(\n get: RefSchemaGetter<Validator<TInput, TOutput>>,\n): RefSchema<Validator<TInput, TOutput>>\nexport function ref<const TValidator extends Validator>(\n get: RefSchemaGetter<TValidator>,\n) {\n return new RefSchema<TValidator>(get)\n}\n"]}
|
package/dist/schema/refine.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { InferInput, PropertyKey, Validator } from '../core.js';
|
|
2
2
|
import { CustomAssertionContext } from './custom.js';
|
|
3
3
|
export type RefinementCheck<T> = {
|
|
4
4
|
check: (value: T, ctx: CustomAssertionContext) => boolean;
|
|
@@ -29,7 +29,7 @@ export type Refinement<T = any, Out extends T = T> = RefinementCheck<T> | Refine
|
|
|
29
29
|
* // result.success === false
|
|
30
30
|
* ```
|
|
31
31
|
*/
|
|
32
|
-
export declare function refine<
|
|
33
|
-
export declare function refine<
|
|
34
|
-
export declare function refine<
|
|
32
|
+
export declare function refine<const TValidator extends Validator, TInput extends InferInput<TValidator>>(schema: TValidator, refinement: RefinementAssertion<InferInput<TValidator>, TInput>): TValidator & Validator<TInput>;
|
|
33
|
+
export declare function refine<const TValidator extends Validator>(schema: TValidator, refinement: RefinementCheck<InferInput<TValidator>>): TValidator;
|
|
34
|
+
export declare function refine<TRefinement extends Refinement, const TValidator extends Validator<InferRefinement<TRefinement>>>(schema: TValidator, refinement: TRefinement): TValidator;
|
|
35
35
|
//# sourceMappingURL=refine.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"refine.d.ts","sourceRoot":"","sources":["../../src/schema/refine.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,
|
|
1
|
+
{"version":3,"file":"refine.d.ts","sourceRoot":"","sources":["../../src/schema/refine.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,UAAU,EAEV,WAAW,EAGX,SAAS,EACV,MAAM,YAAY,CAAA;AACnB,OAAO,EAAE,sBAAsB,EAAE,MAAM,aAAa,CAAA;AAEpD,MAAM,MAAM,eAAe,CAAC,CAAC,IAAI;IAC/B,KAAK,EAAE,CAAC,KAAK,EAAE,CAAC,EAAE,GAAG,EAAE,sBAAsB,KAAK,OAAO,CAAA;IACzD,OAAO,EAAE,MAAM,CAAA;IACf,IAAI,CAAC,EAAE,WAAW,GAAG,SAAS,WAAW,EAAE,CAAA;CAC5C,CAAA;AAED,MAAM,MAA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W,GAAG,UAAU,CAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"refine.js","sourceRoot":"","sources":["../../src/schema/refine.ts"],"names":[],"mappings":";;
|
|
1
|
+
{"version":3,"file":"refine.js","sourceRoot":"","sources":["../../src/schema/refine.ts"],"names":[],"mappings":";;AAkEA,wBAiBC;AAnFD,wCAOmB;AA0DnB,wBAAwB;AACxB,SAAgB,MAAM,CACpB,MAAkB,EAClB,UAA+B;IAE/B,wEAAwE;IACxE,4EAA4E;IAC5E,4EAA4E;IAC5E,6BAA6B;IAC7B,OAAO,MAAM,CAAC,MAAM,CAAC,MAAM,EAAE;QAC3B,iBAAiB,EAAE;YACjB,8DAA8D;YAC9D,KAAK,EAAE,wBAAwB,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,UAAU,EAAE,CAAC;YAC5D,UAAU,EAAE,KAAK;YACjB,QAAQ,EAAE,KAAK;YACf,YAAY,EAAE,IAAI;SACnB;KACF,CAAC,CAAA;AACJ,CAAC;AAED,wBAAwB;AACxB,SAAS,wBAAwB,CAK/B,KAAc,EACd,GAAsB;IAEtB,MAAM,MAAM,GAAG,GAAG,CAAC,QAAQ,CAAC,KAAK,EAAE,IAAI,CAAC,MAAM,CAAC,CAAA;IAC/C,IAAI,CAAC,MAAM,CAAC,OAAO;QAAE,OAAO,MAAM,CAAA;IAElC,MAAM,WAAW,GAAG,IAAI,CAAC,UAAU,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,EAAE,MAAM,CAAC,KAAK,EAAE,GAAG,CAAC,CAAA;IACvE,IAAI,CAAC,WAAW,EAAE,CAAC;QACjB,MAAM,IAAI,GAAG,GAAG,CAAC,UAAU,CAAC,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,CAAA;QACjD,OAAO,GAAG,CAAC,KAAK,CAAC,IAAI,qBAAW,CAAC,IAAI,EAAE,KAAK,EAAE,IAAI,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC,CAAA;IACzE,CAAC;IAED,OAAO,MAAM,CAAA;AACf,CAAC","sourcesContent":["import {\n InferInput,\n IssueCustom,\n PropertyKey,\n ValidationContext,\n ValidationResult,\n Validator,\n} from '../core.js'\nimport { CustomAssertionContext } from './custom.js'\n\nexport type RefinementCheck<T> = {\n check: (value: T, ctx: CustomAssertionContext) => boolean\n message: string\n path?: PropertyKey | readonly PropertyKey[]\n}\n\nexport type RefinementAssertion<T, Out extends T> = {\n check: (this: null, value: T, ctx: CustomAssertionContext) => value is Out\n message: string\n path?: PropertyKey | readonly PropertyKey[]\n}\n\nexport type InferRefinement<R> =\n R extends RefinementCheck<infer T>\n ? T\n : R extends RefinementAssertion<infer T, any>\n ? T\n : never\n\nexport type Refinement<T = any, Out extends T = T> =\n | RefinementCheck<T>\n | RefinementAssertion<T, Out>\n\n/**\n * Create a refined schema based on an existing schema and a refinement check.\n *\n * @param schema - The base schema to refine.\n * @param refinement - The refinement check to apply.\n * @returns A new schema that includes the refinement.\n * @example\n *\n * ```ts\n * const PositiveInt = refine(l.integer(), {\n * check: (value) => value > 0,\n * message: 'Value must be a positive integer',\n * })\n * const result = PositiveInt.validate(-5)\n * // result.success === false\n * ```\n */\nexport function refine<\n const TValidator extends Validator,\n TInput extends InferInput<TValidator>,\n>(\n schema: TValidator,\n refinement: RefinementAssertion<InferInput<TValidator>, TInput>,\n): TValidator & Validator<TInput>\nexport function refine<const TValidator extends Validator>(\n schema: TValidator,\n refinement: RefinementCheck<InferInput<TValidator>>,\n): TValidator\nexport function refine<\n TRefinement extends Refinement,\n const TValidator extends Validator<InferRefinement<TRefinement>>,\n>(schema: TValidator, refinement: TRefinement): TValidator\n/*@__NO_SIDE_EFFECTS__*/\nexport function refine<const TValidator extends Validator>(\n schema: TValidator,\n refinement: Refinement<unknown>,\n): TValidator {\n // This is basically the same as monkey patching the \"validateInContext\"\n // method to the schema, but done in a way that does not mutate the original\n // schema. This is safe to do because Validators don't update their internal\n // state over their lifetime.\n return Object.create(schema, {\n validateInContext: {\n // We do not use an arrow function to avoid creating a closure\n value: validateInContextUnbound.bind({ schema, refinement }),\n enumerable: false,\n writable: false,\n configurable: true,\n },\n })\n}\n\n/*@__NO_SIDE_EFFECTS__*/\nfunction validateInContextUnbound<S extends Validator>(\n this: {\n schema: S\n refinement: Refinement<InferInput<S>>\n },\n input: unknown,\n ctx: ValidationContext,\n): ValidationResult<InferInput<S>> {\n const result = ctx.validate(input, this.schema)\n if (!result.success) return result\n\n const checkResult = this.refinement.check.call(null, result.value, ctx)\n if (!checkResult) {\n const path = ctx.concatPath(this.refinement.path)\n return ctx.issue(new IssueCustom(path, input, this.refinement.message))\n }\n\n return result\n}\n"]}
|
package/dist/schema/regexp.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import { Schema,
|
|
2
|
-
export declare class RegexpSchema<
|
|
1
|
+
import { Schema, ValidationContext } from '../core.js';
|
|
2
|
+
export declare class RegexpSchema<TValue extends string = string> extends Schema<TValue> {
|
|
3
3
|
readonly pattern: RegExp;
|
|
4
4
|
constructor(pattern: RegExp);
|
|
5
|
-
validateInContext(input: unknown, ctx:
|
|
5
|
+
validateInContext(input: unknown, ctx: ValidationContext): import("../core.js").ValidationResult<TValue>;
|
|
6
6
|
}
|
|
7
|
+
export declare function regexp<TInput extends string = string>(pattern: RegExp): RegexpSchema<TInput>;
|
|
7
8
|
//# sourceMappingURL=regexp.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"regexp.d.ts","sourceRoot":"","sources":["../../src/schema/regexp.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,
|
|
1
|
+
{"version":3,"file":"regexp.d.ts","sourceRoot":"","sources":["../../src/schema/regexp.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAA;AAEtD,qBAAa,YAAY,CACvB,MAAM,SAAS,MAAM,GAAG,MAAM,CAC9B,SAAQ,MAAM,CAAC,MAAM,CAAC;aACM,OAAO,EAAE,MAAM;gBAAf,OAAO,EAAE,MAAM;IAI3C,iBAAiB,CAAC,KAAK,EAAE,OAAO,EAAE,GAAG,EAAE,iBAAiB;CAWzD;AAGD,wBAAgB,MAAM,CAAC,MAAM,SAAS,MAAM,GAAG,MAAM,EAAE,OAAO,EAAE,MAAM,wBAErE"}
|
package/dist/schema/regexp.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.RegexpSchema = void 0;
|
|
4
|
+
exports.regexp = regexp;
|
|
4
5
|
const core_js_1 = require("../core.js");
|
|
5
6
|
class RegexpSchema extends core_js_1.Schema {
|
|
6
7
|
pattern;
|
|
@@ -19,4 +20,8 @@ class RegexpSchema extends core_js_1.Schema {
|
|
|
19
20
|
}
|
|
20
21
|
}
|
|
21
22
|
exports.RegexpSchema = RegexpSchema;
|
|
23
|
+
/*@__NO_SIDE_EFFECTS__*/
|
|
24
|
+
function regexp(pattern) {
|
|
25
|
+
return new RegexpSchema(pattern);
|
|
26
|
+
}
|
|
22
27
|
//# sourceMappingURL=regexp.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"regexp.js","sourceRoot":"","sources":["../../src/schema/regexp.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"regexp.js","sourceRoot":"","sources":["../../src/schema/regexp.ts"],"names":[],"mappings":";;;AAuBA,wBAEC;AAzBD,wCAAsD;AAEtD,MAAa,YAEX,SAAQ,gBAAc;IACM;IAA5B,YAA4B,OAAe;QACzC,KAAK,EAAE,CAAA;QADmB,YAAO,GAAP,OAAO,CAAQ;IAE3C,CAAC;IAED,iBAAiB,CAAC,KAAc,EAAE,GAAsB;QACtD,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;YAC9B,OAAO,GAAG,CAAC,gBAAgB,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAA;QAC9C,CAAC;QAED,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC;YAC9B,OAAO,GAAG,CAAC,kBAAkB,CAAC,KAAK,EAAE,IAAI,CAAC,OAAO,CAAC,QAAQ,EAAE,CAAC,CAAA;QAC/D,CAAC;QAED,OAAO,GAAG,CAAC,OAAO,CAAC,KAAe,CAAC,CAAA;IACrC,CAAC;CACF;AAlBD,oCAkBC;AAED,wBAAwB;AACxB,SAAgB,MAAM,CAAiC,OAAe;IACpE,OAAO,IAAI,YAAY,CAAS,OAAO,CAAC,CAAA;AAC1C,CAAC","sourcesContent":["import { Schema, ValidationContext } from '../core.js'\n\nexport class RegexpSchema<\n TValue extends string = string,\n> extends Schema<TValue> {\n constructor(public readonly pattern: RegExp) {\n super()\n }\n\n validateInContext(input: unknown, ctx: ValidationContext) {\n if (typeof input !== 'string') {\n return ctx.issueInvalidType(input, 'string')\n }\n\n if (!this.pattern.test(input)) {\n return ctx.issueInvalidFormat(input, this.pattern.toString())\n }\n\n return ctx.success(input as TValue)\n }\n}\n\n/*@__NO_SIDE_EFFECTS__*/\nexport function regexp<TInput extends string = string>(pattern: RegExp) {\n return new RegexpSchema<TInput>(pattern)\n}\n"]}
|
package/dist/schema/string.d.ts
CHANGED
|
@@ -1,19 +1,18 @@
|
|
|
1
|
-
import { InferStringFormat, Schema, StringFormat,
|
|
1
|
+
import { InferStringFormat, Schema, StringFormat, ValidationContext } from '../core.js';
|
|
2
2
|
export type StringSchemaOptions = {
|
|
3
|
-
default?: string;
|
|
4
3
|
format?: StringFormat;
|
|
5
4
|
minLength?: number;
|
|
6
5
|
maxLength?: number;
|
|
7
6
|
minGraphemes?: number;
|
|
8
7
|
maxGraphemes?: number;
|
|
9
8
|
};
|
|
10
|
-
export
|
|
9
|
+
export declare class StringSchema<const TOptions extends StringSchemaOptions = StringSchemaOptions> extends Schema<TOptions extends {
|
|
11
10
|
format: infer F extends StringFormat;
|
|
12
|
-
} ? InferStringFormat<F> : string
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
validateInContext(input: unknown | undefined, ctx: ValidatorContext): ValidationResult<StringSchemaOutput<Options>>;
|
|
11
|
+
} ? InferStringFormat<F> : string> {
|
|
12
|
+
readonly options?: TOptions | undefined;
|
|
13
|
+
constructor(options?: TOptions | undefined);
|
|
14
|
+
validateInContext(input: unknown, ctx: ValidationContext): import("../core.js").ValidationResult<string>;
|
|
17
15
|
}
|
|
18
16
|
export declare function coerceToString(input: unknown): string | null;
|
|
17
|
+
export declare const string: <const O extends StringSchemaOptions = {}>(options?: StringSchemaOptions & O) => StringSchema<O>;
|
|
19
18
|
//# sourceMappingURL=string.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"string.d.ts","sourceRoot":"","sources":["../../src/schema/string.ts"],"names":[],"mappings":"AACA,OAAO,EACL,iBAAiB,EACjB,MAAM,EACN,YAAY,EACZ,
|
|
1
|
+
{"version":3,"file":"string.d.ts","sourceRoot":"","sources":["../../src/schema/string.ts"],"names":[],"mappings":"AACA,OAAO,EACL,iBAAiB,EACjB,MAAM,EACN,YAAY,EACZ,iBAAiB,EAElB,MAAM,YAAY,CAAA;AAInB,MAAM,MAAM,mBAAmB,GAAG;IAChC,MAAM,CAAC,EAAE,YAAY,CAAA;IACrB,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,YAAY,CAAC,EAAE,MAAM,CAAA;IACrB,YAAY,CAAC,EAAE,MAAM,CAAA;CACtB,CAAA;AAED,qBAAa,YAAY,CACvB,KAAK,CAAC,QAAQ,SAAS,mBAAmB,GAAG,mBAAmB,CAChE,SAAQ,MAAM,CACd,QAAQ,SAAS;IAAE,MAAM,EAAE,MAAM,CAAC,SAAS,YAAY,CAAA;CAAE,GACrD,iBAAiB,CAAC,CAAC,CAAC,GACpB,MAAM,CACX;IACa,QAAQ,CAAC,OAAO,CAAC,EAAE,QAAQ;gBAAlB,OAAO,CAAC,EAAE,QAAQ,YAAA;IAIvC,iBAAiB,CAAC,KAAK,EAAE,OAAO,EAAE,GAAG,EAAE,iBAAiB;CAqDzD;AAED,wBAAgB,cAAc,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,GAAG,IAAI,CAsC5D;AAED,eAAO,MAAM,MAAM,SACX,CAAC,SAAS,mBAAmB,iBACzB,mBAAmB,GAAG,CAAC,oBAEjC,CAAA"}
|
package/dist/schema/string.js
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.StringSchema = void 0;
|
|
3
|
+
exports.string = exports.StringSchema = void 0;
|
|
4
4
|
exports.coerceToString = coerceToString;
|
|
5
5
|
const lex_data_1 = require("@atproto/lex-data");
|
|
6
6
|
const core_js_1 = require("../core.js");
|
|
7
|
+
const memoize_js_1 = require("../util/memoize.js");
|
|
7
8
|
const token_js_1 = require("./token.js");
|
|
8
9
|
class StringSchema extends core_js_1.Schema {
|
|
9
10
|
options;
|
|
@@ -11,22 +12,19 @@ class StringSchema extends core_js_1.Schema {
|
|
|
11
12
|
super();
|
|
12
13
|
this.options = options;
|
|
13
14
|
}
|
|
14
|
-
validateInContext(
|
|
15
|
-
// @NOTE validation will be applied on the default value as well
|
|
16
|
-
input = this.options.default, ctx) {
|
|
17
|
-
const { options } = this;
|
|
15
|
+
validateInContext(input, ctx) {
|
|
18
16
|
const str = coerceToString(input);
|
|
19
17
|
if (str == null) {
|
|
20
18
|
return ctx.issueInvalidType(input, 'string');
|
|
21
19
|
}
|
|
22
20
|
let lazyUtf8Len;
|
|
23
|
-
const
|
|
21
|
+
const minLength = this.options?.minLength;
|
|
24
22
|
if (minLength != null) {
|
|
25
23
|
if ((lazyUtf8Len ??= (0, lex_data_1.utf8Len)(str)) < minLength) {
|
|
26
24
|
return ctx.issueTooSmall(str, 'string', minLength, lazyUtf8Len);
|
|
27
25
|
}
|
|
28
26
|
}
|
|
29
|
-
const
|
|
27
|
+
const maxLength = this.options?.maxLength;
|
|
30
28
|
if (maxLength != null) {
|
|
31
29
|
// Optimization: we can avoid computing the UTF-8 length if the maximum
|
|
32
30
|
// possible length, in bytes, of the input JS string is smaller than the
|
|
@@ -39,7 +37,7 @@ class StringSchema extends core_js_1.Schema {
|
|
|
39
37
|
}
|
|
40
38
|
}
|
|
41
39
|
let lazyGraphLen;
|
|
42
|
-
const
|
|
40
|
+
const minGraphemes = this.options?.minGraphemes;
|
|
43
41
|
if (minGraphemes != null) {
|
|
44
42
|
// Optimization: avoid counting graphemes if the length check already fails
|
|
45
43
|
if (str.length < minGraphemes) {
|
|
@@ -49,22 +47,15 @@ class StringSchema extends core_js_1.Schema {
|
|
|
49
47
|
return ctx.issueTooSmall(str, 'grapheme', minGraphemes, lazyGraphLen);
|
|
50
48
|
}
|
|
51
49
|
}
|
|
52
|
-
const
|
|
50
|
+
const maxGraphemes = this.options?.maxGraphemes;
|
|
53
51
|
if (maxGraphemes != null) {
|
|
54
52
|
if ((lazyGraphLen ??= (0, lex_data_1.graphemeLen)(str)) > maxGraphemes) {
|
|
55
53
|
return ctx.issueTooBig(str, 'grapheme', maxGraphemes, lazyGraphLen);
|
|
56
54
|
}
|
|
57
55
|
}
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
// from @atproto/syntax)
|
|
62
|
-
(0, core_js_1.assertStringFormat)(str, options.format);
|
|
63
|
-
}
|
|
64
|
-
catch (err) {
|
|
65
|
-
const message = err instanceof Error ? err.message : undefined;
|
|
66
|
-
return ctx.issueInvalidFormat(str, options.format, message);
|
|
67
|
-
}
|
|
56
|
+
const format = this.options?.format;
|
|
57
|
+
if (format != null && !(0, core_js_1.isStringFormat)(str, format)) {
|
|
58
|
+
return ctx.issueInvalidFormat(str, format);
|
|
68
59
|
}
|
|
69
60
|
return ctx.success(str);
|
|
70
61
|
}
|
|
@@ -106,4 +97,7 @@ function coerceToString(input) {
|
|
|
106
97
|
return null;
|
|
107
98
|
}
|
|
108
99
|
}
|
|
100
|
+
exports.string = (0, memoize_js_1.memoizedOptions)(function (options) {
|
|
101
|
+
return new StringSchema(options);
|
|
102
|
+
});
|
|
109
103
|
//# sourceMappingURL=string.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"string.js","sourceRoot":"","sources":["../../src/schema/string.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"string.js","sourceRoot":"","sources":["../../src/schema/string.ts"],"names":[],"mappings":";;;AAqFA,wCAsCC;AA3HD,gDAA+D;AAC/D,wCAMmB;AACnB,mDAAoD;AACpD,yCAAwC;AAUxC,MAAa,YAEX,SAAQ,gBAIT;IACsB;IAArB,YAAqB,OAAkB;QACrC,KAAK,EAAE,CAAA;QADY,YAAO,GAAP,OAAO,CAAW;IAEvC,CAAC;IAED,iBAAiB,CAAC,KAAc,EAAE,GAAsB;QACtD,MAAM,GAAG,GAAG,cAAc,CAAC,KAAK,CAAC,CAAA;QACjC,IAAI,GAAG,IAAI,IAAI,EAAE,CAAC;YAChB,OAAO,GAAG,CAAC,gBAAgB,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAA;QAC9C,CAAC;QAED,IAAI,WAAmB,CAAA;QAEvB,MAAM,SAAS,GAAG,IAAI,CAAC,OAAO,EAAE,SAAS,CAAA;QACzC,IAAI,SAAS,IAAI,IAAI,EAAE,CAAC;YACtB,IAAI,CAAC,WAAW,KAAK,IAAA,kBAAO,EAAC,GAAG,CAAC,CAAC,GAAG,SAAS,EAAE,CAAC;gBAC/C,OAAO,GAAG,CAAC,aAAa,CAAC,GAAG,EAAE,QAAQ,EAAE,SAAS,EAAE,WAAW,CAAC,CAAA;YACjE,CAAC;QACH,CAAC;QAED,MAAM,SAAS,GAAG,IAAI,CAAC,OAAO,EAAE,SAAS,CAAA;QACzC,IAAI,SAAS,IAAI,IAAI,EAAE,CAAC;YACtB,uE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AC;AAEY,QAAA,MAAM,GAAiB,IAAA,4BAAe,EAAC,UAElD,OAAiC;IACjC,OAAO,IAAI,YAAY,CAAI,OAAO,CAAC,CAAA;AACrC,CAAC,CAAC,CAAA","sourcesContent":["import { graphemeLen, ifCid, utf8Len } from '@atproto/lex-data'\nimport {\n InferStringFormat,\n Schema,\n StringFormat,\n ValidationContext,\n isStringFormat,\n} from '../core.js'\nimport { memoizedOptions } from '../util/memoize.js'\nimport { TokenSchema } from './token.js'\n\nexport type StringSchemaOptions = {\n format?: StringFormat\n minLength?: number\n maxLength?: number\n minGraphemes?: number\n maxGraphemes?: number\n}\n\nexport class StringSchema<\n const TOptions extends StringSchemaOptions = StringSchemaOptions,\n> extends Schema<\n TOptions extends { format: infer F extends StringFormat }\n ? InferStringFormat<F>\n : string\n> {\n constructor(readonly options?: TOptions) {\n super()\n }\n\n validateInContext(input: unknown, ctx: ValidationContext) {\n const str = coerceToString(input)\n if (str == null) {\n return ctx.issueInvalidType(input, 'string')\n }\n\n let lazyUtf8Len: number\n\n const minLength = this.options?.minLength\n if (minLength != null) {\n if ((lazyUtf8Len ??= utf8Len(str)) < minLength) {\n return ctx.issueTooSmall(str, 'string', minLength, lazyUtf8Len)\n }\n }\n\n const maxLength = this.options?.maxLength\n if (maxLength != null) {\n // Optimization: we can avoid computing the UTF-8 length if the maximum\n // possible length, in bytes, of the input JS string is smaller than the\n // maxLength (in UTF-8 string bytes).\n if (str.length * 3 <= maxLength) {\n // Input string so small it can't possibly exceed maxLength\n } else if ((lazyUtf8Len ??= utf8Len(str)) > maxLength) {\n return ctx.issueTooBig(str, 'string', maxLength, lazyUtf8Len)\n }\n }\n\n let lazyGraphLen: number\n\n const minGraphemes = this.options?.minGraphemes\n if (minGraphemes != null) {\n // Optimization: avoid counting graphemes if the length check already fails\n if (str.length < minGraphemes) {\n return ctx.issueTooSmall(str, 'grapheme', minGraphemes, str.length)\n } else if ((lazyGraphLen ??= graphemeLen(str)) < minGraphemes) {\n return ctx.issueTooSmall(str, 'grapheme', minGraphemes, lazyGraphLen)\n }\n }\n\n const maxGraphemes = this.options?.maxGraphemes\n if (maxGraphemes != null) {\n if ((lazyGraphLen ??= graphemeLen(str)) > maxGraphemes) {\n return ctx.issueTooBig(str, 'grapheme', maxGraphemes, lazyGraphLen)\n }\n }\n\n const format = this.options?.format\n if (format != null && !isStringFormat(str, format)) {\n return ctx.issueInvalidFormat(str, format)\n }\n\n return ctx.success(str)\n }\n}\n\nexport function coerceToString(input: unknown): string | null {\n switch (typeof input) {\n // @NOTE We do *not* coerce numbers/booleans to strings because that can\n // lead to them being accepted as string instead of being coerced to\n // number/boolean when the input is a string and the expected result is\n // number/boolean (e.g. in params).\n case 'string':\n return input\n case 'object': {\n if (input == null) return null\n\n // @NOTE Allow using TokenSchema instances in places expecting strings,\n // converting them to their string value.\n if (input instanceof TokenSchema) {\n return input.toString()\n }\n\n if (input instanceof Date) {\n if (Number.isNaN(input.getTime())) return null\n return input.toISOString()\n }\n\n if (input instanceof URL) {\n return input.toString()\n }\n\n const cid = ifCid(input)\n if (cid) return cid.toString()\n\n if (input instanceof String) {\n return input.valueOf()\n }\n }\n\n // falls through\n default:\n return null\n }\n}\n\nexport const string = /*#__PURE__*/ memoizedOptions(function <\n const O extends StringSchemaOptions = NonNullable<unknown>,\n>(options?: StringSchemaOptions & O) {\n return new StringSchema<O>(options)\n})\n"]}
|
|
@@ -2,7 +2,7 @@ import { Infer, NsidString, Schema } from '../core.js';
|
|
|
2
2
|
import { ParamsSchema } from './params.js';
|
|
3
3
|
export type InferSubscriptionParameters<S extends Subscription> = Infer<S['parameters']>;
|
|
4
4
|
export type InferSubscriptionMessage<S extends Subscription> = Infer<S['message']>;
|
|
5
|
-
export declare class Subscription<TNsid extends NsidString = NsidString, TParameters extends ParamsSchema = ParamsSchema, TMessage extends Schema = Schema, TErrors extends undefined | readonly string[] = undefined | readonly string[]> {
|
|
5
|
+
export declare class Subscription<const TNsid extends NsidString = NsidString, const TParameters extends ParamsSchema = ParamsSchema, const TMessage extends Schema = Schema, const TErrors extends undefined | readonly string[] = undefined | readonly string[]> {
|
|
6
6
|
readonly nsid: TNsid;
|
|
7
7
|
readonly parameters: TParameters;
|
|
8
8
|
readonly message: TMessage;
|
|
@@ -10,4 +10,5 @@ export declare class Subscription<TNsid extends NsidString = NsidString, TParame
|
|
|
10
10
|
readonly type: "subscription";
|
|
11
11
|
constructor(nsid: TNsid, parameters: TParameters, message: TMessage, errors: TErrors);
|
|
12
12
|
}
|
|
13
|
+
export declare function subscription<const N extends NsidString, const P extends ParamsSchema, const M extends Schema, const E extends undefined | readonly string[] = undefined>(nsid: N, parameters: P, message: M, errors?: E): Subscription<N, P, M, E>;
|
|
13
14
|
//# sourceMappingURL=subscription.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"subscription.d.ts","sourceRoot":"","sources":["../../src/schema/subscription.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,YAAY,CAAA;AACtD,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAA;AAE1C,MAAM,MAAM,2BAA2B,CAAC,CAAC,SAAS,YAAY,IAAI,KAAK,CACrE,CAAC,CAAC,YAAY,CAAC,CAChB,CAAA;AAED,MAAM,MAAM,wBAAwB,CAAC,CAAC,SAAS,YAAY,IAAI,KAAK,CAClE,CAAC,CAAC,SAAS,CAAC,CACb,CAAA;AAED,qBAAa,YAAY,CACvB,KAAK,SAAS,UAAU,GAAG,UAAU,
|
|
1
|
+
{"version":3,"file":"subscription.d.ts","sourceRoot":"","sources":["../../src/schema/subscription.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,YAAY,CAAA;AACtD,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAA;AAE1C,MAAM,MAAM,2BAA2B,CAAC,CAAC,SAAS,YAAY,IAAI,KAAK,CACrE,CAAC,CAAC,YAAY,CAAC,CAChB,CAAA;AAED,MAAM,MAAM,wBAAwB,CAAC,CAAC,SAAS,YAAY,IAAI,KAAK,CAClE,CAAC,CAAC,SAAS,CAAC,CACb,CAAA;AAED,qBAAa,YAAY,CACvB,KAAK,CAAC,KAAK,SAAS,UAAU,GAAG,UAAU,EAC3C,KAAK,CAAC,WAAW,SAAS,YAAY,GAAG,YAAY,EACrD,KAAK,CAAC,QAAQ,SAAS,MAAM,GAAG,MAAM,EACtC,KAAK,CAAC,OAAO,SAAS,SAAS,GAAG,SAAS,MAAM,EAAE,GAC/C,SAAS,GACT,SAAS,MAAM,EAAE;IAKnB,QAAQ,CAAC,IAAI,EAAE,KAAK;IACpB,QAAQ,CAAC,UAAU,EAAE,WAAW;IAChC,QAAQ,CAAC,OAAO,EAAE,QAAQ;IAC1B,QAAQ,CAAC,MAAM,EAAE,OAAO;IAN1B,QAAQ,CAAC,IAAI,EAAG,cAAc,CAAS;gBAG5B,IAAI,EAAE,KAAK,EACX,UAAU,EAAE,WAAW,EACvB,OAAO,EAAE,QAAQ,EACjB,MAAM,EAAE,OAAO;CAE3B;AAGD,wBAAgB,YAAY,CAC1B,KAAK,CAAC,CAAC,SAAS,UAAU,EAC1B,KAAK,CAAC,CAAC,SAAS,YAAY,EAC5B,KAAK,CAAC,CAAC,SAAS,MAAM,EACtB,KAAK,CAAC,CAAC,SAAS,SAAS,GAAG,SAAS,MAAM,EAAE,GAAG,SAAS,EACzD,IAAI,EAAE,CAAC,EAAE,UAAU,EAAE,CAAC,EAAE,OAAO,EAAE,CAAC,EAAE,MAAM,GAAE,CAAkB,4BAE/D"}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Subscription = void 0;
|
|
4
|
+
exports.subscription = subscription;
|
|
4
5
|
class Subscription {
|
|
5
6
|
nsid;
|
|
6
7
|
parameters;
|
|
@@ -15,4 +16,8 @@ class Subscription {
|
|
|
15
16
|
}
|
|
16
17
|
}
|
|
17
18
|
exports.Subscription = Subscription;
|
|
19
|
+
/*@__NO_SIDE_EFFECTS__*/
|
|
20
|
+
function subscription(nsid, parameters, message, errors = undefined) {
|
|
21
|
+
return new Subscription(nsid, parameters, message, errors);
|
|
22
|
+
}
|
|
18
23
|
//# sourceMappingURL=subscription.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"subscription.js","sourceRoot":"","sources":["../../src/schema/subscription.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"subscription.js","sourceRoot":"","sources":["../../src/schema/subscription.ts"],"names":[],"mappings":";;;AA8BA,oCAOC;AA1BD,MAAa,YAAY;IAWZ;IACA;IACA;IACA;IANF,IAAI,GAAG,cAAuB,CAAA;IAEvC,YACW,IAAW,EACX,UAAuB,EACvB,OAAiB,EACjB,MAAe;QAHf,SAAI,GAAJ,IAAI,CAAO;QACX,eAAU,GAAV,UAAU,CAAa;QACvB,YAAO,GAAP,OAAO,CAAU;QACjB,WAAM,GAAN,MAAM,CAAS;IACvB,CAAC;CACL;AAhBD,oCAgBC;AAED,wBAAwB;AACxB,SAAgB,YAAY,CAK1B,IAAO,EAAE,UAAa,EAAE,OAAU,EAAE,SAAY,SAAc;IAC9D,OAAO,IAAI,YAAY,CAAa,IAAI,EAAE,UAAU,EAAE,OAAO,EAAE,MAAM,CAAC,CAAA;AACxE,CAAC","sourcesContent":["import { Infer, NsidString, Schema } from '../core.js'\nimport { ParamsSchema } from './params.js'\n\nexport type InferSubscriptionParameters<S extends Subscription> = Infer<\n S['parameters']\n>\n\nexport type InferSubscriptionMessage<S extends Subscription> = Infer<\n S['message']\n>\n\nexport class Subscription<\n const TNsid extends NsidString = NsidString,\n const TParameters extends ParamsSchema = ParamsSchema,\n const TMessage extends Schema = Schema,\n const TErrors extends undefined | readonly string[] =\n | undefined\n | readonly string[],\n> {\n readonly type = 'subscription' as const\n\n constructor(\n readonly nsid: TNsid,\n readonly parameters: TParameters,\n readonly message: TMessage,\n readonly errors: TErrors,\n ) {}\n}\n\n/*@__NO_SIDE_EFFECTS__*/\nexport function subscription<\n const N extends NsidString,\n const P extends ParamsSchema,\n const M extends Schema,\n const E extends undefined | readonly string[] = undefined,\n>(nsid: N, parameters: P, message: M, errors: E = undefined as E) {\n return new Subscription<N, P, M, E>(nsid, parameters, message, errors)\n}\n"]}
|
package/dist/schema/token.d.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export declare class TokenSchema<
|
|
3
|
-
readonly value:
|
|
4
|
-
constructor(value:
|
|
5
|
-
validateInContext(input: unknown, ctx:
|
|
1
|
+
import { NsidString, Schema, ValidationContext } from '../core.js';
|
|
2
|
+
export declare class TokenSchema<const TValue extends string = string> extends Schema<TValue> {
|
|
3
|
+
readonly value: TValue;
|
|
4
|
+
constructor(value: TValue);
|
|
5
|
+
validateInContext(input: unknown, ctx: ValidationContext): import("../core.js").ValidationResult<TValue>;
|
|
6
6
|
toJSON(): string;
|
|
7
7
|
toString(): string;
|
|
8
8
|
}
|
|
9
|
+
export declare function token<const N extends NsidString, const H extends string = 'main'>(nsid: N, hash?: H): TokenSchema<import("../core.js").$Type<N, H>>;
|
|
9
10
|
//# sourceMappingURL=token.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"token.d.ts","sourceRoot":"","sources":["../../src/schema/token.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"token.d.ts","sourceRoot":"","sources":["../../src/schema/token.ts"],"names":[],"mappings":"AAAA,OAAO,EAAS,UAAU,EAAE,MAAM,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAA;AAEzE,qBAAa,WAAW,CACtB,KAAK,CAAC,MAAM,SAAS,MAAM,GAAG,MAAM,CACpC,SAAQ,MAAM,CAAC,MAAM,CAAC;IACV,QAAQ,CAAC,KAAK,EAAE,MAAM;gBAAb,KAAK,EAAE,MAAM;IAIlC,iBAAiB,CAAC,KAAK,EAAE,OAAO,EAAE,GAAG,EAAE,iBAAiB;IAqBxD,MAAM,IAAI,MAAM;IAIhB,QAAQ,IAAI,MAAM;CAGnB;AAGD,wBAAgB,KAAK,CACnB,KAAK,CAAC,CAAC,SAAS,UAAU,EAC1B,KAAK,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM,EAC/B,IAAI,EAAE,CAAC,EAAE,IAAI,GAAE,CAAe,iDAE/B"}
|
package/dist/schema/token.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.TokenSchema = void 0;
|
|
4
|
+
exports.token = token;
|
|
4
5
|
const core_js_1 = require("../core.js");
|
|
5
6
|
class TokenSchema extends core_js_1.Schema {
|
|
6
7
|
value;
|
|
@@ -32,4 +33,8 @@ class TokenSchema extends core_js_1.Schema {
|
|
|
32
33
|
}
|
|
33
34
|
}
|
|
34
35
|
exports.TokenSchema = TokenSchema;
|
|
36
|
+
/*@__NO_SIDE_EFFECTS__*/
|
|
37
|
+
function token(nsid, hash = 'main') {
|
|
38
|
+
return new TokenSchema((0, core_js_1.$type)(nsid, hash));
|
|
39
|
+
}
|
|
35
40
|
//# sourceMappingURL=token.js.map
|
package/dist/schema/token.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"token.js","sourceRoot":"","sources":["../../src/schema/token.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"token.js","sourceRoot":"","sources":["../../src/schema/token.ts"],"names":[],"mappings":";;;AAwCA,sBAKC;AA7CD,wCAAyE;AAEzE,MAAa,WAEX,SAAQ,gBAAc;IACD;IAArB,YAAqB,KAAa;QAChC,KAAK,EAAE,CAAA;QADY,UAAK,GAAL,KAAK,CAAQ;IAElC,CAAC;IAED,iBAAiB,CAAC,KAAc,EAAE,GAAsB;QACtD,IAAI,KAAK,KAAK,IAAI,CAAC,KAAK,EAAE,CAAC;YACzB,OAAO,GAAG,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;QAChC,CAAC;QAED,0EAA0E;QAC1E,eAAe;QACf,IAAI,KAAK,YAAY,WAAW,IAAI,KAAK,CAAC,KAAK,KAAK,IAAI,CAAC,KAAK,EAAE,CAAC;YAC/D,OAAO,GAAG,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;QAChC,CAAC;QAED,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;YAC9B,OAAO,GAAG,CAAC,gBAAgB,CAAC,KAAK,EAAE,OAAO,CAAC,CAAA;QAC7C,CAAC;QAED,OAAO,GAAG,CAAC,iBAAiB,CAAC,KAAK,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAA;IACnD,CAAC;IAED,yEAAyE;IACzE,cAAc;IAEd,MAAM;QACJ,OAAO,IAAI,CAAC,KAAK,CAAA;IACnB,CAAC;IAED,QAAQ;QACN,OAAO,IAAI,CAAC,KAAK,CAAA;IACnB,CAAC;CACF;AAnCD,kCAmCC;AAED,wBAAwB;AACxB,SAAgB,KAAK,CAGnB,IAAO,EAAE,OAAU,MAAW;IAC9B,OAAO,IAAI,WAAW,CAAC,IAAA,eAAK,EAAC,IAAI,EAAE,IAAI,CAAC,CAAC,CAAA;AAC3C,CAAC","sourcesContent":["import { $type, NsidString, Schema, ValidationContext } from '../core.js'\n\nexport class TokenSchema<\n const TValue extends string = string,\n> extends Schema<TValue> {\n constructor(readonly value: TValue) {\n super()\n }\n\n validateInContext(input: unknown, ctx: ValidationContext) {\n if (input === this.value) {\n return ctx.success(this.value)\n }\n\n // @NOTE: allow using the token instance itself (but convert to the actual\n // token value)\n if (input instanceof TokenSchema && input.value === this.value) {\n return ctx.success(this.value)\n }\n\n if (typeof input !== 'string') {\n return ctx.issueInvalidType(input, 'token')\n }\n\n return ctx.issueInvalidValue(input, [this.value])\n }\n\n // When using the TokenSchema instance as data, let's serialize it to the\n // token value\n\n toJSON(): string {\n return this.value\n }\n\n toString(): string {\n return this.value\n }\n}\n\n/*@__NO_SIDE_EFFECTS__*/\nexport function token<\n const N extends NsidString,\n const H extends string = 'main',\n>(nsid: N, hash: H = 'main' as H) {\n return new TokenSchema($type(nsid, hash))\n}\n"]}
|
|
@@ -1,30 +1,47 @@
|
|
|
1
|
-
import { $Type, $Typed, $TypedMaybe,
|
|
2
|
-
|
|
3
|
-
export type TypedObjectSchemaOutput<T extends $Type, S extends Validator<{
|
|
1
|
+
import { $Type, $TypeOf, $Typed, $TypedMaybe, InferInput, InferOutput, NsidString, Schema, Unknown$TypedObject, ValidationContext, Validator } from '../core.js';
|
|
2
|
+
export declare class TypedObjectSchema<const TType extends $Type = $Type, const TShape extends Validator<{
|
|
4
3
|
[k: string]: unknown;
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
$type
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
$type: T;
|
|
25
|
-
} extends infer T_1 ? { [K in keyof T_1]: (Omit<X, "$type"> & {
|
|
26
|
-
$type: T;
|
|
4
|
+
}> = any> extends Schema<$TypedMaybe<InferInput<TShape>, TType>, $TypedMaybe<InferOutput<TShape>, TType>> {
|
|
5
|
+
readonly $type: TType;
|
|
6
|
+
readonly schema: TShape;
|
|
7
|
+
constructor($type: TType, schema: TShape);
|
|
8
|
+
isTypeOf<X extends Record<string, unknown>>(value: X): value is X extends {
|
|
9
|
+
$type?: TType;
|
|
10
|
+
} ? X : $TypedMaybe<Exclude<X, Unknown$TypedObject>, TType>;
|
|
11
|
+
build(input: Omit<InferInput<this>, '$type'>): $Typed<InferOutput<this>, TType>;
|
|
12
|
+
$isTypeOf<X extends Record<string, unknown>>(value: X): value is X extends {
|
|
13
|
+
$type?: TType;
|
|
14
|
+
} ? X : Exclude<X, Unknown$TypedObject> & {
|
|
15
|
+
$type?: TType | undefined;
|
|
16
|
+
} extends infer T ? { [K in keyof T]: (Exclude<X, Unknown$TypedObject> & {
|
|
17
|
+
$type?: TType | undefined;
|
|
18
|
+
})[K]; } : never;
|
|
19
|
+
$build(input: Omit<InferInput<this>, '$type'>): InferOutput<this> & {
|
|
20
|
+
$type: TType;
|
|
21
|
+
} extends infer T ? { [K in keyof T]: (InferOutput<this> & {
|
|
22
|
+
$type: TType;
|
|
27
23
|
})[K]; } : never;
|
|
28
|
-
validateInContext(input: unknown, ctx:
|
|
24
|
+
validateInContext(input: unknown, ctx: ValidationContext): import("../core.js").ValidationResult<InferInput<TShape>>;
|
|
29
25
|
}
|
|
26
|
+
/**
|
|
27
|
+
* This function offers two overloads:
|
|
28
|
+
* - One that allows creating a {@link TypedObjectSchema}, and infer the output
|
|
29
|
+
* type from the provided arguments, without requiring to specify any of the
|
|
30
|
+
* generics. This is useful when you want to define a record without
|
|
31
|
+
* explicitly defining its interface. This version does not support circular
|
|
32
|
+
* references, as TypeScript cannot infer types in such cases.
|
|
33
|
+
* - One allows creating a {@link TypedObjectSchema} with an explicitly defined
|
|
34
|
+
* interface. This will typically be used by codegen (`lex build`) to generate
|
|
35
|
+
* schemas that work even if they contain circular references.
|
|
36
|
+
*/
|
|
37
|
+
export declare function typedObject<const N extends NsidString, const H extends string, const S extends Validator<{
|
|
38
|
+
[k: string]: unknown;
|
|
39
|
+
}>>(nsid: N, hash: H, validator: S): TypedObjectSchema<$Type<N, H>, S>;
|
|
40
|
+
export declare function typedObject<V extends {
|
|
41
|
+
$type?: $Type;
|
|
42
|
+
}>(nsid: V extends {
|
|
43
|
+
$type?: infer T extends string;
|
|
44
|
+
} ? T extends `${infer N}#${string}` ? N : T : never, hash: V extends {
|
|
45
|
+
$type?: infer T extends string;
|
|
46
|
+
} ? T extends `${string}#${infer H}` ? H : 'main' : never, validator: Validator<Omit<V, '$type'>>): TypedObjectSchema<$TypeOf<V>, Validator<V>>;
|
|
30
47
|
//# sourceMappingURL=typed-object.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"typed-object.d.ts","sourceRoot":"","sources":["../../src/schema/typed-object.ts"],"names":[],"mappings":"AACA,OAAO,EACL,KAAK,EACL,MAAM,EACN,WAAW,EACX,
|
|
1
|
+
{"version":3,"file":"typed-object.d.ts","sourceRoot":"","sources":["../../src/schema/typed-object.ts"],"names":[],"mappings":"AACA,OAAO,EACL,KAAK,EACL,OAAO,EACP,MAAM,EACN,WAAW,EAGX,UAAU,EACV,WAAW,EACX,UAAU,EACV,MAAM,EACN,mBAAmB,EACnB,iBAAiB,EACjB,SAAS,EACV,MAAM,YAAY,CAAA;AAEnB,qBAAa,iBAAiB,CAC5B,KAAK,CAAC,KAAK,SAAS,KAAK,GAAG,KAAK,EACjC,KAAK,CAAC,MAAM,SAAS,SAAS,CAAC;IAAE,CAAC,CAAC,EAAE,MAAM,GAAG,OAAO,CAAA;CAAE,CAAC,GAAG,GAAG,CAC9D,SAAQ,MAAM,CACd,WAAW,CAAC,UAAU,CAAC,MAAM,CAAC,EAAE,KAAK,CAAC,EACtC,WAAW,CAAC,WAAW,CAAC,MAAM,CAAC,EAAE,KAAK,CAAC,CACxC;IAEG,QAAQ,CAAC,KAAK,EAAE,KAAK;IACrB,QAAQ,CAAC,MAAM,EAAE,MAAM;gBADd,KAAK,EAAE,KAAK,EACZ,MAAM,EAAE,MAAM;IAKzB,QAAQ,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EACxC,KAAK,EAAE,CAAC,GACP,KAAK,IAAI,CAAC,SAAS;QAAE,KAAK,CAAC,EAAE,KAAK,CAAA;KAAE,GACnC,CAAC,GACD,WAAW,CAAC,OAAO,CAAC,CAAC,EAAE,mBAAmB,CAAC,EAAE,KA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,KAAK,EACT,SAAS,EAAE,SAAS,CAAC,IAAI,CAAC,CAAC,EAAE,OAAO,CAAC,CAAC,GACrC,iBAAiB,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,SAAS,CAAC,CAAC,CAAC,CAAC,CAAA"}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.TypedObjectSchema = void 0;
|
|
4
|
+
exports.typedObject = typedObject;
|
|
4
5
|
const lex_data_1 = require("@atproto/lex-data");
|
|
5
6
|
const core_js_1 = require("../core.js");
|
|
6
7
|
class TypedObjectSchema extends core_js_1.Schema {
|
|
@@ -15,9 +16,7 @@ class TypedObjectSchema extends core_js_1.Schema {
|
|
|
15
16
|
return value.$type === undefined || value.$type === this.$type;
|
|
16
17
|
}
|
|
17
18
|
build(input) {
|
|
18
|
-
return input
|
|
19
|
-
? input
|
|
20
|
-
: { ...input, $type: this.$type };
|
|
19
|
+
return this.parse((0, core_js_1.$typed)(input, this.$type));
|
|
21
20
|
}
|
|
22
21
|
$isTypeOf(value) {
|
|
23
22
|
return this.isTypeOf(value);
|
|
@@ -38,4 +37,8 @@ class TypedObjectSchema extends core_js_1.Schema {
|
|
|
38
37
|
}
|
|
39
38
|
}
|
|
40
39
|
exports.TypedObjectSchema = TypedObjectSchema;
|
|
40
|
+
/*@__NO_SIDE_EFFECTS__*/
|
|
41
|
+
function typedObject(nsid, hash, validator) {
|
|
42
|
+
return new TypedObjectSchema((0, core_js_1.$type)(nsid, hash), validator);
|
|
43
|
+
}
|
|
41
44
|
//# sourceMappingURL=typed-object.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"typed-object.js","sourceRoot":"","sources":["../../src/schema/typed-object.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"typed-object.js","sourceRoot":"","sources":["../../src/schema/typed-object.ts"],"names":[],"mappings":";;;AAuGA,kCAMC;AA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AIzB,IAAO,EAAE,IAAO,EAAE,SAAY;IAC9B,OAAO,IAAI,iBAAiB,CAAiB,IAAA,eAAK,EAAC,IAAI,EAAE,IAAI,CAAC,EAAE,SAAS,CAAC,CAAA;AAC5E,CAAC","sourcesContent":["import { isPlainObject } from '@atproto/lex-data'\nimport {\n $Type,\n $TypeOf,\n $Typed,\n $TypedMaybe,\n $type,\n $typed,\n InferInput,\n InferOutput,\n NsidString,\n Schema,\n Unknown$TypedObject,\n ValidationContext,\n Validator,\n} from '../core.js'\n\nexport class TypedObjectSchema<\n const TType extends $Type = $Type,\n const TShape extends Validator<{ [k: string]: unknown }> = any,\n> extends Schema<\n $TypedMaybe<InferInput<TShape>, TType>,\n $TypedMaybe<InferOutput<TShape>, TType>\n> {\n constructor(\n readonly $type: TType,\n readonly schema: TShape,\n ) {\n super()\n }\n\n isTypeOf<X extends Record<string, unknown>>(\n value: X,\n ): value is X extends { $type?: TType }\n ? X\n : $TypedMaybe<Exclude<X, Unknown$TypedObject>, TType> {\n return value.$type === undefined || value.$type === this.$type\n }\n\n build(\n input: Omit<InferInput<this>, '$type'>,\n ): $Typed<InferOutput<this>, TType> {\n return this.parse($typed(input, this.$type)) as $Typed<\n InferOutput<this>,\n TType\n >\n }\n\n $isTypeOf<X extends Record<string, unknown>>(value: X) {\n return this.isTypeOf(value)\n }\n\n $build(input: Omit<InferInput<this>, '$type'>) {\n return this.build(input)\n }\n\n validateInContext(input: unknown, ctx: ValidationContext) {\n if (!isPlainObject(input)) {\n return ctx.issueInvalidType(input, 'object')\n }\n\n if (\n '$type' in input &&\n input.$type !== undefined &&\n input.$type !== this.$type\n ) {\n return ctx.issueInvalidPropertyValue(input, '$type', [this.$type])\n }\n\n return ctx.validate(input, this.schema)\n }\n}\n\n/**\n * This function offers two overloads:\n * - One that allows creating a {@link TypedObjectSchema}, and infer the output\n * type from the provided arguments, without requiring to specify any of the\n * generics. This is useful when you want to define a record without\n * explicitly defining its interface. This version does not support circular\n * references, as TypeScript cannot infer types in such cases.\n * - One allows creating a {@link TypedObjectSchema} with an explicitly defined\n * interface. This will typically be used by codegen (`lex build`) to generate\n * schemas that work even if they contain circular references.\n */\nexport function typedObject<\n const N extends NsidString,\n const H extends string,\n const S extends Validator<{ [k: string]: unknown }>,\n>(nsid: N, hash: H, validator: S): TypedObjectSchema<$Type<N, H>, S>\nexport function typedObject<V extends { $type?: $Type }>(\n nsid: V extends { $type?: infer T extends string }\n ? T extends `${infer N}#${string}`\n ? N\n : T // (T is a \"main\" type, so already an NSID)\n : never,\n hash: V extends { $type?: infer T extends string }\n ? T extends `${string}#${infer H}`\n ? H\n : 'main'\n : never,\n validator: Validator<Omit<V, '$type'>>,\n): TypedObjectSchema<$TypeOf<V>, Validator<V>>\n/*@__NO_SIDE_EFFECTS__*/\nexport function typedObject<\n const N extends NsidString,\n const H extends string,\n const S extends Validator<{ [k: string]: unknown }>,\n>(nsid: N, hash: H, validator: S) {\n return new TypedObjectSchema<$Type<N, H>, S>($type(nsid, hash), validator)\n}\n"]}
|