@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.
Files changed (261) hide show
  1. package/CHANGELOG.md +54 -0
  2. package/dist/core/$type.d.ts +149 -0
  3. package/dist/core/$type.d.ts.map +1 -1
  4. package/dist/core/$type.js +44 -0
  5. package/dist/core/$type.js.map +1 -1
  6. package/dist/core/record-key.d.ts +44 -0
  7. package/dist/core/record-key.d.ts.map +1 -1
  8. package/dist/core/record-key.js +30 -0
  9. package/dist/core/record-key.js.map +1 -1
  10. package/dist/core/result.d.ts +85 -4
  11. package/dist/core/result.d.ts.map +1 -1
  12. package/dist/core/result.js +60 -4
  13. package/dist/core/result.js.map +1 -1
  14. package/dist/core/schema.d.ts +232 -5
  15. package/dist/core/schema.d.ts.map +1 -1
  16. package/dist/core/schema.js +197 -4
  17. package/dist/core/schema.js.map +1 -1
  18. package/dist/core/string-format.d.ts +244 -11
  19. package/dist/core/string-format.d.ts.map +1 -1
  20. package/dist/core/string-format.js +150 -0
  21. package/dist/core/string-format.js.map +1 -1
  22. package/dist/core/types.d.ts +90 -3
  23. package/dist/core/types.d.ts.map +1 -1
  24. package/dist/core/types.js.map +1 -1
  25. package/dist/core/validation-error.d.ts +60 -0
  26. package/dist/core/validation-error.d.ts.map +1 -1
  27. package/dist/core/validation-error.js +60 -0
  28. package/dist/core/validation-error.js.map +1 -1
  29. package/dist/core/validation-issue.d.ts +61 -0
  30. package/dist/core/validation-issue.d.ts.map +1 -1
  31. package/dist/core/validation-issue.js +54 -1
  32. package/dist/core/validation-issue.js.map +1 -1
  33. package/dist/core/validator.d.ts +356 -11
  34. package/dist/core/validator.d.ts.map +1 -1
  35. package/dist/core/validator.js +203 -4
  36. package/dist/core/validator.js.map +1 -1
  37. package/dist/helpers.d.ts +12 -28
  38. package/dist/helpers.d.ts.map +1 -1
  39. package/dist/helpers.js.map +1 -1
  40. package/dist/schema/array.d.ts +46 -0
  41. package/dist/schema/array.d.ts.map +1 -1
  42. package/dist/schema/array.js +16 -1
  43. package/dist/schema/array.js.map +1 -1
  44. package/dist/schema/blob.d.ts +50 -2
  45. package/dist/schema/blob.d.ts.map +1 -1
  46. package/dist/schema/blob.js +44 -2
  47. package/dist/schema/blob.js.map +1 -1
  48. package/dist/schema/boolean.d.ts +29 -0
  49. package/dist/schema/boolean.d.ts.map +1 -1
  50. package/dist/schema/boolean.js +30 -1
  51. package/dist/schema/boolean.js.map +1 -1
  52. package/dist/schema/bytes.d.ts +39 -0
  53. package/dist/schema/bytes.d.ts.map +1 -1
  54. package/dist/schema/bytes.js +34 -1
  55. package/dist/schema/bytes.js.map +1 -1
  56. package/dist/schema/cid.d.ts +39 -0
  57. package/dist/schema/cid.d.ts.map +1 -1
  58. package/dist/schema/cid.js +35 -1
  59. package/dist/schema/cid.js.map +1 -1
  60. package/dist/schema/custom.d.ts +67 -1
  61. package/dist/schema/custom.d.ts.map +1 -1
  62. package/dist/schema/custom.js +55 -0
  63. package/dist/schema/custom.js.map +1 -1
  64. package/dist/schema/dict.d.ts +45 -0
  65. package/dist/schema/dict.d.ts.map +1 -1
  66. package/dist/schema/dict.js +46 -1
  67. package/dist/schema/dict.js.map +1 -1
  68. package/dist/schema/discriminated-union.d.ts +59 -0
  69. package/dist/schema/discriminated-union.d.ts.map +1 -1
  70. package/dist/schema/discriminated-union.js +47 -1
  71. package/dist/schema/discriminated-union.js.map +1 -1
  72. package/dist/schema/enum.d.ts +49 -0
  73. package/dist/schema/enum.d.ts.map +1 -1
  74. package/dist/schema/enum.js +49 -0
  75. package/dist/schema/enum.js.map +1 -1
  76. package/dist/schema/integer.d.ts +43 -0
  77. package/dist/schema/integer.d.ts.map +1 -1
  78. package/dist/schema/integer.js +38 -1
  79. package/dist/schema/integer.js.map +1 -1
  80. package/dist/schema/intersection.d.ts +55 -0
  81. package/dist/schema/intersection.d.ts.map +1 -1
  82. package/dist/schema/intersection.js +50 -0
  83. package/dist/schema/intersection.js.map +1 -1
  84. package/dist/schema/lex-map.d.ts +37 -0
  85. package/dist/schema/lex-map.d.ts.map +1 -0
  86. package/dist/schema/lex-map.js +60 -0
  87. package/dist/schema/lex-map.js.map +1 -0
  88. package/dist/schema/lex-value.d.ts +35 -0
  89. package/dist/schema/lex-value.d.ts.map +1 -0
  90. package/dist/schema/lex-value.js +87 -0
  91. package/dist/schema/lex-value.js.map +1 -0
  92. package/dist/schema/literal.d.ts +45 -0
  93. package/dist/schema/literal.d.ts.map +1 -1
  94. package/dist/schema/literal.js +45 -0
  95. package/dist/schema/literal.js.map +1 -1
  96. package/dist/schema/never.d.ts +43 -0
  97. package/dist/schema/never.d.ts.map +1 -1
  98. package/dist/schema/never.js +44 -1
  99. package/dist/schema/never.js.map +1 -1
  100. package/dist/schema/null.d.ts +30 -0
  101. package/dist/schema/null.d.ts.map +1 -1
  102. package/dist/schema/null.js +31 -1
  103. package/dist/schema/null.js.map +1 -1
  104. package/dist/schema/nullable.d.ts +42 -0
  105. package/dist/schema/nullable.d.ts.map +1 -1
  106. package/dist/schema/nullable.js +42 -0
  107. package/dist/schema/nullable.js.map +1 -1
  108. package/dist/schema/object.d.ts +57 -0
  109. package/dist/schema/object.d.ts.map +1 -1
  110. package/dist/schema/object.js +53 -1
  111. package/dist/schema/object.js.map +1 -1
  112. package/dist/schema/optional.d.ts +43 -0
  113. package/dist/schema/optional.d.ts.map +1 -1
  114. package/dist/schema/optional.js +43 -0
  115. package/dist/schema/optional.js.map +1 -1
  116. package/dist/schema/params.d.ts +96 -12
  117. package/dist/schema/params.d.ts.map +1 -1
  118. package/dist/schema/params.js +155 -21
  119. package/dist/schema/params.js.map +1 -1
  120. package/dist/schema/payload.d.ts +111 -15
  121. package/dist/schema/payload.d.ts.map +1 -1
  122. package/dist/schema/payload.js +73 -3
  123. package/dist/schema/payload.js.map +1 -1
  124. package/dist/schema/permission-set.d.ts +58 -0
  125. package/dist/schema/permission-set.d.ts.map +1 -1
  126. package/dist/schema/permission-set.js +50 -0
  127. package/dist/schema/permission-set.js.map +1 -1
  128. package/dist/schema/permission.d.ts +42 -0
  129. package/dist/schema/permission.d.ts.map +1 -1
  130. package/dist/schema/permission.js +39 -0
  131. package/dist/schema/permission.js.map +1 -1
  132. package/dist/schema/procedure.d.ts +64 -0
  133. package/dist/schema/procedure.d.ts.map +1 -1
  134. package/dist/schema/procedure.js +64 -0
  135. package/dist/schema/procedure.js.map +1 -1
  136. package/dist/schema/query.d.ts +55 -0
  137. package/dist/schema/query.d.ts.map +1 -1
  138. package/dist/schema/query.js +55 -0
  139. package/dist/schema/query.js.map +1 -1
  140. package/dist/schema/record.d.ts +76 -25
  141. package/dist/schema/record.d.ts.map +1 -1
  142. package/dist/schema/record.js +21 -0
  143. package/dist/schema/record.js.map +1 -1
  144. package/dist/schema/ref.d.ts +51 -0
  145. package/dist/schema/ref.d.ts.map +1 -1
  146. package/dist/schema/ref.js +18 -0
  147. package/dist/schema/ref.js.map +1 -1
  148. package/dist/schema/refine.d.ts +58 -9
  149. package/dist/schema/refine.d.ts.map +1 -1
  150. package/dist/schema/refine.js.map +1 -1
  151. package/dist/schema/regexp.d.ts +45 -0
  152. package/dist/schema/regexp.d.ts.map +1 -1
  153. package/dist/schema/regexp.js +46 -1
  154. package/dist/schema/regexp.js.map +1 -1
  155. package/dist/schema/string.d.ts +72 -6
  156. package/dist/schema/string.d.ts.map +1 -1
  157. package/dist/schema/string.js +56 -8
  158. package/dist/schema/string.js.map +1 -1
  159. package/dist/schema/subscription.d.ts +72 -2
  160. package/dist/schema/subscription.d.ts.map +1 -1
  161. package/dist/schema/subscription.js +59 -0
  162. package/dist/schema/subscription.js.map +1 -1
  163. package/dist/schema/token.d.ts +48 -0
  164. package/dist/schema/token.d.ts.map +1 -1
  165. package/dist/schema/token.js +49 -1
  166. package/dist/schema/token.js.map +1 -1
  167. package/dist/schema/typed-object.d.ts +73 -23
  168. package/dist/schema/typed-object.d.ts.map +1 -1
  169. package/dist/schema/typed-object.js +20 -1
  170. package/dist/schema/typed-object.js.map +1 -1
  171. package/dist/schema/typed-ref.d.ts +54 -0
  172. package/dist/schema/typed-ref.d.ts.map +1 -1
  173. package/dist/schema/typed-ref.js +16 -0
  174. package/dist/schema/typed-ref.js.map +1 -1
  175. package/dist/schema/typed-union.d.ts +51 -1
  176. package/dist/schema/typed-union.d.ts.map +1 -1
  177. package/dist/schema/typed-union.js +52 -2
  178. package/dist/schema/typed-union.js.map +1 -1
  179. package/dist/schema/union.d.ts +46 -0
  180. package/dist/schema/union.d.ts.map +1 -1
  181. package/dist/schema/union.js +41 -0
  182. package/dist/schema/union.js.map +1 -1
  183. package/dist/schema/unknown.d.ts +34 -0
  184. package/dist/schema/unknown.d.ts.map +1 -1
  185. package/dist/schema/unknown.js +34 -0
  186. package/dist/schema/unknown.js.map +1 -1
  187. package/dist/schema/with-default.d.ts +45 -0
  188. package/dist/schema/with-default.d.ts.map +1 -1
  189. package/dist/schema/with-default.js +45 -0
  190. package/dist/schema/with-default.js.map +1 -1
  191. package/dist/schema.d.ts +2 -1
  192. package/dist/schema.d.ts.map +1 -1
  193. package/dist/schema.js +2 -1
  194. package/dist/schema.js.map +1 -1
  195. package/dist/util/if-any.d.ts +2 -0
  196. package/dist/util/if-any.d.ts.map +1 -0
  197. package/dist/util/if-any.js +3 -0
  198. package/dist/util/if-any.js.map +1 -0
  199. package/package.json +3 -3
  200. package/src/core/$type.ts +150 -18
  201. package/src/core/record-key.ts +44 -0
  202. package/src/core/result.ts +86 -4
  203. package/src/core/schema.ts +244 -9
  204. package/src/core/string-format.ts +259 -13
  205. package/src/core/types.ts +91 -3
  206. package/src/core/validation-error.ts +60 -0
  207. package/src/core/validation-issue.ts +68 -2
  208. package/src/core/validator.ts +373 -12
  209. package/src/helpers.test.ts +110 -29
  210. package/src/helpers.ts +54 -25
  211. package/src/schema/array.test.ts +94 -79
  212. package/src/schema/array.ts +48 -1
  213. package/src/schema/blob.ts +50 -1
  214. package/src/schema/boolean.ts +31 -1
  215. package/src/schema/bytes.ts +41 -1
  216. package/src/schema/cid.ts +41 -1
  217. package/src/schema/custom.ts +68 -1
  218. package/src/schema/dict.ts +47 -1
  219. package/src/schema/discriminated-union.ts +61 -1
  220. package/src/schema/enum.ts +50 -0
  221. package/src/schema/integer.ts +45 -1
  222. package/src/schema/intersection.ts +56 -0
  223. package/src/schema/{unknown-object.test.ts → lex-map.test.ts} +9 -9
  224. package/src/schema/lex-map.ts +63 -0
  225. package/src/schema/lex-value.test.ts +81 -0
  226. package/src/schema/lex-value.ts +86 -0
  227. package/src/schema/literal.ts +46 -0
  228. package/src/schema/never.ts +45 -1
  229. package/src/schema/null.ts +32 -1
  230. package/src/schema/nullable.ts +43 -0
  231. package/src/schema/object.ts +59 -1
  232. package/src/schema/optional.ts +44 -0
  233. package/src/schema/params.test.ts +133 -38
  234. package/src/schema/params.ts +237 -37
  235. package/src/schema/payload.test.ts +3 -3
  236. package/src/schema/payload.ts +145 -42
  237. package/src/schema/permission-set.ts +58 -0
  238. package/src/schema/permission.ts +42 -0
  239. package/src/schema/procedure.ts +64 -0
  240. package/src/schema/query.ts +55 -0
  241. package/src/schema/record.ts +82 -16
  242. package/src/schema/ref.ts +52 -0
  243. package/src/schema/refine.ts +58 -9
  244. package/src/schema/regexp.ts +47 -1
  245. package/src/schema/string.test.ts +99 -2
  246. package/src/schema/string.ts +108 -15
  247. package/src/schema/subscription.ts +72 -2
  248. package/src/schema/token.ts +50 -1
  249. package/src/schema/typed-object.ts +81 -16
  250. package/src/schema/typed-ref.ts +55 -0
  251. package/src/schema/typed-union.ts +58 -3
  252. package/src/schema/union.ts +47 -0
  253. package/src/schema/unknown.ts +35 -0
  254. package/src/schema/with-default.ts +46 -0
  255. package/src/schema.ts +2 -1
  256. package/src/util/if-any.ts +3 -0
  257. package/dist/schema/unknown-object.d.ts +0 -8
  258. package/dist/schema/unknown-object.d.ts.map +0 -1
  259. package/dist/schema/unknown-object.js +0 -19
  260. package/dist/schema/unknown-object.js.map +0 -1
  261. package/src/schema/unknown-object.ts +0 -19
package/src/core/types.ts CHANGED
@@ -1,23 +1,95 @@
1
1
  /**
2
- * Same as {@link string} but prevents TypeScript allowing union types to
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 are optional (i.e. may be `undefined`)
20
- * into actual optional properties on the resulting type.
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 or record which includes a "$type" property'
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'