@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
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.memoizedOptions = memoizedOptions;
|
|
4
|
+
exports.memoizedTransformer = memoizedTransformer;
|
|
5
|
+
/*@__NO_SIDE_EFFECTS__*/
|
|
6
|
+
function memoizedOptions(fn, keyFn) {
|
|
7
|
+
let emptyOptionsValue;
|
|
8
|
+
if (keyFn) {
|
|
9
|
+
const cache = new Map();
|
|
10
|
+
const fromCache = (options) => {
|
|
11
|
+
const key = keyFn(options);
|
|
12
|
+
if (key !== undefined) {
|
|
13
|
+
const cached = cache.get(key);
|
|
14
|
+
if (cached)
|
|
15
|
+
return cached;
|
|
16
|
+
const result = fn(options);
|
|
17
|
+
cache.set(key, result);
|
|
18
|
+
return result;
|
|
19
|
+
}
|
|
20
|
+
return fn(options);
|
|
21
|
+
};
|
|
22
|
+
return ((options) => {
|
|
23
|
+
// Non-empty options case
|
|
24
|
+
if (options)
|
|
25
|
+
for (const _ in options)
|
|
26
|
+
return fromCache(options);
|
|
27
|
+
// Empty or missing options case
|
|
28
|
+
return (emptyOptionsValue ??= fn(options));
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
return ((options) => {
|
|
32
|
+
// Non-empty options case
|
|
33
|
+
if (options)
|
|
34
|
+
for (const _ in options)
|
|
35
|
+
return fn(options);
|
|
36
|
+
// Empty or missing options case
|
|
37
|
+
return (emptyOptionsValue ??= fn(options));
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
/*@__NO_SIDE_EFFECTS__*/
|
|
41
|
+
function memoizedTransformer(fn) {
|
|
42
|
+
const cache = new WeakMap();
|
|
43
|
+
return ((arg) => {
|
|
44
|
+
const cached = cache.get(arg);
|
|
45
|
+
if (cached)
|
|
46
|
+
return cached;
|
|
47
|
+
const result = fn(arg);
|
|
48
|
+
cache.set(arg, result);
|
|
49
|
+
return result;
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
//# sourceMappingURL=memoize.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"memoize.js","sourceRoot":"","sources":["../../src/util/memoize.ts"],"names":[],"mappings":";;AACA,0CA2CC;AAGD,kDASC;AAxDD,wBAAwB;AACxB,SAAgB,eAAe,CAG7B,EAAK,EACL,KAE4C;IAE5C,IAAI,iBAA4C,CAAA;IAEhD,IAAI,KAAK,EAAE,CAAC;QACV,MAAM,KAAK,GAAG,IAAI,GAAG,EAAmD,CAAA;QACxE,MAAM,SAAS,GAAG,CAChB,OAAsC,EACvB,EAAE;YACjB,MAAM,GAAG,GAAG,KAAK,CAAC,OAAO,CAAC,CAAA;YAC1B,IAAI,GAAG,KAAK,SAAS,EAAE,CAAC;gBACtB,MAAM,MAAM,GAAG,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;gBAC7B,IAAI,MAAM;oBAAE,OAAO,MAAM,CAAA;gBACzB,MAAM,MAAM,GAAG,EAAE,CAAC,OAAO,CAAkB,CAAA;gBAC3C,KAAK,CAAC,GAAG,CAAC,GAAG,EAAE,MAAM,CAAC,CAAA;gBACtB,OAAO,MAAM,CAAA;YACf,CAAC;YAED,OAAO,EAAE,CAAC,OAAO,CAAC,CAAA;QACpB,CAAC,CAAA;QAED,OAAO,CAAC,CAAC,OAAyB,EAAiB,EAAE;YACnD,yBAAyB;YACzB,IAAI,OAAO;gBAAE,KAAK,MAAM,CAAC,IAAI,OAAO;oBAAE,OAAO,SAAS,CAAC,OAAO,CAAC,CAAA;YAE/D,gCAAgC;YAChC,OAAO,CAAC,iBAAiB,KAAK,EAAE,CAAC,OAAO,CAAC,CAAC,CAAA;QAC5C,CAAC,CAAM,CAAA;IACT,CAAC;IAED,OAAO,CAAC,CAAC,OAAyB,EAAiB,EAAE;QACnD,yBAAyB;QACzB,IAAI,OAAO;YAAE,KAAK,MAAM,CAAC,IAAI,OAAO;gBAAE,OAAO,EAAE,CAAC,OAAO,CAAC,CAAA;QAExD,gCAAgC;QAChC,OAAO,CAAC,iBAAiB,KAAK,EAAE,CAAC,OAAO,CAAC,CAAC,CAAA;IAC5C,CAAC,CAAM,CAAA;AACT,CAAC;AAED,wBAAwB;AACxB,SAAgB,mBAAmB,CAA8B,EAAK;IACpE,MAAM,KAAK,GAAG,IAAI,OAAO,EAAyB,CAAA;IAClD,OAAO,CAAC,CAAC,GAAqB,EAAiB,EAAE;QAC/C,MAAM,MAAM,GAAG,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;QAC7B,IAAI,MAAM;YAAE,OAAO,MAAM,CAAA;QACzB,MAAM,MAAM,GAAG,EAAE,CAAC,GAAG,CAAkB,CAAA;QACvC,KAAK,CAAC,GAAG,CAAC,GAAG,EAAE,MAAM,CAAC,CAAA;QACtB,OAAO,MAAM,CAAA;IACf,CAAC,CAAM,CAAA;AACT,CAAC","sourcesContent":["/*@__NO_SIDE_EFFECTS__*/\nexport function memoizedOptions<\n F extends (options?: NonNullable<unknown>) => any,\n>(\n fn: F,\n keyFn?: (\n options: NonNullable<Parameters<F>[0]>,\n ) => string | number | boolean | null | void,\n): F {\n let emptyOptionsValue: ReturnType<F> | undefined\n\n if (keyFn) {\n const cache = new Map<string | number | boolean | null, ReturnType<F>>()\n const fromCache = (\n options: NonNullable<Parameters<F>[0]>,\n ): ReturnType<F> => {\n const key = keyFn(options)\n if (key !== undefined) {\n const cached = cache.get(key)\n if (cached) return cached\n const result = fn(options) as ReturnType<F>\n cache.set(key, result)\n return result\n }\n\n return fn(options)\n }\n\n return ((options: Parameters<F>[0]): ReturnType<F> => {\n // Non-empty options case\n if (options) for (const _ in options) return fromCache(options)\n\n // Empty or missing options case\n return (emptyOptionsValue ??= fn(options))\n }) as F\n }\n\n return ((options: Parameters<F>[0]): ReturnType<F> => {\n // Non-empty options case\n if (options) for (const _ in options) return fn(options)\n\n // Empty or missing options case\n return (emptyOptionsValue ??= fn(options))\n }) as F\n}\n\n/*@__NO_SIDE_EFFECTS__*/\nexport function memoizedTransformer<F extends (arg: any) => any>(fn: F): F {\n const cache = new WeakMap<object, ReturnType<F>>()\n return ((arg: Parameters<F>[0]): ReturnType<F> => {\n const cached = cache.get(arg)\n if (cached) return cached\n const result = fn(arg) as ReturnType<F>\n cache.set(arg, result)\n return result\n }) as F\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@atproto/lex-schema",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.6",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"description": "Lexicon schema system for AT Lexicons",
|
|
6
6
|
"keywords": [
|
|
@@ -28,22 +28,22 @@
|
|
|
28
28
|
"types": "./dist/index.d.ts",
|
|
29
29
|
"exports": {
|
|
30
30
|
".": {
|
|
31
|
+
"types": "./dist/index.d.ts",
|
|
31
32
|
"browser": "./dist/index.js",
|
|
32
33
|
"import": "./dist/index.js",
|
|
33
|
-
"require": "./dist/index.js"
|
|
34
|
-
"types": "./dist/index.d.ts"
|
|
34
|
+
"require": "./dist/index.js"
|
|
35
35
|
}
|
|
36
36
|
},
|
|
37
37
|
"dependencies": {
|
|
38
38
|
"tslib": "^2.8.1",
|
|
39
39
|
"@atproto/syntax": "0.4.2",
|
|
40
|
-
"@atproto/lex-data": "0.0.
|
|
40
|
+
"@atproto/lex-data": "0.0.5"
|
|
41
41
|
},
|
|
42
42
|
"devDependencies": {
|
|
43
|
-
"
|
|
43
|
+
"vitest": "^4.0.16"
|
|
44
44
|
},
|
|
45
45
|
"scripts": {
|
|
46
46
|
"build": "tsc --build tsconfig.build.json",
|
|
47
|
-
"test": "
|
|
47
|
+
"test": "vitest run"
|
|
48
48
|
}
|
|
49
49
|
}
|
package/src/core/$type.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { NsidString } from './string-format.js'
|
|
2
|
+
import { OmitKey } from './types.js'
|
|
2
3
|
|
|
3
4
|
export type $Type<
|
|
4
5
|
N extends NsidString = NsidString,
|
|
@@ -20,3 +21,6 @@ export function $type<N extends NsidString, H extends string>(
|
|
|
20
21
|
): $Type<N, H> {
|
|
21
22
|
return (hash === 'main' ? nsid : `${nsid}#${hash}`) as $Type<N, H>
|
|
22
23
|
}
|
|
24
|
+
|
|
25
|
+
export type $Typed<V, T extends string = string> = V & { $type: T }
|
|
26
|
+
export type Un$Typed<V extends { $type?: string }> = OmitKey<V, '$type'>
|
package/src/core/result.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export type ResultSuccess<V = any> = { success: true; value: V }
|
|
2
|
-
export type ResultFailure<E = Error> = { success: false;
|
|
2
|
+
export type ResultFailure<E = Error> = { success: false; reason: E }
|
|
3
3
|
|
|
4
4
|
export type Result<V = any, E = Error> = ResultSuccess<V> | ResultFailure<E>
|
|
5
5
|
|
|
@@ -9,13 +9,13 @@ export function success<V>(value: V): ResultSuccess<V> {
|
|
|
9
9
|
}
|
|
10
10
|
|
|
11
11
|
/*@__NO_SIDE_EFFECTS__*/
|
|
12
|
-
export function failure<E>(
|
|
13
|
-
return { success: false,
|
|
12
|
+
export function failure<E>(reason: E): ResultFailure<E> {
|
|
13
|
+
return { success: false, reason }
|
|
14
14
|
}
|
|
15
15
|
|
|
16
16
|
/*@__NO_SIDE_EFFECTS__*/
|
|
17
|
-
export function
|
|
18
|
-
return result.
|
|
17
|
+
export function failureReason<T>(result: ResultFailure<T>): T {
|
|
18
|
+
return result.reason
|
|
19
19
|
}
|
|
20
20
|
|
|
21
21
|
/*@__NO_SIDE_EFFECTS__*/
|
|
@@ -31,9 +31,9 @@ export function successValue<T>(result: ResultSuccess<T>): T {
|
|
|
31
31
|
* @example
|
|
32
32
|
*
|
|
33
33
|
* ```ts
|
|
34
|
-
* declare function someFunction(): Promise<
|
|
34
|
+
* declare function someFunction(): Promise<string>
|
|
35
35
|
*
|
|
36
|
-
* const result = await someFunction().
|
|
36
|
+
* const result = await someFunction().then(success, catchall)
|
|
37
37
|
* if (result.success) {
|
|
38
38
|
* console.log(result.value) // string
|
|
39
39
|
* } else {
|
|
@@ -58,9 +58,10 @@ export function catchall(err: unknown): ResultFailure<Error> {
|
|
|
58
58
|
* class FooError extends Error {}
|
|
59
59
|
* class BarError extends Error {}
|
|
60
60
|
*
|
|
61
|
-
* declare function someFunction(): Promise<
|
|
61
|
+
* declare function someFunction(): Promise<string>
|
|
62
62
|
*
|
|
63
63
|
* const result = await someFunction()
|
|
64
|
+
* .then(success)
|
|
64
65
|
* .catch(createCatcher(FooError))
|
|
65
66
|
* .catch(createCatcher(BarError))
|
|
66
67
|
*
|
|
@@ -5,17 +5,42 @@ import {
|
|
|
5
5
|
ValidatorContext,
|
|
6
6
|
} from './validator.js'
|
|
7
7
|
|
|
8
|
-
export abstract class Schema<Output> implements Validator<Output> {
|
|
9
|
-
declare readonly ['
|
|
8
|
+
export abstract class Schema<Output = any> implements Validator<Output> {
|
|
9
|
+
declare readonly ['__lex']: { output: Output }
|
|
10
10
|
|
|
11
11
|
abstract validateInContext(
|
|
12
12
|
input: unknown,
|
|
13
13
|
ctx: ValidatorContext,
|
|
14
14
|
): ValidationResult<Output>
|
|
15
15
|
|
|
16
|
+
/**
|
|
17
|
+
* @note use {@link check}() instead of {@link assert}() if you encounter a
|
|
18
|
+
* `ts(2775)` error and you are not able to fully type the validator. This
|
|
19
|
+
* will typically arise in generic contexts, where the narrowed type is not
|
|
20
|
+
* needed.
|
|
21
|
+
*/
|
|
16
22
|
assert(input: unknown): asserts input is Output {
|
|
17
23
|
const result = this.safeParse(input, { allowTransform: false })
|
|
18
|
-
if (!result.success) throw result.
|
|
24
|
+
if (!result.success) throw result.reason
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Alias for {@link assert}(). Most useful in generic contexts where the
|
|
29
|
+
* validator is not exactly typed, allowing to avoid "_Assertions require
|
|
30
|
+
* every name in the call target to be declared with an explicit type
|
|
31
|
+
* annotation. ts(2775)_" errors.
|
|
32
|
+
*/
|
|
33
|
+
check(input: unknown): void {
|
|
34
|
+
this.assert(input)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Casts the input (by validating it) to the output type if it matches the
|
|
39
|
+
* schema, otherwise throws. This is the same as calling {@link parse}() with
|
|
40
|
+
* `allowTransform: false`.
|
|
41
|
+
*/
|
|
42
|
+
cast<I>(input: I): I & Output {
|
|
43
|
+
return this.parse(input, { allowTransform: false })
|
|
19
44
|
}
|
|
20
45
|
|
|
21
46
|
matches(input: unknown): input is Output {
|
|
@@ -34,7 +59,7 @@ export abstract class Schema<Output> implements Validator<Output> {
|
|
|
34
59
|
parse(input: unknown, options?: ValidationOptions): Output
|
|
35
60
|
parse(input: unknown, options?: ValidationOptions): Output {
|
|
36
61
|
const result = this.safeParse(input, options)
|
|
37
|
-
if (!result.success) throw result.
|
|
62
|
+
if (!result.success) throw result.reason
|
|
38
63
|
return result.value
|
|
39
64
|
}
|
|
40
65
|
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ensureValidCidString,
|
|
1
|
+
import { ensureValidCidString, isLanguageString } from '@atproto/lex-data'
|
|
2
2
|
import {
|
|
3
3
|
AtIdentifierString,
|
|
4
4
|
AtUriString,
|
|
@@ -17,16 +17,25 @@ import {
|
|
|
17
17
|
ensureValidRecordKey,
|
|
18
18
|
ensureValidTid,
|
|
19
19
|
} from '@atproto/syntax'
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
20
|
+
import {
|
|
21
|
+
AssertFn,
|
|
22
|
+
CastFn,
|
|
23
|
+
CheckFn,
|
|
24
|
+
createAssertFunction,
|
|
25
|
+
createCastFunction,
|
|
26
|
+
createCheckFunction,
|
|
27
|
+
} from '../util/assertion-util.js'
|
|
28
|
+
|
|
29
|
+
// Format utilities missing from @atproto/syntax
|
|
30
|
+
export type CidString = string
|
|
31
|
+
export type LanguageString = string
|
|
32
|
+
export type UriString = `${string}:${string}`
|
|
23
33
|
|
|
24
34
|
/*@__NO_SIDE_EFFECTS__*/
|
|
25
|
-
function
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
}
|
|
35
|
+
export function isUriString<T extends string>(
|
|
36
|
+
input: T,
|
|
37
|
+
): input is T & UriString {
|
|
38
|
+
return /^\w+:(?:\/\/)?[^\s/][^\s]*$/.test(input)
|
|
30
39
|
}
|
|
31
40
|
|
|
32
41
|
// Re-export utility typed as assertion functions so that TypeScript can
|
|
@@ -41,61 +50,72 @@ export type {
|
|
|
41
50
|
NsidString,
|
|
42
51
|
RecordKeyString,
|
|
43
52
|
TidString,
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
export const assertDid: AssertFn<DidString> = ensureValidDid
|
|
47
|
-
export const assertAtUri: AssertFn<AtUriString> = ensureValidAtUri
|
|
48
|
-
export const assertAtIdentifier: AssertFn<AtIdentifierString> =
|
|
49
|
-
ensureValidAtIdentifier
|
|
50
|
-
export const assertNsid: AssertFn<NsidString> = ensureValidNsid
|
|
51
|
-
export const assertTid: AssertFn<TidString> = ensureValidTid
|
|
52
|
-
export const assertRecordKey: AssertFn<RecordKeyString> = ensureValidRecordKey
|
|
53
|
-
export const assertDatetime: AssertFn<DatetimeString> = ensureValidDatetime
|
|
54
|
-
export const assertCidString: AssertFn<string> = ensureValidCidString
|
|
55
|
-
export const assertHandle: AssertFn<HandleString> = ensureValidHandle
|
|
53
|
+
} from '@atproto/syntax'
|
|
56
54
|
|
|
57
55
|
// Export utilities for formats missing from @atproto/syntax
|
|
58
56
|
|
|
59
|
-
export
|
|
60
|
-
|
|
61
|
-
export
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
export const
|
|
57
|
+
export const assertAtIdentifierString: AssertFn<AtIdentifierString> =
|
|
58
|
+
ensureValidAtIdentifier
|
|
59
|
+
export const assertAtUriString: AssertFn<AtUriString> = ensureValidAtUri
|
|
60
|
+
export const assertCidString: AssertFn<CidString> = ensureValidCidString
|
|
61
|
+
export const assertDatetimeString: AssertFn<DatetimeString> =
|
|
62
|
+
ensureValidDatetime
|
|
63
|
+
export const assertDidString: AssertFn<DidString> = ensureValidDid
|
|
64
|
+
export const assertHandleString: AssertFn<HandleString> = ensureValidHandle
|
|
65
|
+
export const assertLanguageString: AssertFn<LanguageString> =
|
|
66
|
+
createAssertFunction<LanguageString>(
|
|
67
|
+
isLanguageString,
|
|
68
|
+
'Invalid BCP 47 string',
|
|
69
|
+
)
|
|
70
|
+
export const assertNsidString: AssertFn<NsidString> = ensureValidNsid
|
|
71
|
+
export const assertRecordKeyString: AssertFn<RecordKeyString> =
|
|
72
|
+
ensureValidRecordKey
|
|
73
|
+
export const assertTidString: AssertFn<TidString> = ensureValidTid
|
|
74
|
+
export const assertUriString: AssertFn<UriString> =
|
|
75
|
+
createAssertFunction<UriString>(isUriString, 'Invalid URI')
|
|
76
|
+
|
|
77
|
+
export const asAtIdentifierString: CastFn<AtIdentifierString> =
|
|
78
|
+
/*#__PURE__*/ createCastFunction(assertAtIdentifierString)
|
|
79
|
+
export const asAtUriString: CastFn<AtUriString> =
|
|
80
80
|
/*#__PURE__*/ createCastFunction(ensureValidAtUri)
|
|
81
|
-
export const
|
|
82
|
-
/*#__PURE__*/ createCastFunction(ensureValidNsid)
|
|
83
|
-
export const asTid: CastFn<TidString> =
|
|
84
|
-
/*#__PURE__*/ createCastFunction(ensureValidTid)
|
|
85
|
-
export const asRecordKey: CastFn<RecordKeyString> =
|
|
86
|
-
/*#__PURE__*/ createCastFunction(ensureValidRecordKey)
|
|
87
|
-
export const asDatetime: CastFn<DatetimeString> =
|
|
88
|
-
/*#__PURE__*/ createCastFunction(ensureValidDatetime)
|
|
89
|
-
export const asCidString: CastFn<string> =
|
|
81
|
+
export const asCidString: CastFn<CidString> =
|
|
90
82
|
/*#__PURE__*/ createCastFunction(ensureValidCidString)
|
|
91
|
-
export const
|
|
83
|
+
export const asDatetimeString: CastFn<DatetimeString> =
|
|
84
|
+
/*#__PURE__*/ createCastFunction(ensureValidDatetime)
|
|
85
|
+
export const asDidString: CastFn<DidString> =
|
|
86
|
+
/*#__PURE__*/ createCastFunction(ensureValidDid)
|
|
87
|
+
export const asHandleString: CastFn<HandleString> =
|
|
92
88
|
/*#__PURE__*/ createCastFunction(ensureValidHandle)
|
|
93
|
-
export const
|
|
94
|
-
/*#__PURE__*/ createCastFunction(
|
|
95
|
-
export const
|
|
96
|
-
/*#__PURE__*/ createCastFunction(
|
|
97
|
-
export const
|
|
98
|
-
/*#__PURE__*/ createCastFunction(
|
|
89
|
+
export const asLanguageString: CastFn<LanguageString> =
|
|
90
|
+
/*#__PURE__*/ createCastFunction(assertLanguageString)
|
|
91
|
+
export const asNsidString: CastFn<NsidString> =
|
|
92
|
+
/*#__PURE__*/ createCastFunction(ensureValidNsid)
|
|
93
|
+
export const asRecordKeyString: CastFn<RecordKeyString> =
|
|
94
|
+
/*#__PURE__*/ createCastFunction(ensureValidRecordKey)
|
|
95
|
+
export const asTidString: CastFn<TidString> =
|
|
96
|
+
/*#__PURE__*/ createCastFunction(ensureValidTid)
|
|
97
|
+
export const asUriString: CastFn<UriString> =
|
|
98
|
+
/*#__PURE__*/ createCastFunction(assertUriString)
|
|
99
|
+
|
|
100
|
+
export { isLanguageString }
|
|
101
|
+
export const isAtIdentifierString: CheckFn<AtIdentifierString> =
|
|
102
|
+
/*#__PURE__*/ createCheckFunction(assertAtIdentifierString)
|
|
103
|
+
export const isAtUriString: CheckFn<AtUriString> =
|
|
104
|
+
/*#__PURE__*/ createCheckFunction(assertAtIdentifierString)
|
|
105
|
+
export const isCidString: CheckFn<CidString> =
|
|
106
|
+
/*#__PURE__*/ createCheckFunction(assertCidString)
|
|
107
|
+
export const isDatetimeString: CheckFn<DatetimeString> =
|
|
108
|
+
/*#__PURE__*/ createCheckFunction(assertDatetimeString)
|
|
109
|
+
export const isDidString: CheckFn<DidString> =
|
|
110
|
+
/*#__PURE__*/ createCheckFunction(assertDidString)
|
|
111
|
+
export const isHandleString: CheckFn<HandleString> =
|
|
112
|
+
/*#__PURE__*/ createCheckFunction(assertHandleString)
|
|
113
|
+
export const isNsidString: CheckFn<NsidString> =
|
|
114
|
+
/*#__PURE__*/ createCheckFunction(assertNsidString)
|
|
115
|
+
export const isRecordKeyString: CheckFn<RecordKeyString> =
|
|
116
|
+
/*#__PURE__*/ createCheckFunction(assertRecordKeyString)
|
|
117
|
+
export const isTidString: CheckFn<TidString> =
|
|
118
|
+
/*#__PURE__*/ createCheckFunction(assertTidString)
|
|
99
119
|
|
|
100
120
|
// String formatting types and utilities
|
|
101
121
|
|
|
@@ -135,17 +155,17 @@ export type InferStringFormat<F> =
|
|
|
135
155
|
string
|
|
136
156
|
|
|
137
157
|
const formatters = /*#__PURE__*/ new Map<StringFormat, (str: string) => void>([
|
|
138
|
-
['
|
|
139
|
-
['uri',
|
|
140
|
-
['at-uri', assertAtUri],
|
|
141
|
-
['did', assertDid],
|
|
142
|
-
['handle', assertHandle],
|
|
143
|
-
['at-identifier', assertAtIdentifier],
|
|
144
|
-
['nsid', assertNsid],
|
|
158
|
+
['at-identifier', assertAtIdentifierString],
|
|
159
|
+
['at-uri', assertAtUriString],
|
|
145
160
|
['cid', assertCidString],
|
|
146
|
-
['
|
|
147
|
-
['
|
|
148
|
-
['
|
|
161
|
+
['datetime', assertDatetimeString],
|
|
162
|
+
['did', assertDidString],
|
|
163
|
+
['handle', assertHandleString],
|
|
164
|
+
['language', assertLanguageString],
|
|
165
|
+
['nsid', assertNsidString],
|
|
166
|
+
['record-key', assertRecordKeyString],
|
|
167
|
+
['tid', assertTidString],
|
|
168
|
+
['uri', assertUriString],
|
|
149
169
|
] as const)
|
|
150
170
|
|
|
151
171
|
/*@__NO_SIDE_EFFECTS__*/
|
package/src/core/types.ts
CHANGED
|
@@ -1,36 +1,44 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { LexError } from '@atproto/lex-data'
|
|
2
2
|
import { arrayAgg } from '../util/array-agg.js'
|
|
3
|
+
import { ResultFailure, failureReason } from './result.js'
|
|
3
4
|
import {
|
|
4
5
|
Issue,
|
|
5
6
|
IssueInvalidType,
|
|
6
7
|
IssueInvalidValue,
|
|
7
8
|
} from './validation-issue.js'
|
|
8
9
|
|
|
9
|
-
export class ValidationError extends
|
|
10
|
+
export class ValidationError extends LexError {
|
|
10
11
|
name = 'ValidationError'
|
|
11
12
|
|
|
12
13
|
readonly issues: Issue[]
|
|
13
14
|
|
|
14
15
|
constructor(issues: Issue[], options?: ErrorOptions) {
|
|
15
16
|
const issuesAgg = aggregateIssues(issues)
|
|
16
|
-
super(issuesAgg.join(', '), options)
|
|
17
|
+
super('InvalidRequest', issuesAgg.join(', '), options)
|
|
17
18
|
this.issues = issuesAgg
|
|
18
19
|
}
|
|
19
20
|
|
|
21
|
+
toJSON() {
|
|
22
|
+
return {
|
|
23
|
+
...super.toJSON(),
|
|
24
|
+
issues: this.issues.map((issue) => issue.toJSON()),
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
20
28
|
static fromFailures(
|
|
21
29
|
failures: ResultFailure<ValidationError>[],
|
|
22
30
|
): ValidationError {
|
|
23
|
-
if (failures.length === 1) return failures[0]
|
|
31
|
+
if (failures.length === 1) return failureReason(failures[0])
|
|
24
32
|
const issues = failures.flatMap(extractFailureIssues)
|
|
25
33
|
return new ValidationError(issues, {
|
|
26
34
|
// Keep the original errors as the cause chain
|
|
27
|
-
cause: failures.map(
|
|
35
|
+
cause: failures.map(failureReason),
|
|
28
36
|
})
|
|
29
37
|
}
|
|
30
38
|
}
|
|
31
39
|
|
|
32
40
|
function extractFailureIssues(result: ResultFailure<ValidationError>) {
|
|
33
|
-
return result.
|
|
41
|
+
return result.reason.issues
|
|
34
42
|
}
|
|
35
43
|
|
|
36
44
|
function aggregateIssues(issues: Issue[]): Issue[] {
|
|
@@ -9,6 +9,14 @@ export abstract class Issue {
|
|
|
9
9
|
) {}
|
|
10
10
|
|
|
11
11
|
abstract toString(): string
|
|
12
|
+
|
|
13
|
+
toJSON() {
|
|
14
|
+
return {
|
|
15
|
+
code: this.code,
|
|
16
|
+
path: this.path,
|
|
17
|
+
message: this.toString(),
|
|
18
|
+
}
|
|
19
|
+
}
|
|
12
20
|
}
|
|
13
21
|
|
|
14
22
|
export class IssueCustom extends Issue {
|
|
@@ -39,6 +47,13 @@ export class IssueInvalidFormat extends Issue {
|
|
|
39
47
|
return `Invalid ${this.formatDescription} format${this.message ? ` (${this.message})` : ''}${stringifyPath(this.path)} (got ${stringifyValue(this.input)})`
|
|
40
48
|
}
|
|
41
49
|
|
|
50
|
+
toJSON() {
|
|
51
|
+
return {
|
|
52
|
+
...super.toJSON(),
|
|
53
|
+
format: this.format,
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
42
57
|
get formatDescription(): string {
|
|
43
58
|
switch (this.format) {
|
|
44
59
|
case 'at-identifier':
|
|
@@ -71,6 +86,13 @@ export class IssueInvalidType extends Issue {
|
|
|
71
86
|
toString() {
|
|
72
87
|
return `Expected ${oneOf(this.expected.map(stringifyExpectedType))} value type${stringifyPath(this.path)} (got ${stringifyType(this.input)})`
|
|
73
88
|
}
|
|
89
|
+
|
|
90
|
+
toJSON() {
|
|
91
|
+
return {
|
|
92
|
+
...super.toJSON(),
|
|
93
|
+
expected: this.expected,
|
|
94
|
+
}
|
|
95
|
+
}
|
|
74
96
|
}
|
|
75
97
|
|
|
76
98
|
export class IssueInvalidValue extends Issue {
|
|
@@ -85,6 +107,13 @@ export class IssueInvalidValue extends Issue {
|
|
|
85
107
|
toString() {
|
|
86
108
|
return `Expected ${oneOf(this.values.map(stringifyValue))}${stringifyPath(this.path)} (got ${stringifyValue(this.input)})`
|
|
87
109
|
}
|
|
110
|
+
|
|
111
|
+
toJSON() {
|
|
112
|
+
return {
|
|
113
|
+
...super.toJSON(),
|
|
114
|
+
values: this.values,
|
|
115
|
+
}
|
|
116
|
+
}
|
|
88
117
|
}
|
|
89
118
|
|
|
90
119
|
export class IssueRequiredKey extends Issue {
|
|
@@ -99,6 +128,13 @@ export class IssueRequiredKey extends Issue {
|
|
|
99
128
|
toString() {
|
|
100
129
|
return `Missing required key "${String(this.key)}"${stringifyPath(this.path)}`
|
|
101
130
|
}
|
|
131
|
+
|
|
132
|
+
toJSON() {
|
|
133
|
+
return {
|
|
134
|
+
...super.toJSON(),
|
|
135
|
+
key: this.key,
|
|
136
|
+
}
|
|
137
|
+
}
|
|
102
138
|
}
|
|
103
139
|
|
|
104
140
|
export type MeasurableType =
|
|
@@ -123,6 +159,14 @@ export class IssueTooBig extends Issue {
|
|
|
123
159
|
toString() {
|
|
124
160
|
return `${this.type} too big (maximum ${this.maximum})${stringifyPath(this.path)} (got ${this.actual})`
|
|
125
161
|
}
|
|
162
|
+
|
|
163
|
+
toJSON() {
|
|
164
|
+
return {
|
|
165
|
+
...super.toJSON(),
|
|
166
|
+
type: this.type,
|
|
167
|
+
maximum: this.maximum,
|
|
168
|
+
}
|
|
169
|
+
}
|
|
126
170
|
}
|
|
127
171
|
|
|
128
172
|
export class IssueTooSmall extends Issue {
|
|
@@ -139,6 +183,14 @@ export class IssueTooSmall extends Issue {
|
|
|
139
183
|
toString() {
|
|
140
184
|
return `${this.type} too small (minimum ${this.minimum})${stringifyPath(this.path)} (got ${this.actual})`
|
|
141
185
|
}
|
|
186
|
+
|
|
187
|
+
toJSON() {
|
|
188
|
+
return {
|
|
189
|
+
...super.toJSON(),
|
|
190
|
+
type: this.type,
|
|
191
|
+
minimum: this.minimum,
|
|
192
|
+
}
|
|
193
|
+
}
|
|
142
194
|
}
|
|
143
195
|
|
|
144
196
|
function stringifyExpectedType(expected: string): string {
|
|
@@ -184,8 +236,18 @@ function stringifyType(value: unknown): string {
|
|
|
184
236
|
if (value instanceof Set) return 'set'
|
|
185
237
|
return 'object'
|
|
186
238
|
case 'number':
|
|
187
|
-
if (Number.isInteger(value))
|
|
188
|
-
|
|
239
|
+
if (Number.isInteger(value) && Number.isSafeInteger(value)) {
|
|
240
|
+
return 'integer'
|
|
241
|
+
}
|
|
242
|
+
if (Number.isNaN(value)) {
|
|
243
|
+
return 'NaN'
|
|
244
|
+
}
|
|
245
|
+
if (value === Infinity) {
|
|
246
|
+
return 'Infinity'
|
|
247
|
+
}
|
|
248
|
+
if (value === -Infinity) {
|
|
249
|
+
return '-Infinity'
|
|
250
|
+
}
|
|
189
251
|
return 'float'
|
|
190
252
|
default:
|
|
191
253
|
return typeof value
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { ResultFailure, ResultSuccess, failure, success } from '../core.js'
|
|
2
1
|
import { PropertyKey } from './property-key.js'
|
|
2
|
+
import { ResultFailure, ResultSuccess, failure, success } from './result.js'
|
|
3
3
|
import { ValidationError } from './validation-error.js'
|
|
4
4
|
import {
|
|
5
5
|
Issue,
|
|
@@ -25,7 +25,7 @@ export type ValidationOptions = {
|
|
|
25
25
|
allowTransform?: boolean
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
-
export type Infer<T extends Validator> = T['
|
|
28
|
+
export type Infer<T extends Validator> = T['__lex']['output']
|
|
29
29
|
|
|
30
30
|
export interface Validator<Output = any> {
|
|
31
31
|
/**
|
|
@@ -34,7 +34,7 @@ export interface Validator<Output = any> {
|
|
|
34
34
|
*
|
|
35
35
|
* @deprecated **INTERNAL API, DO NOT USE**.
|
|
36
36
|
*/
|
|
37
|
-
readonly ['
|
|
37
|
+
readonly ['__lex']: { output: Output }
|
|
38
38
|
|
|
39
39
|
/**
|
|
40
40
|
* @internal **INTERNAL API**: use {@link ValidatorContext.validate} instead
|
|
@@ -162,24 +162,28 @@ export class ValidatorContext {
|
|
|
162
162
|
return success(value)
|
|
163
163
|
}
|
|
164
164
|
|
|
165
|
-
failure(
|
|
166
|
-
return failure(
|
|
165
|
+
failure(reason: ValidationError): ValidationFailure {
|
|
166
|
+
return failure(reason)
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
issue(issue: Issue) {
|
|
170
|
+
return this.failure(new ValidationError([...this.issues, issue]))
|
|
167
171
|
}
|
|
168
172
|
|
|
169
173
|
issueInvalidValue(input: unknown, values: readonly unknown[]) {
|
|
170
|
-
return this.
|
|
174
|
+
return this.issue(new IssueInvalidValue(this.path, input, values))
|
|
171
175
|
}
|
|
172
176
|
|
|
173
177
|
issueInvalidType(input: unknown, expected: string) {
|
|
174
|
-
return this.
|
|
178
|
+
return this.issue(new IssueInvalidType(this.path, input, [expected]))
|
|
175
179
|
}
|
|
176
180
|
|
|
177
181
|
issueRequiredKey(input: object, key: PropertyKey) {
|
|
178
|
-
return this.
|
|
182
|
+
return this.issue(new IssueRequiredKey(this.path, input, key))
|
|
179
183
|
}
|
|
180
184
|
|
|
181
185
|
issueInvalidFormat(input: unknown, format: string, msg?: string) {
|
|
182
|
-
return this.
|
|
186
|
+
return this.issue(new IssueInvalidFormat(this.path, input, format, msg))
|
|
183
187
|
}
|
|
184
188
|
|
|
185
189
|
issueTooBig(
|
|
@@ -188,7 +192,7 @@ export class ValidatorContext {
|
|
|
188
192
|
max: number,
|
|
189
193
|
actual: number,
|
|
190
194
|
) {
|
|
191
|
-
return this.
|
|
195
|
+
return this.issue(new IssueTooBig(this.path, input, max, type, actual))
|
|
192
196
|
}
|
|
193
197
|
|
|
194
198
|
issueTooSmall(
|
|
@@ -197,7 +201,7 @@ export class ValidatorContext {
|
|
|
197
201
|
min: number,
|
|
198
202
|
actual: number,
|
|
199
203
|
) {
|
|
200
|
-
return this.
|
|
204
|
+
return this.issue(new IssueTooSmall(this.path, input, min, type, actual))
|
|
201
205
|
}
|
|
202
206
|
|
|
203
207
|
issueInvalidPropertyValue<I>(
|
|
@@ -207,7 +211,7 @@ export class ValidatorContext {
|
|
|
207
211
|
) {
|
|
208
212
|
const value = input[property]
|
|
209
213
|
const path = this.concatPath(property)
|
|
210
|
-
return this.
|
|
214
|
+
return this.issue(new IssueInvalidValue(path, value, values))
|
|
211
215
|
}
|
|
212
216
|
|
|
213
217
|
issueInvalidPropertyType<I>(
|
|
@@ -217,6 +221,6 @@ export class ValidatorContext {
|
|
|
217
221
|
) {
|
|
218
222
|
const value = input[property]
|
|
219
223
|
const path = this.concatPath(property)
|
|
220
|
-
return this.
|
|
224
|
+
return this.issue(new IssueInvalidType(path, value, [expected]))
|
|
221
225
|
}
|
|
222
226
|
}
|