@atproto/lex-schema 0.0.3 → 0.0.5
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 +44 -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/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/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/external.d.ts +7 -6
- package/dist/external.d.ts.map +1 -1
- package/dist/external.js +1 -0
- package/dist/external.js.map +1 -1
- package/dist/helpers.d.ts +36 -0
- package/dist/helpers.d.ts.map +1 -0
- package/dist/helpers.js +3 -0
- package/dist/helpers.js.map +1 -0
- package/dist/schema/blob.d.ts +1 -0
- package/dist/schema/blob.d.ts.map +1 -1
- package/dist/schema/blob.js +32 -18
- package/dist/schema/blob.js.map +1 -1
- package/dist/schema/custom.js +1 -1
- package/dist/schema/custom.js.map +1 -1
- package/dist/schema/integer.js +1 -1
- package/dist/schema/integer.js.map +1 -1
- package/dist/schema/params.d.ts +0 -1
- package/dist/schema/params.d.ts.map +1 -1
- 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 +13 -12
- package/dist/schema/record.d.ts.map +1 -1
- package/dist/schema/record.js.map +1 -1
- package/dist/schema/refine.js +1 -1
- package/dist/schema/refine.js.map +1 -1
- package/dist/schema/subscription.d.ts +4 -7
- package/dist/schema/subscription.d.ts.map +1 -1
- package/dist/schema/subscription.js.map +1 -1
- package/dist/schema/typed-object.d.ts +7 -6
- package/dist/schema/typed-object.d.ts.map +1 -1
- package/dist/schema/typed-object.js.map +1 -1
- package/dist/schema/union.d.ts.map +1 -1
- package/dist/schema/union.js +1 -4
- package/dist/schema/union.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/validation/schema.d.ts +21 -2
- package/dist/validation/schema.d.ts.map +1 -1
- package/dist/validation/schema.js +25 -2
- package/dist/validation/schema.js.map +1 -1
- package/dist/validation/validation-error.d.ts.map +1 -1
- package/dist/validation/validation-error.js +3 -3
- package/dist/validation/validation-error.js.map +1 -1
- package/dist/validation/validation-issue.js +10 -2
- package/dist/validation/validation-issue.js.map +1 -1
- package/dist/validation/validator.d.ts +4 -3
- package/dist/validation/validator.d.ts.map +1 -1
- package/dist/validation/validator.js +13 -10
- package/dist/validation/validator.js.map +1 -1
- package/package.json +2 -2
- package/src/core/$type.ts +4 -0
- package/src/core/result.ts +9 -8
- package/src/core/string-format.ts +88 -68
- package/src/core/types.ts +4 -0
- package/src/external.ts +9 -8
- package/src/helpers.test.ts +486 -0
- package/src/helpers.ts +61 -0
- package/src/schema/blob.test.ts +2 -4
- package/src/schema/blob.ts +31 -23
- package/src/schema/custom.test.ts +5 -5
- package/src/schema/custom.ts +1 -1
- package/src/schema/integer.ts +1 -1
- package/src/schema/params.ts +0 -7
- package/src/schema/payload.ts +67 -34
- package/src/schema/permission-set.test.ts +36 -36
- package/src/schema/procedure.test.ts +1 -62
- package/src/schema/procedure.ts +8 -20
- package/src/schema/query.test.ts +22 -69
- package/src/schema/query.ts +7 -14
- package/src/schema/record.ts +8 -4
- package/src/schema/refine.ts +1 -1
- package/src/schema/subscription.test.ts +30 -93
- package/src/schema/subscription.ts +11 -24
- package/src/schema/typed-object.ts +7 -3
- package/src/schema/union.ts +1 -4
- package/src/util/assertion-util.ts +40 -0
- package/src/validation/schema.ts +29 -4
- package/src/validation/validation-error.ts +4 -4
- package/src/validation/validation-issue.ts +12 -2
- package/src/validation/validator.ts +16 -12
- package/tsconfig.tests.json +1 -1
|
@@ -10,7 +10,7 @@ export type ValidationOptions = {
|
|
|
10
10
|
/** @default true */
|
|
11
11
|
allowTransform?: boolean;
|
|
12
12
|
};
|
|
13
|
-
export type Infer<T extends Validator> = T['
|
|
13
|
+
export type Infer<T extends Validator> = T['__lex']['output'];
|
|
14
14
|
export interface Validator<Output = any> {
|
|
15
15
|
/**
|
|
16
16
|
* This property is used for type inference purposes and does not actually
|
|
@@ -18,7 +18,7 @@ export interface Validator<Output = any> {
|
|
|
18
18
|
*
|
|
19
19
|
* @deprecated **INTERNAL API, DO NOT USE**.
|
|
20
20
|
*/
|
|
21
|
-
readonly ['
|
|
21
|
+
readonly ['__lex']: {
|
|
22
22
|
output: Output;
|
|
23
23
|
};
|
|
24
24
|
/**
|
|
@@ -70,7 +70,8 @@ export declare class ValidatorContext {
|
|
|
70
70
|
validateChild<I extends object, K extends PropertyKey & keyof I, V extends Validator>(input: I, key: K, validator: V): ValidationResult<Infer<V>>;
|
|
71
71
|
addIssue(issue: Issue): void;
|
|
72
72
|
success<V>(value: V): ValidationResult<V>;
|
|
73
|
-
failure(
|
|
73
|
+
failure(reason: ValidationError): ValidationFailure;
|
|
74
|
+
issue(issue: Issue): ValidationFailure;
|
|
74
75
|
issueInvalidValue(input: unknown, values: readonly unknown[]): ValidationFailure;
|
|
75
76
|
issueInvalidType(input: unknown, expected: string): ValidationFailure;
|
|
76
77
|
issueRequiredKey(input: object, key: PropertyKey): ValidationFailure;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validator.d.ts","sourceRoot":"","sources":["../../src/validation/validator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,aAAa,EAAoB,MAAM,YAAY,CAAA;AAC3E,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAA;AAC/C,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAA;AACvD,OAAO,EACL,KAAK,EAOL,cAAc,EACf,MAAM,uBAAuB,CAAA;AAE9B,MAAM,MAAM,iBAAiB,CAAC,KAAK,GAAG,GAAG,IAAI,aAAa,CAAC,KAAK,CAAC,CAAA;AACjE,MAAM,MAAM,iBAAiB,GAAG,aAAa,CAAC,eAAe,CAAC,CAAA;AAC9D,MAAM,MAAM,gBAAgB,CAAC,KAAK,GAAG,GAAG,IACpC,iBAAiB,CAAC,KAAK,CAAC,GACxB,iBAAiB,CAAA;AAErB,MAAM,MAAM,iBAAiB,GAAG;IAC9B,IAAI,CAAC,EAAE,WAAW,EAAE,CAAA;IAEpB,oBAAoB;IACpB,cAAc,CAAC,EAAE,OAAO,CAAA;CACzB,CAAA;AAED,MAAM,MAAM,KAAK,CAAC,CAAC,SAAS,SAAS,IAAI,CAAC,CAAC,
|
|
1
|
+
{"version":3,"file":"validator.d.ts","sourceRoot":"","sources":["../../src/validation/validator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,aAAa,EAAoB,MAAM,YAAY,CAAA;AAC3E,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAA;AAC/C,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAA;AACvD,OAAO,EACL,KAAK,EAOL,cAAc,EACf,MAAM,uBAAuB,CAAA;AAE9B,MAAM,MAAM,iBAAiB,CAAC,KAAK,GAAG,GAAG,IAAI,aAAa,CAAC,KAAK,CAAC,CAAA;AACjE,MAAM,MAAM,iBAAiB,GAAG,aAAa,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,EAAE,MAAM;CAMnB"}
|
|
@@ -79,36 +79,39 @@ class ValidatorContext {
|
|
|
79
79
|
success(value) {
|
|
80
80
|
return (0, core_js_1.success)(value);
|
|
81
81
|
}
|
|
82
|
-
failure(
|
|
83
|
-
return (0, core_js_1.failure)(
|
|
82
|
+
failure(reason) {
|
|
83
|
+
return (0, core_js_1.failure)(reason);
|
|
84
|
+
}
|
|
85
|
+
issue(issue) {
|
|
86
|
+
return this.failure(new validation_error_js_1.ValidationError([...this.issues, issue]));
|
|
84
87
|
}
|
|
85
88
|
issueInvalidValue(input, values) {
|
|
86
|
-
return this.
|
|
89
|
+
return this.issue(new validation_issue_js_1.IssueInvalidValue(this.path, input, values));
|
|
87
90
|
}
|
|
88
91
|
issueInvalidType(input, expected) {
|
|
89
|
-
return this.
|
|
92
|
+
return this.issue(new validation_issue_js_1.IssueInvalidType(this.path, input, [expected]));
|
|
90
93
|
}
|
|
91
94
|
issueRequiredKey(input, key) {
|
|
92
|
-
return this.
|
|
95
|
+
return this.issue(new validation_issue_js_1.IssueRequiredKey(this.path, input, key));
|
|
93
96
|
}
|
|
94
97
|
issueInvalidFormat(input, format, msg) {
|
|
95
|
-
return this.
|
|
98
|
+
return this.issue(new validation_issue_js_1.IssueInvalidFormat(this.path, input, format, msg));
|
|
96
99
|
}
|
|
97
100
|
issueTooBig(input, type, max, actual) {
|
|
98
|
-
return this.
|
|
101
|
+
return this.issue(new validation_issue_js_1.IssueTooBig(this.path, input, max, type, actual));
|
|
99
102
|
}
|
|
100
103
|
issueTooSmall(input, type, min, actual) {
|
|
101
|
-
return this.
|
|
104
|
+
return this.issue(new validation_issue_js_1.IssueTooSmall(this.path, input, min, type, actual));
|
|
102
105
|
}
|
|
103
106
|
issueInvalidPropertyValue(input, property, values) {
|
|
104
107
|
const value = input[property];
|
|
105
108
|
const path = this.concatPath(property);
|
|
106
|
-
return this.
|
|
109
|
+
return this.issue(new validation_issue_js_1.IssueInvalidValue(path, value, values));
|
|
107
110
|
}
|
|
108
111
|
issueInvalidPropertyType(input, property, expected) {
|
|
109
112
|
const value = input[property];
|
|
110
113
|
const path = this.concatPath(property);
|
|
111
|
-
return this.
|
|
114
|
+
return this.issue(new validation_issue_js_1.IssueInvalidType(path, value, [expected]));
|
|
112
115
|
}
|
|
113
116
|
}
|
|
114
117
|
exports.ValidatorContext = ValidatorContext;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validator.js","sourceRoot":"","sources":["../../src/validation/validator.ts"],"names":[],"mappings":";;;AAAA,wCAA2E;AAE3E,+DAAuD;AACvD,+DAS8B;AA0D9B,MAAa,gBAAgB;IAiBI;IAhB/B;;;OAGG;IACH,MAAM,CAAC,QAAQ,CACb,KAAc,EACd,SAAuB,EACvB,UAA6B,EAAE;QAE/B,MAAM,OAAO,GAAG,IAAI,gBAAgB,CAAC,OAAO,CAAC,CAAA;QAC7C,OAAO,OAAO,CAAC,QAAQ,CAAC,KAAK,EAAE,SAAS,CAAC,CAAA;IAC3C,CAAC;IAEgB,WAAW,CAAe;IAC1B,MAAM,GAAY,EAAE,CAAA;IAErC,YAA+B,OAA0B;QAA1B,YAAO,GAAP,OAAO,CAAmB;QACvD,yEAAyE;QACzE,IAAI,CAAC,WAAW,GAAG,OAAO,EAAE,IAAI,IAAI,IAAI,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,EAAE,CAAA;IAC1E,CAAC;IAED,IAAI,IAAI;QACN,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,WAAW,CAAC,CAAA;IACrC,CAAC;IAED,UAAU,CAAC,IAA2C;QACpD,IAAI,IAAI,IAAI,IAAI;YAAE,OAAO,IAAI,CAAC,IAAI,CAAA;QAClC,OAAO,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,IAAI,CAAC,CAAA;IACtC,CAAC;IAED;;;;OAIG;IACH,QAAQ,CAAI,KAAc,EAAE,SAAuB;QACj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AW,EACX,MAAc;QAEd,OAAO,IAAI,CAAC,OAAO,CAAC,IAAI,mCAAa,CAAC,IAAI,CAAC,IAAI,EAAE,KAAK,EAAE,GAAG,EAAE,IAAI,EAAE,MAAM,CAAC,CAAC,CAAA;IAC7E,CAAC;IAED,yBAAyB,CACvB,KAAQ,EACR,QAA+B,EAC/B,MAA0B;QAE1B,MAAM,KAAK,GAAG,KAAK,CAAC,QAAQ,CAAC,CAAA;QAC7B,MAAM,IAAI,GAAG,IAAI,CAAC,UAAU,CAAC,QAAQ,CAAC,CAAA;QACtC,OAAO,IAAI,CAAC,OAAO,CAAC,IAAI,uCAAiB,CAAC,IAAI,EAAE,KAAK,EAAE,MAAM,CAAC,CAAC,CAAA;IACjE,CAAC;IAED,wBAAwB,CACtB,KAAQ,EACR,QAA+B,EAC/B,QAAgB;QAEhB,MAAM,KAAK,GAAG,KAAK,CAAC,QAAQ,CAAC,CAAA;QAC7B,MAAM,IAAI,GAAG,IAAI,CAAC,UAAU,CAAC,QAAQ,CAAC,CAAA;QACtC,OAAO,IAAI,CAAC,OAAO,CAAC,IAAI,sCAAgB,CAAC,IAAI,EAAE,KAAK,EAAE,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAA;IACpE,CAAC;CACF;AAvJD,4CAuJC","sourcesContent":["import { ResultFailure, ResultSuccess, failure, success } from '../core.js'\nimport { PropertyKey } from './property-key.js'\nimport { ValidationError } from './validation-error.js'\nimport {\n Issue,\n IssueInvalidFormat,\n IssueInvalidType,\n IssueInvalidValue,\n IssueRequiredKey,\n IssueTooBig,\n IssueTooSmall,\n MeasurableType,\n} from './validation-issue.js'\n\nexport type ValidationSuccess<Value = any> = ResultSuccess<Value>\nexport type ValidationFailure = ResultFailure<ValidationError>\nexport type ValidationResult<Value = any> =\n | ValidationSuccess<Value>\n | ValidationFailure\n\nexport type ValidationOptions = {\n path?: PropertyKey[]\n\n /** @default true */\n allowTransform?: boolean\n}\n\nexport type Infer<T extends Validator> = T['_lex']['output']\n\nexport interface Validator<Output = any> {\n /**\n * This property is used for type inference purposes and does not actually\n * exist at runtime.\n *\n * @deprecated **INTERNAL API, DO NOT USE**.\n */\n readonly ['_lex']: { output: Output }\n\n /**\n * @internal **INTERNAL API**: use {@link ValidatorContext.validate} instead\n *\n * This method is implemented by subclasses to perform transformation and\n * validation of the input value. Do not call this method directly; as the\n * {@link ValidatorContext.options.allowTransform} option will **not** be\n * enforced. See {@link ValidatorContext.validate} for details. When\n * delegating validation from one validator sub-class implementation to\n * another schema, {@link ValidatorContext.validate} must be used instead of\n * calling {@link Validator.validateInContext}. This will allow to stop the\n * validation process if the value was transformed (by the other schema) but\n * transformations are not allowed.\n *\n * By convention, the {@link ValidationResult} must return the original input\n * value if validation was successful and no transformation was applied (i.e.\n * the input already conformed to the schema). If a default value, or any\n * other transformation was applied, the returned value c&an be different from\n * the input.\n *\n * This convention allows the {@link Validator.check check} and\n * {@link Validator.assert assert} methods to check whether the input value\n * exactly matches the schema (without defaults or transformations), by\n * checking if the returned value is strictly equal to the input.\n *\n * @see {@link ValidatorContext.validate}\n */\n validateInContext(\n input: unknown,\n ctx: ValidatorContext,\n ): ValidationResult<Output>\n}\n\nexport class ValidatorContext {\n /**\n * Creates a new validation context and validates the input using the\n * provided validator.\n */\n static validate<V>(\n input: unknown,\n validator: Validator<V>,\n options: ValidationOptions = {},\n ): ValidationResult<V> {\n const context = new ValidatorContext(options)\n return context.validate(input, validator)\n }\n\n private readonly currentPath: PropertyKey[]\n private readonly issues: Issue[] = []\n\n protected constructor(readonly options: ValidationOptions) {\n // Create a copy because we will be mutating the array during validation.\n this.currentPath = options?.path != null ? Array.from(options.path) : []\n }\n\n get path() {\n return Array.from(this.currentPath)\n }\n\n concatPath(path?: PropertyKey | readonly PropertyKey[]) {\n if (path == null) return this.path\n return this.currentPath.concat(path)\n }\n\n /**\n * This is basically the entry point for validation within a context. Use this\n * method instead of {@link Validator.validateInContext} directly, because\n * this method enforces the {@link ValidationOptions.allowTransform} option.\n */\n validate<V>(input: unknown, validator: Validator<V>): ValidationResult<V> {\n // This is the only place where validateInContext should be called.\n const result = validator.validateInContext(input, this)\n\n if (result.success) {\n if (\n // Defaults to true\n this.options?.allowTransform === false &&\n !Object.is(result.value, input)\n ) {\n // If the value changed, it means that a default (or some other\n // transformation) was applied, meaning that the original value did\n // *not* match the (output) schema. When \"allowTransform\" is false, we\n // consider this a failure.\n\n // This check is the reason why Validator.validateInContext should not\n // be used directly, and ValidatorContext.validate should be used\n // instead, even when delegating validation from one validator to\n // another.\n\n // This if block comes before the next one because 'this.issues' will\n // end-up being appended to the returned ValidationError (see the\n // \"failure\" method below), resulting in a more complete error report.\n return this.issueInvalidValue(input, [result.value])\n }\n\n if (this.issues.length > 0) {\n // Validator returned a success but issues were added via the context.\n // This means the overall validation failed.\n return failure(new ValidationError(Array.from(this.issues)))\n }\n }\n\n return result as ValidationResult<V>\n }\n\n validateChild<\n I extends object,\n K extends PropertyKey & keyof I,\n V extends Validator,\n >(input: I, key: K, validator: V): ValidationResult<Infer<V>> {\n // Instead of creating a new context, we just push/pop the path segment.\n this.currentPath.push(key)\n try {\n return this.validate(input[key], validator)\n } finally {\n this.currentPath.length--\n }\n }\n\n addIssue(issue: Issue): void {\n this.issues.push(issue)\n }\n\n success<V>(value: V): ValidationResult<V> {\n return success(value)\n }\n\n failure(issue: Issue): ValidationFailure {\n return failure(new ValidationError([...this.issues, issue]))\n }\n\n issueInvalidValue(input: unknown, values: readonly unknown[]) {\n return this.failure(new IssueInvalidValue(this.path, input, values))\n }\n\n issueInvalidType(input: unknown, expected: string) {\n return this.failure(new IssueInvalidType(this.path, input, [expected]))\n }\n\n issueRequiredKey(input: object, key: PropertyKey) {\n return this.failure(new IssueRequiredKey(this.path, input, key))\n }\n\n issueInvalidFormat(input: unknown, format: string, msg?: string) {\n return this.failure(new IssueInvalidFormat(this.path, input, format, msg))\n }\n\n issueTooBig(\n input: unknown,\n type: MeasurableType,\n max: number,\n actual: number,\n ) {\n return this.failure(new IssueTooBig(this.path, input, max, type, actual))\n }\n\n issueTooSmall(\n input: unknown,\n type: MeasurableType,\n min: number,\n actual: number,\n ) {\n return this.failure(new IssueTooSmall(this.path, input, min, type, actual))\n }\n\n issueInvalidPropertyValue<I>(\n input: I,\n property: keyof I & PropertyKey,\n values: readonly unknown[],\n ) {\n const value = input[property]\n const path = this.concatPath(property)\n return this.failure(new IssueInvalidValue(path, value, values))\n }\n\n issueInvalidPropertyType<I>(\n input: I,\n property: keyof I & PropertyKey,\n expected: string,\n ) {\n const value = input[property]\n const path = this.concatPath(property)\n return this.failure(new IssueInvalidType(path, value, [expected]))\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"validator.js","sourceRoot":"","sources":["../../src/validation/validator.ts"],"names":[],"mappings":";;;AAAA,wCAA2E;AAE3E,+DAAuD;AACvD,+DAS8B;AA0D9B,MAAa,gBAAgB;IAiBI;IAhB/B;;;OAGG;IACH,MAAM,CAAC,QAAQ,CACb,KAAc,EACd,SAAuB,EACvB,UAA6B,EAAE;QAE/B,MAAM,OAAO,GAAG,IAAI,gBAAgB,CAAC,OAAO,CAAC,CAAA;QAC7C,OAAO,OAAO,CAAC,QAAQ,CAAC,KAAK,EAAE,SAAS,CAAC,CAAA;IAC3C,CAAC;IAEgB,WAAW,CAAe;IAC1B,MAAM,GAAY,EAAE,CAAA;IAErC,YAA+B,OAA0B;QAA1B,YAAO,GAAP,OAAO,CAAmB;QACvD,yEAAyE;QACzE,IAAI,CAAC,WAAW,GAAG,OAAO,EAAE,IAAI,IAAI,IAAI,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,EAAE,CAAA;IAC1E,CAAC;IAED,IAAI,IAAI;QACN,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,WAAW,CAAC,CAAA;IACrC,CAAC;IAED,UAAU,CAAC,IAA2C;QACpD,IAAI,IAAI,IAAI,IAAI;YAAE,OAAO,IAAI,CA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ACT,KAAc,EACd,IAAoB,EACpB,GAAW,EACX,MAAc;QAEd,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,iCAAW,CAAC,IAAI,CAAC,IAAI,EAAE,KAAK,EAAE,GAAG,EAAE,IAAI,EAAE,MAAM,CAAC,CAAC,CAAA;IACzE,CAAC;IAED,aAAa,CACX,KAAc,EACd,IAAoB,EACpB,GAAW,EACX,MAAc;QAEd,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,mCAAa,CAAC,IAAI,CAAC,IAAI,EAAE,KAAK,EAAE,GAAG,EAAE,IAAI,EAAE,MAAM,CAAC,CAAC,CAAA;IAC3E,CAAC;IAED,yBAAyB,CACvB,KAAQ,EACR,QAA+B,EAC/B,MAA0B;QAE1B,MAAM,KAAK,GAAG,KAAK,CAAC,QAAQ,CAAC,CAAA;QAC7B,MAAM,IAAI,GAAG,IAAI,CAAC,UAAU,CAAC,QAAQ,CAAC,CAAA;QACtC,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,uCAAiB,CAAC,IAAI,EAAE,KAAK,EAAE,MAAM,CAAC,CAAC,CAAA;IAC/D,CAAC;IAED,wBAAwB,CACtB,KAAQ,EACR,QAA+B,EAC/B,QAAgB;QAEhB,MAAM,KAAK,GAAG,KAAK,CAAC,QAAQ,CAAC,CAAA;QAC7B,MAAM,IAAI,GAAG,IAAI,CAAC,UAAU,CAAC,QAAQ,CAAC,CAAA;QACtC,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,sCAAgB,CAAC,IAAI,EAAE,KAAK,EAAE,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAA;IAClE,CAAC;CACF;AA3JD,4CA2JC","sourcesContent":["import { ResultFailure, ResultSuccess, failure, success } from '../core.js'\nimport { PropertyKey } from './property-key.js'\nimport { ValidationError } from './validation-error.js'\nimport {\n Issue,\n IssueInvalidFormat,\n IssueInvalidType,\n IssueInvalidValue,\n IssueRequiredKey,\n IssueTooBig,\n IssueTooSmall,\n MeasurableType,\n} from './validation-issue.js'\n\nexport type ValidationSuccess<Value = any> = ResultSuccess<Value>\nexport type ValidationFailure = ResultFailure<ValidationError>\nexport type ValidationResult<Value = any> =\n | ValidationSuccess<Value>\n | ValidationFailure\n\nexport type ValidationOptions = {\n path?: PropertyKey[]\n\n /** @default true */\n allowTransform?: boolean\n}\n\nexport type Infer<T extends Validator> = T['__lex']['output']\n\nexport interface Validator<Output = any> {\n /**\n * This property is used for type inference purposes and does not actually\n * exist at runtime.\n *\n * @deprecated **INTERNAL API, DO NOT USE**.\n */\n readonly ['__lex']: { output: Output }\n\n /**\n * @internal **INTERNAL API**: use {@link ValidatorContext.validate} instead\n *\n * This method is implemented by subclasses to perform transformation and\n * validation of the input value. Do not call this method directly; as the\n * {@link ValidatorContext.options.allowTransform} option will **not** be\n * enforced. See {@link ValidatorContext.validate} for details. When\n * delegating validation from one validator sub-class implementation to\n * another schema, {@link ValidatorContext.validate} must be used instead of\n * calling {@link Validator.validateInContext}. This will allow to stop the\n * validation process if the value was transformed (by the other schema) but\n * transformations are not allowed.\n *\n * By convention, the {@link ValidationResult} must return the original input\n * value if validation was successful and no transformation was applied (i.e.\n * the input already conformed to the schema). If a default value, or any\n * other transformation was applied, the returned value c&an be different from\n * the input.\n *\n * This convention allows the {@link Validator.check check} and\n * {@link Validator.assert assert} methods to check whether the input value\n * exactly matches the schema (without defaults or transformations), by\n * checking if the returned value is strictly equal to the input.\n *\n * @see {@link ValidatorContext.validate}\n */\n validateInContext(\n input: unknown,\n ctx: ValidatorContext,\n ): ValidationResult<Output>\n}\n\nexport class ValidatorContext {\n /**\n * Creates a new validation context and validates the input using the\n * provided validator.\n */\n static validate<V>(\n input: unknown,\n validator: Validator<V>,\n options: ValidationOptions = {},\n ): ValidationResult<V> {\n const context = new ValidatorContext(options)\n return context.validate(input, validator)\n }\n\n private readonly currentPath: PropertyKey[]\n private readonly issues: Issue[] = []\n\n protected constructor(readonly options: ValidationOptions) {\n // Create a copy because we will be mutating the array during validation.\n this.currentPath = options?.path != null ? Array.from(options.path) : []\n }\n\n get path() {\n return Array.from(this.currentPath)\n }\n\n concatPath(path?: PropertyKey | readonly PropertyKey[]) {\n if (path == null) return this.path\n return this.currentPath.concat(path)\n }\n\n /**\n * This is basically the entry point for validation within a context. Use this\n * method instead of {@link Validator.validateInContext} directly, because\n * this method enforces the {@link ValidationOptions.allowTransform} option.\n */\n validate<V>(input: unknown, validator: Validator<V>): ValidationResult<V> {\n // This is the only place where validateInContext should be called.\n const result = validator.validateInContext(input, this)\n\n if (result.success) {\n if (\n // Defaults to true\n this.options?.allowTransform === false &&\n !Object.is(result.value, input)\n ) {\n // If the value changed, it means that a default (or some other\n // transformation) was applied, meaning that the original value did\n // *not* match the (output) schema. When \"allowTransform\" is false, we\n // consider this a failure.\n\n // This check is the reason why Validator.validateInContext should not\n // be used directly, and ValidatorContext.validate should be used\n // instead, even when delegating validation from one validator to\n // another.\n\n // This if block comes before the next one because 'this.issues' will\n // end-up being appended to the returned ValidationError (see the\n // \"failure\" method below), resulting in a more complete error report.\n return this.issueInvalidValue(input, [result.value])\n }\n\n if (this.issues.length > 0) {\n // Validator returned a success but issues were added via the context.\n // This means the overall validation failed.\n return failure(new ValidationError(Array.from(this.issues)))\n }\n }\n\n return result as ValidationResult<V>\n }\n\n validateChild<\n I extends object,\n K extends PropertyKey & keyof I,\n V extends Validator,\n >(input: I, key: K, validator: V): ValidationResult<Infer<V>> {\n // Instead of creating a new context, we just push/pop the path segment.\n this.currentPath.push(key)\n try {\n return this.validate(input[key], validator)\n } finally {\n this.currentPath.length--\n }\n }\n\n addIssue(issue: Issue): void {\n this.issues.push(issue)\n }\n\n success<V>(value: V): ValidationResult<V> {\n return success(value)\n }\n\n failure(reason: ValidationError): ValidationFailure {\n return failure(reason)\n }\n\n issue(issue: Issue) {\n return this.failure(new ValidationError([...this.issues, issue]))\n }\n\n issueInvalidValue(input: unknown, values: readonly unknown[]) {\n return this.issue(new IssueInvalidValue(this.path, input, values))\n }\n\n issueInvalidType(input: unknown, expected: string) {\n return this.issue(new IssueInvalidType(this.path, input, [expected]))\n }\n\n issueRequiredKey(input: object, key: PropertyKey) {\n return this.issue(new IssueRequiredKey(this.path, input, key))\n }\n\n issueInvalidFormat(input: unknown, format: string, msg?: string) {\n return this.issue(new IssueInvalidFormat(this.path, input, format, msg))\n }\n\n issueTooBig(\n input: unknown,\n type: MeasurableType,\n max: number,\n actual: number,\n ) {\n return this.issue(new IssueTooBig(this.path, input, max, type, actual))\n }\n\n issueTooSmall(\n input: unknown,\n type: MeasurableType,\n min: number,\n actual: number,\n ) {\n return this.issue(new IssueTooSmall(this.path, input, min, type, actual))\n }\n\n issueInvalidPropertyValue<I>(\n input: I,\n property: keyof I & PropertyKey,\n values: readonly unknown[],\n ) {\n const value = input[property]\n const path = this.concatPath(property)\n return this.issue(new IssueInvalidValue(path, value, values))\n }\n\n issueInvalidPropertyType<I>(\n input: I,\n property: keyof I & PropertyKey,\n expected: string,\n ) {\n const value = input[property]\n const path = this.concatPath(property)\n return this.issue(new IssueInvalidType(path, value, [expected]))\n }\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.5",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"description": "Lexicon schema system for AT Lexicons",
|
|
6
6
|
"keywords": [
|
|
@@ -37,7 +37,7 @@
|
|
|
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.4"
|
|
41
41
|
},
|
|
42
42
|
"devDependencies": {
|
|
43
43
|
"jest": "^28.1.2"
|
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
|
*
|
|
@@ -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
package/src/external.ts
CHANGED
|
@@ -31,7 +31,7 @@ import {
|
|
|
31
31
|
ParamsSchema,
|
|
32
32
|
ParamsSchemaShape,
|
|
33
33
|
Payload,
|
|
34
|
-
|
|
34
|
+
PayloadSchema,
|
|
35
35
|
Permission,
|
|
36
36
|
PermissionOptions,
|
|
37
37
|
PermissionSet,
|
|
@@ -57,9 +57,10 @@ import {
|
|
|
57
57
|
UnknownSchema,
|
|
58
58
|
refine,
|
|
59
59
|
} from './schema.js'
|
|
60
|
-
import { Infer, PropertyKey, Validator } from './validation.js'
|
|
60
|
+
import { Infer, PropertyKey, Schema, Validator } from './validation.js'
|
|
61
61
|
|
|
62
62
|
export * from './core.js'
|
|
63
|
+
export * from './helpers.js'
|
|
63
64
|
export * from './schema.js'
|
|
64
65
|
export * from './validation.js'
|
|
65
66
|
|
|
@@ -262,7 +263,7 @@ export function typedUnion<
|
|
|
262
263
|
export function typedObject<
|
|
263
264
|
const N extends NsidString,
|
|
264
265
|
const H extends string,
|
|
265
|
-
const S extends Validator<{ [
|
|
266
|
+
const S extends Validator<{ [k: string]: unknown }>,
|
|
266
267
|
>(nsid: N, hash: H, schema: S): TypedObjectSchema<$Type<N, H>, S>
|
|
267
268
|
export function typedObject<V extends { $type?: $Type }>(
|
|
268
269
|
nsid: V extends { $type?: infer T extends string }
|
|
@@ -281,7 +282,7 @@ export function typedObject<V extends { $type?: $Type }>(
|
|
|
281
282
|
export function typedObject<
|
|
282
283
|
const N extends NsidString,
|
|
283
284
|
const H extends string,
|
|
284
|
-
const S extends Validator<{ [
|
|
285
|
+
const S extends Validator<{ [k: string]: unknown }>,
|
|
285
286
|
>(nsid: N, hash: H, schema: S) {
|
|
286
287
|
return new TypedObjectSchema<$Type<N, H>, S>($type(nsid, hash), schema)
|
|
287
288
|
}
|
|
@@ -305,7 +306,7 @@ type AsNsid<T> = T extends `${string}#${string}` ? never : T
|
|
|
305
306
|
export function record<
|
|
306
307
|
const K extends LexiconRecordKey,
|
|
307
308
|
const T extends NsidString,
|
|
308
|
-
const S extends Validator<{ [
|
|
309
|
+
const S extends Validator<{ [k: string]: unknown }>,
|
|
309
310
|
>(key: K, type: AsNsid<T>, schema: S): RecordSchema<K, T, S>
|
|
310
311
|
export function record<
|
|
311
312
|
const K extends LexiconRecordKey,
|
|
@@ -319,7 +320,7 @@ export function record<
|
|
|
319
320
|
export function record<
|
|
320
321
|
const K extends LexiconRecordKey,
|
|
321
322
|
const T extends NsidString,
|
|
322
|
-
const S extends Validator<{ [
|
|
323
|
+
const S extends Validator<{ [k: string]: unknown }>,
|
|
323
324
|
>(key: K, type: T, schema: S) {
|
|
324
325
|
return new RecordSchema<K, T, S>(key, type, schema)
|
|
325
326
|
}
|
|
@@ -334,7 +335,7 @@ export function params<
|
|
|
334
335
|
/*@__NO_SIDE_EFFECTS__*/
|
|
335
336
|
export function payload<
|
|
336
337
|
const E extends string | undefined = undefined,
|
|
337
|
-
const S extends
|
|
338
|
+
const S extends PayloadSchema<E> = undefined,
|
|
338
339
|
>(encoding: E = undefined as E, schema: S = undefined as S) {
|
|
339
340
|
return new Payload<E, S>(encoding, schema)
|
|
340
341
|
}
|
|
@@ -364,7 +365,7 @@ export function procedure<
|
|
|
364
365
|
export function subscription<
|
|
365
366
|
const N extends NsidString,
|
|
366
367
|
const P extends ParamsSchema,
|
|
367
|
-
const M extends
|
|
368
|
+
const M extends Schema,
|
|
368
369
|
const E extends undefined | readonly string[] = undefined,
|
|
369
370
|
>(nsid: N, parameters: P, message: M, errors: E = undefined as E) {
|
|
370
371
|
return new Subscription<N, P, M, E>(nsid, parameters, message, errors)
|