@atproto/lex-schema 0.0.14 → 0.0.16
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 +30 -0
- package/dist/core/schema.d.ts +5 -4
- package/dist/core/schema.d.ts.map +1 -1
- package/dist/core/schema.js +7 -2
- package/dist/core/schema.js.map +1 -1
- package/dist/core/standard-schema.d.ts +14 -0
- package/dist/core/standard-schema.d.ts.map +1 -0
- package/dist/core/standard-schema.js +27 -0
- package/dist/core/standard-schema.js.map +1 -0
- package/dist/core/string-format.d.ts +24 -17
- package/dist/core/string-format.d.ts.map +1 -1
- package/dist/core/string-format.js +57 -30
- package/dist/core/string-format.js.map +1 -1
- package/dist/core/validation-error.d.ts +10 -2
- package/dist/core/validation-error.d.ts.map +1 -1
- package/dist/core/validation-error.js +10 -0
- package/dist/core/validation-error.js.map +1 -1
- package/dist/core/validation-issue.d.ts +15 -15
- package/dist/core/validation-issue.d.ts.map +1 -1
- package/dist/core/validation-issue.js +33 -29
- package/dist/core/validation-issue.js.map +1 -1
- package/dist/core/validator.d.ts +29 -14
- package/dist/core/validator.d.ts.map +1 -1
- package/dist/core/validator.js +4 -2
- package/dist/core/validator.js.map +1 -1
- package/dist/core.d.ts +0 -1
- package/dist/core.d.ts.map +1 -1
- package/dist/core.js +0 -1
- package/dist/core.js.map +1 -1
- package/dist/schema/blob.d.ts +10 -8
- package/dist/schema/blob.d.ts.map +1 -1
- package/dist/schema/blob.js +39 -14
- package/dist/schema/blob.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.map +1 -1
- package/dist/schema/never.d.ts +1 -1
- package/dist/schema/nullable.d.ts +1 -1
- package/dist/schema/payload.d.ts +2 -2
- package/dist/schema/payload.d.ts.map +1 -1
- package/dist/schema/payload.js.map +1 -1
- package/dist/schema/record.d.ts +1 -1
- package/dist/schema/ref.d.ts +1 -1
- package/dist/schema/ref.d.ts.map +1 -1
- package/dist/schema/ref.js.map +1 -1
- package/dist/schema/refine.d.ts +1 -1
- package/dist/schema/refine.d.ts.map +1 -1
- package/dist/schema/refine.js.map +1 -1
- package/dist/schema/string.js +1 -1
- package/dist/schema/string.js.map +1 -1
- package/dist/schema/typed-ref.d.ts +1 -1
- package/dist/schema/typed-union.d.ts +1 -1
- package/dist/schema/union.d.ts +2 -2
- package/dist/schema/union.d.ts.map +1 -1
- package/package.json +5 -3
- package/src/core/schema.ts +12 -11
- package/src/core/standard-schema.test.ts +124 -0
- package/src/core/standard-schema.ts +31 -0
- package/src/core/string-format.ts +73 -31
- package/src/core/validation-error.ts +16 -1
- package/src/core/validation-issue.ts +32 -32
- package/src/core/validator.ts +26 -6
- package/src/core.ts +0 -1
- package/src/schema/array.test.ts +2 -2
- package/src/schema/blob.test.ts +317 -49
- package/src/schema/blob.ts +56 -23
- package/src/schema/custom.ts +1 -7
- package/src/schema/params.test.ts +2 -2
- package/src/schema/payload.ts +2 -2
- package/src/schema/ref.ts +1 -5
- package/src/schema/refine.ts +0 -1
- package/src/schema/string.test.ts +63 -0
- package/src/schema/string.ts +1 -1
- package/dist/core/property-key.d.ts +0 -2
- package/dist/core/property-key.d.ts.map +0 -1
- package/dist/core/property-key.js +0 -3
- package/dist/core/property-key.js.map +0 -1
- package/src/core/property-key.ts +0 -1
|
@@ -23,8 +23,12 @@ import { Issue } from './validation-issue.js';
|
|
|
23
23
|
* console.log(error.toJSON())
|
|
24
24
|
* // { error: 'InvalidRequest', message: '...', issues: [...] }
|
|
25
25
|
* ```
|
|
26
|
+
*
|
|
27
|
+
* @note this class implements {@link ResultFailure} to allow it to be used
|
|
28
|
+
* directly as a failure reason in validation results, avoiding the need for
|
|
29
|
+
* wrapping it in an additional object.
|
|
26
30
|
*/
|
|
27
|
-
export declare class LexValidationError extends LexError<'InvalidRequest'> {
|
|
31
|
+
export declare class LexValidationError extends LexError<'InvalidRequest'> implements ResultFailure<LexValidationError> {
|
|
28
32
|
name: string;
|
|
29
33
|
/**
|
|
30
34
|
* The list of validation issues that caused this error.
|
|
@@ -43,6 +47,10 @@ export declare class LexValidationError extends LexError<'InvalidRequest'> {
|
|
|
43
47
|
* @param options - Standard Error options (e.g., `cause`)
|
|
44
48
|
*/
|
|
45
49
|
constructor(issues: Issue[], options?: ErrorOptions);
|
|
50
|
+
/** @see {ResultFailure.success} */
|
|
51
|
+
readonly success: false;
|
|
52
|
+
/** @see {ResultFailure.reason} */
|
|
53
|
+
get reason(): this;
|
|
46
54
|
/**
|
|
47
55
|
* Converts the error to a JSON-serializable object.
|
|
48
56
|
*
|
|
@@ -51,7 +59,7 @@ export declare class LexValidationError extends LexError<'InvalidRequest'> {
|
|
|
51
59
|
toJSON(): {
|
|
52
60
|
issues: {
|
|
53
61
|
code: string;
|
|
54
|
-
path: readonly
|
|
62
|
+
path: readonly PropertyKey[];
|
|
55
63
|
message: string;
|
|
56
64
|
}[];
|
|
57
65
|
error: import("@atproto/lex-data").LexErrorCode;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validation-error.d.ts","sourceRoot":"","sources":["../../src/core/validation-error.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAA;AAE5C,OAAO,EAAE,aAAa,EAAiB,MAAM,aAAa,CAAA;AAC1D,OAAO,EACL,KAAK,EAGN,MAAM,uBAAuB,CAAA;AAE9B
|
|
1
|
+
{"version":3,"file":"validation-error.d.ts","sourceRoot":"","sources":["../../src/core/validation-error.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAA;AAE5C,OAAO,EAAE,aAAa,EAAiB,MAAM,aAAa,CAAA;AAC1D,OAAO,EACL,KAAK,EAGN,MAAM,uBAAuB,CAAA;AAE9B;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,qBAAa,kBACX,SAAQ,QAAQ,CAAC,gBAAgB,CACjC,YAAW,aAAa,CAAC,kBAAkB,CAAC;IAE5C,IAAI,SAAuB;IAE3B;;;;;OAKG;IACH,QAAQ,CAAC,MAAM,EAAE,KAAK,EAAE,CAAA;IAExB;;;;;;;;OAQG;gBACS,MAAM,EAAE,KAAK,EAAE,EAAE,OAAO,CAAC,EAAE,YAAY;IAMnD,mCAAmC;IACnC,QAAQ,CAAC,OAAO,EAAG,KAAK,CAAS;IAEjC,kCAAkC;IAClC,IAAI,MAAM,SAET;IAED;;;;OAIG;IACM,MAAM;;;;;;;;;IAOf;;;;;;;;;;;;;;;;OAgBG;IACH,MAAM,CAAC,YAAY,CACjB,QAAQ,EAAE,SAAS,aAAa,CAAC,kBAAkB,CAAC,EAAE,GACrD,kBAAkB;CAQtB"}
|
|
@@ -27,6 +27,10 @@ const validation_issue_js_1 = require("./validation-issue.js");
|
|
|
27
27
|
* console.log(error.toJSON())
|
|
28
28
|
* // { error: 'InvalidRequest', message: '...', issues: [...] }
|
|
29
29
|
* ```
|
|
30
|
+
*
|
|
31
|
+
* @note this class implements {@link ResultFailure} to allow it to be used
|
|
32
|
+
* directly as a failure reason in validation results, avoiding the need for
|
|
33
|
+
* wrapping it in an additional object.
|
|
30
34
|
*/
|
|
31
35
|
class LexValidationError extends lex_data_1.LexError {
|
|
32
36
|
name = 'LexValidationError';
|
|
@@ -51,6 +55,12 @@ class LexValidationError extends lex_data_1.LexError {
|
|
|
51
55
|
super('InvalidRequest', issuesAgg.join(', '), options);
|
|
52
56
|
this.issues = issuesAgg;
|
|
53
57
|
}
|
|
58
|
+
/** @see {ResultFailure.success} */
|
|
59
|
+
success = false;
|
|
60
|
+
/** @see {ResultFailure.reason} */
|
|
61
|
+
get reason() {
|
|
62
|
+
return this;
|
|
63
|
+
}
|
|
54
64
|
/**
|
|
55
65
|
* Converts the error to a JSON-serializable object.
|
|
56
66
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validation-error.js","sourceRoot":"","sources":["../../src/core/validation-error.ts"],"names":[],"mappings":";;;AAAA,gDAA4C;AAC5C,uDAA+C;AAC/C,2CAA0D;AAC1D,+DAI8B;AAE9B
|
|
1
|
+
{"version":3,"file":"validation-error.js","sourceRoot":"","sources":["../../src/core/validation-error.ts"],"names":[],"mappings":";;;AAAA,gDAA4C;AAC5C,uDAA+C;AAC/C,2CAA0D;AAC1D,+DAI8B;AAE9B;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,MAAa,kBACX,SAAQ,mBAA0B;IAGlC,IAAI,GAAG,oBAAoB,CAAA;IAE3B;;;;;OAKG;IACM,MAAM,CAAS;IAExB;;;;;;;;OAQG;IACH,YAAY,MAAe,EAAE,OAAsB;QACjD,MAAM,SAAS,GAAG,eAAe,CAAC,MAAM,CAAC,CAAA;QACzC,KAAK,CAAC,gBAAgB,EAAE,SAAS,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,OAAO,CAAC,CAAA;QACtD,IAAI,CAAC,MAAM,GAAG,SAAS,CAAA;IACzB,CAAC;IAED,mCAAmC;IAC1B,OAAO,GAAG,KAAc,CAAA;IAEjC,kCAAkC;IAClC,IAAI,MAAM;QACR,OAAO,IAAI,CAAA;IACb,CAAC;IAED;;;;OAIG;IACM,MAAM;QACb,OAAO;YACL,GAAG,KAAK,CAAC,MAAM,EAAE;YACjB,MAAM,EAAE,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,MAAM,EAAE,CAAC;SACnD,CAAA;IACH,CAAC;IAED;;;;;;;;;;;;;;;;OAgBG;IACH,MAAM,CAAC,YAAY,CACjB,QAAsD;QAEtD,IAAI,QAAQ,CAAC,MAAM,KAAK,CAAC;YAAE,OAAO,IAAA,yBAAa,EAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAA;QAC5D,MAAM,MAAM,GAAG,QAAQ,CAAC,OAAO,CAAC,oBAAoB,CAAC,CAAA;QACrD,OAAO,IAAI,kBAAkB,CAAC,MAAM,EA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CAAC,CAAC,IAAI,CAAC,EAC9C,CAAC,MAAM,EAAE,EAAE,CACT,IAAI,uCAAiB,CACnB,MAAM,CAAC,CAAC,CAAC,CAAC,IAAI,EACd,MAAM,CAAC,CAAC,CAAC,CAAC,KAAK,EACf,KAAK,CAAC,IAAI,CAAC,IAAI,GAAG,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,CACzD,CACJ;QACD,4BAA4B;QAC5B,GAAG,MAAM,CAAC,MAAM,CACd,CAAC,KAAK,EAAE,EAAE,CACR,CAAC,CAAC,KAAK,YAAY,sCAAgB,CAAC;YACpC,CAAC,CAAC,KAAK,YAAY,uCAAiB,CAAC,CACxC;KACF,CAAA;AACH,CAAC;AAED,wBAAwB;AACxB,SAAS,oBAAoB,CAC3B,CAAyB,EACzB,CAAyB;IAEzB,IAAI,CAAC,CAAC,MAAM,KAAK,CAAC,CAAC,MAAM;QAAE,OAAO,KAAK,CAAA;IACvC,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;QAClC,IAAI,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;YAAE,OAAO,KAAK,CAAA;IACjC,CAAC;IACD,OAAO,IAAI,CAAA;AACb,CAAC","sourcesContent":["import { LexError } from '@atproto/lex-data'\nimport { arrayAgg } from '../util/array-agg.js'\nimport { ResultFailure, failureReason } from './result.js'\nimport {\n Issue,\n IssueInvalidType,\n IssueInvalidValue,\n} from './validation-issue.js'\n\n/**\n * Error thrown when validation fails.\n *\n * Contains detailed information about all validation issues encountered,\n * including the path to each invalid value and descriptions of what was\n * expected vs what was received.\n *\n * Extends {@link LexError} with the error name \"InvalidRequest\" for\n * consistency with the AT Protocol error handling conventions.\n *\n * @example\n * ```typescript\n * const error = new LexValidationError([\n * new IssueInvalidType(['user', 'age'], 'hello', ['number'])\n * ])\n * console.log(error.message)\n * // \"Expected number value type at $.user.age (got string)\"\n *\n * console.log(error.issues.length) // 1\n * console.log(error.toJSON())\n * // { error: 'InvalidRequest', message: '...', issues: [...] }\n * ```\n *\n * @note this class implements {@link ResultFailure} to allow it to be used\n * directly as a failure reason in validation results, avoiding the need for\n * wrapping it in an additional object.\n */\nexport class LexValidationError\n extends LexError<'InvalidRequest'>\n implements ResultFailure<LexValidationError>\n{\n name = 'LexValidationError'\n\n /**\n * The list of validation issues that caused this error.\n *\n * Issues are aggregated when possible (e.g., multiple invalid type issues\n * at the same path are combined into a single issue listing all expected types).\n */\n readonly issues: Issue[]\n\n /**\n * Creates a new validation error from a list of issues.\n *\n * Issues are automatically aggregated to combine related issues at the same\n * path (e.g., multiple type expectations from a union schema).\n *\n * @param issues - The validation issues that caused this error\n * @param options - Standard Error options (e.g., `cause`)\n */\n constructor(issues: Issue[], options?: ErrorOptions) {\n const issuesAgg = aggregateIssues(issues)\n super('InvalidRequest', issuesAgg.join(', '), options)\n this.issues = issuesAgg\n }\n\n /** @see {ResultFailure.success} */\n readonly success = false as const\n\n /** @see {ResultFailure.reason} */\n get reason() {\n return this\n }\n\n /**\n * Converts the error to a JSON-serializable object.\n *\n * @returns An object containing the error details and issues details\n */\n override toJSON() {\n return {\n ...super.toJSON(),\n issues: this.issues.map((issue) => issue.toJSON()),\n }\n }\n\n /**\n * Creates a validation error by combining multiple validation failures.\n *\n * This is useful when validating against multiple possible schemas (e.g., unions)\n * and all branches fail. The resulting error contains issues from all failures.\n *\n * @param failures - The validation failures to combine\n * @returns A single validation error containing all issues from the failures\n *\n * @example\n * ```typescript\n * const failures = schemas.map(s => s.safeValidate(data)).filter(r => !r.success)\n * if (failures.length === schemas.length) {\n * throw LexValidationError.fromFailures(failures)\n * }\n * ```\n */\n static fromFailures(\n failures: readonly ResultFailure<LexValidationError>[],\n ): LexValidationError {\n if (failures.length === 1) return failureReason(failures[0])\n const issues = failures.flatMap(extractFailureIssues)\n return new LexValidationError(issues, {\n // Keep the original errors as the cause chain\n cause: failures.map(failureReason),\n })\n }\n}\n\nfunction extractFailureIssues(result: ResultFailure<LexValidationError>) {\n return result.reason.issues\n}\n\nfunction aggregateIssues(issues: Issue[]): Issue[] {\n // Quick path for common cases\n if (issues.length <= 1) return issues\n if (issues.length === 2 && issues[0].code !== issues[1].code) return issues\n\n return [\n // Aggregate invalid_type with identical paths\n ...arrayAgg(\n issues.filter((issue) => issue instanceof IssueInvalidType),\n (a, b) => comparePropertyPaths(a.path, b.path),\n (issues) =>\n new IssueInvalidType(\n issues[0].path,\n issues[0].input,\n Array.from(new Set(issues.flatMap((iss) => iss.expected))),\n ),\n ),\n // Aggregate invalid_value with identical paths\n ...arrayAgg(\n issues.filter((issue) => issue instanceof IssueInvalidValue),\n (a, b) => comparePropertyPaths(a.path, b.path),\n (issues) =>\n new IssueInvalidValue(\n issues[0].path,\n issues[0].input,\n Array.from(new Set(issues.flatMap((iss) => iss.values))),\n ),\n ),\n // Pass through other issues\n ...issues.filter(\n (issue) =>\n !(issue instanceof IssueInvalidType) &&\n !(issue instanceof IssueInvalidValue),\n ),\n ]\n}\n\n/*@__NO_SIDE_EFFECTS__*/\nfunction comparePropertyPaths(\n a: readonly PropertyKey[],\n b: readonly PropertyKey[],\n) {\n if (a.length !== b.length) return false\n for (let i = 0; i < a.length; i++) {\n if (a[i] !== b[i]) return false\n }\n return true\n}\n"]}
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { PropertyKey } from './property-key.js';
|
|
2
1
|
/**
|
|
3
2
|
* Abstract base class for all validation issues.
|
|
4
3
|
*
|
|
@@ -7,18 +6,20 @@ import { PropertyKey } from './property-key.js';
|
|
|
7
6
|
* - The path to the invalid value in the data structure
|
|
8
7
|
* - The actual input value that failed validation
|
|
9
8
|
*
|
|
10
|
-
* Subclasses add specific properties relevant to each issue type and
|
|
11
|
-
*
|
|
9
|
+
* Subclasses add specific properties relevant to each issue type and implement
|
|
10
|
+
* the {@link message} property for human-readable error messages (that don't
|
|
11
|
+
* contain the error path)
|
|
12
12
|
*/
|
|
13
13
|
export declare abstract class Issue {
|
|
14
14
|
readonly code: string;
|
|
15
15
|
readonly path: readonly PropertyKey[];
|
|
16
16
|
readonly input: unknown;
|
|
17
|
+
abstract readonly message: string;
|
|
17
18
|
constructor(code: string, path: readonly PropertyKey[], input: unknown);
|
|
18
19
|
/**
|
|
19
20
|
* Returns a human-readable description of the validation issue.
|
|
20
21
|
*/
|
|
21
|
-
|
|
22
|
+
toString(): string;
|
|
22
23
|
/**
|
|
23
24
|
* Converts the issue to a JSON-serializable object.
|
|
24
25
|
*
|
|
@@ -40,7 +41,6 @@ export declare class IssueCustom extends Issue {
|
|
|
40
41
|
readonly input: unknown;
|
|
41
42
|
readonly message: string;
|
|
42
43
|
constructor(path: readonly PropertyKey[], input: unknown, message: string);
|
|
43
|
-
toString(): string;
|
|
44
44
|
}
|
|
45
45
|
/**
|
|
46
46
|
* Issue for string values that don't match an expected format.
|
|
@@ -49,17 +49,17 @@ export declare class IssueCustom extends Issue {
|
|
|
49
49
|
*/
|
|
50
50
|
export declare class IssueInvalidFormat extends Issue {
|
|
51
51
|
readonly format: string;
|
|
52
|
-
readonly
|
|
53
|
-
constructor(path: readonly PropertyKey[], input: unknown, format: string,
|
|
54
|
-
|
|
52
|
+
readonly detail?: string | undefined;
|
|
53
|
+
constructor(path: readonly PropertyKey[], input: unknown, format: string, detail?: string | undefined);
|
|
54
|
+
get message(): string;
|
|
55
|
+
/** Returns a human-readable description of the expected format. */
|
|
56
|
+
get formatDescription(): string;
|
|
55
57
|
toJSON(): {
|
|
56
58
|
format: string;
|
|
57
59
|
code: string;
|
|
58
60
|
path: readonly PropertyKey[];
|
|
59
61
|
message: string;
|
|
60
62
|
};
|
|
61
|
-
/** Returns a human-readable description of the expected format. */
|
|
62
|
-
get formatDescription(): string;
|
|
63
63
|
}
|
|
64
64
|
/**
|
|
65
65
|
* Issue for values that have an unexpected type.
|
|
@@ -70,7 +70,7 @@ export declare class IssueInvalidFormat extends Issue {
|
|
|
70
70
|
export declare class IssueInvalidType extends Issue {
|
|
71
71
|
readonly expected: readonly string[];
|
|
72
72
|
constructor(path: readonly PropertyKey[], input: unknown, expected: readonly string[]);
|
|
73
|
-
|
|
73
|
+
get message(): string;
|
|
74
74
|
toJSON(): {
|
|
75
75
|
expected: readonly string[];
|
|
76
76
|
code: string;
|
|
@@ -87,7 +87,7 @@ export declare class IssueInvalidType extends Issue {
|
|
|
87
87
|
export declare class IssueInvalidValue extends Issue {
|
|
88
88
|
readonly values: readonly unknown[];
|
|
89
89
|
constructor(path: readonly PropertyKey[], input: unknown, values: readonly unknown[]);
|
|
90
|
-
|
|
90
|
+
get message(): string;
|
|
91
91
|
toJSON(): {
|
|
92
92
|
values: readonly unknown[];
|
|
93
93
|
code: string;
|
|
@@ -101,7 +101,7 @@ export declare class IssueInvalidValue extends Issue {
|
|
|
101
101
|
export declare class IssueRequiredKey extends Issue {
|
|
102
102
|
readonly key: PropertyKey;
|
|
103
103
|
constructor(path: readonly PropertyKey[], input: unknown, key: PropertyKey);
|
|
104
|
-
|
|
104
|
+
get message(): string;
|
|
105
105
|
toJSON(): {
|
|
106
106
|
key: PropertyKey;
|
|
107
107
|
code: string;
|
|
@@ -128,7 +128,7 @@ export declare class IssueTooBig extends Issue {
|
|
|
128
128
|
readonly type: MeasurableType;
|
|
129
129
|
readonly actual: number;
|
|
130
130
|
constructor(path: readonly PropertyKey[], input: unknown, maximum: number, type: MeasurableType, actual: number);
|
|
131
|
-
|
|
131
|
+
get message(): string;
|
|
132
132
|
toJSON(): {
|
|
133
133
|
type: MeasurableType;
|
|
134
134
|
maximum: number;
|
|
@@ -145,7 +145,7 @@ export declare class IssueTooSmall extends Issue {
|
|
|
145
145
|
readonly type: MeasurableType;
|
|
146
146
|
readonly actual: number;
|
|
147
147
|
constructor(path: readonly PropertyKey[], input: unknown, minimum: number, type: MeasurableType, actual: number);
|
|
148
|
-
|
|
148
|
+
get message(): string;
|
|
149
149
|
toJSON(): {
|
|
150
150
|
type: MeasurableType;
|
|
151
151
|
minimum: number;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validation-issue.d.ts","sourceRoot":"","sources":["../../src/core/validation-issue.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"validation-issue.d.ts","sourceRoot":"","sources":["../../src/core/validation-issue.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;GAWG;AACH,8BAAsB,KAAK;IAIvB,QAAQ,CAAC,IAAI,EAAE,MAAM;IACrB,QAAQ,CAAC,IAAI,EAAE,SAAS,WAAW,EAAE;IACrC,QAAQ,CAAC,KAAK,EAAE,OAAO;IALzB,QAAQ,CAAC,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAA;gBAGtB,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,SAAS,WAAW,EAAE,EAC5B,KAAK,EAAE,OAAO;IAGzB;;OAEG;IACH,QAAQ;IAIR;;;;OAIG;IACH,MAAM;;;;;CAOP;AAED;;;;GAIG;AACH,qBAAa,WAAY,SAAQ,KAAK;IAElC,QAAQ,CAAC,IAAI,EAAE,SAAS,WAAW,EAAE;IACrC,QAAQ,CAAC,KAAK,EAAE,OAAO;IACvB,QAAQ,CAAC,OAAO,EAAE,MAAM;gBAFf,IAAI,EAAE,SAAS,WAAW,EAAE,EAC5B,KAAK,EAAE,OAAO,EACd,OAAO,EAAE,MAAM;CAI3B;AAED;;;;GAIG;AACH,qBAAa,kBAAmB,SAAQ,KAAK;IAIzC,QAAQ,CAAC,MAAM,EAAE,MAAM;IACvB,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM;gBAHxB,IAAI,EAAE,SAAS,WAAW,EAAE,EAC5B,KAAK,EAAE,OAAO,EACL,MAAM,EAAE,MAAM,EACd,MAAM,CAAC,EAAE,MAAM,YAAA;IAK1B,IAAa,OAAO,IAAI,MAAM,CAE7B;IAED,mEAAmE;IACnE,IAAI,i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IAAI,MAAM,CAE7B;IAED,MAAM;;;;;;;CAOP;AAED;;GAEG;AACH,qBAAa,aAAc,SAAQ,KAAK;IAIpC,QAAQ,CAAC,OAAO,EAAE,MAAM;IACxB,QAAQ,CAAC,IAAI,EAAE,cAAc;IAC7B,QAAQ,CAAC,MAAM,EAAE,MAAM;gBAJvB,IAAI,EAAE,SAAS,WAAW,EAAE,EAC5B,KAAK,EAAE,OAAO,EACL,OAAO,EAAE,MAAM,EACf,IAAI,EAAE,cAAc,EACpB,MAAM,EAAE,MAAM;IAKzB,IAAa,OAAO,IAAI,MAAM,CAE7B;IAED,MAAM;;;;;;;CAOP"}
|
|
@@ -10,8 +10,9 @@ const lex_data_1 = require("@atproto/lex-data");
|
|
|
10
10
|
* - The path to the invalid value in the data structure
|
|
11
11
|
* - The actual input value that failed validation
|
|
12
12
|
*
|
|
13
|
-
* Subclasses add specific properties relevant to each issue type and
|
|
14
|
-
*
|
|
13
|
+
* Subclasses add specific properties relevant to each issue type and implement
|
|
14
|
+
* the {@link message} property for human-readable error messages (that don't
|
|
15
|
+
* contain the error path)
|
|
15
16
|
*/
|
|
16
17
|
class Issue {
|
|
17
18
|
code;
|
|
@@ -22,6 +23,12 @@ class Issue {
|
|
|
22
23
|
this.path = path;
|
|
23
24
|
this.input = input;
|
|
24
25
|
}
|
|
26
|
+
/**
|
|
27
|
+
* Returns a human-readable description of the validation issue.
|
|
28
|
+
*/
|
|
29
|
+
toString() {
|
|
30
|
+
return `${this.message}${stringifyPath(this.path)}`;
|
|
31
|
+
}
|
|
25
32
|
/**
|
|
26
33
|
* Converts the issue to a JSON-serializable object.
|
|
27
34
|
*
|
|
@@ -31,7 +38,7 @@ class Issue {
|
|
|
31
38
|
return {
|
|
32
39
|
code: this.code,
|
|
33
40
|
path: this.path,
|
|
34
|
-
message: this.
|
|
41
|
+
message: this.message,
|
|
35
42
|
};
|
|
36
43
|
}
|
|
37
44
|
}
|
|
@@ -51,9 +58,6 @@ class IssueCustom extends Issue {
|
|
|
51
58
|
this.input = input;
|
|
52
59
|
this.message = message;
|
|
53
60
|
}
|
|
54
|
-
toString() {
|
|
55
|
-
return `${this.message}${stringifyPath(this.path)}`;
|
|
56
|
-
}
|
|
57
61
|
}
|
|
58
62
|
exports.IssueCustom = IssueCustom;
|
|
59
63
|
/**
|
|
@@ -63,20 +67,14 @@ exports.IssueCustom = IssueCustom;
|
|
|
63
67
|
*/
|
|
64
68
|
class IssueInvalidFormat extends Issue {
|
|
65
69
|
format;
|
|
66
|
-
|
|
67
|
-
constructor(path, input, format,
|
|
70
|
+
detail;
|
|
71
|
+
constructor(path, input, format, detail) {
|
|
68
72
|
super('invalid_format', path, input);
|
|
69
73
|
this.format = format;
|
|
70
|
-
this.
|
|
74
|
+
this.detail = detail;
|
|
71
75
|
}
|
|
72
|
-
|
|
73
|
-
return `Invalid ${this.formatDescription}${this.
|
|
74
|
-
}
|
|
75
|
-
toJSON() {
|
|
76
|
-
return {
|
|
77
|
-
...super.toJSON(),
|
|
78
|
-
format: this.format,
|
|
79
|
-
};
|
|
76
|
+
get message() {
|
|
77
|
+
return `Invalid ${this.formatDescription}${this.detail ? ` (${this.detail}, ` : ' ('}got ${stringifyValue(this.input)})`;
|
|
80
78
|
}
|
|
81
79
|
/** Returns a human-readable description of the expected format. */
|
|
82
80
|
get formatDescription() {
|
|
@@ -97,6 +95,12 @@ class IssueInvalidFormat extends Issue {
|
|
|
97
95
|
return this.format;
|
|
98
96
|
}
|
|
99
97
|
}
|
|
98
|
+
toJSON() {
|
|
99
|
+
return {
|
|
100
|
+
...super.toJSON(),
|
|
101
|
+
format: this.format,
|
|
102
|
+
};
|
|
103
|
+
}
|
|
100
104
|
}
|
|
101
105
|
exports.IssueInvalidFormat = IssueInvalidFormat;
|
|
102
106
|
/**
|
|
@@ -111,8 +115,8 @@ class IssueInvalidType extends Issue {
|
|
|
111
115
|
super('invalid_type', path, input);
|
|
112
116
|
this.expected = expected;
|
|
113
117
|
}
|
|
114
|
-
|
|
115
|
-
return `Expected ${oneOf(this.expected.map(stringifyExpectedType))} value type
|
|
118
|
+
get message() {
|
|
119
|
+
return `Expected ${oneOf(this.expected.map(stringifyExpectedType))} value type (got ${stringifyType(this.input)})`;
|
|
116
120
|
}
|
|
117
121
|
toJSON() {
|
|
118
122
|
return {
|
|
@@ -134,8 +138,8 @@ class IssueInvalidValue extends Issue {
|
|
|
134
138
|
super('invalid_value', path, input);
|
|
135
139
|
this.values = values;
|
|
136
140
|
}
|
|
137
|
-
|
|
138
|
-
return `Expected ${oneOf(this.values.map(stringifyValue))}
|
|
141
|
+
get message() {
|
|
142
|
+
return `Expected ${oneOf(this.values.map(stringifyValue))} (got ${stringifyValue(this.input)})`;
|
|
139
143
|
}
|
|
140
144
|
toJSON() {
|
|
141
145
|
return {
|
|
@@ -154,8 +158,8 @@ class IssueRequiredKey extends Issue {
|
|
|
154
158
|
super('required_key', path, input);
|
|
155
159
|
this.key = key;
|
|
156
160
|
}
|
|
157
|
-
|
|
158
|
-
return `Missing required key "${String(this.key)}"
|
|
161
|
+
get message() {
|
|
162
|
+
return `Missing required key "${String(this.key)}"`;
|
|
159
163
|
}
|
|
160
164
|
toJSON() {
|
|
161
165
|
return {
|
|
@@ -178,8 +182,8 @@ class IssueTooBig extends Issue {
|
|
|
178
182
|
this.type = type;
|
|
179
183
|
this.actual = actual;
|
|
180
184
|
}
|
|
181
|
-
|
|
182
|
-
return `${this.type} too big (maximum ${this.maximum}
|
|
185
|
+
get message() {
|
|
186
|
+
return `${this.type} too big (maximum ${this.maximum}, got ${this.actual})`;
|
|
183
187
|
}
|
|
184
188
|
toJSON() {
|
|
185
189
|
return {
|
|
@@ -203,8 +207,8 @@ class IssueTooSmall extends Issue {
|
|
|
203
207
|
this.type = type;
|
|
204
208
|
this.actual = actual;
|
|
205
209
|
}
|
|
206
|
-
|
|
207
|
-
return `${this.type} too small (minimum ${this.minimum}
|
|
210
|
+
get message() {
|
|
211
|
+
return `${this.type} too small (minimum ${this.minimum}, got ${this.actual})`;
|
|
208
212
|
}
|
|
209
213
|
toJSON() {
|
|
210
214
|
return {
|
|
@@ -231,8 +235,8 @@ function buildJsonPath(path) {
|
|
|
231
235
|
return `$${path.map(toJsonPathSegment).join('')}`;
|
|
232
236
|
}
|
|
233
237
|
function toJsonPathSegment(segment) {
|
|
234
|
-
if (typeof segment === 'number') {
|
|
235
|
-
return `[${segment}]`;
|
|
238
|
+
if (typeof segment === 'number' || typeof segment === 'symbol') {
|
|
239
|
+
return `[${String(segment)}]`;
|
|
236
240
|
}
|
|
237
241
|
else if (/^[a-zA-Z_$][a-zA-Z0-9_]*$/.test(segment)) {
|
|
238
242
|
return `.${segment}`;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validation-issue.js","sourceRoot":"","sources":["../../src/core/validation-issue.ts"],"names":[],"mappings":";;;AAAA,gDAAyE;AAGzE;;;;;;;;;;GAUG;AACH,MAAsB,KAAK;IAEd;IACA;IACA;IAHX,YACW,IAAY,EACZ,IAA4B,EAC5B,KAAc;QAFd,SAAI,GAAJ,IAAI,CAAQ;QACZ,SAAI,GAAJ,IAAI,CAAwB;QAC5B,UAAK,GAAL,KAAK,CAAS;IACtB,CAAC;IAOJ;;;;OAIG;IACH,MAAM;QACJ,OAAO;YACL,IAAI,EAAE,IAAI,CAAC,IAAI;YACf,IAAI,EAAE,IAAI,CAAC,IAAI;YACf,OAAO,EAAE,IAAI,CAAC,QAAQ,EAAE;SACzB,CAAA;IACH,CAAC;CACF;AAxBD,sBAwBC;AAED;;;;GAIG;AACH,MAAa,WAAY,SAAQ,KAAK;IAEzB;IACA;IACA;IAHX,YACW,IAA4B,EAC5B,KAAc,EACd,OAAe;QAExB,KAAK,CAAC,QAAQ,EAAE,IAAI,EAAE,KAAK,CAAC,CAAA;QAJnB,SAAI,GAAJ,IAAI,CAAwB;QAC5B,UAAK,GAAL,KAAK,CAAS;QACd,YAAO,GAAP,OAAO,CAAQ;IAG1B,CAAC;IAED,QAAQ;QACN,OAA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{ PropertyKey } from './property-key.js'\n\n/**\n * Abstract base class for all validation issues.\n *\n * An issue represents a single validation failure, containing:\n * - A code identifying the type of issue\n * - The path to the invalid value in the data structure\n * - The actual input value that failed validation\n *\n * Subclasses add specific properties relevant to each issue type and\n * implement the {@link toString} method for human-readable error messages.\n */\nexport abstract class Issue {\n constructor(\n readonly code: string,\n readonly path: readonly PropertyKey[],\n readonly input: unknown,\n ) {}\n\n /**\n * Returns a human-readable description of the validation issue.\n */\n abstract toString(): string\n\n /**\n * Converts the issue to a JSON-serializable object.\n *\n * @returns An object containing the issue code, path, and message\n */\n toJSON() {\n return {\n code: this.code,\n path: this.path,\n message: this.toString(),\n }\n }\n}\n\n/**\n * A custom validation issue with a user-defined message.\n *\n * Use this for validation rules that don't fit into the standard issue categories.\n */\nexport class IssueCustom extends Issue {\n constructor(\n readonly path: readonly PropertyKey[],\n readonly input: unknown,\n readonly message: string,\n ) {\n super('custom', path, input)\n }\n\n toString() {\n return `${this.message}${stringifyPath(this.path)}`\n }\n}\n\n/**\n * Issue for string values that don't match an expected format.\n *\n * Used for AT Protocol specific formats like DID, handle, NSID, AT-URI, etc.\n */\nexport class IssueInvalidFormat extends Issue {\n constructor(\n path: readonly PropertyKey[],\n input: unknown,\n readonly format: string,\n readonly message?: string,\n ) {\n super('invalid_format', path, input)\n }\n\n toString() {\n return `Invalid ${this.formatDescription}${this.message ? ` (${this.message})` : ''}${stringifyPath(this.path)} (got ${stringifyValue(this.input)})`\n }\n\n toJSON() {\n return {\n ...super.toJSON(),\n format: this.format,\n }\n }\n\n /** Returns a human-readable description of the expected format. */\n get formatDescription(): string {\n switch (this.format) {\n case 'at-identifier':\n return `AT identifier`\n case 'did':\n return `DID`\n case 'nsid':\n return `NSID`\n case 'cid':\n return `CID string`\n case 'tid':\n return `TID string`\n case 'record-key':\n return `record key`\n default:\n return this.format\n }\n }\n}\n\n/**\n * Issue for values that have an unexpected type.\n *\n * This is one of the most common validation issues, occurring when the\n * runtime type of a value doesn't match the expected schema type.\n */\nexport class IssueInvalidType extends Issue {\n constructor(\n path: readonly PropertyKey[],\n input: unknown,\n readonly expected: readonly string[],\n ) {\n super('invalid_type', path, input)\n }\n\n toString() {\n return `Expected ${oneOf(this.expected.map(stringifyExpectedType))} value type${stringifyPath(this.path)} (got ${stringifyType(this.input)})`\n }\n\n toJSON() {\n return {\n ...super.toJSON(),\n expected: this.expected,\n }\n }\n}\n\n/**\n * Issue for values that don't match any of the expected literal values.\n *\n * Used when a value must be one of a specific set of allowed values\n * (e.g., enum-like constraints).\n */\nexport class IssueInvalidValue extends Issue {\n constructor(\n path: readonly PropertyKey[],\n input: unknown,\n readonly values: readonly unknown[],\n ) {\n super('invalid_value', path, input)\n }\n\n toString() {\n return `Expected ${oneOf(this.values.map(stringifyValue))}${stringifyPath(this.path)} (got ${stringifyValue(this.input)})`\n }\n\n toJSON() {\n return {\n ...super.toJSON(),\n values: this.values,\n }\n }\n}\n\n/**\n * Issue for missing required object properties.\n */\nexport class IssueRequiredKey extends Issue {\n constructor(\n path: readonly PropertyKey[],\n input: unknown,\n readonly key: PropertyKey,\n ) {\n super('required_key', path, input)\n }\n\n toString() {\n return `Missing required key \"${String(this.key)}\"${stringifyPath(this.path)}`\n }\n\n toJSON() {\n return {\n ...super.toJSON(),\n key: this.key,\n }\n }\n}\n\n/**\n * The type of measurement for size constraint issues.\n *\n * - `'array'` - Array length\n * - `'string'` - String length in characters\n * - `'integer'` - Numeric value\n * - `'grapheme'` - String length in grapheme clusters\n * - `'bytes'` - Byte length\n * - `'blob'` - Blob size\n */\nexport type MeasurableType =\n | 'array'\n | 'string'\n | 'integer'\n | 'grapheme'\n | 'bytes'\n | 'blob'\n\n/**\n * Issue for values that exceed a maximum constraint.\n */\nexport class IssueTooBig extends Issue {\n constructor(\n path: readonly PropertyKey[],\n input: unknown,\n readonly maximum: number,\n readonly type: MeasurableType,\n readonly actual: number,\n ) {\n super('too_big', path, input)\n }\n\n toString() {\n return `${this.type} too big (maximum ${this.maximum})${stringifyPath(this.path)} (got ${this.actual})`\n }\n\n toJSON() {\n return {\n ...super.toJSON(),\n type: this.type,\n maximum: this.maximum,\n }\n }\n}\n\n/**\n * Issue for values that are below a minimum constraint.\n */\nexport class IssueTooSmall extends Issue {\n constructor(\n path: readonly PropertyKey[],\n input: unknown,\n readonly minimum: number,\n readonly type: MeasurableType,\n readonly actual: number,\n ) {\n super('too_small', path, input)\n }\n\n toString() {\n return `${this.type} too small (minimum ${this.minimum})${stringifyPath(this.path)} (got ${this.actual})`\n }\n\n toJSON() {\n return {\n ...super.toJSON(),\n type: this.type,\n minimum: this.minimum,\n }\n }\n}\n\n// -----------------------------------------------------------------------------\n// Helper functions for formatting error messages\n// -----------------------------------------------------------------------------\n\nfunction stringifyExpectedType(expected: string): string {\n if (expected === '$typed') {\n return 'an object which includes the \"$type\" property'\n }\n return expected\n}\n\nfunction stringifyPath(path: readonly PropertyKey[]) {\n return ` at ${buildJsonPath(path)}`\n}\n\nfunction buildJsonPath(path: readonly PropertyKey[]): string {\n return `$${path.map(toJsonPathSegment).join('')}`\n}\n\nfunction toJsonPathSegment(segment: PropertyKey): string {\n if (typeof segment === 'number') {\n return `[${segment}]`\n } else if (/^[a-zA-Z_$][a-zA-Z0-9_]*$/.test(segment as string)) {\n return `.${segment}`\n } else {\n return `[${JSON.stringify(segment)}]`\n }\n}\n\nfunction oneOf(arr: readonly string[]): string {\n if (arr.length === 0) return ''\n if (arr.length === 1) return arr[0]\n return `one of ${arr.slice(0, -1).join(', ')} or ${arr.at(-1)}`\n}\n\nfunction stringifyType(value: unknown): string {\n switch (typeof value) {\n case 'object':\n if (value === null) return 'null'\n if (Array.isArray(value)) return 'array'\n if (ifCid(value)) return 'cid'\n if (isLegacyBlobRef(value)) return 'legacy-blob'\n if (value instanceof Date) return 'date'\n if (value instanceof RegExp) return 'regexp'\n if (value instanceof Map) return 'map'\n if (value instanceof Set) return 'set'\n return 'object'\n case 'number':\n if (Number.isInteger(value) && Number.isSafeInteger(value)) {\n return 'integer'\n }\n if (Number.isNaN(value)) {\n return 'NaN'\n }\n if (value === Infinity) {\n return 'Infinity'\n }\n if (value === -Infinity) {\n return '-Infinity'\n }\n return 'float'\n default:\n return typeof value\n }\n}\n\nfunction stringifyValue(value: unknown): string {\n switch (typeof value) {\n case 'bigint':\n return `${value}n`\n case 'number':\n case 'string':\n case 'boolean':\n return JSON.stringify(value)\n case 'object':\n if (Array.isArray(value)) {\n return `[${stringifyArray(value, stringifyValue)}]`\n }\n if (isPlainObject(value)) {\n return `{${stringifyArray(Object.entries(value), stringifyObjectEntry)}}`\n }\n // fallthrough\n default:\n return stringifyType(value)\n }\n}\n\n/*@__NO_SIDE_EFFECTS__*/\nfunction stringifyObjectEntry([key, _value]: [PropertyKey, unknown]): string {\n return `${JSON.stringify(key)}: ...`\n}\n\n/*@__NO_SIDE_EFFECTS__*/\nfunction stringifyArray<T>(\n arr: readonly T[],\n fn: (item: T) => string,\n n = 2,\n): string {\n return arr.slice(0, n).map(fn).join(', ') + (arr.length > n ? ', ...' : '')\n}\n"]}
|
|
1
|
+
{"version":3,"file":"validation-issue.js","sourceRoot":"","sources":["../../src/core/validation-issue.ts"],"names":[],"mappings":";;;AAAA,gDAAyE;AAEzE;;;;;;;;;;;GAWG;AACH,MAAsB,KAAK;IAId;IACA;IACA;IAHX,YACW,IAAY,EACZ,IAA4B,EAC5B,KAAc;QAFd,SAAI,GAAJ,IAAI,CAAQ;QACZ,SAAI,GAAJ,IAAI,CAAwB;QAC5B,UAAK,GAAL,KAAK,CAAS;IACtB,CAAC;IAEJ;;OAEG;IACH,QAAQ;QACN,OAAO,GAAG,IAAI,CAAC,OAAO,GAAG,aAAa,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAA;IACrD,CAAC;IAED;;;;OAIG;IACH,MAAM;QACJ,OAAO;YACL,IAAI,EAAE,IAAI,CAAC,IAAI;YACf,IAAI,EAAE,IAAI,CAAC,IAAI;YACf,OAAO,EAAE,IAAI,CAAC,OAAO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esContent":["import { ifCid, isLegacyBlobRef, isPlainObject } from '@atproto/lex-data'\n\n/**\n * Abstract base class for all validation issues.\n *\n * An issue represents a single validation failure, containing:\n * - A code identifying the type of issue\n * - The path to the invalid value in the data structure\n * - The actual input value that failed validation\n *\n * Subclasses add specific properties relevant to each issue type and implement\n * the {@link message} property for human-readable error messages (that don't\n * contain the error path)\n */\nexport abstract class Issue {\n abstract readonly message: string\n\n constructor(\n readonly code: string,\n readonly path: readonly PropertyKey[],\n readonly input: unknown,\n ) {}\n\n /**\n * Returns a human-readable description of the validation issue.\n */\n toString() {\n return `${this.message}${stringifyPath(this.path)}`\n }\n\n /**\n * Converts the issue to a JSON-serializable object.\n *\n * @returns An object containing the issue code, path, and message\n */\n toJSON() {\n return {\n code: this.code,\n path: this.path,\n message: this.message,\n }\n }\n}\n\n/**\n * A custom validation issue with a user-defined message.\n *\n * Use this for validation rules that don't fit into the standard issue categories.\n */\nexport class IssueCustom extends Issue {\n constructor(\n readonly path: readonly PropertyKey[],\n readonly input: unknown,\n readonly message: string,\n ) {\n super('custom', path, input)\n }\n}\n\n/**\n * Issue for string values that don't match an expected format.\n *\n * Used for AT Protocol specific formats like DID, handle, NSID, AT-URI, etc.\n */\nexport class IssueInvalidFormat extends Issue {\n constructor(\n path: readonly PropertyKey[],\n input: unknown,\n readonly format: string,\n readonly detail?: string,\n ) {\n super('invalid_format', path, input)\n }\n\n override get message(): string {\n return `Invalid ${this.formatDescription}${this.detail ? ` (${this.detail}, ` : ' ('}got ${stringifyValue(this.input)})`\n }\n\n /** Returns a human-readable description of the expected format. */\n get formatDescription(): string {\n switch (this.format) {\n case 'at-identifier':\n return `AT identifier`\n case 'did':\n return `DID`\n case 'nsid':\n return `NSID`\n case 'cid':\n return `CID string`\n case 'tid':\n return `TID string`\n case 'record-key':\n return `record key`\n default:\n return this.format\n }\n }\n\n toJSON() {\n return {\n ...super.toJSON(),\n format: this.format,\n }\n }\n}\n\n/**\n * Issue for values that have an unexpected type.\n *\n * This is one of the most common validation issues, occurring when the\n * runtime type of a value doesn't match the expected schema type.\n */\nexport class IssueInvalidType extends Issue {\n constructor(\n path: readonly PropertyKey[],\n input: unknown,\n readonly expected: readonly string[],\n ) {\n super('invalid_type', path, input)\n }\n\n override get message(): string {\n return `Expected ${oneOf(this.expected.map(stringifyExpectedType))} value type (got ${stringifyType(this.input)})`\n }\n\n toJSON() {\n return {\n ...super.toJSON(),\n expected: this.expected,\n }\n }\n}\n\n/**\n * Issue for values that don't match any of the expected literal values.\n *\n * Used when a value must be one of a specific set of allowed values\n * (e.g., enum-like constraints).\n */\nexport class IssueInvalidValue extends Issue {\n constructor(\n path: readonly PropertyKey[],\n input: unknown,\n readonly values: readonly unknown[],\n ) {\n super('invalid_value', path, input)\n }\n\n override get message(): string {\n return `Expected ${oneOf(this.values.map(stringifyValue))} (got ${stringifyValue(this.input)})`\n }\n\n toJSON() {\n return {\n ...super.toJSON(),\n values: this.values,\n }\n }\n}\n\n/**\n * Issue for missing required object properties.\n */\nexport class IssueRequiredKey extends Issue {\n constructor(\n path: readonly PropertyKey[],\n input: unknown,\n readonly key: PropertyKey,\n ) {\n super('required_key', path, input)\n }\n\n override get message(): string {\n return `Missing required key \"${String(this.key)}\"`\n }\n\n toJSON() {\n return {\n ...super.toJSON(),\n key: this.key,\n }\n }\n}\n\n/**\n * The type of measurement for size constraint issues.\n *\n * - `'array'` - Array length\n * - `'string'` - String length in characters\n * - `'integer'` - Numeric value\n * - `'grapheme'` - String length in grapheme clusters\n * - `'bytes'` - Byte length\n * - `'blob'` - Blob size\n */\nexport type MeasurableType =\n | 'array'\n | 'string'\n | 'integer'\n | 'grapheme'\n | 'bytes'\n | 'blob'\n\n/**\n * Issue for values that exceed a maximum constraint.\n */\nexport class IssueTooBig extends Issue {\n constructor(\n path: readonly PropertyKey[],\n input: unknown,\n readonly maximum: number,\n readonly type: MeasurableType,\n readonly actual: number,\n ) {\n super('too_big', path, input)\n }\n\n override get message(): string {\n return `${this.type} too big (maximum ${this.maximum}, got ${this.actual})`\n }\n\n toJSON() {\n return {\n ...super.toJSON(),\n type: this.type,\n maximum: this.maximum,\n }\n }\n}\n\n/**\n * Issue for values that are below a minimum constraint.\n */\nexport class IssueTooSmall extends Issue {\n constructor(\n path: readonly PropertyKey[],\n input: unknown,\n readonly minimum: number,\n readonly type: MeasurableType,\n readonly actual: number,\n ) {\n super('too_small', path, input)\n }\n\n override get message(): string {\n return `${this.type} too small (minimum ${this.minimum}, got ${this.actual})`\n }\n\n toJSON() {\n return {\n ...super.toJSON(),\n type: this.type,\n minimum: this.minimum,\n }\n }\n}\n\n// -----------------------------------------------------------------------------\n// Helper functions for formatting error messages\n// -----------------------------------------------------------------------------\n\nfunction stringifyExpectedType(expected: string): string {\n if (expected === '$typed') {\n return 'an object which includes the \"$type\" property'\n }\n return expected\n}\n\nfunction stringifyPath(path: readonly PropertyKey[]) {\n return ` at ${buildJsonPath(path)}`\n}\n\nfunction buildJsonPath(path: readonly PropertyKey[]): string {\n return `$${path.map(toJsonPathSegment).join('')}`\n}\n\nfunction toJsonPathSegment(segment: PropertyKey): string {\n if (typeof segment === 'number' || typeof segment === 'symbol') {\n return `[${String(segment)}]`\n } else if (/^[a-zA-Z_$][a-zA-Z0-9_]*$/.test(segment)) {\n return `.${segment}`\n } else {\n return `[${JSON.stringify(segment)}]`\n }\n}\n\nfunction oneOf(arr: readonly string[]): string {\n if (arr.length === 0) return ''\n if (arr.length === 1) return arr[0]\n return `one of ${arr.slice(0, -1).join(', ')} or ${arr.at(-1)}`\n}\n\nfunction stringifyType(value: unknown): string {\n switch (typeof value) {\n case 'object':\n if (value === null) return 'null'\n if (Array.isArray(value)) return 'array'\n if (ifCid(value)) return 'cid'\n if (isLegacyBlobRef(value)) return 'legacy-blob'\n if (value instanceof Date) return 'date'\n if (value instanceof RegExp) return 'regexp'\n if (value instanceof Map) return 'map'\n if (value instanceof Set) return 'set'\n return 'object'\n case 'number':\n if (Number.isInteger(value) && Number.isSafeInteger(value)) {\n return 'integer'\n }\n if (Number.isNaN(value)) {\n return 'NaN'\n }\n if (value === Infinity) {\n return 'Infinity'\n }\n if (value === -Infinity) {\n return '-Infinity'\n }\n return 'float'\n default:\n return typeof value\n }\n}\n\nfunction stringifyValue(value: unknown): string {\n switch (typeof value) {\n case 'bigint':\n return `${value}n`\n case 'number':\n case 'string':\n case 'boolean':\n return JSON.stringify(value)\n case 'object':\n if (Array.isArray(value)) {\n return `[${stringifyArray(value, stringifyValue)}]`\n }\n if (isPlainObject(value)) {\n return `{${stringifyArray(Object.entries(value), stringifyObjectEntry)}}`\n }\n // fallthrough\n default:\n return stringifyType(value)\n }\n}\n\n/*@__NO_SIDE_EFFECTS__*/\nfunction stringifyObjectEntry([key, _value]: [PropertyKey, unknown]): string {\n return `${JSON.stringify(key)}: ...`\n}\n\n/*@__NO_SIDE_EFFECTS__*/\nfunction stringifyArray<T>(\n arr: readonly T[],\n fn: (item: T) => string,\n n = 2,\n): string {\n return arr.slice(0, n).map(fn).join(', ') + (arr.length > n ? ', ...' : '')\n}\n"]}
|
package/dist/core/validator.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { ResultFailure, ResultSuccess } from './result.js';
|
|
1
|
+
import { ResultSuccess } from './result.js';
|
|
3
2
|
import { LexValidationError } from './validation-error.js';
|
|
4
3
|
import { Issue, MeasurableType } from './validation-issue.js';
|
|
5
4
|
/**
|
|
@@ -10,8 +9,12 @@ import { Issue, MeasurableType } from './validation-issue.js';
|
|
|
10
9
|
export type ValidationSuccess<Value = unknown> = ResultSuccess<Value>;
|
|
11
10
|
/**
|
|
12
11
|
* Represents a failed validation result containing a {@link LexValidationError}.
|
|
12
|
+
*
|
|
13
|
+
* @extends ResultFailure<LexValidationError>
|
|
14
|
+
* @see {@link ResultFailure}
|
|
15
|
+
* @see {@link LexValidationError}
|
|
13
16
|
*/
|
|
14
|
-
export type ValidationFailure =
|
|
17
|
+
export type ValidationFailure = LexValidationError;
|
|
15
18
|
/**
|
|
16
19
|
* Discriminated union representing the outcome of a validation operation.
|
|
17
20
|
*
|
|
@@ -128,11 +131,13 @@ export type ValidationOptions = {
|
|
|
128
131
|
/**
|
|
129
132
|
* The validation mode determining how transformations are handled.
|
|
130
133
|
*
|
|
131
|
-
* - `"validate"
|
|
134
|
+
* - `"validate"`: Strict validation where the result must be
|
|
132
135
|
* strictly equal to the input value. No transformations such as applying
|
|
133
136
|
* default values are allowed.
|
|
134
137
|
* - `"parse"`: Allows the schema to transform the input value, such as
|
|
135
138
|
* applying default values or performing type coercion.
|
|
139
|
+
*
|
|
140
|
+
* @default "validate"
|
|
136
141
|
*/
|
|
137
142
|
mode?: 'validate' | 'parse';
|
|
138
143
|
/**
|
|
@@ -148,6 +153,16 @@ export type ValidationOptions = {
|
|
|
148
153
|
* ```
|
|
149
154
|
*/
|
|
150
155
|
path?: readonly PropertyKey[];
|
|
156
|
+
/**
|
|
157
|
+
* Whether to enforce strict validation rules (e.g., MIME type matching, size
|
|
158
|
+
* limits, datetime format).
|
|
159
|
+
*
|
|
160
|
+
* This is typically useful to allow more lax validation when parsing server
|
|
161
|
+
* responses, while enforcing strict validation for user input.
|
|
162
|
+
*
|
|
163
|
+
* @default true
|
|
164
|
+
*/
|
|
165
|
+
strict?: boolean;
|
|
151
166
|
};
|
|
152
167
|
/**
|
|
153
168
|
* Manages the state and context for validation operations.
|
|
@@ -314,7 +329,7 @@ export declare class ValidationContext {
|
|
|
314
329
|
* @param issue - The validation issue that caused the failure
|
|
315
330
|
* @returns A failed validation result
|
|
316
331
|
*/
|
|
317
|
-
issue(issue: Issue):
|
|
332
|
+
issue(issue: Issue): LexValidationError;
|
|
318
333
|
/**
|
|
319
334
|
* Creates a failure for an invalid value that doesn't match expected values.
|
|
320
335
|
*
|
|
@@ -322,7 +337,7 @@ export declare class ValidationContext {
|
|
|
322
337
|
* @param values - The expected valid values
|
|
323
338
|
* @returns A failed validation result with an invalid value issue
|
|
324
339
|
*/
|
|
325
|
-
issueInvalidValue(input: unknown, values: readonly unknown[]):
|
|
340
|
+
issueInvalidValue(input: unknown, values: readonly unknown[]): LexValidationError;
|
|
326
341
|
/**
|
|
327
342
|
* Creates a failure for an invalid type.
|
|
328
343
|
*
|
|
@@ -330,7 +345,7 @@ export declare class ValidationContext {
|
|
|
330
345
|
* @param expected - An array of expected type names
|
|
331
346
|
* @returns A failed validation result with an invalid type issue
|
|
332
347
|
*/
|
|
333
|
-
issueInvalidType(input: unknown, expected: readonly string[]):
|
|
348
|
+
issueInvalidType(input: unknown, expected: readonly string[]): LexValidationError;
|
|
334
349
|
/**
|
|
335
350
|
* Creates a failure for an invalid type.
|
|
336
351
|
*
|
|
@@ -338,7 +353,7 @@ export declare class ValidationContext {
|
|
|
338
353
|
* @param expected - The expected type name
|
|
339
354
|
* @returns A failed validation result with an invalid type issue
|
|
340
355
|
*/
|
|
341
|
-
issueUnexpectedType(input: unknown, expected: string):
|
|
356
|
+
issueUnexpectedType(input: unknown, expected: string): LexValidationError;
|
|
342
357
|
/**
|
|
343
358
|
* Creates a failure for a missing required key in an object.
|
|
344
359
|
*
|
|
@@ -346,7 +361,7 @@ export declare class ValidationContext {
|
|
|
346
361
|
* @param key - The name of the required key
|
|
347
362
|
* @returns A failed validation result with a required key issue
|
|
348
363
|
*/
|
|
349
|
-
issueRequiredKey(input: object, key: PropertyKey):
|
|
364
|
+
issueRequiredKey(input: object, key: PropertyKey): LexValidationError;
|
|
350
365
|
/**
|
|
351
366
|
* Creates a failure for an invalid string format.
|
|
352
367
|
*
|
|
@@ -355,7 +370,7 @@ export declare class ValidationContext {
|
|
|
355
370
|
* @param msg - Optional additional message describing the format error
|
|
356
371
|
* @returns A failed validation result with an invalid format issue
|
|
357
372
|
*/
|
|
358
|
-
issueInvalidFormat(input: unknown, format: string, msg?: string):
|
|
373
|
+
issueInvalidFormat(input: unknown, format: string, msg?: string): LexValidationError;
|
|
359
374
|
/**
|
|
360
375
|
* Creates a failure for a value that exceeds a maximum constraint.
|
|
361
376
|
*
|
|
@@ -365,7 +380,7 @@ export declare class ValidationContext {
|
|
|
365
380
|
* @param actual - The actual measured value
|
|
366
381
|
* @returns A failed validation result with a too big issue
|
|
367
382
|
*/
|
|
368
|
-
issueTooBig(input: unknown, type: MeasurableType, max: number, actual: number):
|
|
383
|
+
issueTooBig(input: unknown, type: MeasurableType, max: number, actual: number): LexValidationError;
|
|
369
384
|
/**
|
|
370
385
|
* Creates a failure for a value that is below a minimum constraint.
|
|
371
386
|
*
|
|
@@ -375,7 +390,7 @@ export declare class ValidationContext {
|
|
|
375
390
|
* @param actual - The actual measured value
|
|
376
391
|
* @returns A failed validation result with a too small issue
|
|
377
392
|
*/
|
|
378
|
-
issueTooSmall(input: unknown, type: MeasurableType, min: number, actual: number):
|
|
393
|
+
issueTooSmall(input: unknown, type: MeasurableType, min: number, actual: number): LexValidationError;
|
|
379
394
|
/**
|
|
380
395
|
* Creates a failure for an invalid property value within an object.
|
|
381
396
|
*
|
|
@@ -388,7 +403,7 @@ export declare class ValidationContext {
|
|
|
388
403
|
* @param values - The expected valid values
|
|
389
404
|
* @returns A failed validation result with an invalid value issue at the property path
|
|
390
405
|
*/
|
|
391
|
-
issueInvalidPropertyValue<I>(input: I, property: keyof I & PropertyKey, values: readonly unknown[]):
|
|
406
|
+
issueInvalidPropertyValue<I>(input: I, property: keyof I & PropertyKey, values: readonly unknown[]): LexValidationError;
|
|
392
407
|
/**
|
|
393
408
|
* Creates a failure for an invalid property type within an object.
|
|
394
409
|
*
|
|
@@ -401,7 +416,7 @@ export declare class ValidationContext {
|
|
|
401
416
|
* @param expected - The expected type name
|
|
402
417
|
* @returns A failed validation result with an invalid type issue at the property path
|
|
403
418
|
*/
|
|
404
|
-
issueInvalidPropertyType<I>(input: I, property: keyof I & PropertyKey, expected: string):
|
|
419
|
+
issueInvalidPropertyType<I>(input: I, property: keyof I & PropertyKey, expected: string): LexValidationError;
|
|
405
420
|
}
|
|
406
421
|
/**
|
|
407
422
|
* Recursively unwraps a wrapped validator to its innermost validator type.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validator.d.ts","sourceRoot":"","sources":["../../src/core/validator.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"validator.d.ts","sourceRoot":"","sources":["../../src/core/validator.ts"],"names":[],"mappings":"AACA,OAAO,EAAiB,aAAa,EAAW,MAAM,aAAa,CAAA;AACnE,OAAO,EAAE,kBAAkB,EAAE,MAAM,uBAAuB,CAAA;AAC1D,OAAO,EACL,KAAK,EAOL,cAAc,EACf,MAAM,uBAAuB,CAAA;AAE9B;;;;GAIG;AACH,MAAM,MAAM,iBAAiB,CAAC,KAAK,GAAG,OAAO,IAAI,aAAa,CAAC,KAAK,CAAC,CAAA;AAErE;;;;;;GAMG;AACH,MAAM,MAAM,iBAAiB,GAAG,kBAAkB,CAAA;AAElD;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,MAAM,gBAAgB,CAAC,KAAK,GAAG,OAAO,IACxC,iBAAiB,CAAC,KAAK,CAAC,GACxB,iBAAiB,CAAA;AAErB;;;;;;;;;;;;;GAaG;AACH,MAAM,MAAM,UAAU,CAAC,CAAC,SAAS,SAAS,IAAI,CAAC,CAAC,OAAO,CAAC,CAAC,OAAO,CAAC,CAAA;AAEjE;;;;;;;;;;;;;;GAcG;AACH,MAAM,MAAM,WAAW,CAAC,CAAC,SAAS,SAAS,IAAI,CAAC,CAAC,OAAO,CAAC,CAAC,QAAQ,CAAC,CAAA;AAEnE;;;;GAIG;AACH,YAAY,EAAE,UAAU,IAAI,KAAK,EAAE,CAAA;AAEnC,MAAM,WAAW,SAAS,CAAC,MAAM,GAAG,OAAO,EAAE,OAAO,GAAG,MAAM;IAC3D;;;;;;OAMG;IACH,QAAQ,CAAC,CAAC,OAAO,CAAC,EAAE;QAClB,KAAK,EAAE,MAAM,CAAA;QACb,MAAM,EAAE,OAAO,CAAA;KAChB,CAAA;IAED;;;;;;;;;;;;;;;;;;;;;;;;;OA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
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.ValidationContext = void 0;
|
|
4
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
4
5
|
const result_js_1 = require("./result.js");
|
|
5
6
|
const validation_error_js_1 = require("./validation-error.js");
|
|
6
7
|
const validation_issue_js_1 = require("./validation-issue.js");
|
|
@@ -38,6 +39,7 @@ class ValidationContext {
|
|
|
38
39
|
const context = new ValidationContext({
|
|
39
40
|
path: options?.path ?? [],
|
|
40
41
|
mode: options?.mode ?? 'validate',
|
|
42
|
+
strict: options?.strict ?? true,
|
|
41
43
|
});
|
|
42
44
|
return context.validate(input, validator);
|
|
43
45
|
}
|
|
@@ -98,7 +100,7 @@ class ValidationContext {
|
|
|
98
100
|
if (this.issues.length > 0) {
|
|
99
101
|
// Validator returned a success but issues were added via the context.
|
|
100
102
|
// This means the overall validation failed.
|
|
101
|
-
return
|
|
103
|
+
return new validation_error_js_1.LexValidationError(Array.from(this.issues));
|
|
102
104
|
}
|
|
103
105
|
if (this.options.mode !== 'parse' && !Object.is(result.value, input)) {
|
|
104
106
|
// If the value changed, it means that a default (or some other
|
|
@@ -186,7 +188,7 @@ class ValidationContext {
|
|
|
186
188
|
* @returns A failed validation result
|
|
187
189
|
*/
|
|
188
190
|
failure(reason) {
|
|
189
|
-
return
|
|
191
|
+
return reason;
|
|
190
192
|
}
|
|
191
193
|
/**
|
|
192
194
|
* Creates a failed validation result from a single issue.
|