@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
@@ -6,18 +6,74 @@ exports.failureReason = failureReason;
6
6
  exports.successValue = successValue;
7
7
  exports.catchall = catchall;
8
8
  exports.createCatcher = createCatcher;
9
+ /**
10
+ * Creates a successful result wrapping the given value.
11
+ *
12
+ * @typeParam V - The type of the value
13
+ * @param value - The success value to wrap
14
+ * @returns {ResultSuccess} A success result containing the value
15
+ *
16
+ * @example
17
+ * ```typescript
18
+ * const result = success(42)
19
+ * console.log(result.success) // true
20
+ * console.log(result.value) // 42
21
+ * ```
22
+ */
9
23
  /*@__NO_SIDE_EFFECTS__*/
10
24
  function success(value) {
11
25
  return { success: true, value };
12
26
  }
27
+ /**
28
+ * Creates a failed result wrapping the given error reason.
29
+ *
30
+ * @typeParam E - The type of the error reason
31
+ * @param reason - The error reason to wrap
32
+ * @returns {ResultFailure} A failure result containing the error
33
+ *
34
+ * @example
35
+ * ```typescript
36
+ * const result = failure(new Error('Something went wrong'))
37
+ * console.log(result.success) // false
38
+ * console.log(result.reason.message) // "Something went wrong"
39
+ * ```
40
+ */
13
41
  /*@__NO_SIDE_EFFECTS__*/
14
42
  function failure(reason) {
15
43
  return { success: false, reason };
16
44
  }
45
+ /**
46
+ * Extracts the error reason from a failure result.
47
+ *
48
+ * @typeParam T - The type of the error reason
49
+ * @param result - A failure result
50
+ * @returns {T} The error reason
51
+ *
52
+ * @example
53
+ * ```typescript
54
+ * const result = failure(new Error('oops'))
55
+ * const error = failureReason(result)
56
+ * console.log(error.message) // "oops"
57
+ * ```
58
+ */
17
59
  /*@__NO_SIDE_EFFECTS__*/
18
60
  function failureReason(result) {
19
61
  return result.reason;
20
62
  }
63
+ /**
64
+ * Extracts the value from a success result.
65
+ *
66
+ * @typeParam T - The type of the success value
67
+ * @param result - A success result
68
+ * @returns {T} The success value
69
+ *
70
+ * @example
71
+ * ```typescript
72
+ * const result = success(42)
73
+ * const value = successValue(result)
74
+ * console.log(value) // 42
75
+ * ```
76
+ */
21
77
  /*@__NO_SIDE_EFFECTS__*/
