@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/dist/core/types.d.ts
CHANGED
|
@@ -1,28 +1,115 @@
|
|
|
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>;
|
|
19
|
+
/**
|
|
20
|
+
* Simplifies a type by expanding intersections and mapped types.
|
|
21
|
+
*
|
|
22
|
+
* This improves IDE tooltips by showing the actual shape of a type
|
|
23
|
+
* rather than complex intersections.
|
|
24
|
+
*
|
|
25
|
+
* @typeParam T - The type to simplify
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* ```typescript
|
|
29
|
+
* type Complex = { a: string } & { b: number }
|
|
30
|
+
* type Simple = Simplify<Complex>
|
|
31
|
+
* // Hover shows: { a: string; b: number }
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
6
34
|
export type Simplify<T> = {
|
|
7
35
|
[K in keyof T]: T[K];
|
|
8
36
|
} & NonNullable<unknown>;
|
|
37
|
+
/**
|
|
38
|
+
* Internal symbol for branding restricted types.
|
|
39
|
+
* @internal
|
|
40
|
+
*/
|
|
9
41
|
declare const __restricted: unique symbol;
|
|
10
42
|
/**
|
|
11
43
|
* A type that represents a value that cannot be used, with a custom
|
|
12
44
|
* message explaining the restriction.
|
|
45
|
+
*
|
|
46
|
+
* This is useful for creating "never use this" types that provide
|
|
47
|
+
* helpful error messages when someone tries to use them.
|
|
48
|
+
*
|
|
49
|
+
* @typeParam Message - A string literal type containing the error message
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* ```typescript
|
|
53
|
+
* type DeprecatedField = Restricted<'This field has been deprecated. Use newField instead.'>
|
|
54
|
+
*
|
|
55
|
+
* interface MyType {
|
|
56
|
+
* oldField?: DeprecatedField
|
|
57
|
+
* newField: string
|
|
58
|
+
* }
|
|
59
|
+
*
|
|
60
|
+
* const obj: MyType = {
|
|
61
|
+
* oldField: 'value', // Error: Type 'string' is not assignable to type 'Restricted<...>'
|
|
62
|
+
* newField: 'value'
|
|
63
|
+
* }
|
|
64
|
+
* ```
|
|
13
65
|
*/
|
|
14
66
|
export type Restricted<Message extends string> = typeof __restricted & {
|
|
15
67
|
[__restricted]: Message;
|
|
16
68
|
};
|
|
17
69
|
/**
|
|
18
|
-
* Converts all properties of `P` that
|
|
19
|
-
*
|
|
70
|
+
* Converts all properties of `P` that may be `undefined` into actual
|
|
71
|
+
* optional properties on the resulting type.
|
|
72
|
+
*
|
|
73
|
+
* This is useful when working with types where some properties are typed as
|
|
74
|
+
* `T | undefined` but should really be optional (`T?`).
|
|
75
|
+
*
|
|
76
|
+
* @typeParam P - The object type to transform
|
|
77
|
+
*
|
|
78
|
+
* @example
|
|
79
|
+
* ```typescript
|
|
80
|
+
* type Input = {
|
|
81
|
+
* required: string
|
|
82
|
+
* optional: string | undefined
|
|
83
|
+
* }
|
|
84
|
+
*
|
|
85
|
+
* type Output = WithOptionalProperties<Input>
|
|
86
|
+
* // Result: {
|
|
87
|
+
* // required: string
|
|
88
|
+
* // optional?: string | undefined
|
|
89
|
+
* // }
|
|
90
|
+
* ```
|
|
20
91
|
*/
|
|
21
92
|
export type WithOptionalProperties<P> = Simplify<{
|
|
22
93
|
-readonly [K in keyof P as undefined extends P[K] ? never : K]-?: P[K];
|
|
23
94
|
} & {
|
|
24
95
|
-readonly [K in keyof P as undefined extends P[K] ? K : never]?: P[K];
|
|
25
96
|
}>;
|
|
97
|
+
/**
|
|
98
|
+
* Creates a type by omitting a specific key from an object type.
|
|
99
|
+
*
|
|
100
|
+
* Similar to TypeScript's built-in `Omit`, but preserves the type structure
|
|
101
|
+
* more accurately in some edge cases.
|
|
102
|
+
*
|
|
103
|
+
* @typeParam T - The object type to transform
|
|
104
|
+
* @typeParam K - The key to omit (must be a key of T)
|
|
105
|
+
*
|
|
106
|
+
* @example
|
|
107
|
+
* ```typescript
|
|
108
|
+
* type Person = { name: string; age: number; email: string }
|
|
109
|
+
* type PersonWithoutEmail = OmitKey<Person, 'email'>
|
|
110
|
+
* // Result: { name: string; age: number }
|
|
111
|
+
* ```
|
|
112
|
+
*/
|
|
26
113
|
export type OmitKey<T, K extends keyof T> = {
|
|
27
114
|
[K2 in keyof T as K2 extends K ? never : K2]: T[K2];
|
|
28
115
|
};
|
package/dist/core/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/core/types.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/core/types.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,MAAM,aAAa,GAAG,MAAM,GAAG,WAAW,CAAC,OAAO,CAAC,CAAA;AAEzD;;;;;;;;;;;;;;GAcG;AACH,MAAM,MAAM,QAAQ,CAAC,CAAC,IAAI;KAAG,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;CAAE,GAAG,WAAW,CAAC,OAAO,CAAC,CAAA;AAEzE;;;GAGG;AACH,OAAO,CAAC,MAAM,YAAY,EAAE,OAAO,MAAM,CAAA;AAEzC;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,MAAM,MAAM,UAAU,CAAC,OAAO,SAAS,MAAM,IAAI,OAAO,YAAY,GAAG;IACrE,CAAC,YAAY,CAAC,EAAE,OAAO,CAAA;CACxB,CAAA;AAED;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,MAAM,MAAM,sBAAsB,CAAC,CAAC,IAAI,QAAQ,CAC9C;IACE,CAAC,UAAU,CAAC,IAAI,MAAM,CAAC,IAAI,SAAS,SAAS,CAAC,CAAC,CAAC,CAAC,GAAG,KAAK,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;CACvE,GAAG;IACF,CAAC,UAAU,CAAC,IAAI,MAAM,CAAC,IAAI,SAAS,SAAS,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;CACtE,CACF,CAAA;AAED;;;;;;;;;;;;;;;GAeG;AACH,MAAM,MAAM,OAAO,CAAC,CAAC,EAAE,CAAC,SAAS,MAAM,CAAC,IAAI;KACzC,EAAE,IAAI,MAAM,CAAC,IAAI,EAAE,SAAS,CAAC,GAAG,KAAK,GAAG,EAAE,GAAG,CAAC,CAAC,EAAE,CAAC;CACpD,CAAA"}
|
package/dist/core/types.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/core/types.ts"],"names":[],"mappings":"","sourcesContent":["/**\n * Same as
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/core/types.ts"],"names":[],"mappings":"","sourcesContent":["/**\n * Same as `string` but prevents TypeScript from allowing union types to\n * be widened to `string` in IDEs.\n *\n * This is useful when you want autocompletion for known string values\n * while still allowing arbitrary strings.\n *\n * @example\n * ```typescript\n * // With plain string, union is widened:\n * type Status1 = 'active' | 'inactive' | string // just becomes \"string\"\n *\n * // With UnknownString, union is preserved:\n * type Status2 = 'active' | 'inactive' | UnknownString\n * // Autocomplete will suggest 'active' and 'inactive'\n * ```\n */\nexport type UnknownString = string & NonNullable<unknown>\n\n/**\n * Simplifies a type by expanding intersections and mapped types.\n *\n * This improves IDE tooltips by showing the actual shape of a type\n * rather than complex intersections.\n *\n * @typeParam T - The type to simplify\n *\n * @example\n * ```typescript\n * type Complex = { a: string } & { b: number }\n * type Simple = Simplify<Complex>\n * // Hover shows: { a: string; b: number }\n * ```\n */\nexport type Simplify<T> = { [K in keyof T]: T[K] } & NonNullable<unknown>\n\n/**\n * Internal symbol for branding restricted types.\n * @internal\n */\ndeclare const __restricted: unique symbol\n\n/**\n * A type that represents a value that cannot be used, with a custom\n * message explaining the restriction.\n *\n * This is useful for creating \"never use this\" types that provide\n * helpful error messages when someone tries to use them.\n *\n * @typeParam Message - A string literal type containing the error message\n *\n * @example\n * ```typescript\n * type DeprecatedField = Restricted<'This field has been deprecated. Use newField instead.'>\n *\n * interface MyType {\n * oldField?: DeprecatedField\n * newField: string\n * }\n *\n * const obj: MyType = {\n * oldField: 'value', // Error: Type 'string' is not assignable to type 'Restricted<...>'\n * newField: 'value'\n * }\n * ```\n */\nexport type Restricted<Message extends string> = typeof __restricted & {\n [__restricted]: Message\n}\n\n/**\n * Converts all properties of `P` that may be `undefined` into actual\n * optional properties on the resulting type.\n *\n * This is useful when working with types where some properties are typed as\n * `T | undefined` but should really be optional (`T?`).\n *\n * @typeParam P - The object type to transform\n *\n * @example\n * ```typescript\n * type Input = {\n * required: string\n * optional: string | undefined\n * }\n *\n * type Output = WithOptionalProperties<Input>\n * // Result: {\n * // required: string\n * // optional?: string | undefined\n * // }\n * ```\n */\nexport type WithOptionalProperties<P> = Simplify<\n {\n -readonly [K in keyof P as undefined extends P[K] ? never : K]-?: P[K]\n } & {\n -readonly [K in keyof P as undefined extends P[K] ? K : never]?: P[K]\n }\n>\n\n/**\n * Creates a type by omitting a specific key from an object type.\n *\n * Similar to TypeScript's built-in `Omit`, but preserves the type structure\n * more accurately in some edge cases.\n *\n * @typeParam T - The object type to transform\n * @typeParam K - The key to omit (must be a key of T)\n *\n * @example\n * ```typescript\n * type Person = { name: string; age: number; email: string }\n * type PersonWithoutEmail = OmitKey<Person, 'email'>\n * // Result: { name: string; age: number }\n * ```\n */\nexport type OmitKey<T, K extends keyof T> = {\n [K2 in keyof T as K2 extends K ? never : K2]: T[K2]\n}\n"]}
|
|
@@ -1,10 +1,53 @@
|
|
|
1
1
|
import { LexError } from '@atproto/lex-data';
|
|
2
2
|
import { ResultFailure } from './result.js';
|
|
3
3
|
import { Issue } from './validation-issue.js';
|
|
4
|
+
/**
|
|
5
|
+
* Error thrown when validation fails.
|
|
6
|
+
*
|
|
7
|
+
* Contains detailed information about all validation issues encountered,
|
|
8
|
+
* including the path to each invalid value and descriptions of what was
|
|
9
|
+
* expected vs what was received.
|
|
10
|
+
*
|
|
11
|
+
* Extends {@link LexError} with the error name "InvalidRequest" for
|
|
12
|
+
* consistency with the AT Protocol error handling conventions.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* const error = new ValidationError([
|
|
17
|
+
* new IssueInvalidType(['user', 'age'], 'hello', ['number'])
|
|
18
|
+
* ])
|
|
19
|
+
* console.log(error.message)
|
|
20
|
+
* // "Expected number value type at $.user.age (got string)"
|
|
21
|
+
*
|
|
22
|
+
* console.log(error.issues.length) // 1
|
|
23
|
+
* console.log(error.toJSON())
|
|
24
|
+
* // { error: 'InvalidRequest', message: '...', issues: [...] }
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
4
27
|
export declare class ValidationError extends LexError {
|
|
5
28
|
name: string;
|
|
29
|
+
/**
|
|
30
|
+
* The list of validation issues that caused this error.
|
|
31
|
+
*
|
|
32
|
+
* Issues are aggregated when possible (e.g., multiple invalid type issues
|
|
33
|
+
* at the same path are combined into a single issue listing all expected types).
|
|
34
|
+
*/
|
|
6
35
|
readonly issues: Issue[];
|
|
36
|
+
/**
|
|
37
|
+
* Creates a new validation error from a list of issues.
|
|
38
|
+
*
|
|
39
|
+
* Issues are automatically aggregated to combine related issues at the same
|
|
40
|
+
* path (e.g., multiple type expectations from a union schema).
|
|
41
|
+
*
|
|
42
|
+
* @param issues - The validation issues that caused this error
|
|
43
|
+
* @param options - Standard Error options (e.g., `cause`)
|
|
44
|
+
*/
|
|
7
45
|
constructor(issues: Issue[], options?: ErrorOptions);
|
|
46
|
+
/**
|
|
47
|
+
* Converts the error to a JSON-serializable object.
|
|
48
|
+
*
|
|
49
|
+
* @returns An object containing the error details and all issues in JSON format
|
|
50
|
+
*/
|
|
8
51
|
toJSON(): {
|
|
9
52
|
issues: {
|
|
10
53
|
code: string;
|
|
@@ -14,6 +57,23 @@ export declare class ValidationError extends LexError {
|
|
|
14
57
|
error: import("@atproto/lex-data").LexErrorCode;
|
|
15
58
|
message?: string;
|
|
16
59
|
};
|
|
60
|
+
/**
|
|
61
|
+
* Creates a validation error by combining multiple validation failures.
|
|
62
|
+
*
|
|
63
|
+
* This is useful when validating against multiple possible schemas (e.g., unions)
|
|
64
|
+
* and all branches fail. The resulting error contains issues from all failures.
|
|
65
|
+
*
|
|
66
|
+
* @param failures - The validation failures to combine
|
|
67
|
+
* @returns A single validation error containing all issues from the failures
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* ```typescript
|
|
71
|
+
* const failures = schemas.map(s => s.safeValidate(data)).filter(r => !r.success)
|
|
72
|
+
* if (failures.length === schemas.length) {
|
|
73
|
+
* throw ValidationError.fromFailures(failures)
|
|
74
|
+
* }
|
|
75
|
+
* ```
|
|
76
|
+
*/
|
|
17
77
|
static fromFailures(failures: ResultFailure<ValidationError>[]): ValidationError;
|
|
18
78
|
}
|
|
19
79
|
//# sourceMappingURL=validation-error.d.ts.map
|
|
@@ -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,qBAAa,eAAgB,SAAQ,QAAQ;IAC3C,IAAI,SAAoB;IAExB,QAAQ,CAAC,MAAM,EAAE,KAAK,EAAE,CAAA;
|
|
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;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,qBAAa,eAAgB,SAAQ,QAAQ;IAC3C,IAAI,SAAoB;IAExB;;;;;OAKG;IACH,QAAQ,CAAC,MAAM,EAAE,KAAK,EAAE,CAAA;IAExB;;;;;;;;OAQG;gBACS,MAAM,EAAE,KAAK,EAAE,EAAE,OAAO,CAAC,EAAE,YAAY;IAMnD;;;;OAIG;IACH,MAAM;;;;;;;;;IAON;;;;;;;;;;;;;;;;OAgBG;IACH,MAAM,CAAC,YAAY,CACjB,QAAQ,EAAE,aAAa,CAAC,eAAe,CAAC,EAAE,GACzC,eAAe;CAQnB"}
|
|
@@ -5,20 +5,80 @@ const lex_data_1 = require("@atproto/lex-data");
|
|
|
5
5
|
const array_agg_js_1 = require("../util/array-agg.js");
|
|
6
6
|
const result_js_1 = require("./result.js");
|
|
7
7
|
const validation_issue_js_1 = require("./validation-issue.js");
|
|
8
|
+
/**
|
|
9
|
+
* Error thrown when validation fails.
|
|
10
|
+
*
|
|
11
|
+
* Contains detailed information about all validation issues encountered,
|
|
12
|
+
* including the path to each invalid value and descriptions of what was
|
|
13
|
+
* expected vs what was received.
|
|
14
|
+
*
|
|
15
|
+
* Extends {@link LexError} with the error name "InvalidRequest" for
|
|
16
|
+
* consistency with the AT Protocol error handling conventions.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```typescript
|
|
20
|
+
* const error = new ValidationError([
|
|
21
|
+
* new IssueInvalidType(['user', 'age'], 'hello', ['number'])
|
|
22
|
+
* ])
|
|
23
|
+
* console.log(error.message)
|
|
24
|
+
* // "Expected number value type at $.user.age (got string)"
|
|
25
|
+
*
|
|
26
|
+
* console.log(error.issues.length) // 1
|
|
27
|
+
* console.log(error.toJSON())
|
|
28
|
+
* // { error: 'InvalidRequest', message: '...', issues: [...] }
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
8
31
|
class ValidationError extends lex_data_1.LexError {
|
|
9
32
|
name = 'ValidationError';
|
|
33
|
+
/**
|
|
34
|
+
* The list of validation issues that caused this error.
|
|
35
|
+
*
|
|
36
|
+
* Issues are aggregated when possible (e.g., multiple invalid type issues
|
|
37
|
+
* at the same path are combined into a single issue listing all expected types).
|
|
38
|
+
*/
|
|
10
39
|
issues;
|
|
40
|
+
/**
|
|
41
|
+
* Creates a new validation error from a list of issues.
|
|
42
|
+
*
|
|
43
|
+
* Issues are automatically aggregated to combine related issues at the same
|
|
44
|
+
* path (e.g., multiple type expectations from a union schema).
|
|
45
|
+
*
|
|
46
|
+
* @param issues - The validation issues that caused this error
|
|
47
|
+
* @param options - Standard Error options (e.g., `cause`)
|
|
48
|
+
*/
|
|
11
49
|
constructor(issues, options) {
|
|
12
50
|
const issuesAgg = aggregateIssues(issues);
|
|
13
51
|
super('InvalidRequest', issuesAgg.join(', '), options);
|
|
14
52
|
this.issues = issuesAgg;
|
|
15
53
|
}
|
|
54
|
+
/**
|
|
55
|
+
* Converts the error to a JSON-serializable object.
|
|
56
|
+
*
|
|
57
|
+
* @returns An object containing the error details and all issues in JSON format
|
|
58
|
+
*/
|
|
16
59
|
toJSON() {
|
|
17
60
|
return {
|
|
18
61
|
...super.toJSON(),
|
|
19
62
|
issues: this.issues.map((issue) => issue.toJSON()),
|
|
20
63
|
};
|
|
21
64
|
}
|
|
65
|
+
/**
|
|
66
|
+
* Creates a validation error by combining multiple validation failures.
|
|
67
|
+
*
|
|
68
|
+
* This is useful when validating against multiple possible schemas (e.g., unions)
|
|
69
|
+
* and all branches fail. The resulting error contains issues from all failures.
|
|
70
|
+
*
|
|
71
|
+
* @param failures - The validation failures to combine
|
|
72
|
+
* @returns A single validation error containing all issues from the failures
|
|
73
|
+
*
|
|
74
|
+
* @example
|
|
75
|
+
* ```typescript
|
|
76
|
+
* const failures = schemas.map(s => s.safeValidate(data)).filter(r => !r.success)
|
|
77
|
+
* if (failures.length === schemas.length) {
|
|
78
|
+
* throw ValidationError.fromFailures(failures)
|
|
79
|
+
* }
|
|
80
|
+
* ```
|
|
81
|
+
*/
|
|
22
82
|
static fromFailures(failures) {
|
|
23
83
|
if (failures.length === 1)
|
|
24
84
|
return (0, result_js_1.failureReason)(failures[0]);
|
|
@@ -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,2CAA0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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 ValidationError([\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 */\nexport class ValidationError extends LexError {\n name = 'ValidationError'\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 /**\n * Converts the error to a JSON-serializable object.\n *\n * @returns An object containing the error details and all issues in JSON format\n */\n 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 ValidationError.fromFailures(failures)\n * }\n * ```\n */\n static fromFailures(\n failures: ResultFailure<ValidationError>[],\n ): ValidationError {\n if (failures.length === 1) return failureReason(failures[0])\n const issues = failures.flatMap(extractFailureIssues)\n return new ValidationError(issues, {\n // Keep the original errors as the cause chain\n cause: failures.map(failureReason),\n })\n }\n}\n\nfunction extractFailureIssues(result: ResultFailure<ValidationError>) {\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,16 +1,40 @@
|
|
|
1
1
|
import { PropertyKey } from './property-key.js';
|
|
2
|
+
/**
|
|
3
|
+
* Abstract base class for all validation issues.
|
|
4
|
+
*
|
|
5
|
+
* An issue represents a single validation failure, containing:
|
|
6
|
+
* - A code identifying the type of issue
|
|
7
|
+
* - The path to the invalid value in the data structure
|
|
8
|
+
* - The actual input value that failed validation
|
|
9
|
+
*
|
|
10
|
+
* Subclasses add specific properties relevant to each issue type and
|
|
11
|
+
* implement the {@link toString} method for human-readable error messages.
|
|
12
|
+
*/
|
|
2
13
|
export declare abstract class Issue {
|
|
3
14
|
readonly code: string;
|
|
4
15
|
readonly path: readonly PropertyKey[];
|
|
5
16
|
readonly input: unknown;
|
|
6
17
|
constructor(code: string, path: readonly PropertyKey[], input: unknown);
|
|
18
|
+
/**
|
|
19
|
+
* Returns a human-readable description of the validation issue.
|
|
20
|
+
*/
|
|
7
21
|
abstract toString(): string;
|
|
22
|
+
/**
|
|
23
|
+
* Converts the issue to a JSON-serializable object.
|
|
24
|
+
*
|
|
25
|
+
* @returns An object containing the issue code, path, and message
|
|
26
|
+
*/
|
|
8
27
|
toJSON(): {
|
|
9
28
|
code: string;
|
|
10
29
|
path: readonly PropertyKey[];
|
|
11
30
|
message: string;
|
|
12
31
|
};
|
|
13
32
|
}
|
|
33
|
+
/**
|
|
34
|
+
* A custom validation issue with a user-defined message.
|
|
35
|
+
*
|
|
36
|
+
* Use this for validation rules that don't fit into the standard issue categories.
|
|
37
|
+
*/
|
|
14
38
|
export declare class IssueCustom extends Issue {
|
|
15
39
|
readonly path: readonly PropertyKey[];
|
|
16
40
|
readonly input: unknown;
|
|
@@ -18,6 +42,11 @@ export declare class IssueCustom extends Issue {
|
|
|
18
42
|
constructor(path: readonly PropertyKey[], input: unknown, message: string);
|
|
19
43
|
toString(): string;
|
|
20
44
|
}
|
|
45
|
+
/**
|
|
46
|
+
* Issue for string values that don't match an expected format.
|
|
47
|
+
*
|
|
48
|
+
* Used for AT Protocol specific formats like DID, handle, NSID, AT-URI, etc.
|
|
49
|
+
*/
|
|
21
50
|
export declare class IssueInvalidFormat extends Issue {
|
|
22
51
|
readonly format: string;
|
|
23
52
|
readonly message?: string | undefined;
|
|
@@ -29,8 +58,15 @@ export declare class IssueInvalidFormat extends Issue {
|
|
|
29
58
|
path: readonly PropertyKey[];
|
|
30
59
|
message: string;
|
|
31
60
|
};
|
|
61
|
+
/** Returns a human-readable description of the expected format. */
|
|
32
62
|
get formatDescription(): string;
|
|
33
63
|
}
|
|
64
|
+
/**
|
|
65
|
+
* Issue for values that have an unexpected type.
|
|
66
|
+
*
|
|
67
|
+
* This is one of the most common validation issues, occurring when the
|
|
68
|
+
* runtime type of a value doesn't match the expected schema type.
|
|
69
|
+
*/
|
|
34
70
|
export declare class IssueInvalidType extends Issue {
|
|
35
71
|
readonly expected: readonly string[];
|
|
36
72
|
constructor(path: readonly PropertyKey[], input: unknown, expected: readonly string[]);
|
|
@@ -42,6 +78,12 @@ export declare class IssueInvalidType extends Issue {
|
|
|
42
78
|
message: string;
|
|
43
79
|
};
|
|
44
80
|
}
|
|
81
|
+
/**
|
|
82
|
+
* Issue for values that don't match any of the expected literal values.
|
|
83
|
+
*
|
|
84
|
+
* Used when a value must be one of a specific set of allowed values
|
|
85
|
+
* (e.g., enum-like constraints).
|
|
86
|
+
*/
|
|
45
87
|
export declare class IssueInvalidValue extends Issue {
|
|
46
88
|
readonly values: readonly unknown[];
|
|
47
89
|
constructor(path: readonly PropertyKey[], input: unknown, values: readonly unknown[]);
|
|
@@ -53,6 +95,9 @@ export declare class IssueInvalidValue extends Issue {
|
|
|
53
95
|
message: string;
|
|
54
96
|
};
|
|
55
97
|
}
|
|
98
|
+
/**
|
|
99
|
+
* Issue for missing required object properties.
|
|
100
|
+
*/
|
|
56
101
|
export declare class IssueRequiredKey extends Issue {
|
|
57
102
|
readonly key: PropertyKey;
|
|
58
103
|
constructor(path: readonly PropertyKey[], input: unknown, key: PropertyKey);
|
|
@@ -64,7 +109,20 @@ export declare class IssueRequiredKey extends Issue {
|
|
|
64
109
|
message: string;
|
|
65
110
|
};
|
|
66
111
|
}
|
|
112
|
+
/**
|
|
113
|
+
* The type of measurement for size constraint issues.
|
|
114
|
+
*
|
|
115
|
+
* - `'array'` - Array length
|
|
116
|
+
* - `'string'` - String length in characters
|
|
117
|
+
* - `'integer'` - Numeric value
|
|
118
|
+
* - `'grapheme'` - String length in grapheme clusters
|
|
119
|
+
* - `'bytes'` - Byte length
|
|
120
|
+
* - `'blob'` - Blob size
|
|
121
|
+
*/
|
|
67
122
|
export type MeasurableType = 'array' | 'string' | 'integer' | 'grapheme' | 'bytes' | 'blob';
|
|
123
|
+
/**
|
|
124
|
+
* Issue for values that exceed a maximum constraint.
|
|
125
|
+
*/
|
|
68
126
|
export declare class IssueTooBig extends Issue {
|
|
69
127
|
readonly maximum: number;
|
|
70
128
|
readonly type: MeasurableType;
|
|
@@ -79,6 +137,9 @@ export declare class IssueTooBig extends Issue {
|
|
|
79
137
|
message: string;
|
|
80
138
|
};
|
|
81
139
|
}
|
|
140
|
+
/**
|
|
141
|
+
* Issue for values that are below a minimum constraint.
|
|
142
|
+
*/
|
|
82
143
|
export declare class IssueTooSmall extends Issue {
|
|
83
144
|
readonly minimum: number;
|
|
84
145
|
readonly type: MeasurableType;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validation-issue.d.ts","sourceRoot":"","sources":["../../src/core/validation-issue.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAA;AAE/C,8BAAsB,KAAK;IAEvB,QAAQ,CAAC,IAAI,EAAE,MAAM;IACrB,QAAQ,CAAC,IAAI,EAAE,SAAS,WAAW,EAAE;IACrC,QAAQ,CAAC,KAAK,EAAE,OAAO;gBAFd,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,SAAS,WAAW,EAAE,EAC5B,KAAK,EAAE,OAAO;IAGzB,QAAQ,CAAC,QAAQ,IAAI,MAAM;IAE3B,MAAM;;;;;CAOP;AAED,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;IAK1B,QAAQ;CAGT;AAED,qBAAa,kBAAmB,SAAQ,KAAK;IAIzC,QAAQ,CAAC,MAAM,EAAE,MAAM;IACvB,QAAQ,CAAC,OAAO,CAAC,EAAE,MAAM;gBAHzB,IAAI,EAAE,SAAS,WAAW,EAAE,EAC5B,KAAK,EAAE,OAAO,EACL,MAAM,EAAE,MAAM,EACd,OAAO,CAAC,EAAE,MAAM,YAAA;IAK3B,QAAQ;IAIR,MAAM;;;;;;IAON,IAAI,iBAAiB,IAAI,MAAM,CAiB9B;CACF;AAED,qBAAa,gBAAiB,SAAQ,KAAK;IAIvC,QAA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AQ,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,QAAQ;IAIR,MAAM;;;;;;;CAOP"}
|
|
@@ -2,6 +2,17 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.IssueTooSmall = exports.IssueTooBig = exports.IssueRequiredKey = exports.IssueInvalidValue = exports.IssueInvalidType = exports.IssueInvalidFormat = exports.IssueCustom = exports.Issue = void 0;
|
|
4
4
|
const lex_data_1 = require("@atproto/lex-data");
|
|
5
|
+
/**
|
|
6
|
+
* Abstract base class for all validation issues.
|
|
7
|
+
*
|
|
8
|
+
* An issue represents a single validation failure, containing:
|
|
9
|
+
* - A code identifying the type of issue
|
|
10
|
+
* - The path to the invalid value in the data structure
|
|
11
|
+
* - The actual input value that failed validation
|
|
12
|
+
*
|
|
13
|
+
* Subclasses add specific properties relevant to each issue type and
|
|
14
|
+
* implement the {@link toString} method for human-readable error messages.
|
|
15
|
+
*/
|
|
5
16
|
class Issue {
|
|
6
17
|
code;
|
|
7
18
|
path;
|
|
@@ -11,6 +22,11 @@ class Issue {
|
|
|
11
22
|
this.path = path;
|
|
12
23
|
this.input = input;
|
|
13
24
|
}
|
|
25
|
+
/**
|
|
26
|
+
* Converts the issue to a JSON-serializable object.
|
|
27
|
+
*
|
|
28
|
+
* @returns An object containing the issue code, path, and message
|
|
29
|
+
*/
|
|
14
30
|
toJSON() {
|
|
15
31
|
return {
|
|
16
32
|
code: this.code,
|
|
@@ -20,6 +36,11 @@ class Issue {
|
|
|
20
36
|
}
|
|
21
37
|
}
|
|
22
38
|
exports.Issue = Issue;
|
|
39
|
+
/**
|
|
40
|
+
* A custom validation issue with a user-defined message.
|
|
41
|
+
*
|
|
42
|
+
* Use this for validation rules that don't fit into the standard issue categories.
|
|
43
|
+
*/
|
|
23
44
|
class IssueCustom extends Issue {
|
|
24
45
|
path;
|
|
25
46
|
input;
|
|
@@ -35,6 +56,11 @@ class IssueCustom extends Issue {
|
|
|
35
56
|
}
|
|
36
57
|
}
|
|
37
58
|
exports.IssueCustom = IssueCustom;
|
|
59
|
+
/**
|
|
60
|
+
* Issue for string values that don't match an expected format.
|
|
61
|
+
*
|
|
62
|
+
* Used for AT Protocol specific formats like DID, handle, NSID, AT-URI, etc.
|
|
63
|
+
*/
|
|
38
64
|
class IssueInvalidFormat extends Issue {
|
|
39
65
|
format;
|
|
40
66
|
message;
|
|
@@ -52,6 +78,7 @@ class IssueInvalidFormat extends Issue {
|
|
|
52
78
|
format: this.format,
|
|
53
79
|
};
|
|
54
80
|
}
|
|
81
|
+
/** Returns a human-readable description of the expected format. */
|
|
55
82
|
get formatDescription() {
|
|
56
83
|
switch (this.format) {
|
|
57
84
|
case 'at-identifier':
|
|
@@ -72,6 +99,12 @@ class IssueInvalidFormat extends Issue {
|
|
|
72
99
|
}
|
|
73
100
|
}
|
|
74
101
|
exports.IssueInvalidFormat = IssueInvalidFormat;
|
|
102
|
+
/**
|
|
103
|
+
* Issue for values that have an unexpected type.
|
|
104
|
+
*
|
|
105
|
+
* This is one of the most common validation issues, occurring when the
|
|
106
|
+
* runtime type of a value doesn't match the expected schema type.
|
|
107
|
+
*/
|
|
75
108
|
class IssueInvalidType extends Issue {
|
|
76
109
|
expected;
|
|
77
110
|
constructor(path, input, expected) {
|
|
@@ -89,6 +122,12 @@ class IssueInvalidType extends Issue {
|
|
|
89
122
|
}
|
|
90
123
|
}
|
|
91
124
|
exports.IssueInvalidType = IssueInvalidType;
|
|
125
|
+
/**
|
|
126
|
+
* Issue for values that don't match any of the expected literal values.
|
|
127
|
+
*
|
|
128
|
+
* Used when a value must be one of a specific set of allowed values
|
|
129
|
+
* (e.g., enum-like constraints).
|
|
130
|
+
*/
|
|
92
131
|
class IssueInvalidValue extends Issue {
|
|
93
132
|
values;
|
|
94
133
|
constructor(path, input, values) {
|
|
@@ -106,6 +145,9 @@ class IssueInvalidValue extends Issue {
|
|
|
106
145
|
}
|
|
107
146
|
}
|
|
108
147
|
exports.IssueInvalidValue = IssueInvalidValue;
|
|
148
|
+
/**
|
|
149
|
+
* Issue for missing required object properties.
|
|
150
|
+
*/
|
|
109
151
|
class IssueRequiredKey extends Issue {
|
|
110
152
|
key;
|
|
111
153
|
constructor(path, input, key) {
|
|
@@ -123,6 +165,9 @@ class IssueRequiredKey extends Issue {
|
|
|
123
165
|
}
|
|
124
166
|
}
|
|
125
167
|
exports.IssueRequiredKey = IssueRequiredKey;
|
|
168
|
+
/**
|
|
169
|
+
* Issue for values that exceed a maximum constraint.
|
|
170
|
+
*/
|
|
126
171
|
class IssueTooBig extends Issue {
|
|
127
172
|
maximum;
|
|
128
173
|
type;
|
|
@@ -145,6 +190,9 @@ class IssueTooBig extends Issue {
|
|
|
145
190
|
}
|
|
146
191
|
}
|
|
147
192
|
exports.IssueTooBig = IssueTooBig;
|
|
193
|
+
/**
|
|
194
|
+
* Issue for values that are below a minimum constraint.
|
|
195
|
+
*/
|
|
148
196
|
class IssueTooSmall extends Issue {
|
|
149
197
|
minimum;
|
|
150
198
|
type;
|
|
@@ -167,9 +215,12 @@ class IssueTooSmall extends Issue {
|
|
|
167
215
|
}
|
|
168
216
|
}
|
|
169
217
|
exports.IssueTooSmall = IssueTooSmall;
|
|
218
|
+
// -----------------------------------------------------------------------------
|
|
219
|
+
// Helper functions for formatting error messages
|
|
220
|
+
// -----------------------------------------------------------------------------
|
|
170
221
|
function stringifyExpectedType(expected) {
|
|
171
222
|
if (expected === '$typed') {
|
|
172
|
-
return 'an object
|
|
223
|
+
return 'an object which includes the "$type" property';
|
|
173
224
|
}
|
|
174
225
|
return expected;
|
|
175
226
|
}
|
|
@@ -206,6 +257,8 @@ function stringifyType(value) {
|
|
|
206
257
|
return 'array';
|
|
207
258
|
if ((0, lex_data_1.ifCid)(value))
|
|
208
259
|
return 'cid';
|
|
260
|
+
if ((0, lex_data_1.isLegacyBlobRef)(value))
|
|
261
|
+
return 'legacy-blob';
|
|
209
262
|
if (value instanceof Date)
|
|
210
263
|
return 'date';
|
|
211
264
|
if (value instanceof RegExp)
|