@atproto/lex-schema 0.0.11 → 0.0.13
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 +54 -0
- package/dist/core/$type.d.ts +149 -0
- package/dist/core/$type.d.ts.map +1 -1
- package/dist/core/$type.js +44 -0
- package/dist/core/$type.js.map +1 -1
- package/dist/core/record-key.d.ts +44 -0
- package/dist/core/record-key.d.ts.map +1 -1
- package/dist/core/record-key.js +30 -0
- package/dist/core/record-key.js.map +1 -1
- package/dist/core/result.d.ts +85 -4
- package/dist/core/result.d.ts.map +1 -1
- package/dist/core/result.js +60 -4
- package/dist/core/result.js.map +1 -1
- package/dist/core/schema.d.ts +232 -5
- package/dist/core/schema.d.ts.map +1 -1
- package/dist/core/schema.js +197 -4
- package/dist/core/schema.js.map +1 -1
- package/dist/core/string-format.d.ts +244 -11
- package/dist/core/string-format.d.ts.map +1 -1
- package/dist/core/string-format.js +150 -0
- package/dist/core/string-format.js.map +1 -1
- package/dist/core/types.d.ts +90 -3
- package/dist/core/types.d.ts.map +1 -1
- package/dist/core/types.js.map +1 -1
- package/dist/core/validation-error.d.ts +60 -0
- package/dist/core/validation-error.d.ts.map +1 -1
- package/dist/core/validation-error.js +60 -0
- package/dist/core/validation-error.js.map +1 -1
- package/dist/core/validation-issue.d.ts +61 -0
- package/dist/core/validation-issue.d.ts.map +1 -1
- package/dist/core/validation-issue.js +54 -1
- package/dist/core/validation-issue.js.map +1 -1
- package/dist/core/validator.d.ts +356 -11
- package/dist/core/validator.d.ts.map +1 -1
- package/dist/core/validator.js +203 -4
- package/dist/core/validator.js.map +1 -1
- package/dist/helpers.d.ts +12 -28
- package/dist/helpers.d.ts.map +1 -1
- package/dist/helpers.js.map +1 -1
- package/dist/schema/array.d.ts +46 -0
- package/dist/schema/array.d.ts.map +1 -1
- package/dist/schema/array.js +16 -1
- package/dist/schema/array.js.map +1 -1
- package/dist/schema/blob.d.ts +50 -2
- package/dist/schema/blob.d.ts.map +1 -1
- package/dist/schema/blob.js +44 -2
- package/dist/schema/blob.js.map +1 -1
- package/dist/schema/boolean.d.ts +29 -0
- package/dist/schema/boolean.d.ts.map +1 -1
- package/dist/schema/boolean.js +30 -1
- package/dist/schema/boolean.js.map +1 -1
- package/dist/schema/bytes.d.ts +39 -0
- package/dist/schema/bytes.d.ts.map +1 -1
- package/dist/schema/bytes.js +34 -1
- package/dist/schema/bytes.js.map +1 -1
- package/dist/schema/cid.d.ts +39 -0
- package/dist/schema/cid.d.ts.map +1 -1
- package/dist/schema/cid.js +35 -1
- package/dist/schema/cid.js.map +1 -1
- package/dist/schema/custom.d.ts +67 -1
- package/dist/schema/custom.d.ts.map +1 -1
- package/dist/schema/custom.js +55 -0
- package/dist/schema/custom.js.map +1 -1
- package/dist/schema/dict.d.ts +45 -0
- package/dist/schema/dict.d.ts.map +1 -1
- package/dist/schema/dict.js +46 -1
- package/dist/schema/dict.js.map +1 -1
- package/dist/schema/discriminated-union.d.ts +59 -0
- package/dist/schema/discriminated-union.d.ts.map +1 -1
- package/dist/schema/discriminated-union.js +47 -1
- package/dist/schema/discriminated-union.js.map +1 -1
- package/dist/schema/enum.d.ts +49 -0
- package/dist/schema/enum.d.ts.map +1 -1
- package/dist/schema/enum.js +49 -0
- package/dist/schema/enum.js.map +1 -1
- package/dist/schema/integer.d.ts +43 -0
- package/dist/schema/integer.d.ts.map +1 -1
- package/dist/schema/integer.js +38 -1
- package/dist/schema/integer.js.map +1 -1
- package/dist/schema/intersection.d.ts +55 -0
- package/dist/schema/intersection.d.ts.map +1 -1
- package/dist/schema/intersection.js +50 -0
- package/dist/schema/intersection.js.map +1 -1
- package/dist/schema/lex-map.d.ts +37 -0
- package/dist/schema/lex-map.d.ts.map +1 -0
- package/dist/schema/lex-map.js +60 -0
- package/dist/schema/lex-map.js.map +1 -0
- package/dist/schema/lex-value.d.ts +35 -0
- package/dist/schema/lex-value.d.ts.map +1 -0
- package/dist/schema/lex-value.js +87 -0
- package/dist/schema/lex-value.js.map +1 -0
- package/dist/schema/literal.d.ts +45 -0
- package/dist/schema/literal.d.ts.map +1 -1
- package/dist/schema/literal.js +45 -0
- package/dist/schema/literal.js.map +1 -1
- package/dist/schema/never.d.ts +43 -0
- package/dist/schema/never.d.ts.map +1 -1
- package/dist/schema/never.js +44 -1
- package/dist/schema/never.js.map +1 -1
- package/dist/schema/null.d.ts +30 -0
- package/dist/schema/null.d.ts.map +1 -1
- package/dist/schema/null.js +31 -1
- package/dist/schema/null.js.map +1 -1
- package/dist/schema/nullable.d.ts +42 -0
- package/dist/schema/nullable.d.ts.map +1 -1
- package/dist/schema/nullable.js +42 -0
- package/dist/schema/nullable.js.map +1 -1
- package/dist/schema/object.d.ts +57 -0
- package/dist/schema/object.d.ts.map +1 -1
- package/dist/schema/object.js +53 -1
- package/dist/schema/object.js.map +1 -1
- package/dist/schema/optional.d.ts +43 -0
- package/dist/schema/optional.d.ts.map +1 -1
- package/dist/schema/optional.js +43 -0
- package/dist/schema/optional.js.map +1 -1
- package/dist/schema/params.d.ts +96 -12
- package/dist/schema/params.d.ts.map +1 -1
- package/dist/schema/params.js +155 -21
- package/dist/schema/params.js.map +1 -1
- package/dist/schema/payload.d.ts +111 -15
- package/dist/schema/payload.d.ts.map +1 -1
- package/dist/schema/payload.js +73 -3
- package/dist/schema/payload.js.map +1 -1
- package/dist/schema/permission-set.d.ts +58 -0
- package/dist/schema/permission-set.d.ts.map +1 -1
- package/dist/schema/permission-set.js +50 -0
- package/dist/schema/permission-set.js.map +1 -1
- package/dist/schema/permission.d.ts +42 -0
- package/dist/schema/permission.d.ts.map +1 -1
- package/dist/schema/permission.js +39 -0
- package/dist/schema/permission.js.map +1 -1
- package/dist/schema/procedure.d.ts +64 -0
- package/dist/schema/procedure.d.ts.map +1 -1
- package/dist/schema/procedure.js +64 -0
- package/dist/schema/procedure.js.map +1 -1
- package/dist/schema/query.d.ts +55 -0
- package/dist/schema/query.d.ts.map +1 -1
- package/dist/schema/query.js +55 -0
- package/dist/schema/query.js.map +1 -1
- package/dist/schema/record.d.ts +76 -25
- package/dist/schema/record.d.ts.map +1 -1
- package/dist/schema/record.js +21 -0
- package/dist/schema/record.js.map +1 -1
- package/dist/schema/ref.d.ts +51 -0
- package/dist/schema/ref.d.ts.map +1 -1
- package/dist/schema/ref.js +18 -0
- package/dist/schema/ref.js.map +1 -1
- package/dist/schema/refine.d.ts +58 -9
- package/dist/schema/refine.d.ts.map +1 -1
- package/dist/schema/refine.js.map +1 -1
- package/dist/schema/regexp.d.ts +45 -0
- package/dist/schema/regexp.d.ts.map +1 -1
- package/dist/schema/regexp.js +46 -1
- package/dist/schema/regexp.js.map +1 -1
- package/dist/schema/string.d.ts +72 -6
- package/dist/schema/string.d.ts.map +1 -1
- package/dist/schema/string.js +56 -8
- package/dist/schema/string.js.map +1 -1
- package/dist/schema/subscription.d.ts +72 -2
- package/dist/schema/subscription.d.ts.map +1 -1
- package/dist/schema/subscription.js +59 -0
- package/dist/schema/subscription.js.map +1 -1
- package/dist/schema/token.d.ts +48 -0
- package/dist/schema/token.d.ts.map +1 -1
- package/dist/schema/token.js +49 -1
- package/dist/schema/token.js.map +1 -1
- package/dist/schema/typed-object.d.ts +73 -23
- package/dist/schema/typed-object.d.ts.map +1 -1
- package/dist/schema/typed-object.js +20 -1
- package/dist/schema/typed-object.js.map +1 -1
- package/dist/schema/typed-ref.d.ts +54 -0
- package/dist/schema/typed-ref.d.ts.map +1 -1
- package/dist/schema/typed-ref.js +16 -0
- package/dist/schema/typed-ref.js.map +1 -1
- package/dist/schema/typed-union.d.ts +51 -1
- package/dist/schema/typed-union.d.ts.map +1 -1
- package/dist/schema/typed-union.js +52 -2
- package/dist/schema/typed-union.js.map +1 -1
- package/dist/schema/union.d.ts +46 -0
- package/dist/schema/union.d.ts.map +1 -1
- package/dist/schema/union.js +41 -0
- package/dist/schema/union.js.map +1 -1
- package/dist/schema/unknown.d.ts +34 -0
- package/dist/schema/unknown.d.ts.map +1 -1
- package/dist/schema/unknown.js +34 -0
- package/dist/schema/unknown.js.map +1 -1
- package/dist/schema/with-default.d.ts +45 -0
- package/dist/schema/with-default.d.ts.map +1 -1
- package/dist/schema/with-default.js +45 -0
- package/dist/schema/with-default.js.map +1 -1
- package/dist/schema.d.ts +2 -1
- package/dist/schema.d.ts.map +1 -1
- package/dist/schema.js +2 -1
- package/dist/schema.js.map +1 -1
- package/dist/util/if-any.d.ts +2 -0
- package/dist/util/if-any.d.ts.map +1 -0
- package/dist/util/if-any.js +3 -0
- package/dist/util/if-any.js.map +1 -0
- package/package.json +3 -3
- package/src/core/$type.ts +150 -18
- package/src/core/record-key.ts +44 -0
- package/src/core/result.ts +86 -4
- package/src/core/schema.ts +244 -9
- package/src/core/string-format.ts +259 -13
- package/src/core/types.ts +91 -3
- package/src/core/validation-error.ts +60 -0
- package/src/core/validation-issue.ts +68 -2
- package/src/core/validator.ts +373 -12
- package/src/helpers.test.ts +110 -29
- package/src/helpers.ts +54 -25
- package/src/schema/array.test.ts +94 -79
- package/src/schema/array.ts +48 -1
- package/src/schema/blob.ts +50 -1
- package/src/schema/boolean.ts +31 -1
- package/src/schema/bytes.ts +41 -1
- package/src/schema/cid.ts +41 -1
- package/src/schema/custom.ts +68 -1
- package/src/schema/dict.ts +47 -1
- package/src/schema/discriminated-union.ts +61 -1
- package/src/schema/enum.ts +50 -0
- package/src/schema/integer.ts +45 -1
- package/src/schema/intersection.ts +56 -0
- package/src/schema/{unknown-object.test.ts → lex-map.test.ts} +9 -9
- package/src/schema/lex-map.ts +63 -0
- package/src/schema/lex-value.test.ts +81 -0
- package/src/schema/lex-value.ts +86 -0
- package/src/schema/literal.ts +46 -0
- package/src/schema/never.ts +45 -1
- package/src/schema/null.ts +32 -1
- package/src/schema/nullable.ts +43 -0
- package/src/schema/object.ts +59 -1
- package/src/schema/optional.ts +44 -0
- package/src/schema/params.test.ts +133 -38
- package/src/schema/params.ts +237 -37
- package/src/schema/payload.test.ts +3 -3
- package/src/schema/payload.ts +145 -42
- package/src/schema/permission-set.ts +58 -0
- package/src/schema/permission.ts +42 -0
- package/src/schema/procedure.ts +64 -0
- package/src/schema/query.ts +55 -0
- package/src/schema/record.ts +82 -16
- package/src/schema/ref.ts +52 -0
- package/src/schema/refine.ts +58 -9
- package/src/schema/regexp.ts +47 -1
- package/src/schema/string.test.ts +99 -2
- package/src/schema/string.ts +108 -15
- package/src/schema/subscription.ts +72 -2
- package/src/schema/token.ts +50 -1
- package/src/schema/typed-object.ts +81 -16
- package/src/schema/typed-ref.ts +55 -0
- package/src/schema/typed-union.ts +58 -3
- package/src/schema/union.ts +47 -0
- package/src/schema/unknown.ts +35 -0
- package/src/schema/with-default.ts +46 -0
- package/src/schema.ts +2 -1
- package/src/util/if-any.ts +3 -0
- package/dist/schema/unknown-object.d.ts +0 -8
- package/dist/schema/unknown-object.d.ts.map +0 -1
- package/dist/schema/unknown-object.js +0 -19
- package/dist/schema/unknown-object.js.map +0 -1
- package/src/schema/unknown-object.ts +0 -19
package/src/core/types.ts
CHANGED
|
@@ -1,23 +1,95 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Same as
|
|
2
|
+
* Same as `string` but prevents TypeScript from allowing union types to
|
|
3
3
|
* be widened to `string` in IDEs.
|
|
4
|
+
*
|
|
5
|
+
* This is useful when you want autocompletion for known string values
|
|
6
|
+
* while still allowing arbitrary strings.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* // With plain string, union is widened:
|
|
11
|
+
* type Status1 = 'active' | 'inactive' | string // just becomes "string"
|
|
12
|
+
*
|
|
13
|
+
* // With UnknownString, union is preserved:
|
|
14
|
+
* type Status2 = 'active' | 'inactive' | UnknownString
|
|
15
|
+
* // Autocomplete will suggest 'active' and 'inactive'
|
|
16
|
+
* ```
|
|
4
17
|
*/
|
|
5
18
|
export type UnknownString = string & NonNullable<unknown>
|
|
6
19
|
|
|
20
|
+
/**
|
|
21
|
+
* Simplifies a type by expanding intersections and mapped types.
|
|
22
|
+
*
|
|
23
|
+
* This improves IDE tooltips by showing the actual shape of a type
|
|
24
|
+
* rather than complex intersections.
|
|
25
|
+
*
|
|
26
|
+
* @typeParam T - The type to simplify
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```typescript
|
|
30
|
+
* type Complex = { a: string } & { b: number }
|
|
31
|
+
* type Simple = Simplify<Complex>
|
|
32
|
+
* // Hover shows: { a: string; b: number }
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
7
35
|
export type Simplify<T> = { [K in keyof T]: T[K] } & NonNullable<unknown>
|
|
8
36
|
|
|
37
|
+
/**
|
|
38
|
+
* Internal symbol for branding restricted types.
|
|
39
|
+
* @internal
|
|
40
|
+
*/
|
|
9
41
|
declare const __restricted: unique symbol
|
|
42
|
+
|
|
10
43
|
/**
|
|
11
44
|
* A type that represents a value that cannot be used, with a custom
|
|
12
45
|
* message explaining the restriction.
|
|
46
|
+
*
|
|
47
|
+
* This is useful for creating "never use this" types that provide
|
|
48
|
+
* helpful error messages when someone tries to use them.
|
|
49
|
+
*
|
|
50
|
+
* @typeParam Message - A string literal type containing the error message
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* ```typescript
|
|
54
|
+
* type DeprecatedField = Restricted<'This field has been deprecated. Use newField instead.'>
|
|
55
|
+
*
|
|
56
|
+
* interface MyType {
|
|
57
|
+
* oldField?: DeprecatedField
|
|
58
|
+
* newField: string
|
|
59
|
+
* }
|
|
60
|
+
*
|
|
61
|
+
* const obj: MyType = {
|
|
62
|
+
* oldField: 'value', // Error: Type 'string' is not assignable to type 'Restricted<...>'
|
|
63
|
+
* newField: 'value'
|
|
64
|
+
* }
|
|
65
|
+
* ```
|
|
13
66
|
*/
|
|
14
67
|
export type Restricted<Message extends string> = typeof __restricted & {
|
|
15
68
|
[__restricted]: Message
|
|
16
69
|
}
|
|
17
70
|
|
|
18
71
|
/**
|
|
19
|
-
* Converts all properties of `P` that
|
|
20
|
-
*
|
|
72
|
+
* Converts all properties of `P` that may be `undefined` into actual
|
|
73
|
+
* optional properties on the resulting type.
|
|
74
|
+
*
|
|
75
|
+
* This is useful when working with types where some properties are typed as
|
|
76
|
+
* `T | undefined` but should really be optional (`T?`).
|
|
77
|
+
*
|
|
78
|
+
* @typeParam P - The object type to transform
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
* ```typescript
|
|
82
|
+
* type Input = {
|
|
83
|
+
* required: string
|
|
84
|
+
* optional: string | undefined
|
|
85
|
+
* }
|
|
86
|
+
*
|
|
87
|
+
* type Output = WithOptionalProperties<Input>
|
|
88
|
+
* // Result: {
|
|
89
|
+
* // required: string
|
|
90
|
+
* // optional?: string | undefined
|
|
91
|
+
* // }
|
|
92
|
+
* ```
|
|
21
93
|
*/
|
|
22
94
|
export type WithOptionalProperties<P> = Simplify<
|
|
23
95
|
{
|
|
@@ -27,6 +99,22 @@ export type WithOptionalProperties<P> = Simplify<
|
|
|
27
99
|
}
|
|
28
100
|
>
|
|
29
101
|
|
|
102
|
+
/**
|
|
103
|
+
* Creates a type by omitting a specific key from an object type.
|
|
104
|
+
*
|
|
105
|
+
* Similar to TypeScript's built-in `Omit`, but preserves the type structure
|
|
106
|
+
* more accurately in some edge cases.
|
|
107
|
+
*
|
|
108
|
+
* @typeParam T - The object type to transform
|
|
109
|
+
* @typeParam K - The key to omit (must be a key of T)
|
|
110
|
+
*
|
|
111
|
+
* @example
|
|
112
|
+
* ```typescript
|
|
113
|
+
* type Person = { name: string; age: number; email: string }
|
|
114
|
+
* type PersonWithoutEmail = OmitKey<Person, 'email'>
|
|
115
|
+
* // Result: { name: string; age: number }
|
|
116
|
+
* ```
|
|
117
|
+
*/
|
|
30
118
|
export type OmitKey<T, K extends keyof T> = {
|
|
31
119
|
[K2 in keyof T as K2 extends K ? never : K2]: T[K2]
|
|
32
120
|
}
|
|
@@ -7,17 +7,60 @@ import {
|
|
|
7
7
|
IssueInvalidValue,
|
|
8
8
|
} from './validation-issue.js'
|
|
9
9
|
|
|
10
|
+
/**
|
|
11
|
+
* Error thrown when validation fails.
|
|
12
|
+
*
|
|
13
|
+
* Contains detailed information about all validation issues encountered,
|
|
14
|
+
* including the path to each invalid value and descriptions of what was
|
|
15
|
+
* expected vs what was received.
|
|
16
|
+
*
|
|
17
|
+
* Extends {@link LexError} with the error name "InvalidRequest" for
|
|
18
|
+
* consistency with the AT Protocol error handling conventions.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```typescript
|
|
22
|
+
* const error = new ValidationError([
|
|
23
|
+
* new IssueInvalidType(['user', 'age'], 'hello', ['number'])
|
|
24
|
+
* ])
|
|
25
|
+
* console.log(error.message)
|
|
26
|
+
* // "Expected number value type at $.user.age (got string)"
|
|
27
|
+
*
|
|
28
|
+
* console.log(error.issues.length) // 1
|
|
29
|
+
* console.log(error.toJSON())
|
|
30
|
+
* // { error: 'InvalidRequest', message: '...', issues: [...] }
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
10
33
|
export class ValidationError extends LexError {
|
|
11
34
|
name = 'ValidationError'
|
|
12
35
|
|
|
36
|
+
/**
|
|
37
|
+
* The list of validation issues that caused this error.
|
|
38
|
+
*
|
|
39
|
+
* Issues are aggregated when possible (e.g., multiple invalid type issues
|
|
40
|
+
* at the same path are combined into a single issue listing all expected types).
|
|
41
|
+
*/
|
|
13
42
|
readonly issues: Issue[]
|
|
14
43
|
|
|
44
|
+
/**
|
|
45
|
+
* Creates a new validation error from a list of issues.
|
|
46
|
+
*
|
|
47
|
+
* Issues are automatically aggregated to combine related issues at the same
|
|
48
|
+
* path (e.g., multiple type expectations from a union schema).
|
|
49
|
+
*
|
|
50
|
+
* @param issues - The validation issues that caused this error
|
|
51
|
+
* @param options - Standard Error options (e.g., `cause`)
|
|
52
|
+
*/
|
|
15
53
|
constructor(issues: Issue[], options?: ErrorOptions) {
|
|
16
54
|
const issuesAgg = aggregateIssues(issues)
|
|
17
55
|
super('InvalidRequest', issuesAgg.join(', '), options)
|
|
18
56
|
this.issues = issuesAgg
|
|
19
57
|
}
|
|
20
58
|
|
|
59
|
+
/**
|
|
60
|
+
* Converts the error to a JSON-serializable object.
|
|
61
|
+
*
|
|
62
|
+
* @returns An object containing the error details and all issues in JSON format
|
|
63
|
+
*/
|
|
21
64
|
toJSON() {
|
|
22
65
|
return {
|
|
23
66
|
...super.toJSON(),
|
|
@@ -25,6 +68,23 @@ export class ValidationError extends LexError {
|
|
|
25
68
|
}
|
|
26
69
|
}
|
|
27
70
|
|
|
71
|
+
/**
|
|
72
|
+
* Creates a validation error by combining multiple validation failures.
|
|
73
|
+
*
|
|
74
|
+
* This is useful when validating against multiple possible schemas (e.g., unions)
|
|
75
|
+
* and all branches fail. The resulting error contains issues from all failures.
|
|
76
|
+
*
|
|
77
|
+
* @param failures - The validation failures to combine
|
|
78
|
+
* @returns A single validation error containing all issues from the failures
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
* ```typescript
|
|
82
|
+
* const failures = schemas.map(s => s.safeValidate(data)).filter(r => !r.success)
|
|
83
|
+
* if (failures.length === schemas.length) {
|
|
84
|
+
* throw ValidationError.fromFailures(failures)
|
|
85
|
+
* }
|
|
86
|
+
* ```
|
|
87
|
+
*/
|
|
28
88
|
static fromFailures(
|
|
29
89
|
failures: ResultFailure<ValidationError>[],
|
|
30
90
|
): ValidationError {
|
|
@@ -1,6 +1,17 @@
|
|
|
1
|
-
import { ifCid, isPlainObject } from '@atproto/lex-data'
|
|
1
|
+
import { ifCid, isLegacyBlobRef, isPlainObject } from '@atproto/lex-data'
|
|
2
2
|
import { PropertyKey } from './property-key.js'
|
|
3
3
|
|
|
4
|
+
/**
|
|
5
|
+
* Abstract base class for all validation issues.
|
|
6
|
+
*
|
|
7
|
+
* An issue represents a single validation failure, containing:
|
|
8
|
+
* - A code identifying the type of issue
|
|
9
|
+
* - The path to the invalid value in the data structure
|
|
10
|
+
* - The actual input value that failed validation
|
|
11
|
+
*
|
|
12
|
+
* Subclasses add specific properties relevant to each issue type and
|
|
13
|
+
* implement the {@link toString} method for human-readable error messages.
|
|
14
|
+
*/
|
|
4
15
|
export abstract class Issue {
|
|
5
16
|
constructor(
|
|
6
17
|
readonly code: string,
|
|
@@ -8,8 +19,16 @@ export abstract class Issue {
|
|
|
8
19
|
readonly input: unknown,
|
|
9
20
|
) {}
|
|
10
21
|
|
|
22
|
+
/**
|
|
23
|
+
* Returns a human-readable description of the validation issue.
|
|
24
|
+
*/
|
|
11
25
|
abstract toString(): string
|
|
12
26
|
|
|
27
|
+
/**
|
|
28
|
+
* Converts the issue to a JSON-serializable object.
|
|
29
|
+
*
|
|
30
|
+
* @returns An object containing the issue code, path, and message
|
|
31
|
+
*/
|
|
13
32
|
toJSON() {
|
|
14
33
|
return {
|
|
15
34
|
code: this.code,
|
|
@@ -19,6 +38,11 @@ export abstract class Issue {
|
|
|
19
38
|
}
|
|
20
39
|
}
|
|
21
40
|
|
|
41
|
+
/**
|
|
42
|
+
* A custom validation issue with a user-defined message.
|
|
43
|
+
*
|
|
44
|
+
* Use this for validation rules that don't fit into the standard issue categories.
|
|
45
|
+
*/
|
|
22
46
|
export class IssueCustom extends Issue {
|
|
23
47
|
constructor(
|
|
24
48
|
readonly path: readonly PropertyKey[],
|
|
@@ -33,6 +57,11 @@ export class IssueCustom extends Issue {
|
|
|
33
57
|
}
|
|
34
58
|
}
|
|
35
59
|
|
|
60
|
+
/**
|
|
61
|
+
* Issue for string values that don't match an expected format.
|
|
62
|
+
*
|
|
63
|
+
* Used for AT Protocol specific formats like DID, handle, NSID, AT-URI, etc.
|
|
64
|
+
*/
|
|
36
65
|
export class IssueInvalidFormat extends Issue {
|
|
37
66
|
constructor(
|
|
38
67
|
path: readonly PropertyKey[],
|
|
@@ -54,6 +83,7 @@ export class IssueInvalidFormat extends Issue {
|
|
|
54
83
|
}
|
|
55
84
|
}
|
|
56
85
|
|
|
86
|
+
/** Returns a human-readable description of the expected format. */
|
|
57
87
|
get formatDescription(): string {
|
|
58
88
|
switch (this.format) {
|
|
59
89
|
case 'at-identifier':
|
|
@@ -74,6 +104,12 @@ export class IssueInvalidFormat extends Issue {
|
|
|
74
104
|
}
|
|
75
105
|
}
|
|
76
106
|
|
|
107
|
+
/**
|
|
108
|
+
* Issue for values that have an unexpected type.
|
|
109
|
+
*
|
|
110
|
+
* This is one of the most common validation issues, occurring when the
|
|
111
|
+
* runtime type of a value doesn't match the expected schema type.
|
|
112
|
+
*/
|
|
77
113
|
export class IssueInvalidType extends Issue {
|
|
78
114
|
constructor(
|
|
79
115
|
path: readonly PropertyKey[],
|
|
@@ -95,6 +131,12 @@ export class IssueInvalidType extends Issue {
|
|
|
95
131
|
}
|
|
96
132
|
}
|
|
97
133
|
|
|
134
|
+
/**
|
|
135
|
+
* Issue for values that don't match any of the expected literal values.
|
|
136
|
+
*
|
|
137
|
+
* Used when a value must be one of a specific set of allowed values
|
|
138
|
+
* (e.g., enum-like constraints).
|
|
139
|
+
*/
|
|
98
140
|
export class IssueInvalidValue extends Issue {
|
|
99
141
|
constructor(
|
|
100
142
|
path: readonly PropertyKey[],
|
|
@@ -116,6 +158,9 @@ export class IssueInvalidValue extends Issue {
|
|
|
116
158
|
}
|
|
117
159
|
}
|
|
118
160
|
|
|
161
|
+
/**
|
|
162
|
+
* Issue for missing required object properties.
|
|
163
|
+
*/
|
|
119
164
|
export class IssueRequiredKey extends Issue {
|
|
120
165
|
constructor(
|
|
121
166
|
path: readonly PropertyKey[],
|
|
@@ -137,6 +182,16 @@ export class IssueRequiredKey extends Issue {
|
|
|
137
182
|
}
|
|
138
183
|
}
|
|
139
184
|
|
|
185
|
+
/**
|
|
186
|
+
* The type of measurement for size constraint issues.
|
|
187
|
+
*
|
|
188
|
+
* - `'array'` - Array length
|
|
189
|
+
* - `'string'` - String length in characters
|
|
190
|
+
* - `'integer'` - Numeric value
|
|
191
|
+
* - `'grapheme'` - String length in grapheme clusters
|
|
192
|
+
* - `'bytes'` - Byte length
|
|
193
|
+
* - `'blob'` - Blob size
|
|
194
|
+
*/
|
|
140
195
|
export type MeasurableType =
|
|
141
196
|
| 'array'
|
|
142
197
|
| 'string'
|
|
@@ -145,6 +200,9 @@ export type MeasurableType =
|
|
|
145
200
|
| 'bytes'
|
|
146
201
|
| 'blob'
|
|
147
202
|
|
|
203
|
+
/**
|
|
204
|
+
* Issue for values that exceed a maximum constraint.
|
|
205
|
+
*/
|
|
148
206
|
export class IssueTooBig extends Issue {
|
|
149
207
|
constructor(
|
|
150
208
|
path: readonly PropertyKey[],
|
|
@@ -169,6 +227,9 @@ export class IssueTooBig extends Issue {
|
|
|
169
227
|
}
|
|
170
228
|
}
|
|
171
229
|
|
|
230
|
+
/**
|
|
231
|
+
* Issue for values that are below a minimum constraint.
|
|
232
|
+
*/
|
|
172
233
|
export class IssueTooSmall extends Issue {
|
|
173
234
|
constructor(
|
|
174
235
|
path: readonly PropertyKey[],
|
|
@@ -193,9 +254,13 @@ export class IssueTooSmall extends Issue {
|
|
|
193
254
|
}
|
|
194
255
|
}
|
|
195
256
|
|
|
257
|
+
// -----------------------------------------------------------------------------
|
|
258
|
+
// Helper functions for formatting error messages
|
|
259
|
+
// -----------------------------------------------------------------------------
|
|
260
|
+
|
|
196
261
|
function stringifyExpectedType(expected: string): string {
|
|
197
262
|
if (expected === '$typed') {
|
|
198
|
-
return 'an object
|
|
263
|
+
return 'an object which includes the "$type" property'
|
|
199
264
|
}
|
|
200
265
|
return expected
|
|
201
266
|
}
|
|
@@ -230,6 +295,7 @@ function stringifyType(value: unknown): string {
|
|
|
230
295
|
if (value === null) return 'null'
|
|
231
296
|
if (Array.isArray(value)) return 'array'
|
|
232
297
|
if (ifCid(value)) return 'cid'
|
|
298
|
+
if (isLegacyBlobRef(value)) return 'legacy-blob'
|
|
233
299
|
if (value instanceof Date) return 'date'
|
|
234
300
|
if (value instanceof RegExp) return 'regexp'
|
|
235
301
|
if (value instanceof Map) return 'map'
|