22
78
  function successValue(result) {
23
79
  return result.value;
@@ -26,7 +82,7 @@ function successValue(result) {
26
82
  * Catches any error and wraps it in a {@link ResultFailure<Error>}.
27
83
  *
28
84
  * @param err - The error to catch.
29
- * @returns A {@link ResultFailure<Error>} containing the caught error.
85
+ * @returns {ResultFailure} A failure result containing the error.
30
86
  * @example
31
87
  *
32
88
  * ```ts
@@ -36,8 +92,8 @@ function successValue(result) {
36
92
  * if (result.success) {
37
93
  * console.log(result.value) // string
38
94
  * } else {
39
- * console.error(result.error instanceof Error) // true
40
- * console.error(result.error.message) // string
95
+ * console.error(result.reason instanceof Error) // true
96
+ * console.error(result.reason.message) // string
41
97
  * }
42
98
  * ```
43
99
  */
@@ -67,7 +123,7 @@ function catchall(err) {
67
123
  * if (result.success) {
68
124
  * console.log(result.value) // string
69
125
  * } else {
70
- * console.error(result.error) // FooError | BarError
126
+ * console.error(result.reason) // FooError | BarError
71
127
  * }
72
128
  */
73
129
  /*@__NO_SIDE_EFFECTS__*/
@@ -1 +1 @@
1
- {"version":3,"file":"result.js","sourceRoot":"","sources":["../../src/core/result.ts"],"names":[],"mappings":";;AAMA,0BAEC;AAGD,0BAEC;AAGD,sCAEC;AAGD,oCAEC;AAsBD,4BAGC;AA0BD,sCAKC;AA1ED,wBAAwB;AACxB,SAAgB,OAAO,CAAI,KAAQ;IACjC,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,CAAA;AACjC,CAAC;AAED,wBAAwB;AACxB,SAAgB,OAAO,CAAI,MAAS;IAClC,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,CAAA;AACnC,CAAC;AAED,wBAAwB;AACxB,SAAgB,aAAa,CAAI,MAAwB;IACvD,OAAO,MAAM,CAAC,MAAM,CAAA;AACtB,CAAC;AAED,wBAAwB;AACxB,SAAgB,YAAY,CAAI,MAAwB;IACtD,OAAO,MAAM,CAAC,KAAK,CAAA;AACrB,CAAC;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAwB;AACxB,SAAgB,QAAQ,CAAC,GAAY;IACnC,IAAI,GAAG,YAAY,KAAK;QAAE,OAAO,OAAO,CAAC,GAAG,CAAC,CAAA;IAC7C,OAAO,OAAO,CAAC,IAAI,KAAK,CAAC,eAAe,EAAE,EAAE,KAAK,EAAE,GAAG,EAAE,CAAC,CAAC,CAAA;AAC5D,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAwB;AACxB,SAAgB,aAAa,CAAI,IAA+B;IAC9D,OAAO,CAAC,GAAY,EAAoB,EAAE;QACxC,IAAI,GAAG,YAAY,IAAI;YAAE,OAAO,OAAO,CAAC,GAAG,CAAC,CAAA;QAC5C,MAAM,GAAG,CAAA;IACX,CAAC,CAAA;AACH,CAAC","sourcesContent":["export type ResultSuccess<V = any> = { success: true; value: V }\nexport type ResultFailure<E = Error> = { success: false; reason: E }\n\nexport type Result<V = any, E = Error> = ResultSuccess<V> | ResultFailure<E>\n\n/*@__NO_SIDE_EFFECTS__*/\nexport function success<V>(value: V): ResultSuccess<V> {\n return { success: true, value }\n}\n\n/*@__NO_SIDE_EFFECTS__*/\nexport function failure<E>(reason: E): ResultFailure<E> {\n return { success: false, reason }\n}\n\n/*@__NO_SIDE_EFFECTS__*/\nexport function failureReason<T>(result: ResultFailure<T>): T {\n return result.reason\n}\n\n/*@__NO_SIDE_EFFECTS__*/\nexport function successValue<T>(result: ResultSuccess<T>): T {\n return result.value\n}\n\n/**\n * Catches any error and wraps it in a {@link ResultFailure<Error>}.\n *\n * @param err - The error to catch.\n * @returns A {@link ResultFailure<Error>} containing the caught error.\n * @example\n *\n * ```ts\n * declare function someFunction(): Promise<string>\n *\n * const result = await someFunction().then(success, catchall)\n * if (result.success) {\n * console.log(result.value) // string\n * } else {\n * console.error(result.error instanceof Error) // true\n * console.error(result.error.message) // string\n * }\n * ```\n */\n/*@__NO_SIDE_EFFECTS__*/\nexport function catchall(err: unknown): ResultFailure<Error> {\n if (err instanceof Error) return failure(err)\n return failure(new Error('Unknown error', { cause: err }))\n}\n\n/**\n * Creates a catcher function for the given constructor that wraps caught errors\n * in a {@link ResultFailure}.\n *\n * @example\n *\n * ```ts\n * class FooError extends Error {}\n * class BarError extends Error {}\n *\n * declare function someFunction(): Promise<string>\n *\n * const result = await someFunction()\n * .then(success)\n * .catch(createCatcher(FooError))\n * .catch(createCatcher(BarError))\n *\n * if (result.success) {\n * console.log(result.value) // string\n * } else {\n * console.error(result.error) // FooError | BarError\n * }\n */\n/*@__NO_SIDE_EFFECTS__*/\nexport function createCatcher<T>(Ctor: new (...args: any[]) => T) {\n return (err: unknown): ResultFailure<T> => {\n if (err instanceof Ctor) return failure(err)\n throw err\n }\n}\n"]}
1
+ {"version":3,"file":"result.js","sourceRoot":"","sources":["../../src/core/result.ts"],"names":[],"mappings":";;AA8CA,0BAEC;AAiBD,0BAEC;AAiBD,sCAEC;AAiBD,oCAEC;AAsBD,4BAGC;AA0BD,sCAKC;AAlID;;;;;;;;;;;;;GAaG;AACH,wBAAwB;AACxB,SAAgB,OAAO,CAAI,KAAQ;IACjC,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,CAAA;AACjC,CAAC;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAwB;AACxB,SAAgB,OAAO,CAAI,MAAS;IAClC,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,CAAA;AACnC,CAAC;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAwB;AACxB,SAAgB,aAAa,CAAI,MAAwB;IACvD,OAAO,MAAM,CAAC,MAAM,CAAA;AACtB,CAAC;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAwB;AACxB,SAAgB,YAAY,CAAI,MAAwB;IACtD,OAAO,MAAM,CAAC,KAAK,CAAA;AACrB,CAAC;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAwB;AACxB,SAAgB,QAAQ,CAAC,GAAY;IACnC,IAAI,GAAG,YAAY,KAAK;QAAE,OAAO,OAAO,CAAC,GAAG,CAAC,CAAA;IAC7C,OAAO,OAAO,CAAC,IAAI,KAAK,CAAC,eAAe,EAAE,EAAE,KAAK,EAAE,GAAG,EAAE,CAAC,CAAC,CAAA;AAC5D,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAwB;AACxB,SAAgB,aAAa,CAAI,IAA+B;IAC9D,OAAO,CAAC,GAAY,EAAoB,EAAE;QACxC,IAAI,GAAG,YAAY,IAAI;YAAE,OAAO,OAAO,CAAC,GAAG,CAAC,CAAA;QAC5C,MAAM,GAAG,CAAA;IACX,CAAC,CAAA;AACH,CAAC","sourcesContent":["export type ResultSuccess<V = any> = { success: true; value: V }\n\n/**\n * Represents a failed result containing an error reason.\n *\n * @typeParam E - The type of the error reason\n */\nexport type ResultFailure<E = Error> = { success: false; reason: E }\n\n/**\n * A discriminated union type representing either a success or failure outcome.\n *\n * Check the `success` property to determine the outcome and access the\n * appropriate property (`value` for success, `reason` for failure).\n *\n * @typeParam V - The type of the success value\n * @typeParam E - The type of the error reason\n *\n * @example\n * ```typescript\n * function parseJson(text: string): Result<unknown, SyntaxError> {\n * try {\n * return success(JSON.parse(text))\n * } catch (e) {\n * return failure(e as SyntaxError)\n * }\n * }\n * ```\n */\nexport type Result<V = any, E = Error> = ResultSuccess<V> | ResultFailure<E>\n\n/**\n * Creates a successful result wrapping the given value.\n *\n * @typeParam V - The type of the value\n * @param value - The success value to wrap\n * @returns {ResultSuccess} A success result containing the value\n *\n * @example\n * ```typescript\n * const result = success(42)\n * console.log(result.success) // true\n * console.log(result.value) // 42\n * ```\n */\n/*@__NO_SIDE_EFFECTS__*/\nexport function success<V>(value: V): ResultSuccess<V> {\n return { success: true, value }\n}\n\n/**\n * Creates a failed result wrapping the given error reason.\n *\n * @typeParam E - The type of the error reason\n * @param reason - The error reason to wrap\n * @returns {ResultFailure} A failure result containing the error\n *\n * @example\n * ```typescript\n * const result = failure(new Error('Something went wrong'))\n * console.log(result.success) // false\n * console.log(result.reason.message) // \"Something went wrong\"\n * ```\n */\n/*@__NO_SIDE_EFFECTS__*/\nexport function failure<E>(reason: E): ResultFailure<E> {\n return { success: false, reason }\n}\n\n/**\n * Extracts the error reason from a failure result.\n *\n * @typeParam T - The type of the error reason\n * @param result - A failure result\n * @returns {T} The error reason\n *\n * @example\n * ```typescript\n * const result = failure(new Error('oops'))\n * const error = failureReason(result)\n * console.log(error.message) // \"oops\"\n * ```\n */\n/*@__NO_SIDE_EFFECTS__*/\nexport function failureReason<T>(result: ResultFailure<T>): T {\n return result.reason\n}\n\n/**\n * Extracts the value from a success result.\n *\n * @typeParam T - The type of the success value\n * @param result - A success result\n * @returns {T} The success value\n *\n * @example\n * ```typescript\n * const result = success(42)\n * const value = successValue(result)\n * console.log(value) // 42\n * ```\n */\n/*@__NO_SIDE_EFFECTS__*/\nexport function successValue<T>(result: ResultSuccess<T>): T {\n return result.value\n}\n\n/**\n * Catches any error and wraps it in a {@link ResultFailure<Error>}.\n *\n * @param err - The error to catch.\n * @returns {ResultFailure} A failure result containing the error.\n * @example\n *\n * ```ts\n * declare function someFunction(): Promise<string>\n *\n * const result = await someFunction().then(success, catchall)\n * if (result.success) {\n * console.log(result.value) // string\n * } else {\n * console.error(result.reason instanceof Error) // true\n * console.error(result.reason.message) // string\n * }\n * ```\n */\n/*@__NO_SIDE_EFFECTS__*/\nexport function catchall(err: unknown): ResultFailure<Error> {\n if (err instanceof Error) return failure(err)\n return failure(new Error('Unknown error', { cause: err }))\n}\n\n/**\n * Creates a catcher function for the given constructor that wraps caught errors\n * in a {@link ResultFailure}.\n *\n * @example\n *\n * ```ts\n * class FooError extends Error {}\n * class BarError extends Error {}\n *\n * declare function someFunction(): Promise<string>\n *\n * const result = await someFunction()\n * .then(success)\n * .catch(createCatcher(FooError))\n * .catch(createCatcher(BarError))\n *\n * if (result.success) {\n * console.log(result.value) // string\n * } else {\n * console.error(result.reason) // FooError | BarError\n * }\n */\n/*@__NO_SIDE_EFFECTS__*/\nexport function createCatcher<T>(Ctor: new (...args: any[]) => T) {\n return (err: unknown): ResultFailure<T> => {\n if (err instanceof Ctor) return failure(err)\n throw err\n }\n}\n"]}
@@ -1,14 +1,87 @@
1
1
  import { InferInput, InferOutput, ValidationContext, ValidationOptions, ValidationResult, Validator } from './validator.js';
2
- type ParseOptions = Omit<ValidationOptions, 'mode'>;
3
- type ValidateOptions = Omit<ValidationOptions, 'mode'>;
2
+ /**
3
+ * Options for parsing operations.
4
+ * Excludes the `mode` option as it is implicitly set to `"parse"`.
5
+ */
6
+ export type ParseOptions = Omit<ValidationOptions, 'mode'>;
7
+ /**
8
+ * Options for validation operations.
9
+ * Excludes the `mode` option as it is implicitly set to `"validate"`.
10
+ */
11
+ export type ValidateOptions = Omit<ValidationOptions, 'mode'>;
12
+ /**
13
+ * Internal type structure for schema type inference.
14
+ *
15
+ * This interface defines the phantom types used for compile-time type inference
16
+ * without affecting runtime behavior. The `input` and `output` properties
17
+ * represent the expected input type during validation and the resulting output
18
+ * type after parsing, respectively.
19
+ *
20
+ * @typeParam TInput - The type accepted as input during validation
21
+ * @typeParam TOutput - The type returned after parsing (may differ from input due to coercion)
22
+ */
4
23
  export interface SchemaInternals<out TInput = unknown, out TOutput = TInput> {
5
- /** @internal The inferred validation type */
6
24
  input: TInput;
7
- /** @internal The inferred parse type */
8
25
  output: TOutput;
9
26
  }
27
+ /**
28
+ * Abstract base class for all schema validators in the lexicon system.
29
+ *
30
+ * This class provides the standard validation interface that all schema types
31
+ * implement. It offers multiple methods for validating and parsing data:
32
+ *
33
+ * - **Assertion methods**: `assert()`, `check()` - throw on invalid input
34
+ * - **Type guard methods**: `matches()`, `ifMatches()` - return boolean or optional value
35
+ * - **Parse methods**: `parse()`, `safeParse()` - allow value transformation/coercion
36
+ * - **Validate methods**: `validate()`, `safeValidate()` - strict validation without coercion
37
+ *
38
+ * All methods are also available with a `$` prefix (e.g., `$parse()`, `$validate()`)
39
+ * for consistent access in generated lexicon namespaces.
40
+ *
41
+ * @typeParam TInput - The type accepted as valid input during validation
42
+ * @typeParam TOutput - The type returned after parsing (may include transformations)
43
+ * @typeParam TInternals - Internal type structure for type inference
44
+ *
45
+ * @example
46
+ * ```typescript
47
+ * class MySchema extends Schema<string> {
48
+ * validateInContext(input: unknown, ctx: ValidationContext): ValidationResult {
49
+ * if (typeof input !== 'string') {
50
+ * return ctx.issueUnexpectedType(input, 'string')
51
+ * }
52
+ * return ctx.success(input)
53
+ * }
54
+ * }
55
+ *
56
+ * const schema = new MySchema()
57
+ * schema.assert('hello') // OK
58
+ * schema.assert(123) // Throws ValidationError
59
+ * schema.matches('hello') // true
60
+ * schema.matches(123) // false
61
+ * ```
62
+ */
10
63
  export declare abstract class Schema<out TInput = unknown, out TOutput = TInput, out TInternals extends SchemaInternals<TInput, TOutput> = SchemaInternals<TInput, TOutput>> implements Validator<TInternals['input'], TInternals['output']> {
64
+ /**
65
+ * Internal phantom property for type inference.
66
+ * This property does not exist at runtime.
67
+ *
68
+ * @internal
69
+ */
11
70
  readonly ['__lex']: TInternals;
71
+ abstract readonly type: string;
72
+ /**
73
+ * Performs validation of the input value within a validation context.
74
+ *
75
+ * This method must be implemented by subclasses to define the actual
76
+ * validation logic. It should not be called directly; use
77
+ * {@link ValidationContext.validate} instead to ensure proper mode enforcement.
78
+ *
79
+ * @param input - The value to validate
80
+ * @param ctx - The validation context providing path tracking and issue reporting
81
+ * @returns A validation result indicating success with the validated value or failure with issues
82
+ *
83
+ * @internal
84
+ */
12
85
  abstract validateInContext(input: unknown, ctx: ValidationContext): ValidationResult;
13
86
  /**
14
87
  * @note use {@link check}() instead of {@link assert}() if you encounter a
@@ -30,21 +103,175 @@ export declare abstract class Schema<out TInput = unknown, out TOutput = TInput,
30
103
  * `mode: "validate"`.
31
104
  */
32
105
  cast<I>(input: I): I & InferInput<this>;
106
+ /**
107
+ * Type guard that checks if the input matches this schema.
108
+ *
109
+ * @param input - The value to check
110
+ * @returns `true` if the input is valid according to this schema
111
+ *
112
+ * @example
113
+ * ```typescript
114
+ * if (schema.matches(data)) {
115
+ * // data is narrowed to the schema's input type
116
+ * console.log(data)
117
+ * }
118
+ * ```
119
+ */
33
120
  matches<I>(input: I): input is I & InferInput<this>;
121
+ /**
122
+ * Returns the input if it matches this schema, otherwise returns `undefined`.
123
+ *
124
+ * This is useful for optional filtering operations where you want to
125
+ * conditionally extract values that match a schema.
126
+ *
127
+ * @param input - The value to check
128
+ * @returns The input value with narrowed type if valid, otherwise `undefined`
129
+ *
130
+ * @example
131
+ * ```typescript
132
+ * const validData = schema.ifMatches(data)
133
+ * if (validData !== undefined) {
134
+ * // validData is the schema's input type
135
+ * console.log(validData)
136
+ * }
137
+ * ```
138
+ */
34
139
  ifMatches<I>(input: I): (I & InferInput<this>) | undefined;
140
+ /**
141
+ * Parses the input, allowing value transformations and coercion.
142
+ *
143
+ * Unlike {@link validate}, this method allows the schema to transform
144
+ * the input value (e.g., applying default values, type coercion).
145
+ * Throws a {@link ValidationError} if the input is invalid.
146
+ *
147
+ * @param input - The value to parse
148
+ * @param options - Optional parsing configuration
149
+ * @returns The parsed and potentially transformed value
150
+ * @throws {ValidationError} If the input fails validation
151
+ *
152
+ * @example
153
+ * ```typescript
154
+ * const result = schema.parse(rawData)
155
+ * // result has defaults applied and is fully typed
156
+ * ```
157
+ */
35
158
  parse(input: unknown, options?: ParseOptions): InferOutput<this>;
159
+ /**
160
+ * Safely parses the input without throwing, returning a result object.
161
+ *
162
+ * This method allows value transformations like {@link parse}, but
163
+ * returns a discriminated union result instead of throwing on error.
164
+ *
165
+ * @param input - The value to parse
166
+ * @param options - Optional parsing configuration
167
+ * @returns A {@link ValidationResult} with either the parsed value or validation errors
168
+ *
169
+ * @example
170
+ * ```typescript
171
+ * const result = schema.safeParse(data)
172
+ * if (result.success) {
173
+ * console.log(result.value)
174
+ * } else {
175
+ * console.error(result.reason.issues)
176
+ * }
177
+ * ```
178
+ */
36
179
  safeParse(input: unknown, options?: ParseOptions): ValidationResult<InferOutput<this>>;
180
+ /**
181
+ * Validates the input strictly without allowing transformations.
182
+ *
183
+ * Unlike {@link parse}, this method requires the input to exactly match
184
+ * the schema without any transformations (no defaults applied, no coercion).
185
+ * Throws a {@link ValidationError} if the input is invalid or would require transformation.
186
+ *
187
+ * @typeParam I - The input type (preserved in the return type)
188
+ * @param input - The value to validate
189
+ * @param options - Optional validation configuration
190
+ * @returns The validated input with narrowed type
191
+ * @throws {ValidationError} If the input fails validation or requires transformation
192
+ *
193
+ * @example
194
+ * ```typescript
195
+ * const validated = schema.validate(data)
196
+ * // validated is typed as the intersection of input type and schema type
197
+ * ```
198
+ */
37
199
  validate<I>(input: I, options?: ValidateOptions): I & InferInput<this>;
200
+ /**
201
+ * Safely validates the input without throwing, returning a result object.
202
+ *
203
+ * This method performs strict validation like {@link validate}, but
204
+ * returns a discriminated union result instead of throwing on error.
205
+ *
206
+ * @typeParam I - The input type (preserved in the result value type)
207
+ * @param input - The value to validate
208
+ * @param options - Optional validation configuration
209
+ * @returns A {@link ValidationResult} with either the validated value or validation errors
210
+ *
211
+ * @example
212
+ * ```typescript
213
+ * const result = schema.safeValidate(data)
214
+ * if (result.success) {
215
+ * console.log(result.value)
216
+ * } else {
217
+ * console.error(result.reason.issues)
218
+ * }
219
+ * ```
220
+ */
38
221
  safeValidate<I>(input: I, options?: ValidateOptions): ValidationResult<I & InferInput<this>>;
222
+ /**
223
+ * Alias for {@link assert} with `$` prefix for namespace compatibility.
224
+ *
225
+ * @see {@link assert}
226
+ */
39
227
  $assert(input: unknown): asserts input is InferInput<this>;
228
+ /**
229
+ * Alias for {@link check} with `$` prefix for namespace compatibility.
230
+ *
231
+ * @see {@link check}
232
+ */
40
233
  $check(input: unknown): void;
234
+ /**
235
+ * Alias for {@link cast} with `$` prefix for namespace compatibility.
236
+ *
237
+ * @see {@link cast}
238
+ */
41
239
  $cast<I>(input: I): I & InferInput<this>;
240
+ /**
241
+ * Alias for {@link matches} with `$` prefix for namespace compatibility.
242
+ *
243
+ * @see {@link matches}
244
+ */
42
245
  $matches(input: unknown): input is InferInput<this>;
246
+ /**
247
+ * Alias for {@link ifMatches} with `$` prefix for namespace compatibility.
248
+ *
249
+ * @see {@link ifMatches}
250
+ */
43
251
  $ifMatches<I>(input: I): (I & InferInput<this>) | undefined;
252
+ /**
253
+ * Alias for {@link parse} with `$` prefix for namespace compatibility.
254
+ *
255
+ * @see {@link parse}
256
+ */
44
257
  $parse(input: unknown, options?: ValidateOptions): InferOutput<this>;
258
+ /**
259
+ * Alias for {@link safeParse} with `$` prefix for namespace compatibility.
260
+ *
261
+ * @see {@link safeParse}
262
+ */
45
263
  $safeParse(input: unknown, options?: ValidateOptions): ValidationResult<InferOutput<this>>;
264
+ /**
265
+ * Alias for {@link validate} with `$` prefix for namespace compatibility.
266
+ *
267
+ * @see {@link validate}
268
+ */
46
269
  $validate<I>(input: I, options?: ValidateOptions): I & InferInput<this>;
270
+ /**
271
+ * Alias for {@link safeValidate} with `$` prefix for namespace compatibility.
272
+ *
273
+ * @see {@link safeValidate}
274
+ */
47
275
  $safeValidate<I>(input: I, options?: ValidateOptions): ValidationResult<I & InferInput<this>>;
48
276
  }
49
- export {};
50
277
  //# sourceMappingURL=schema.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../../src/core/schema.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,UAAU,EACV,WAAW,EACX,iBAAiB,EACjB,iBAAiB,EACjB,gBAAgB,EAChB,SAAS,EACV,MAAM,gBAAgB,CAAA;AAEvB,KAAK,YAAY,GAAG,IAAI,CAAC,iBAAiB,EAAE,MAAM,CAAC,CAAA;AACnD,KAAK,eAAe,GAAG,IAAI,CAAC,iBAAiB,EAAE,MAAM,CAAC,CAAA;AAEtD,MAAM,WAAW,eAAe,CAAC,GAAG,CAAC,MAAM,GAAG,OAAO,EAAE,GAAG,CAAC,OAAO,GAAG,MAAM;IACzE,6CAA6C;IA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AAU,CAAC,IAAI,CAAC;IAIxC;;;;OAIG;IACH,QAAQ,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,UAAU,CAAC,IAAI,CAAC;IAInD;;;;OAIG;IACH,UAAU,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,CAAC,CAAC,GAAG,UAAU,CAAC,IAAI,CAAC,CAAC,GAAG,SAAS;IAI3D;;;;OAIG;IACH,MAAM,CAAC,KAAK,EAAE,OAAO,EAAE,OAAO,CAAC,EAAE,eAAe,GAAG,WAAW,CAAC,IAAI,CAAC;IAIpE;;;;OAIG;IACH,UAAU,CACR,KAAK,EAAE,OAAO,EACd,OAAO,CAAC,EAAE,eAAe,GACxB,gBAAgB,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC;IAItC;;;;OAIG;IACH,SAAS,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,OAAO,CAAC,EAAE,eAAe,GAAG,CAAC,GAAG,UAAU,CAAC,IAAI,CAAC;IAIvE;;;;OAIG;IACH,aAAa,CAAC,CAAC,EACb,KAAK,EAAE,CAAC,EACR,OAAO,CAAC,EAAE,eAAe,GACxB,gBAAgB,CAAC,CAAC,GAAG,UAAU,CAAC,IAAI,CAAC,CAAC;CAG1C"}
@@ -2,6 +2,42 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Schema = void 0;
4
4
  const validator_js_1 = require("./validator.js");
5
+ /**
6
+ * Abstract base class for all schema validators in the lexicon system.
7
+ *
8
+ * This class provides the standard validation interface that all schema types
9
+ * implement. It offers multiple methods for validating and parsing data:
10
+ *
11
+ * - **Assertion methods**: `assert()`, `check()` - throw on invalid input
12
+ * - **Type guard methods**: `matches()`, `ifMatches()` - return boolean or optional value
13
+ * - **Parse methods**: `parse()`, `safeParse()` - allow value transformation/coercion
14
+ * - **Validate methods**: `validate()`, `safeValidate()` - strict validation without coercion
15
+ *
16
+ * All methods are also available with a `$` prefix (e.g., `$parse()`, `$validate()`)
17
+ * for consistent access in generated lexicon namespaces.
18
+ *
19
+ * @typeParam TInput - The type accepted as valid input during validation
20
+ * @typeParam TOutput - The type returned after parsing (may include transformations)
21
+ * @typeParam TInternals - Internal type structure for type inference
22
+ *
23
+ * @example
24
+ * ```typescript
25
+ * class MySchema extends Schema<string> {
26
+ * validateInContext(input: unknown, ctx: ValidationContext): ValidationResult {
27
+ * if (typeof input !== 'string') {
28
+ * return ctx.issueUnexpectedType(input, 'string')
29
+ * }
30
+ * return ctx.success(input)
31
+ * }
32
+ * }
33
+ *
34
+ * const schema = new MySchema()
35
+ * schema.assert('hello') // OK
36
+ * schema.assert(123) // Throws ValidationError
37
+ * schema.matches('hello') // true
38
+ * schema.matches(123) // false
39
+ * ```
40
+ */
5
41
  class Schema {
6
42
  /**
7
43
  * @note use {@link check}() instead of {@link assert}() if you encounter a
@@ -34,41 +70,153 @@ class Schema {
34
70
  return result.value;
35
71
  throw result.reason;
36
72
  }
73
+ /**
74
+ * Type guard that checks if the input matches this schema.
75
+ *
76
+ * @param input - The value to check
77
+ * @returns `true` if the input is valid according to this schema
78
+ *
79
+ * @example
80
+ * ```typescript
81
+ * if (schema.matches(data)) {
82
+ * // data is narrowed to the schema's input type
83
+ * console.log(data)
84
+ * }
85
+ * ```
86
+ */
37
87
  matches(input) {
38
88
  const result = validator_js_1.ValidationContext.validate(input, this);
39
89
  return result.success;
40
90
  }
91
+ /**
92
+ * Returns the input if it matches this schema, otherwise returns `undefined`.
93
+ *
94
+ * This is useful for optional filtering operations where you want to
95
+ * conditionally extract values that match a schema.
96
+ *
97
+ * @param input - The value to check
98
+ * @returns The input value with narrowed type if valid, otherwise `undefined`
99
+ *
100
+ * @example
101
+ * ```typescript
102
+ * const validData = schema.ifMatches(data)
103
+ * if (validData !== undefined) {
104
+ * // validData is the schema's input type
105
+ * console.log(validData)
106
+ * }
107
+ * ```
108
+ */
41
109
  ifMatches(input) {
42
110
  return this.matches(input) ? input : undefined;
43
111
  }
112
+ /**
113
+ * Parses the input, allowing value transformations and coercion.
114
+ *
115
+ * Unlike {@link validate}, this method allows the schema to transform
116
+ * the input value (e.g., applying default values, type coercion).
117
+ * Throws a {@link ValidationError} if the input is invalid.
118
+ *
119
+ * @param input - The value to parse
120
+ * @param options - Optional parsing configuration
121
+ * @returns The parsed and potentially transformed value
122
+ * @throws {ValidationError} If the input fails validation
123
+ *
124
+ * @example
125
+ * ```typescript
126
+ * const result = schema.parse(rawData)
127
+ * // result has defaults applied and is fully typed
128
+ * ```
129
+ */
44
130
  parse(input, options) {
45
131
  const result = this.safeParse(input, options);
46
132
  if (result.success)
47
133
  return result.value;
48
134
  throw result.reason;
49
135
  }
136
+ /**
137
+ * Safely parses the input without throwing, returning a result object.
138
+ *
139
+ * This method allows value transformations like {@link parse}, but
140
+ * returns a discriminated union result instead of throwing on error.
141
+ *
142
+ * @param input - The value to parse
143
+ * @param options - Optional parsing configuration
144
+ * @returns A {@link ValidationResult} with either the parsed value or validation errors
145
+ *
146
+ * @example
147
+ * ```typescript
148
+ * const result = schema.safeParse(data)
149
+ * if (result.success) {
150
+ * console.log(result.value)
151
+ * } else {
152
+ * console.error(result.reason.issues)
153
+ * }
154
+ * ```
155
+ */
50
156
  safeParse(input, options) {
51
157
  return validator_js_1.ValidationContext.validate(input, this, {
52
158
  ...options,
53
159
  mode: 'parse',
54
160
  });
55
161
  }
162
+ /**
163
+ * Validates the input strictly without allowing transformations.
164
+ *
165
+ * Unlike {@link parse}, this method requires the input to exactly match
166
+ * the schema without any transformations (no defaults applied, no coercion).
167
+ * Throws a {@link ValidationError} if the input is invalid or would require transformation.
168
+ *
169
+ * @typeParam I - The input type (preserved in the return type)
170
+ * @param input - The value to validate
171
+ * @param options - Optional validation configuration
172
+ * @returns The validated input with narrowed type
173
+ * @throws {ValidationError} If the input fails validation or requires transformation
174
+ *
175
+ * @example
176
+ * ```typescript
177
+ * const validated = schema.validate(data)
178
+ * // validated is typed as the intersection of input type and schema type
179
+ * ```
180
+ */
56
181
  validate(input, options) {
57
182
  const result = this.safeValidate(input, options);
58
183
  if (result.success)
59
184
  return result.value;
60
185
  throw result.reason;
61
186
  }
187
+ /**
188
+ * Safely validates the input without throwing, returning a result object.
189
+ *
190
+ * This method performs strict validation like {@link validate}, but
191
+ * returns a discriminated union result instead of throwing on error.
192
+ *
193
+ * @typeParam I - The input type (preserved in the result value type)
194
+ * @param input - The value to validate
195
+ * @param options - Optional validation configuration
196
+ * @returns A {@link ValidationResult} with either the validated value or validation errors
197
+ *
198
+ * @example
199
+ * ```typescript
200
+ * const result = schema.safeValidate(data)
201
+ * if (result.success) {
202
+ * console.log(result.value)
203
+ * } else {
204
+ * console.error(result.reason.issues)
205
+ * }
206
+ * ```
207
+ */
62
208
  safeValidate(input, options) {
63
209
  return validator_js_1.ValidationContext.validate(input, this, {
64
210
  ...options,
65
211
  mode: 'validate',
66
212
  });
67
213
  }
68
- // @NOTE The built lexicons namespaces will export utility functions that
69
- // allow accessing the schema's methods without the need to specify ".main."
70
- // as part of the namespace. This way, a utility for a particular record type
71
- // can be called like "app.bsky.feed.post.<utility>()" instead of
214
+ // @NOTE Dollar-prefixed aliases
215
+ //
216
+ // The built lexicons namespaces export utility functions that allow accessing
217
+ // the schema's methods without the need to specify ".main." as part of the
218
+ // namespace. This way, a utility for a particular record type can be called
219
+ // like "app.bsky.feed.post.<utility>()" instead of
72
220
  // "app.bsky.feed.post.main.<utility>()". Because those utilities could
73
221
  // conflict with other schemas (e.g. if there is a lexicon definition at
74
222
  // "#<utility>"), those exported utilities will be prefixed with "$". In order
@@ -78,30 +226,75 @@ class Schema {
78
226
  //
79
227
  // - "app.bsky.feed.post.$parse(...)" // calls a utility function created by "lex build"
80
228
  // - "app.bsky.feed.defs.postView.$parse(...)" // uses the alias defined below on the schema instance
229
+ /**
230
+ * Alias for {@link assert} with `$` prefix for namespace compatibility.
231
+ *
232
+ * @see {@link assert}
233
+ */
81
234
  $assert(input) {
82
235
  return this.assert(input);
83
236
  }
237
+ /**
238
+ * Alias for {@link check} with `$` prefix for namespace compatibility.
239
+ *
240
+ * @see {@link check}
241
+ */
84
242
  $check(input) {
85
243
  return this.check(input);
86
244
  }
245
+ /**
246
+ * Alias for {@link cast} with `$` prefix for namespace compatibility.
247
+ *
248
+ * @see {@link cast}
249
+ */
87
250
  $cast(input) {
88
251
  return this.cast(input);
89
252
  }
253
+ /**
254
+ * Alias for {@link matches} with `$` prefix for namespace compatibility.
255
+ *
256
+ * @see {@link matches}
257
+ */
90
258
  $matches(input) {
91
259
  return this.matches(input);
92
260
  }
261
+ /**
262
+ * Alias for {@link ifMatches} with `$` prefix for namespace compatibility.
263
+ *
264
+ * @see {@link ifMatches}
265
+ */
93
266
  $ifMatches(input) {
94
267
  return this.ifMatches(input);
95
268
  }
269
+ /**
270
+ * Alias for {@link parse} with `$` prefix for namespace compatibility.
271
+ *
272
+ * @see {@link parse}
273
+ */
96
274
  $parse(input, options) {
97
275
  return this.parse(input, options);
98
276
  }
277
+ /**
278
+ * Alias for {@link safeParse} with `$` prefix for namespace compatibility.
279
+ *
280
+ * @see {@link safeParse}
281
+ */
99
282
  $safeParse(input, options) {
100
283
  return this.safeParse(input, options);
101
284
  }
285
+ /**
286
+ * Alias for {@link validate} with `$` prefix for namespace compatibility.
287
+ *
288
+ * @see {@link validate}
289
+ */
102
290
  $validate(input, options) {
103
291
  return this.validate(input, options);
104
292
  }
293
+ /**
294
+ * Alias for {@link safeValidate} with `$` prefix for namespace compatibility.
295
+ *
296
+ * @see {@link safeValidate}
297
+ */
105
298
  $safeValidate(input, options) {
106
299
  return this.safeValidate(input, options);
107
300
  }