@atproto/lex-schema 0.0.1 → 0.0.3
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 +68 -0
- package/dist/core/$type.d.ts +6 -3
- package/dist/core/$type.d.ts.map +1 -1
- package/dist/core/$type.js +1 -0
- package/dist/core/$type.js.map +1 -1
- package/dist/core/record-key.d.ts +3 -3
- package/dist/core/record-key.d.ts.map +1 -1
- package/dist/core/record-key.js +12 -6
- package/dist/core/record-key.js.map +1 -1
- package/dist/core/result.d.ts.map +1 -1
- package/dist/core/result.js +6 -0
- package/dist/core/result.js.map +1 -1
- package/dist/core/string-format.d.ts +30 -27
- package/dist/core/string-format.d.ts.map +1 -1
- package/dist/core/string-format.js +56 -42
- package/dist/core/string-format.js.map +1 -1
- package/dist/core/types.d.ts +9 -1
- package/dist/core/types.d.ts.map +1 -1
- package/dist/core/types.js.map +1 -1
- package/dist/external.d.ts +31 -28
- package/dist/external.d.ts.map +1 -1
- package/dist/external.js +33 -17
- package/dist/external.js.map +1 -1
- package/dist/schema/_parameters.d.ts +2 -2
- package/dist/schema/_parameters.d.ts.map +1 -1
- package/dist/schema/array.d.ts +5 -6
- package/dist/schema/array.d.ts.map +1 -1
- package/dist/schema/array.js +5 -6
- package/dist/schema/array.js.map +1 -1
- package/dist/schema/blob.d.ts +2 -3
- package/dist/schema/blob.d.ts.map +1 -1
- package/dist/schema/blob.js +1 -2
- package/dist/schema/blob.js.map +1 -1
- package/dist/schema/boolean.d.ts +4 -5
- package/dist/schema/boolean.d.ts.map +1 -1
- package/dist/schema/boolean.js +2 -3
- package/dist/schema/boolean.js.map +1 -1
- package/dist/schema/bytes.d.ts +3 -4
- package/dist/schema/bytes.d.ts.map +1 -1
- package/dist/schema/bytes.js +2 -3
- package/dist/schema/bytes.js.map +1 -1
- package/dist/schema/cid.d.ts +13 -6
- package/dist/schema/cid.d.ts.map +1 -1
- package/dist/schema/cid.js +2 -4
- package/dist/schema/cid.js.map +1 -1
- package/dist/schema/custom.d.ts +3 -4
- package/dist/schema/custom.d.ts.map +1 -1
- package/dist/schema/custom.js +4 -3
- package/dist/schema/custom.js.map +1 -1
- package/dist/schema/dict.d.ts +3 -3
- package/dist/schema/dict.d.ts.map +1 -1
- package/dist/schema/dict.js +1 -1
- package/dist/schema/dict.js.map +1 -1
- package/dist/schema/discriminated-union.d.ts +15 -24
- package/dist/schema/discriminated-union.d.ts.map +1 -1
- package/dist/schema/discriminated-union.js +40 -64
- package/dist/schema/discriminated-union.js.map +1 -1
- package/dist/schema/enum.d.ts +8 -4
- package/dist/schema/enum.d.ts.map +1 -1
- package/dist/schema/enum.js +5 -3
- package/dist/schema/enum.js.map +1 -1
- package/dist/schema/integer.d.ts +3 -4
- package/dist/schema/integer.d.ts.map +1 -1
- package/dist/schema/integer.js +3 -4
- package/dist/schema/integer.js.map +1 -1
- package/dist/schema/intersection.d.ts +22 -14
- package/dist/schema/intersection.d.ts.map +1 -1
- package/dist/schema/intersection.js +12 -22
- package/dist/schema/intersection.js.map +1 -1
- package/dist/schema/literal.d.ts +8 -4
- package/dist/schema/literal.d.ts.map +1 -1
- package/dist/schema/literal.js +5 -3
- package/dist/schema/literal.js.map +1 -1
- package/dist/schema/never.d.ts +2 -2
- package/dist/schema/never.d.ts.map +1 -1
- package/dist/schema/never.js +1 -1
- package/dist/schema/never.js.map +1 -1
- package/dist/schema/null.d.ts +2 -3
- package/dist/schema/null.d.ts.map +1 -1
- package/dist/schema/null.js +1 -2
- package/dist/schema/null.js.map +1 -1
- package/dist/schema/nullable.d.ts +7 -0
- package/dist/schema/nullable.d.ts.map +1 -0
- package/dist/schema/nullable.js +19 -0
- package/dist/schema/nullable.js.map +1 -0
- package/dist/schema/object.d.ts +10 -44
- package/dist/schema/object.d.ts.map +1 -1
- package/dist/schema/object.js +13 -56
- package/dist/schema/object.js.map +1 -1
- package/dist/schema/optional.d.ts +7 -0
- package/dist/schema/optional.d.ts.map +1 -0
- package/dist/schema/optional.js +25 -0
- package/dist/schema/optional.js.map +1 -0
- package/dist/schema/params.d.ts +14 -19
- package/dist/schema/params.d.ts.map +1 -1
- package/dist/schema/params.js +10 -24
- package/dist/schema/params.js.map +1 -1
- package/dist/schema/payload.d.ts +4 -4
- package/dist/schema/payload.d.ts.map +1 -1
- package/dist/schema/payload.js.map +1 -1
- package/dist/schema/permission-set.d.ts +6 -6
- package/dist/schema/permission-set.d.ts.map +1 -1
- package/dist/schema/permission-set.js +1 -2
- package/dist/schema/permission-set.js.map +1 -1
- package/dist/schema/permission.d.ts +0 -1
- package/dist/schema/permission.d.ts.map +1 -1
- package/dist/schema/permission.js +0 -1
- package/dist/schema/permission.js.map +1 -1
- package/dist/schema/procedure.d.ts +8 -9
- package/dist/schema/procedure.d.ts.map +1 -1
- package/dist/schema/procedure.js +0 -1
- package/dist/schema/procedure.js.map +1 -1
- package/dist/schema/query.d.ts +7 -8
- package/dist/schema/query.d.ts.map +1 -1
- package/dist/schema/query.js +0 -1
- package/dist/schema/query.js.map +1 -1
- package/dist/schema/record.d.ts +34 -28
- package/dist/schema/record.d.ts.map +1 -1
- package/dist/schema/record.js +1 -2
- package/dist/schema/record.js.map +1 -1
- package/dist/schema/ref.d.ts +2 -3
- package/dist/schema/ref.d.ts.map +1 -1
- package/dist/schema/ref.js +1 -2
- package/dist/schema/ref.js.map +1 -1
- package/dist/schema/refine.d.ts +18 -0
- package/dist/schema/refine.d.ts.map +1 -0
- package/dist/schema/refine.js +33 -0
- package/dist/schema/refine.js.map +1 -0
- package/dist/schema/regexp.d.ts +7 -0
- package/dist/schema/regexp.d.ts.map +1 -0
- package/dist/schema/regexp.js +22 -0
- package/dist/schema/regexp.js.map +1 -0
- package/dist/schema/string.d.ts +4 -8
- package/dist/schema/string.d.ts.map +1 -1
- package/dist/schema/string.js +6 -3
- package/dist/schema/string.js.map +1 -1
- package/dist/schema/subscription.d.ts +7 -6
- package/dist/schema/subscription.d.ts.map +1 -1
- package/dist/schema/subscription.js.map +1 -1
- package/dist/schema/token.d.ts +2 -3
- package/dist/schema/token.d.ts.map +1 -1
- package/dist/schema/token.js +1 -2
- package/dist/schema/token.js.map +1 -1
- package/dist/schema/typed-object.d.ts +29 -27
- package/dist/schema/typed-object.d.ts.map +1 -1
- package/dist/schema/typed-object.js +1 -2
- package/dist/schema/typed-object.js.map +1 -1
- package/dist/schema/typed-ref.d.ts +2 -2
- package/dist/schema/typed-ref.d.ts.map +1 -1
- package/dist/schema/typed-ref.js +1 -1
- package/dist/schema/typed-ref.js.map +1 -1
- package/dist/schema/typed-union.d.ts +3 -4
- package/dist/schema/typed-union.d.ts.map +1 -1
- package/dist/schema/typed-union.js +3 -10
- package/dist/schema/typed-union.js.map +1 -1
- package/dist/schema/union.d.ts +2 -2
- package/dist/schema/union.d.ts.map +1 -1
- package/dist/schema/union.js +1 -1
- package/dist/schema/union.js.map +1 -1
- package/dist/schema/unknown-object.d.ts +2 -3
- package/dist/schema/unknown-object.d.ts.map +1 -1
- package/dist/schema/unknown-object.js +1 -2
- package/dist/schema/unknown-object.js.map +1 -1
- package/dist/schema/unknown.d.ts +2 -2
- package/dist/schema/unknown.d.ts.map +1 -1
- package/dist/schema/unknown.js +1 -1
- package/dist/schema/unknown.js.map +1 -1
- package/dist/schema.d.ts +4 -0
- package/dist/schema.d.ts.map +1 -1
- package/dist/schema.js +6 -1
- package/dist/schema.js.map +1 -1
- package/dist/util/array-agg.d.ts.map +1 -1
- package/dist/util/array-agg.js +1 -0
- package/dist/util/array-agg.js.map +1 -1
- package/dist/util/lazy-property.d.ts +2 -0
- package/dist/util/lazy-property.d.ts.map +1 -0
- package/dist/util/lazy-property.js +14 -0
- package/dist/util/lazy-property.js.map +1 -0
- package/dist/validation/schema.d.ts +24 -0
- package/dist/validation/schema.d.ts.map +1 -0
- package/dist/validation/schema.js +57 -0
- package/dist/validation/schema.js.map +1 -0
- package/dist/validation/validation-error.d.ts +3 -3
- package/dist/validation/validation-error.d.ts.map +1 -1
- package/dist/validation/validation-error.js +32 -4
- package/dist/validation/validation-error.js.map +1 -1
- package/dist/validation/validation-issue.d.ts +32 -24
- package/dist/validation/validation-issue.d.ts.map +1 -1
- package/dist/validation/validation-issue.js +136 -92
- package/dist/validation/validation-issue.js.map +1 -1
- package/dist/validation/validator.d.ts +20 -50
- package/dist/validation/validator.d.ts.map +1 -1
- package/dist/validation/validator.js +40 -134
- package/dist/validation/validator.js.map +1 -1
- package/dist/validation.d.ts +1 -0
- package/dist/validation.d.ts.map +1 -1
- package/dist/validation.js +1 -0
- package/dist/validation.js.map +1 -1
- package/package.json +8 -4
- package/src/core/$type.ts +7 -4
- package/src/core/record-key.ts +12 -5
- package/src/core/result.ts +6 -0
- package/src/core/string-format.ts +97 -61
- package/src/core/types.ts +12 -6
- package/src/external.ts +92 -70
- package/src/schema/_parameters.test.ts +416 -0
- package/src/schema/array.test.ts +237 -0
- package/src/schema/array.ts +17 -11
- package/src/schema/blob.test.ts +506 -0
- package/src/schema/blob.ts +3 -5
- package/src/schema/boolean.test.ts +116 -0
- package/src/schema/boolean.ts +5 -7
- package/src/schema/bytes.test.ts +226 -0
- package/src/schema/bytes.ts +4 -6
- package/src/schema/cid.test.ts +155 -0
- package/src/schema/cid.ts +14 -8
- package/src/schema/custom.test.ts +413 -0
- package/src/schema/custom.ts +10 -8
- package/src/schema/dict.test.ts +198 -0
- package/src/schema/dict.ts +6 -8
- package/src/schema/discriminated-union.test.ts +675 -0
- package/src/schema/discriminated-union.ts +68 -95
- package/src/schema/enum.test.ts +396 -0
- package/src/schema/enum.ts +12 -5
- package/src/schema/integer.test.ts +312 -0
- package/src/schema/integer.ts +5 -7
- package/src/schema/intersection.test.ts +32 -0
- package/src/schema/intersection.ts +37 -40
- package/src/schema/literal.test.ts +531 -0
- package/src/schema/literal.ts +12 -5
- package/src/schema/never.test.ts +174 -0
- package/src/schema/never.ts +3 -10
- package/src/schema/null.test.ts +79 -0
- package/src/schema/null.ts +3 -5
- package/src/schema/nullable.test.ts +480 -0
- package/src/schema/nullable.ts +23 -0
- package/src/schema/object.test.ts +47 -115
- package/src/schema/object.ts +23 -134
- package/src/schema/optional.test.ts +485 -0
- package/src/schema/optional.ts +31 -0
- package/src/schema/params.test.ts +582 -0
- package/src/schema/params.ts +37 -55
- package/src/schema/payload.test.ts +345 -0
- package/src/schema/payload.ts +5 -5
- package/src/schema/permission-set.test.ts +679 -0
- package/src/schema/permission-set.ts +6 -8
- package/src/schema/permission.test.ts +536 -0
- package/src/schema/permission.ts +0 -2
- package/src/schema/procedure.test.ts +443 -0
- package/src/schema/procedure.ts +11 -13
- package/src/schema/query.test.ts +408 -0
- package/src/schema/query.ts +9 -11
- package/src/schema/record.test.ts +694 -0
- package/src/schema/record.ts +38 -36
- package/src/schema/ref.test.ts +365 -0
- package/src/schema/ref.ts +8 -5
- package/src/schema/refine.test.ts +578 -0
- package/src/schema/refine.ts +85 -0
- package/src/schema/regexp.test.ts +580 -0
- package/src/schema/regexp.ts +22 -0
- package/src/schema/string.test.ts +612 -0
- package/src/schema/string.ts +11 -17
- package/src/schema/subscription.test.ts +689 -0
- package/src/schema/subscription.ts +13 -8
- package/src/schema/token.test.ts +428 -0
- package/src/schema/token.ts +3 -5
- package/src/schema/typed-object.test.ts +612 -0
- package/src/schema/typed-object.ts +23 -20
- package/src/schema/typed-ref.test.ts +823 -0
- package/src/schema/typed-ref.ts +10 -5
- package/src/schema/typed-union.test.ts +378 -0
- package/src/schema/typed-union.ts +6 -15
- package/src/schema/union.test.ts +200 -0
- package/src/schema/union.ts +5 -4
- package/src/schema/unknown-object.test.ts +592 -0
- package/src/schema/unknown-object.ts +3 -5
- package/src/schema/unknown.test.ts +312 -0
- package/src/schema/unknown.ts +3 -3
- package/src/schema.ts +7 -1
- package/src/util/array-agg.ts +1 -0
- package/src/util/lazy-property.ts +14 -0
- package/src/validation/schema.ts +92 -0
- package/src/validation/validation-error.ts +60 -9
- package/src/validation/validation-issue.ts +141 -144
- package/src/validation/validator.ts +67 -206
- package/src/validation.ts +1 -0
- package/tsconfig.build.json +12 -0
- package/tsconfig.json +7 -0
- package/tsconfig.tests.json +9 -0
|
@@ -2,31 +2,21 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.IntersectionSchema = void 0;
|
|
4
4
|
const validation_js_1 = require("../validation.js");
|
|
5
|
-
class IntersectionSchema extends validation_js_1.
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
class IntersectionSchema extends validation_js_1.Schema {
|
|
6
|
+
left;
|
|
7
|
+
right;
|
|
8
|
+
constructor(left, right) {
|
|
8
9
|
super();
|
|
9
|
-
this.
|
|
10
|
+
this.left = left;
|
|
11
|
+
this.right = right;
|
|
10
12
|
}
|
|
11
13
|
validateInContext(input, ctx) {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
// validators, we need to ensure the input remains unchanged only gets
|
|
19
|
-
// transformed by the first validator.
|
|
20
|
-
if (i !== 0 && input !== result.value) {
|
|
21
|
-
// The alternative would be to allow transforms on a first pass
|
|
22
|
-
// (ignoring errors) and then re-validate the final value against all
|
|
23
|
-
// validators (without allowing further transforms). This would be way
|
|
24
|
-
// less efficient (we could make this optional).
|
|
25
|
-
return ctx.issueInvalidValue(input, [result.value]);
|
|
26
|
-
}
|
|
27
|
-
input = result.value;
|
|
28
|
-
}
|
|
29
|
-
return ctx.success(input);
|
|
14
|
+
const leftResult = ctx.validate(input, this.left);
|
|
15
|
+
if (!leftResult.success)
|
|
16
|
+
return leftResult;
|
|
17
|
+
return this.right.validateInContext(leftResult.value, ctx, {
|
|
18
|
+
ignoredKeys: this.left.validatorsMap,
|
|
19
|
+
});
|
|
30
20
|
}
|
|
31
21
|
}
|
|
32
22
|
exports.IntersectionSchema = IntersectionSchema;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"intersection.js","sourceRoot":"","sources":["../../src/schema/intersection.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"intersection.js","sourceRoot":"","sources":["../../src/schema/intersection.ts"],"names":[],"mappings":";;;AACA,oDAKyB;AAyBzB,MAAa,kBAGX,SAAQ,sBAA6C;IAEhC;IACA;IAFrB,YACqB,IAAU,EACV,KAAY;QAE/B,KAAK,EAAE,CAAA;QAHY,SAAI,GAAJ,IAAI,CAAM;QACV,UAAK,GAAL,KAAK,CAAO;IAGjC,CAAC;IAED,iBAAiB,CACf,KAAc,EACd,GAAqB;QAErB,MAAM,UAAU,GAAG,GAAG,CAAC,QAAQ,CAAC,KAAK,EAAE,IAAI,CAAC,IAAI,CAAC,CAAA;QACjD,IAAI,CAAC,UAAU,CAAC,OAAO;YAAE,OAAO,UAAU,CAAA;QAE1C,OAAO,IAAI,CAAC,KAAK,CAAC,iBAAiB,CAAC,UAAU,CAAC,KAAK,EAAE,GAAG,EAAE;YACzD,WAAW,EAAE,IAAI,CAAC,IAAI,CAAC,aAAa;SACrC,CAA4D,CAAA;IAC/D,CAAC;CACF;AAtBD,gDAsBC","sourcesContent":["import { Simplify } from '../core.js'\nimport {\n Infer,\n Schema,\n ValidationResult,\n ValidatorContext,\n} from '../validation.js'\nimport { DictSchema } from './dict.js'\nimport { ObjectSchema } from './object.js'\n\n/**\n * Allows to more accurately represent the intersection of two object types\n * where both types may share some keys, and one of them uses an index\n * signature.\n *\n * @see {@link https://www.typescriptlang.org/play/?#code/C4TwDgpgBAglC8UDeUBmB7dAuKByARgIYBOuUAvlAGTJQDaA+lAJYB2UAzsMWwOYC6OVgFcAtvgjEKAKGkATCAGMANiWiL0rLlEI4YsjVuBQA1hBA4uPVrwRQARBnT2Dm7QDdCy4dESE6ZiD8UAD0IVAi4pJQABQcABbowspyUBIORMT2AJSyEAAeYOjExqCQUACSrMCSHErAzJoAPNJQsFAFNaxyHFAASkrFck1WfAA0UMKsJqzoAO6sAHxjrVAAQh35XT39g8TDozYTUzPzSyuLdqtwVKttMYHoqO00j88bnRDdvawQ7pJ3NpQAD860BbRwSHBQLadAA0ix2G91oJ1vDggAfWABcxPF5QOH8aFtci5aRlaAwVDMfIQVKIKo1Yh1RQNZq0Jw4AgkMjkCYoRiIzjcPioyISKTkRayBQqNRQQzaQgAMRpdL01NpclcRignm8EFVWrsKrVchxQVC4XF0SxmSAA Playground link}\n */\nexport type Intersect<A, B> = B[keyof B] extends never\n ? A\n : keyof A & keyof B extends never\n ? // If A and B don't overlap, just return A & B\n A & B\n : // Otherwise, properly represent the fact that accessing using an\n // index signature could return a value from either A or B\n A & { [K in keyof B]: B[K] | A[keyof A & K] }\n\nexport type IntersectionSchemaOutput<\n Left extends ObjectSchema,\n Right extends DictSchema,\n> = Simplify<Intersect<Infer<Left>, Infer<Right>>>\n\nexport class IntersectionSchema<\n const Left extends ObjectSchema = any,\n const Right extends DictSchema = any,\n> extends Schema<IntersectionSchemaOutput<Left, Right>> {\n constructor(\n protected readonly left: Left,\n protected readonly right: Right,\n ) {\n super()\n }\n\n validateInContext(\n input: unknown,\n ctx: ValidatorContext,\n ): ValidationResult<IntersectionSchemaOutput<Left, Right>> {\n const leftResult = ctx.validate(input, this.left)\n if (!leftResult.success) return leftResult\n\n return this.right.validateInContext(leftResult.value, ctx, {\n ignoredKeys: this.left.validatorsMap,\n }) as ValidationResult<IntersectionSchemaOutput<Left, Right>>\n }\n}\n"]}
|
package/dist/schema/literal.d.ts
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export
|
|
1
|
+
import { Schema, ValidationResult, ValidatorContext } from '../validation.js';
|
|
2
|
+
export type LiteralSchemaOptions<T extends null | string | number | boolean> = {
|
|
3
|
+
default?: T;
|
|
4
|
+
};
|
|
5
|
+
export declare class LiteralSchema<Output extends null | string | number | boolean = any> extends Schema<Output> {
|
|
3
6
|
readonly value: Output;
|
|
4
|
-
|
|
5
|
-
|
|
7
|
+
readonly options?: LiteralSchemaOptions<Output> | undefined;
|
|
8
|
+
constructor(value: Output, options?: LiteralSchemaOptions<Output> | undefined);
|
|
9
|
+
validateInContext(input: unknown | undefined, ctx: ValidatorContext): ValidationResult<Output>;
|
|
6
10
|
}
|
|
7
11
|
//# sourceMappingURL=literal.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"literal.d.ts","sourceRoot":"","sources":["../../src/schema/literal.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"literal.d.ts","sourceRoot":"","sources":["../../src/schema/literal.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,gBAAgB,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAA;AAE7E,MAAM,MAAM,oBAAoB,CAAC,CAAC,SAAS,IAAI,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,IAAI;IAC7E,OAAO,CAAC,EAAE,CAAC,CAAA;CACZ,CAAA;AAED,qBAAa,aAAa,CACxB,MAAM,SAAS,IAAI,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,GAAG,CACrD,SAAQ,MAAM,CAAC,MAAM,CAAC;IAEpB,QAAQ,CAAC,KAAK,EAAE,MAAM;IACtB,QAAQ,CAAC,OAAO,CAAC,EAAE,oBAAoB,CAAC,MAAM,CAAC;gBADtC,KAAK,EAAE,MAAM,EACb,OAAO,CAAC,EAAE,oBAAoB,CAAC,MAAM,CAAC,YAAA;IAKjD,iBAAiB,CACf,KAAK,EAAE,OAAO,YAAwB,EACtC,GAAG,EAAE,gBAAgB,GACpB,gBAAgB,CAAC,MAAM,CAAC;CAO5B"}
|
package/dist/schema/literal.js
CHANGED
|
@@ -2,13 +2,15 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.LiteralSchema = void 0;
|
|
4
4
|
const validation_js_1 = require("../validation.js");
|
|
5
|
-
class LiteralSchema extends validation_js_1.
|
|
5
|
+
class LiteralSchema extends validation_js_1.Schema {
|
|
6
6
|
value;
|
|
7
|
-
|
|
7
|
+
options;
|
|
8
|
+
constructor(value, options) {
|
|
8
9
|
super();
|
|
9
10
|
this.value = value;
|
|
11
|
+
this.options = options;
|
|
10
12
|
}
|
|
11
|
-
validateInContext(input, ctx) {
|
|
13
|
+
validateInContext(input = this.options?.default, ctx) {
|
|
12
14
|
if (input !== this.value) {
|
|
13
15
|
return ctx.issueInvalidValue(input, [this.value]);
|
|
14
16
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"literal.js","sourceRoot":"","sources":["../../src/schema/literal.ts"],"names":[],"mappings":";;;AAAA,
|
|
1
|
+
{"version":3,"file":"literal.js","sourceRoot":"","sources":["../../src/schema/literal.ts"],"names":[],"mappings":";;;AAAA,oDAA6E;AAM7E,MAAa,aAEX,SAAQ,sBAAc;IAEX;IACA;IAFX,YACW,KAAa,EACb,OAAsC;QAE/C,KAAK,EAAE,CAAA;QAHE,UAAK,GAAL,KAAK,CAAQ;QACb,YAAO,GAAP,OAAO,CAA+B;IAGjD,CAAC;IAED,iBAAiB,CACf,QAAiB,IAAI,CAAC,OAAO,EAAE,OAAO,EACtC,GAAqB;QAErB,IAAI,KAAK,KAAK,IAAI,CAAC,KAAK,EAAE,CAAC;YACzB,OAAO,GAAG,CAAC,iBAAiB,CAAC,KAAK,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAA;QACnD,CAAC;QAED,OAAO,GAAG,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;IAChC,CAAC;CACF;AApBD,sCAoBC","sourcesContent":["import { Schema, ValidationResult, ValidatorContext } from '../validation.js'\n\nexport type LiteralSchemaOptions<T extends null | string | number | boolean> = {\n default?: T\n}\n\nexport class LiteralSchema<\n Output extends null | string | number | boolean = any,\n> extends Schema<Output> {\n constructor(\n readonly value: Output,\n readonly options?: LiteralSchemaOptions<Output>,\n ) {\n super()\n }\n\n validateInContext(\n input: unknown = this.options?.default,\n ctx: ValidatorContext,\n ): ValidationResult<Output> {\n if (input !== this.value) {\n return ctx.issueInvalidValue(input, [this.value])\n }\n\n return ctx.success(this.value)\n }\n}\n"]}
|
package/dist/schema/never.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export declare class NeverSchema extends
|
|
1
|
+
import { Schema, ValidationFailure, ValidatorContext } from '../validation.js';
|
|
2
|
+
export declare class NeverSchema extends Schema<never> {
|
|
3
3
|
validateInContext(input: unknown, ctx: ValidatorContext): ValidationFailure;
|
|
4
4
|
}
|
|
5
5
|
//# sourceMappingURL=never.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"never.d.ts","sourceRoot":"","sources":["../../src/schema/never.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"never.d.ts","sourceRoot":"","sources":["../../src/schema/never.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,iBAAiB,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAA;AAE9E,qBAAa,WAAY,SAAQ,MAAM,CAAC,KAAK,CAAC;IAC5C,iBAAiB,CAAC,KAAK,EAAE,OAAO,EAAE,GAAG,EAAE,gBAAgB,GAAG,iBAAiB;CAG5E"}
|
package/dist/schema/never.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.NeverSchema = void 0;
|
|
4
4
|
const validation_js_1 = require("../validation.js");
|
|
5
|
-
class NeverSchema extends validation_js_1.
|
|
5
|
+
class NeverSchema extends validation_js_1.Schema {
|
|
6
6
|
validateInContext(input, ctx) {
|
|
7
7
|
return ctx.issueInvalidType(input, 'never');
|
|
8
8
|
}
|
package/dist/schema/never.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"never.js","sourceRoot":"","sources":["../../src/schema/never.ts"],"names":[],"mappings":";;;AAAA,
|
|
1
|
+
{"version":3,"file":"never.js","sourceRoot":"","sources":["../../src/schema/never.ts"],"names":[],"mappings":";;;AAAA,oDAA8E;AAE9E,MAAa,WAAY,SAAQ,sBAAa;IAC5C,iBAAiB,CAAC,KAAc,EAAE,GAAqB;QACrD,OAAO,GAAG,CAAC,gBAAgB,CAAC,KAAK,EAAE,OAAO,CAAC,CAAA;IAC7C,CAAC;CACF;AAJD,kCAIC","sourcesContent":["import { Schema, ValidationFailure, ValidatorContext } from '../validation.js'\n\nexport class NeverSchema extends Schema<never> {\n validateInContext(input: unknown, ctx: ValidatorContext): ValidationFailure {\n return ctx.issueInvalidType(input, 'never')\n }\n}\n"]}
|
package/dist/schema/null.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export declare class NullSchema extends
|
|
3
|
-
readonly lexiconType: "null";
|
|
1
|
+
import { Schema, ValidationResult, ValidatorContext } from '../validation.js';
|
|
2
|
+
export declare class NullSchema extends Schema<null> {
|
|
4
3
|
constructor();
|
|
5
4
|
validateInContext(input: unknown, ctx: ValidatorContext): ValidationResult<null>;
|
|
6
5
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"null.d.ts","sourceRoot":"","sources":["../../src/schema/null.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"null.d.ts","sourceRoot":"","sources":["../../src/schema/null.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,gBAAgB,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAA;AAE7E,qBAAa,UAAW,SAAQ,MAAM,CAAC,IAAI,CAAC;;IAK1C,iBAAiB,CACf,KAAK,EAAE,OAAO,EACd,GAAG,EAAE,gBAAgB,GACpB,gBAAgB,CAAC,IAAI,CAAC;CAO1B"}
|
package/dist/schema/null.js
CHANGED
|
@@ -2,8 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.NullSchema = void 0;
|
|
4
4
|
const validation_js_1 = require("../validation.js");
|
|
5
|
-
class NullSchema extends validation_js_1.
|
|
6
|
-
lexiconType = 'null';
|
|
5
|
+
class NullSchema extends validation_js_1.Schema {
|
|
7
6
|
constructor() {
|
|
8
7
|
super();
|
|
9
8
|
}
|
package/dist/schema/null.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"null.js","sourceRoot":"","sources":["../../src/schema/null.ts"],"names":[],"mappings":";;;AAAA,
|
|
1
|
+
{"version":3,"file":"null.js","sourceRoot":"","sources":["../../src/schema/null.ts"],"names":[],"mappings":";;;AAAA,oDAA6E;AAE7E,MAAa,UAAW,SAAQ,sBAAY;IAC1C;QACE,KAAK,EAAE,CAAA;IACT,CAAC;IAED,iBAAiB,CACf,KAAc,EACd,GAAqB;QAErB,IAAI,KAAK,KAAK,IAAI,EAAE,CAAC;YACnB,OAAO,GAAG,CAAC,gBAAgB,CAAC,KAAK,EAAE,MAAM,CAAC,CAAA;QAC5C,CAAC;QAED,OAAO,GAAG,CAAC,OAAO,CAAC,IAAI,CAAC,CAAA;IAC1B,CAAC;CACF;AAfD,gCAeC","sourcesContent":["import { Schema, ValidationResult, ValidatorContext } from '../validation.js'\n\nexport class NullSchema extends Schema<null> {\n constructor() {\n super()\n }\n\n validateInContext(\n input: unknown,\n ctx: ValidatorContext,\n ): ValidationResult<null> {\n if (input !== null) {\n return ctx.issueInvalidType(input, 'null')\n }\n\n return ctx.success(null)\n }\n}\n"]}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { Schema, ValidationResult, Validator, ValidatorContext } from '../validation.js';
|
|
2
|
+
export declare class NullableSchema<V> extends Schema<V | null> {
|
|
3
|
+
readonly schema: Validator<V>;
|
|
4
|
+
constructor(schema: Validator<V>);
|
|
5
|
+
validateInContext(input: unknown, ctx: ValidatorContext): ValidationResult<V | null>;
|
|
6
|
+
}
|
|
7
|
+
//# sourceMappingURL=nullable.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"nullable.d.ts","sourceRoot":"","sources":["../../src/schema/nullable.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,MAAM,EACN,gBAAgB,EAChB,SAAS,EACT,gBAAgB,EACjB,MAAM,kBAAkB,CAAA;AAEzB,qBAAa,cAAc,CAAC,CAAC,CAAE,SAAQ,MAAM,CAAC,CAAC,GAAG,IAAI,CAAC;IACzC,QAAQ,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC,CAAC;gBAApB,MAAM,EAAE,SAAS,CAAC,CAAC,CAAC;IAIzC,iBAAiB,CACf,KAAK,EAAE,OAAO,EACd,GAAG,EAAE,gBAAgB,GACpB,gBAAgB,CAAC,CAAC,GAAG,IAAI,CAAC;CAO9B"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.NullableSchema = void 0;
|
|
4
|
+
const validation_js_1 = require("../validation.js");
|
|
5
|
+
class NullableSchema extends validation_js_1.Schema {
|
|
6
|
+
schema;
|
|
7
|
+
constructor(schema) {
|
|
8
|
+
super();
|
|
9
|
+
this.schema = schema;
|
|
10
|
+
}
|
|
11
|
+
validateInContext(input, ctx) {
|
|
12
|
+
if (input === null) {
|
|
13
|
+
return ctx.success(null);
|
|
14
|
+
}
|
|
15
|
+
return ctx.validate(input, this.schema);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
exports.NullableSchema = NullableSchema;
|
|
19
|
+
//# sourceMappingURL=nullable.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"nullable.js","sourceRoot":"","sources":["../../src/schema/nullable.ts"],"names":[],"mappings":";;;AAAA,oDAKyB;AAEzB,MAAa,cAAkB,SAAQ,sBAAgB;IAChC;IAArB,YAAqB,MAAoB;QACvC,KAAK,EAAE,CAAA;QADY,WAAM,GAAN,MAAM,CAAc;IAEzC,CAAC;IAED,iBAAiB,CACf,KAAc,EACd,GAAqB;QAErB,IAAI,KAAK,KAAK,IAAI,EAAE,CAAC;YACnB,OAAO,GAAG,CAAC,OAAO,CAAC,IAAI,CAAC,CAAA;QAC1B,CAAC;QAED,OAAO,GAAG,CAAC,QAAQ,CAAC,KAAK,EAAE,IAAI,CAAC,MAAM,CAAC,CAAA;IACzC,CAAC;CACF;AAfD,wCAeC","sourcesContent":["import {\n Schema,\n ValidationResult,\n Validator,\n ValidatorContext,\n} from '../validation.js'\n\nexport class NullableSchema<V> extends Schema<V | null> {\n constructor(readonly schema: Validator<V>) {\n super()\n }\n\n validateInContext(\n input: unknown,\n ctx: ValidatorContext,\n ): ValidationResult<V | null> {\n if (input === null) {\n return ctx.success(null)\n }\n\n return ctx.validate(input, this.schema)\n }\n}\n"]}
|
package/dist/schema/object.d.ts
CHANGED
|
@@ -1,47 +1,13 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { Infer, ValidationResult, Validator, ValidatorContext } from '../validation.js';
|
|
3
|
-
|
|
4
|
-
export type
|
|
5
|
-
[
|
|
6
|
-
}
|
|
7
|
-
export
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
unknownProperties?: 'strict' | DictSchema;
|
|
11
|
-
};
|
|
12
|
-
export type ObjectSchemaNullValue<O extends ObjectSchemaOptions, K extends string> = O extends {
|
|
13
|
-
nullable: readonly (infer N extends string)[];
|
|
14
|
-
} ? K extends N ? null : never : never;
|
|
15
|
-
export type ObjectSchemaPropertiesOutput<P extends ObjectSchemaProperties, O extends ObjectSchemaOptions> = O extends {
|
|
16
|
-
required: readonly (infer R extends string)[];
|
|
17
|
-
} ? {
|
|
18
|
-
-readonly [K in string & keyof P & R]-?: Infer<P[K]> | ObjectSchemaNullValue<O, K>;
|
|
19
|
-
} & {
|
|
20
|
-
-readonly [K in Exclude<string & keyof P, R>]?: Infer<P[K]> | ObjectSchemaNullValue<O, K>;
|
|
21
|
-
} : {
|
|
22
|
-
-readonly [K in string & keyof P]?: Infer<P[K]> | ObjectSchemaNullValue<O, K>;
|
|
23
|
-
};
|
|
24
|
-
/**
|
|
25
|
-
* Allows to more accurately represent the intersection of two object types
|
|
26
|
-
* where both types may share some keys, and one of them uses an index
|
|
27
|
-
* signature.
|
|
28
|
-
*
|
|
29
|
-
* @see {@link https://www.typescriptlang.org/play/?#code/C4TwDgpgBAglC8UDeUBmB7dAuKByARgIYBOuUAvlAGTJQDaA+lAJYB2UAzsMWwOYC6OVgFcAtvgjEKAKGkATCAGMANiWiL0rLlEI4YsjVuBQA1hBA4uPVrwRQARBnT2Dm7QDdCy4dESE6ZiD8UAD0IVAi4pJQABQcABbowspyUBIORMT2AJSyEAAeYOjExqCQUACSrMCSHErAzJoAPNJQsFAFNaxyHFAASkrFck1WfAA0UMKsJqzoAO6sAHxjrVAAQh35XT39g8TDozYTUzPzSyuLdqtwVKttMYHoqO00j88bnRDdvawQ7pJ3NpQAD860BbRwSHBQLadAA0ix2G91oJ1vDggAfWABcxPF5QOH8aFtci5aRlaAwVDMfIQVKIKo1Yh1RQNZq0Jw4AgkMjkCYoRiIzjcPioyISKTkRayBQqNRQQzaQgAMRpdL01NpclcRignm8EFVWrsKrVchxQVC4XF0SxmSAA Playground link}
|
|
30
|
-
*/
|
|
31
|
-
type Intersect<A extends Record<string, unknown>, B extends Record<string, unknown>> = B[keyof B] extends never ? A : keyof A & keyof B extends never ? // If A and B don't overlap, just return A & B
|
|
32
|
-
A & B : // Otherwise, properly represent the fact that accessing using an
|
|
33
|
-
A & {
|
|
34
|
-
[K in keyof B]: B[K] | A[keyof A & K];
|
|
35
|
-
};
|
|
36
|
-
export type ObjectSchemaOutput<P extends ObjectSchemaProperties, O extends ObjectSchemaOptions> = O extends {
|
|
37
|
-
unknownProperties: Validator<infer D extends Record<string, unknown>>;
|
|
38
|
-
} ? Simplify<Intersect<ObjectSchemaPropertiesOutput<P, O>, D>> : Simplify<ObjectSchemaPropertiesOutput<P, O>>;
|
|
39
|
-
export declare class ObjectSchema<const Validators extends ObjectSchemaProperties = any, const Options extends ObjectSchemaOptions = any, const Output extends ObjectSchemaOutput<Validators, Options> = ObjectSchemaOutput<Validators, Options>> extends Validator<Output> {
|
|
40
|
-
readonly validators: Validators;
|
|
41
|
-
readonly options: Options;
|
|
42
|
-
constructor(validators: Validators, options: Options);
|
|
1
|
+
import { WithOptionalProperties } from '../core.js';
|
|
2
|
+
import { Infer, Schema, ValidationResult, Validator, ValidatorContext } from '../validation.js';
|
|
3
|
+
export type ObjectSchemaShape = Record<string, Validator>;
|
|
4
|
+
export type ObjectSchemaOutput<Shape extends ObjectSchemaShape> = WithOptionalProperties<{
|
|
5
|
+
[K in keyof Shape]: Infer<Shape[K]>;
|
|
6
|
+
}>;
|
|
7
|
+
export declare class ObjectSchema<const Shape extends ObjectSchemaShape = any> extends Schema<ObjectSchemaOutput<Shape>> {
|
|
8
|
+
readonly shape: Shape;
|
|
9
|
+
constructor(shape: Shape);
|
|
43
10
|
get validatorsMap(): Map<string, Validator>;
|
|
44
|
-
validateInContext(input: unknown, ctx: ValidatorContext): ValidationResult<
|
|
11
|
+
validateInContext(input: unknown, ctx: ValidatorContext): ValidationResult<ObjectSchemaOutput<Shape>>;
|
|
45
12
|
}
|
|
46
|
-
export {};
|
|
47
13
|
//# sourceMappingURL=object.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"object.d.ts","sourceRoot":"","sources":["../../src/schema/object.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"object.d.ts","sourceRoot":"","sources":["../../src/schema/object.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,sBAAsB,EAAE,MAAM,YAAY,CAAA;AAEnD,OAAO,EACL,KAAK,EACL,MAAM,EACN,gBAAgB,EAChB,SAAS,EACT,gBAAgB,EACjB,MAAM,kBAAkB,CAAA;AAEzB,MAAM,MAAM,iBAAiB,GAAG,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,CAAA;AAEzD,MAAM,MAAM,kBAAkB,CAAC,KAAK,SAAS,iBAAiB,IAC5D,sBAAsB,CAAC;KACpB,CAAC,IAAI,MAAM,KAAK,GAAG,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;CACpC,CAAC,CAAA;AAEJ,qBAAa,YAAY,CACvB,KAAK,CAAC,KAAK,SAAS,iBAAiB,GAAG,GAAG,CAC3C,SAAQ,MAAM,CAAC,kBAAkB,CAAC,KAAK,CAAC,CAAC;IAC7B,QAAQ,CAAC,KAAK,EAAE,KAAK;gBAAZ,KAAK,EAAE,KAAK;IAIjC,IAAI,aAAa,IAAI,GAAG,CAAC,MAAM,EAAE,SAAS,CAAC,CAI1C;IAED,iBAAiB,CACf,KAAK,EAAE,OAAO,EACd,GAAG,EAAE,gBAAgB,GACpB,gBAAgB,CAAC,kBAAkB,CAAC,KAAK,CAAC,CAAC;CAkC/C"}
|
package/dist/schema/object.js
CHANGED
|
@@ -2,85 +2,42 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.ObjectSchema = void 0;
|
|
4
4
|
const lex_data_1 = require("@atproto/lex-data");
|
|
5
|
+
const lazy_property_js_1 = require("../util/lazy-property.js");
|
|
5
6
|
const validation_js_1 = require("../validation.js");
|
|
6
|
-
class ObjectSchema extends validation_js_1.
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
constructor(validators, options) {
|
|
7
|
+
class ObjectSchema extends validation_js_1.Schema {
|
|
8
|
+
shape;
|
|
9
|
+
constructor(shape) {
|
|
10
10
|
super();
|
|
11
|
-
this.
|
|
12
|
-
this.options = options;
|
|
11
|
+
this.shape = shape;
|
|
13
12
|
}
|
|
14
13
|
get validatorsMap() {
|
|
15
|
-
const map = new Map(Object.entries(this.
|
|
16
|
-
|
|
17
|
-
Object.defineProperty(this, 'validatorsMap', {
|
|
18
|
-
value: map,
|
|
19
|
-
writable: false,
|
|
20
|
-
enumerable: false,
|
|
21
|
-
configurable: true,
|
|
22
|
-
});
|
|
23
|
-
return map;
|
|
14
|
+
const map = new Map(Object.entries(this.shape));
|
|
15
|
+
return (0, lazy_property_js_1.lazyProperty)(this, 'validatorsMap', map);
|
|
24
16
|
}
|
|
25
17
|
validateInContext(input, ctx) {
|
|
26
18
|
if (!(0, lex_data_1.isPlainObject)(input)) {
|
|
27
|
-
return ctx.issueInvalidType(input,
|
|
19
|
+
return ctx.issueInvalidType(input, 'object');
|
|
28
20
|
}
|
|
29
21
|
// Lazily copy value
|
|
30
22
|
let copy;
|
|
31
23
|
for (const [key, propDef] of this.validatorsMap) {
|
|
32
|
-
if (input[key] === null && this.options.nullable?.includes(key)) {
|
|
33
|
-
continue;
|
|
34
|
-
}
|
|
35
24
|
const result = ctx.validateChild(input, key, propDef);
|
|
36
25
|
if (!result.success) {
|
|
37
|
-
// Because default values are provided by child validators, we need to
|
|
38
|
-
// run the validator to get the default value and, in case of failure,
|
|
39
|
-
// ignore validation error that were caused by missing keys.
|
|
40
26
|
if (!(key in input)) {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
return ctx.issueRequiredKey(input, key);
|
|
44
|
-
}
|
|
45
|
-
// Ignore missing non-required key
|
|
46
|
-
continue;
|
|
27
|
+
// Transform into "required key" issue
|
|
28
|
+
return ctx.issueRequiredKey(input, key);
|
|
47
29
|
}
|
|
48
30
|
return result;
|
|
49
31
|
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
// and "key with undefined value"
|
|
54
|
-
if (!(key in input)) {
|
|
55
|
-
// Input was missing the key (was "undefined")
|
|
56
|
-
if (this.options.required?.includes(key)) {
|
|
57
|
-
return ctx.issueRequiredKey(input, key);
|
|
58
|
-
}
|
|
59
|
-
// Ignore missing non-required key
|
|
60
|
-
continue;
|
|
61
|
-
}
|
|
62
|
-
// if "key" existed in input (would typically be "undefined"), we keep
|
|
63
|
-
// it as-is by continuing processing as if it was any other value.
|
|
32
|
+
// Skip copying if key is not present in input (and value is undefined)
|
|
33
|
+
if (result.value === undefined && !(key in input)) {
|
|
34
|
+
continue;
|
|
64
35
|
}
|
|
65
36
|
if (result.value !== input[key]) {
|
|
66
37
|
copy ??= { ...input };
|
|
67
38
|
copy[key] = result.value;
|
|
68
39
|
}
|
|
69
40
|
}
|
|
70
|
-
if (this.options.unknownProperties === 'strict') {
|
|
71
|
-
for (const key of Object.keys(input)) {
|
|
72
|
-
if (!this.validatorsMap.has(key)) {
|
|
73
|
-
return ctx.issueInvalidPropertyType(input, key, 'undefined');
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
else if (this.options.unknownProperties) {
|
|
78
|
-
const result = this.options.unknownProperties.validateInContext(copy ?? input, ctx, { ignoredKeys: this.validatorsMap });
|
|
79
|
-
if (!result.success)
|
|
80
|
-
return result;
|
|
81
|
-
if (result.value !== input)
|
|
82
|
-
copy = result.value;
|
|
83
|
-
}
|
|
84
41
|
const output = (copy ?? input);
|
|
85
42
|
return ctx.success(output);
|
|
86
43
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"object.js","sourceRoot":"","sources":["../../src/schema/object.ts"],"names":[],"mappings":";;;AAAA,gDAAiD;AAEjD,
|
|
1
|
+
{"version":3,"file":"object.js","sourceRoot":"","sources":["../../src/schema/object.ts"],"names":[],"mappings":";;;AAAA,gDAAiD;AAEjD,+DAAuD;AACvD,oDAMyB;AASzB,MAAa,YAEX,SAAQ,sBAAiC;IACpB;IAArB,YAAqB,KAAY;QAC/B,KAAK,EAAE,CAAA;QADY,UAAK,GAAL,KAAK,CAAO;IAEjC,CAAC;IAED,IAAI,aAAa;QACf,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAA;QAE/C,OAAO,IAAA,+BAAY,EAAC,IAAI,EAAE,eAAe,EAAE,GAAG,CAAC,CAAA;IACjD,CAAC;IAED,iBAAiB,CACf,KAAc,EACd,GAAqB;QAErB,IAAI,CAAC,IAAA,wBAAa,EAAC,KAAK,CAAC,EAAE,CAAC;YAC1B,OAAO,GAAG,CAAC,gBAAgB,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAA;QAC9C,CAAC;QAED,oBAAoB;QACpB,IAAI,IAAyC,CAAA;QAE7C,KAAK,MAAM,CAAC,GAAG,EAAE,OAAO,CAAC,IAAI,IAAI,CAAC,aAAa,EAAE,CAAC;YAChD,MAAM,MAAM,GAAG,GAAG,CAAC,aAAa,CAAC,KAAK,EAAE,GAAG,EAAE,OAAO,CAAC,CAAA;YACrD,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC;gBACpB,IAAI,CAAC,CAAC,GAAG,IAAI,KAAK,CAAC,EAAE,CAAC;oBACpB,sCAAsC;oBACtC,OAAO,GAAG,CAAC,gBAAgB,CAAC,KAAK,EAAE,GAAG,CAAC,CAAA;gBACzC,CAAC;gBAED,OAAO,MAAM,CAAA;YACf,CAAC;YAED,uEAAuE;YACvE,IAAI,MAAM,CAAC,KAAK,KAAK,SAAS,IAAI,CAAC,CAAC,GAAG,IAAI,KAAK,CAAC,EAAE,CAAC;gBAClD,SAAQ;YACV,CAAC;YAED,IAAI,MAAM,CAAC,KAAK,KAAK,KAAK,CAAC,GAAG,CAAC,EAAE,CAAC;gBAChC,IAAI,KAAK,EAAE,GAAG,KAAK,EAAE,CAAA;gBACrB,IAAI,CAAC,GAAG,CAAC,GAAG,MAAM,CAAC,KAAK,CAAA;YAC1B,CAAC;QACH,CAAC;QAED,MAAM,MAAM,GAAG,CAAC,IAAI,IAAI,KAAK,CAA8B,CAAA;QAE3D,OAAO,GAAG,CAAC,OAAO,CAAC,MAAM,CAAC,CAAA;IAC5B,CAAC;CACF;AAlDD,oCAkDC","sourcesContent":["import { isPlainObject } from '@atproto/lex-data'\nimport { WithOptionalProperties } from '../core.js'\nimport { lazyProperty } from '../util/lazy-property.js'\nimport {\n Infer,\n Schema,\n ValidationResult,\n Validator,\n ValidatorContext,\n} from '../validation.js'\n\nexport type ObjectSchemaShape = Record<string, Validator>\n\nexport type ObjectSchemaOutput<Shape extends ObjectSchemaShape> =\n WithOptionalProperties<{\n [K in keyof Shape]: Infer<Shape[K]>\n }>\n\nexport class ObjectSchema<\n const Shape extends ObjectSchemaShape = any,\n> extends Schema<ObjectSchemaOutput<Shape>> {\n constructor(readonly shape: Shape) {\n super()\n }\n\n get validatorsMap(): Map<string, Validator> {\n const map = new Map(Object.entries(this.shape))\n\n return lazyProperty(this, 'validatorsMap', map)\n }\n\n validateInContext(\n input: unknown,\n ctx: ValidatorContext,\n ): ValidationResult<ObjectSchemaOutput<Shape>> {\n if (!isPlainObject(input)) {\n return ctx.issueInvalidType(input, 'object')\n }\n\n // Lazily copy value\n let copy: undefined | Record<string, unknown>\n\n for (const [key, propDef] of this.validatorsMap) {\n const result = ctx.validateChild(input, key, propDef)\n if (!result.success) {\n if (!(key in input)) {\n // Transform into \"required key\" issue\n return ctx.issueRequiredKey(input, key)\n }\n\n return result\n }\n\n // Skip copying if key is not present in input (and value is undefined)\n if (result.value === undefined && !(key in input)) {\n continue\n }\n\n if (result.value !== input[key]) {\n copy ??= { ...input }\n copy[key] = result.value\n }\n }\n\n const output = (copy ?? input) as ObjectSchemaOutput<Shape>\n\n return ctx.success(output)\n }\n}\n"]}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { Schema, ValidationResult, Validator, ValidatorContext } from '../validation.js';
|
|
2
|
+
export declare class OptionalSchema<V> extends Schema<V | undefined> {
|
|
3
|
+
readonly schema: Validator<V>;
|
|
4
|
+
constructor(schema: Validator<V>);
|
|
5
|
+
validateInContext(input: unknown, ctx: ValidatorContext): ValidationResult<V | undefined>;
|
|
6
|
+
}
|
|
7
|
+
//# sourceMappingURL=optional.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"optional.d.ts","sourceRoot":"","sources":["../../src/schema/optional.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,MAAM,EACN,gBAAgB,EAChB,SAAS,EACT,gBAAgB,EACjB,MAAM,kBAAkB,CAAA;AAEzB,qBAAa,cAAc,CAAC,CAAC,CAAE,SAAQ,MAAM,CAAC,CAAC,GAAG,SAAS,CAAC;IAC9C,QAAQ,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC,CAAC;gBAApB,MAAM,EAAE,SAAS,CAAC,CAAC,CAAC;IAIzC,iBAAiB,CACf,KAAK,EAAE,OAAO,EACd,GAAG,EAAE,gBAAgB,GACpB,gBAAgB,CAAC,CAAC,GAAG,SAAS,CAAC;CAenC"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.OptionalSchema = void 0;
|
|
4
|
+
const validation_js_1 = require("../validation.js");
|
|
5
|
+
class OptionalSchema extends validation_js_1.Schema {
|
|
6
|
+
schema;
|
|
7
|
+
constructor(schema) {
|
|
8
|
+
super();
|
|
9
|
+
this.schema = schema;
|
|
10
|
+
}
|
|
11
|
+
validateInContext(input, ctx) {
|
|
12
|
+
// @NOTE The inner schema might apply a default value so we need to run it
|
|
13
|
+
// first, even if input is undefined.
|
|
14
|
+
const result = ctx.validate(input, this.schema);
|
|
15
|
+
if (result.success) {
|
|
16
|
+
return result;
|
|
17
|
+
}
|
|
18
|
+
if (input === undefined) {
|
|
19
|
+
return ctx.success(input);
|
|
20
|
+
}
|
|
21
|
+
return result;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
exports.OptionalSchema = OptionalSchema;
|
|
25
|
+
//# sourceMappingURL=optional.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"optional.js","sourceRoot":"","sources":["../../src/schema/optional.ts"],"names":[],"mappings":";;;AAAA,oDAKyB;AAEzB,MAAa,cAAkB,SAAQ,sBAAqB;IACrC;IAArB,YAAqB,MAAoB;QACvC,KAAK,EAAE,CAAA;QADY,WAAM,GAAN,MAAM,CAAc;IAEzC,CAAC;IAED,iBAAiB,CACf,KAAc,EACd,GAAqB;QAErB,0EAA0E;QAC1E,qCAAqC;QACrC,MAAM,MAAM,GAAG,GAAG,CAAC,QAAQ,CAAC,KAAK,EAAE,IAAI,CAAC,MAAM,CAAC,CAAA;QAE/C,IAAI,MAAM,CAAC,OAAO,EAAE,CAAC;YACnB,OAAO,MAAM,CAAA;QACf,CAAC;QAED,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;YACxB,OAAO,GAAG,CAAC,OAAO,CAAC,KAAK,CAAC,CAAA;QAC3B,CAAC;QAED,OAAO,MAAM,CAAA;IACf,CAAC;CACF;AAvBD,wCAuBC","sourcesContent":["import {\n Schema,\n ValidationResult,\n Validator,\n ValidatorContext,\n} from '../validation.js'\n\nexport class OptionalSchema<V> extends Schema<V | undefined> {\n constructor(readonly schema: Validator<V>) {\n super()\n }\n\n validateInContext(\n input: unknown,\n ctx: ValidatorContext,\n ): ValidationResult<V | undefined> {\n // @NOTE The inner schema might apply a default value so we need to run it\n // first, even if input is undefined.\n const result = ctx.validate(input, this.schema)\n\n if (result.success) {\n return result\n }\n\n if (input === undefined) {\n return ctx.success(input)\n }\n\n return result\n }\n}\n"]}
|
package/dist/schema/params.d.ts
CHANGED
|
@@ -1,22 +1,17 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { WithOptionalProperties } from '../core.js';
|
|
2
|
+
import { Infer, Schema, ValidationResult, Validator, ValidatorContext } from '../validation.js';
|
|
2
3
|
import { Param } from './_parameters.js';
|
|
3
|
-
|
|
4
|
-
export type
|
|
5
|
-
[
|
|
6
|
-
}
|
|
7
|
-
export type
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
readonly lexiconType: "params";
|
|
16
|
-
constructor(validators: Validators, options: Options);
|
|
17
|
-
get validatorsMap(): Map<string, Validator<Param>>;
|
|
18
|
-
validateInContext(input: unknown | undefined, ctx: ValidatorContext): ValidationResult<Output>;
|
|
19
|
-
fromURLSearchParams(urlSearchParams: URLSearchParams): Output;
|
|
20
|
-
toURLSearchParams(input: Output): URLSearchParams;
|
|
4
|
+
export type ParamsSchemaShape = Record<string, Validator<Param | undefined>>;
|
|
5
|
+
export type ParamsSchemaOutput<Shape extends ParamsSchemaShape> = WithOptionalProperties<{
|
|
6
|
+
[K in keyof Shape]: Infer<Shape[K]>;
|
|
7
|
+
}>;
|
|
8
|
+
export type InferParamsSchema<T> = T extends ParamsSchema<infer P> ? NonNullable<unknown> extends ParamsSchemaOutput<P> ? ParamsSchemaOutput<P> | undefined : ParamsSchemaOutput<P> : never;
|
|
9
|
+
export declare class ParamsSchema<const Shape extends ParamsSchemaShape = ParamsSchemaShape> extends Schema<ParamsSchemaOutput<Shape>> {
|
|
10
|
+
readonly validators: Shape;
|
|
11
|
+
constructor(validators: Shape);
|
|
12
|
+
get validatorsMap(): Map<string, Validator<Param | undefined>>;
|
|
13
|
+
validateInContext(input: unknown | undefined, ctx: ValidatorContext): ValidationResult<ParamsSchemaOutput<Shape>>;
|
|
14
|
+
fromURLSearchParams(urlSearchParams: URLSearchParams): ParamsSchemaOutput<Shape>;
|
|
15
|
+
toURLSearchParams(input: ParamsSchemaOutput<Shape>): URLSearchParams;
|
|
21
16
|
}
|
|
22
17
|
//# sourceMappingURL=params.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"params.d.ts","sourceRoot":"","sources":["../../src/schema/params.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"params.d.ts","sourceRoot":"","sources":["../../src/schema/params.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,sBAAsB,EAAE,MAAM,YAAY,CAAA;AAEnD,OAAO,EACL,KAAK,EACL,MAAM,EACN,gBAAgB,EAChB,SAAS,EACT,gBAAgB,EACjB,MAAM,kBAAkB,CAAA;AACzB,OAAO,EAAE,KAAK,EAA4B,MAAM,kBAAkB,CAAA;AAGlE,MAAM,MAAM,iBAAiB,GAAG,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,KAAK,GAAG,SAAS,CAAC,CAAC,CAAA;AAE5E,MAAM,MAAM,kBAAkB,CAAC,KAAK,SAAS,iBAAiB,IAC5D,sBAAsB,CAAC;KACpB,CAAC,IAAI,MAAM,KAAK,GAAG,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;CACpC,CAAC,CAAA;AAEJ,MAAM,MAAM,iBAAiB,CAAC,CAAC,IAC7B,CAAC,SAAS,YAAY,CAAC,MAAM,CAAC,CAAC,GAC3B,WAAW,CAAC,OAAO,CAAC,SAAS,kBAAkB,CAAC,CAAC,CAAC,GAChD,kBAAkB,CAAC,CAAC,CAAC,GAAG,SAAS,GACjC,kBAAkB,CAAC,CAAC,CAAC,GACvB,KAAK,CAAA;AAEX,qBAAa,YAAY,CACvB,KAAK,CAAC,KAAK,SAAS,iBAAiB,GAAG,iBAAiB,CACzD,SAAQ,MAAM,CAAC,kBAAkB,CAAC,KAAK,CAAC,CAAC;IAC7B,QAAQ,CAAC,UAAU,EAAE,KAAK;gBAAjB,UAAU,EAAE,KAAK;IAItC,IAAI,aAAa,IAAI,GAAG,CAAC,MAAM,EAAE,SAAS,CAAC,KAAK,GAAG,SAAS,CAAC,CAAC,CAI7D;IAED,iBAAiB,CACf,KAAK,EAAE,OAAO,YAAK,EACnB,GAAG,EAAE,gBAAgB,GACpB,gBAAgB,CAAC,kBAAkB,CAAC,KAAK,CAAC,CAAC;IA+C9C,mBAAmB,CACjB,eAAe,EAAE,eAAe,GAC/B,kBAAkB,CAAC,KAAK,CAAC;IA6B5B,iBAAiB,CAAC,KAAK,EAAE,kBAAkB,CAAC,KAAK,CAAC,GAAG,eAAe;CAiBrE"}
|
package/dist/schema/params.js
CHANGED
|
@@ -2,28 +2,19 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.ParamsSchema = void 0;
|
|
4
4
|
const lex_data_1 = require("@atproto/lex-data");
|
|
5
|
+
const lazy_property_js_1 = require("../util/lazy-property.js");
|
|
5
6
|
const validation_js_1 = require("../validation.js");
|
|
6
7
|
const _parameters_js_1 = require("./_parameters.js");
|
|
7
8
|
const string_js_1 = require("./string.js");
|
|
8
|
-
class ParamsSchema extends validation_js_1.
|
|
9
|
+
class ParamsSchema extends validation_js_1.Schema {
|
|
9
10
|
validators;
|
|
10
|
-
|
|
11
|
-
lexiconType = 'params';
|
|
12
|
-
constructor(validators, options) {
|
|
11
|
+
constructor(validators) {
|
|
13
12
|
super();
|
|
14
13
|
this.validators = validators;
|
|
15
|
-
this.options = options;
|
|
16
14
|
}
|
|
17
15
|
get validatorsMap() {
|
|
18
16
|
const map = new Map(Object.entries(this.validators));
|
|
19
|
-
|
|
20
|
-
Object.defineProperty(this, 'validatorsMap', {
|
|
21
|
-
value: map,
|
|
22
|
-
writable: false,
|
|
23
|
-
enumerable: false,
|
|
24
|
-
configurable: true,
|
|
25
|
-
});
|
|
26
|
-
return map;
|
|
17
|
+
return (0, lazy_property_js_1.lazyProperty)(this, 'validatorsMap', map);
|
|
27
18
|
}
|
|
28
19
|
validateInContext(input = {}, ctx) {
|
|
29
20
|
if (!(0, lex_data_1.isPlainObject)(input)) {
|
|
@@ -46,21 +37,16 @@ class ParamsSchema extends validation_js_1.Validator {
|
|
|
46
37
|
for (const [key, propDef] of this.validatorsMap) {
|
|
47
38
|
const result = ctx.validateChild(input, key, propDef);
|
|
48
39
|
if (!result.success) {
|
|
49
|
-
// Because default values are provided by child validators, we need to
|
|
50
|
-
// run the validator to get the default value and, in case of failure,
|
|
51
|
-
// ignore validation error that were caused by missing keys.
|
|
52
40
|
if (!(key in input)) {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
continue;
|
|
56
|
-
}
|
|
57
|
-
else {
|
|
58
|
-
// Transform into "required key" issue
|
|
59
|
-
return ctx.issueRequiredKey(input, key);
|
|
60
|
-
}
|
|
41
|
+
// Transform into "required key" issue
|
|
42
|
+
return ctx.issueRequiredKey(input, key);
|
|
61
43
|
}
|
|
62
44
|
return result;
|
|
63
45
|
}
|
|
46
|
+
// Skip copying if key is not present in input (and value is undefined)
|
|
47
|
+
if (result.value === undefined && !(key in input)) {
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
64
50
|
if (result.value !== input[key]) {
|
|
65
51
|
// Copy on write
|
|
66
52
|
copy ??= { ...input };
|