@atproto/lex-schema 0.0.11 → 0.0.12

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 (238) hide show
  1. package/CHANGELOG.md +19 -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 +229 -2
  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 +51 -0
  32. package/dist/core/validation-issue.js.map +1 -1
  33. package/dist/core/validator.d.ts +347 -10
  34. package/dist/core/validator.d.ts.map +1 -1
  35. package/dist/core/validator.js +184 -3
  36. package/dist/core/validator.js.map +1 -1
  37. package/dist/helpers.d.ts +9 -24
  38. package/dist/helpers.d.ts.map +1 -1
  39. package/dist/helpers.js.map +1 -1
  40. package/dist/schema/array.d.ts +45 -0
  41. package/dist/schema/array.d.ts.map +1 -1
  42. package/dist/schema/array.js +14 -0
  43. package/dist/schema/array.js.map +1 -1
  44. package/dist/schema/blob.d.ts +46 -0
  45. package/dist/schema/blob.d.ts.map +1 -1
  46. package/dist/schema/blob.js +39 -0
  47. package/dist/schema/blob.js.map +1 -1
  48. package/dist/schema/boolean.d.ts +28 -0
  49. package/dist/schema/boolean.d.ts.map +1 -1
  50. package/dist/schema/boolean.js +28 -0
  51. package/dist/schema/boolean.js.map +1 -1
  52. package/dist/schema/bytes.d.ts +38 -0
  53. package/dist/schema/bytes.d.ts.map +1 -1
  54. package/dist/schema/bytes.js +32 -0
  55. package/dist/schema/bytes.js.map +1 -1
  56. package/dist/schema/cid.d.ts +38 -0
  57. package/dist/schema/cid.d.ts.map +1 -1
  58. package/dist/schema/cid.js +33 -0
  59. package/dist/schema/cid.js.map +1 -1
  60. package/dist/schema/custom.d.ts +66 -1
  61. package/dist/schema/custom.d.ts.map +1 -1
  62. package/dist/schema/custom.js +54 -0
  63. package/dist/schema/custom.js.map +1 -1
  64. package/dist/schema/dict.d.ts +44 -0
  65. package/dist/schema/dict.d.ts.map +1 -1
  66. package/dist/schema/dict.js +44 -0
  67. package/dist/schema/dict.js.map +1 -1
  68. package/dist/schema/discriminated-union.d.ts +58 -0
  69. package/dist/schema/discriminated-union.d.ts.map +1 -1
  70. package/dist/schema/discriminated-union.js +45 -0
  71. package/dist/schema/discriminated-union.js.map +1 -1
  72. package/dist/schema/enum.d.ts +48 -0
  73. package/dist/schema/enum.d.ts.map +1 -1
  74. package/dist/schema/enum.js +48 -0
  75. package/dist/schema/enum.js.map +1 -1
  76. package/dist/schema/integer.d.ts +42 -0
  77. package/dist/schema/integer.d.ts.map +1 -1
  78. package/dist/schema/integer.js +36 -0
  79. package/dist/schema/integer.js.map +1 -1
  80. package/dist/schema/intersection.d.ts +54 -0
  81. package/dist/schema/intersection.d.ts.map +1 -1
  82. package/dist/schema/intersection.js +49 -0
  83. package/dist/schema/intersection.js.map +1 -1
  84. package/dist/schema/literal.d.ts +44 -0
  85. package/dist/schema/literal.d.ts.map +1 -1
  86. package/dist/schema/literal.js +44 -0
  87. package/dist/schema/literal.js.map +1 -1
  88. package/dist/schema/never.d.ts +42 -0
  89. package/dist/schema/never.d.ts.map +1 -1
  90. package/dist/schema/never.js +42 -0
  91. package/dist/schema/never.js.map +1 -1
  92. package/dist/schema/null.d.ts +29 -0
  93. package/dist/schema/null.d.ts.map +1 -1
  94. package/dist/schema/null.js +29 -0
  95. package/dist/schema/null.js.map +1 -1
  96. package/dist/schema/nullable.d.ts +41 -0
  97. package/dist/schema/nullable.d.ts.map +1 -1
  98. package/dist/schema/nullable.js +41 -0
  99. package/dist/schema/nullable.js.map +1 -1
  100. package/dist/schema/object.d.ts +56 -0
  101. package/dist/schema/object.d.ts.map +1 -1
  102. package/dist/schema/object.js +51 -0
  103. package/dist/schema/object.js.map +1 -1
  104. package/dist/schema/optional.d.ts +42 -0
  105. package/dist/schema/optional.d.ts.map +1 -1
  106. package/dist/schema/optional.js +42 -0
  107. package/dist/schema/optional.js.map +1 -1
  108. package/dist/schema/params.d.ts +90 -10
  109. package/dist/schema/params.d.ts.map +1 -1
  110. package/dist/schema/params.js +84 -10
  111. package/dist/schema/params.js.map +1 -1
  112. package/dist/schema/payload.d.ts +111 -15
  113. package/dist/schema/payload.d.ts.map +1 -1
  114. package/dist/schema/payload.js +70 -0
  115. package/dist/schema/payload.js.map +1 -1
  116. package/dist/schema/permission-set.d.ts +58 -0
  117. package/dist/schema/permission-set.d.ts.map +1 -1
  118. package/dist/schema/permission-set.js +50 -0
  119. package/dist/schema/permission-set.js.map +1 -1
  120. package/dist/schema/permission.d.ts +42 -0
  121. package/dist/schema/permission.d.ts.map +1 -1
  122. package/dist/schema/permission.js +39 -0
  123. package/dist/schema/permission.js.map +1 -1
  124. package/dist/schema/procedure.d.ts +64 -0
  125. package/dist/schema/procedure.d.ts.map +1 -1
  126. package/dist/schema/procedure.js +64 -0
  127. package/dist/schema/procedure.js.map +1 -1
  128. package/dist/schema/query.d.ts +55 -0
  129. package/dist/schema/query.d.ts.map +1 -1
  130. package/dist/schema/query.js +55 -0
  131. package/dist/schema/query.js.map +1 -1
  132. package/dist/schema/record.d.ts +63 -8
  133. package/dist/schema/record.d.ts.map +1 -1
  134. package/dist/schema/record.js +20 -0
  135. package/dist/schema/record.js.map +1 -1
  136. package/dist/schema/ref.d.ts +50 -0
  137. package/dist/schema/ref.d.ts.map +1 -1
  138. package/dist/schema/ref.js +17 -0
  139. package/dist/schema/ref.js.map +1 -1
  140. package/dist/schema/refine.d.ts +58 -9
  141. package/dist/schema/refine.d.ts.map +1 -1
  142. package/dist/schema/refine.js.map +1 -1
  143. package/dist/schema/regexp.d.ts +44 -0
  144. package/dist/schema/regexp.d.ts.map +1 -1
  145. package/dist/schema/regexp.js +44 -0
  146. package/dist/schema/regexp.js.map +1 -1
  147. package/dist/schema/string.d.ts +50 -0
  148. package/dist/schema/string.d.ts.map +1 -1
  149. package/dist/schema/string.js +41 -0
  150. package/dist/schema/string.js.map +1 -1
  151. package/dist/schema/subscription.d.ts +72 -2
  152. package/dist/schema/subscription.d.ts.map +1 -1
  153. package/dist/schema/subscription.js +59 -0
  154. package/dist/schema/subscription.js.map +1 -1
  155. package/dist/schema/token.d.ts +47 -0
  156. package/dist/schema/token.d.ts.map +1 -1
  157. package/dist/schema/token.js +47 -0
  158. package/dist/schema/token.js.map +1 -1
  159. package/dist/schema/typed-object.d.ts +62 -8
  160. package/dist/schema/typed-object.d.ts.map +1 -1
  161. package/dist/schema/typed-object.js +18 -0
  162. package/dist/schema/typed-object.js.map +1 -1
  163. package/dist/schema/typed-ref.d.ts +53 -0
  164. package/dist/schema/typed-ref.d.ts.map +1 -1
  165. package/dist/schema/typed-ref.js +15 -0
  166. package/dist/schema/typed-ref.js.map +1 -1
  167. package/dist/schema/typed-union.d.ts +50 -1
  168. package/dist/schema/typed-union.d.ts.map +1 -1
  169. package/dist/schema/typed-union.js +50 -1
  170. package/dist/schema/typed-union.js.map +1 -1
  171. package/dist/schema/union.d.ts +45 -0
  172. package/dist/schema/union.d.ts.map +1 -1
  173. package/dist/schema/union.js +40 -0
  174. package/dist/schema/union.js.map +1 -1
  175. package/dist/schema/unknown-object.d.ts +34 -0
  176. package/dist/schema/unknown-object.d.ts.map +1 -1
  177. package/dist/schema/unknown-object.js +31 -0
  178. package/dist/schema/unknown-object.js.map +1 -1
  179. package/dist/schema/unknown.d.ts +33 -0
  180. package/dist/schema/unknown.d.ts.map +1 -1
  181. package/dist/schema/unknown.js +33 -0
  182. package/dist/schema/unknown.js.map +1 -1
  183. package/dist/schema/with-default.d.ts +44 -0
  184. package/dist/schema/with-default.d.ts.map +1 -1
  185. package/dist/schema/with-default.js +44 -0
  186. package/dist/schema/with-default.js.map +1 -1
  187. package/package.json +3 -3
  188. package/src/core/$type.ts +150 -18
  189. package/src/core/record-key.ts +44 -0
  190. package/src/core/result.ts +86 -4
  191. package/src/core/schema.ts +236 -7
  192. package/src/core/string-format.ts +259 -13
  193. package/src/core/types.ts +91 -3
  194. package/src/core/validation-error.ts +60 -0
  195. package/src/core/validation-issue.ts +65 -0
  196. package/src/core/validator.ts +351 -10
  197. package/src/helpers.test.ts +110 -29
  198. package/src/helpers.ts +9 -14
  199. package/src/schema/array.test.ts +94 -79
  200. package/src/schema/array.ts +45 -0
  201. package/src/schema/blob.ts +46 -0
  202. package/src/schema/boolean.ts +28 -0
  203. package/src/schema/bytes.ts +38 -0
  204. package/src/schema/cid.ts +38 -0
  205. package/src/schema/custom.ts +66 -1
  206. package/src/schema/dict.ts +44 -0
  207. package/src/schema/discriminated-union.ts +58 -0
  208. package/src/schema/enum.ts +48 -0
  209. package/src/schema/integer.ts +42 -0
  210. package/src/schema/intersection.ts +54 -0
  211. package/src/schema/literal.ts +44 -0
  212. package/src/schema/never.ts +42 -0
  213. package/src/schema/null.ts +29 -0
  214. package/src/schema/nullable.ts +41 -0
  215. package/src/schema/object.ts +56 -0
  216. package/src/schema/optional.ts +42 -0
  217. package/src/schema/params.test.ts +58 -2
  218. package/src/schema/params.ts +125 -19
  219. package/src/schema/payload.test.ts +3 -3
  220. package/src/schema/payload.ts +142 -38
  221. package/src/schema/permission-set.ts +58 -0
  222. package/src/schema/permission.ts +42 -0
  223. package/src/schema/procedure.ts +64 -0
  224. package/src/schema/query.ts +55 -0
  225. package/src/schema/record.ts +63 -8
  226. package/src/schema/ref.ts +50 -0
  227. package/src/schema/refine.ts +58 -9
  228. package/src/schema/regexp.ts +44 -0
  229. package/src/schema/string.ts +50 -0
  230. package/src/schema/subscription.ts +72 -2
  231. package/src/schema/token.ts +47 -0
  232. package/src/schema/typed-object.ts +62 -8
  233. package/src/schema/typed-ref.ts +53 -0
  234. package/src/schema/typed-union.ts +55 -2
  235. package/src/schema/union.ts +45 -0
  236. package/src/schema/unknown-object.ts +34 -0
  237. package/src/schema/unknown.ts +33 -0
  238. package/src/schema/with-default.ts +44 -0
@@ -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,86 @@
1
1
  import { InferInput, InferOutput, ValidationContext, ValidationOptions, ValidationResult, Validator } from './validator.js';
2
+ /**
3
+ * Options for parsing operations.
4
+ * Excludes the `mode` option as it is implicitly set to `"parse"`.
5
+ */
2
6
  type ParseOptions = Omit<ValidationOptions, 'mode'>;
7
+ /**
8
+ * Options for validation operations.
9
+ * Excludes the `mode` option as it is implicitly set to `"validate"`.
10
+ */
3
11
  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.issueInvalidType(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
+ /**
72
+ * Performs validation of the input value within a validation context.
73
+ *
74
+ * This method must be implemented by subclasses to define the actual
75
+ * validation logic. It should not be called directly; use
76
+ * {@link ValidationContext.validate} instead to ensure proper mode enforcement.
77
+ *
78
+ * @param input - The value to validate
79
+ * @param ctx - The validation context providing path tracking and issue reporting
80
+ * @returns A validation result indicating success with the validated value or failure with issues
81
+ *
82
+ * @internal
83
+ */
12
84
  abstract validateInContext(input: unknown, ctx: ValidationContext): ValidationResult;
13
85
  /**
14
86
  * @note use {@link check}() instead of {@link assert}() if you encounter a
@@ -30,20 +102,175 @@ export declare abstract class Schema<out TInput = unknown, out TOutput = TInput,
30
102
  * `mode: "validate"`.
31
103
  */
32
104
  cast<I>(input: I): I & InferInput<this>;
105
+ /**
106
+ * Type guard that checks if the input matches this schema.
107
+ *
108
+ * @param input - The value to check
109
+ * @returns `true` if the input is valid according to this schema
110
+ *
111
+ * @example
112
+ * ```typescript
113
+ * if (schema.matches(data)) {
114
+ * // data is narrowed to the schema's input type
115
+ * console.log(data)
116
+ * }
117
+ * ```
118
+ */
33
119
  matches<I>(input: I): input is I & InferInput<this>;
120
+ /**
121
+ * Returns the input if it matches this schema, otherwise returns `undefined`.
122
+ *
123
+ * This is useful for optional filtering operations where you want to
124
+ * conditionally extract values that match a schema.
125
+ *
126
+ * @param input - The value to check
127
+ * @returns The input value with narrowed type if valid, otherwise `undefined`
128
+ *
129
+ * @example
130
+ * ```typescript
131
+ * const validData = schema.ifMatches(data)
132
+ * if (validData !== undefined) {
133
+ * // validData is the schema's input type
134
+ * console.log(validData)
135
+ * }
136
+ * ```
137
+ */
34
138
  ifMatches<I>(input: I): (I & InferInput<this>) | undefined;
139
+ /**
140
+ * Parses the input, allowing value transformations and coercion.
141
+ *
142
+ * Unlike {@link validate}, this method allows the schema to transform
143
+ * the input value (e.g., applying default values, type coercion).
144
+ * Throws a {@link ValidationError} if the input is invalid.
145
+ *
146
+ * @param input - The value to parse
147
+ * @param options - Optional parsing configuration
148
+ * @returns The parsed and potentially transformed value
149
+ * @throws {ValidationError} If the input fails validation
150
+ *
151
+ * @example
152
+ * ```typescript
153
+ * const result = schema.parse(rawData)
154
+ * // result has defaults applied and is fully typed
155
+ * ```
156
+ */
35
157
  parse(input: unknown, options?: ParseOptions): InferOutput<this>;
158
+ /**
159
+ * Safely parses the input without throwing, returning a result object.
160
+ *
161
+ * This method allows value transformations like {@link parse}, but
162
+ * returns a discriminated union result instead of throwing on error.
163
+ *
164
+ * @param input - The value to parse
165
+ * @param options - Optional parsing configuration
166
+ * @returns A {@link ValidationResult} with either the parsed value or validation errors
167
+ *
168
+ * @example
169
+ * ```typescript
170
+ * const result = schema.safeParse(data)
171
+ * if (result.success) {
172
+ * console.log(result.value)
173
+ * } else {
174
+ * console.error(result.reason.issues)
175
+ * }
176
+ * ```
177
+ */
36
178
  safeParse(input: unknown, options?: ParseOptions): ValidationResult<InferOutput<this>>;
179
+ /**
180
+ * Validates the input strictly without allowing transformations.
181
+ *
182
+ * Unlike {@link parse}, this method requires the input to exactly match
183
+ * the schema without any transformations (no defaults applied, no coercion).
184
+ * Throws a {@link ValidationError} if the input is invalid or would require transformation.
185
+ *
186
+ * @typeParam I - The input type (preserved in the return type)
187
+ * @param input - The value to validate
188
+ * @param options - Optional validation configuration
189
+ * @returns The validated input with narrowed type
190
+ * @throws {ValidationError} If the input fails validation or requires transformation
191
+ *
192
+ * @example
193
+ * ```typescript
194
+ * const validated = schema.validate(data)
195
+ * // validated is typed as the intersection of input type and schema type
196
+ * ```
197
+ */
37
198
  validate<I>(input: I, options?: ValidateOptions): I & InferInput<this>;
199
+ /**
200
+ * Safely validates the input without throwing, returning a result object.
201
+ *
202
+ * This method performs strict validation like {@link validate}, but
203
+ * returns a discriminated union result instead of throwing on error.
204
+ *
205
+ * @typeParam I - The input type (preserved in the result value type)
206
+ * @param input - The value to validate
207
+ * @param options - Optional validation configuration
208
+ * @returns A {@link ValidationResult} with either the validated value or validation errors
209
+ *
210
+ * @example
211
+ * ```typescript
212
+ * const result = schema.safeValidate(data)
213
+ * if (result.success) {
214
+ * console.log(result.value)
215
+ * } else {
216
+ * console.error(result.reason.issues)
217
+ * }
218
+ * ```
219
+ */
38
220
  safeValidate<I>(input: I, options?: ValidateOptions): ValidationResult<I & InferInput<this>>;
221
+ /**
222
+ * Alias for {@link assert} with `$` prefix for namespace compatibility.
223
+ *
224
+ * @see {@link assert}
225
+ */
39
226
  $assert(input: unknown): asserts input is InferInput<this>;
227
+ /**
228
+ * Alias for {@link check} with `$` prefix for namespace compatibility.
229
+ *
230
+ * @see {@link check}
231
+ */
40
232
  $check(input: unknown): void;
233
+ /**
234
+ * Alias for {@link cast} with `$` prefix for namespace compatibility.
235
+ *
236
+ * @see {@link cast}
237
+ */
41
238
  $cast<I>(input: I): I & InferInput<this>;
239
+ /**
240
+ * Alias for {@link matches} with `$` prefix for namespace compatibility.
241
+ *
242
+ * @see {@link matches}
243
+ */
42
244
  $matches(input: unknown): input is InferInput<this>;
245
+ /**
246
+ * Alias for {@link ifMatches} with `$` prefix for namespace compatibility.
247
+ *
248
+ * @see {@link ifMatches}
249
+ */
43
250
  $ifMatches<I>(input: I): (I & InferInput<this>) | undefined;
251
+ /**
252
+ * Alias for {@link parse} with `$` prefix for namespace compatibility.
253
+ *
254
+ * @see {@link parse}
255
+ */
44
256
  $parse(input: unknown, options?: ValidateOptions): InferOutput<this>;
257
+ /**
258
+ * Alias for {@link safeParse} with `$` prefix for namespace compatibility.
259
+ *
260
+ * @see {@link safeParse}
261
+ */
45
262
  $safeParse(input: unknown, options?: ValidateOptions): ValidationResult<InferOutput<this>>;
263
+ /**
264
+ * Alias for {@link validate} with `$` prefix for namespace compatibility.
265
+ *
266
+ * @see {@link validate}
267
+ */
46
268
  $validate<I>(input: I, options?: ValidateOptions): I & InferInput<this>;
269
+ /**
270
+ * Alias for {@link safeValidate} with `$` prefix for namespace compatibility.
271
+ *
272
+ * @see {@link safeValidate}
273
+ */
47
274
  $safeValidate<I>(input: I, options?: ValidateOptions): ValidationResult<I & InferInput<this>>;
48
275
  }
49
276
  export {};
@@ -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;IAC7C,KAAK,EAAE,MAAM,CAAA;IAEb,wCAAwC;IACxC,MAAM,EAAE,OAAO,CAAA;CAChB;AAED,8BAAsB,MAAM,CAC1B,GAAG,CAAC,MAAM,GAAG,OAAO,EACpB,GAAG,CAAC,OAAO,GA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C,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.issueInvalidType(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
  }
@@ -1 +1 @@
1
- {"version":3,"file":"schema.js","sourceRoot":"","sources":["../../src/core/schema.ts"],"names":[],"mappings":";;;AAAA,iDAOuB;AAavB,MAAsB,MAAM;IAgB1B;;;;;OAKG;IACH,MAAM,CAAC,KAAc;QACnB,MAAM,MAAM,GAAG,gCAAiB,CAAC,QAAQ,CAAC,KAAK,EAAE,IAAI,CAAC,CAAA;QACtD,IAAI,CAAC,MAAM,CAAC,OAAO;YAAE,MAAM,MAAM,CAAC,MAAM,CAAA;IAC1C,CAAC;IAED;;;;;OAKG;IACH,KAAK,CAAC,KAAc;QAClB,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAA;IACpB,CAAC;IAED;;;;OAIG;IACH,IAAI,CAAI,KAAQ;QACd,MAAM,M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idationResult,\n Validator,\n} from './validator.js'\n\ntype ParseOptions = Omit<ValidationOptions, 'mode'>\ntype ValidateOptions = Omit<ValidationOptions, 'mode'>\n\nexport interface SchemaInternals<out TInput = unknown, out TOutput = TInput> {\n /** @internal The inferred validation type */\n input: TInput\n\n /** @internal The inferred parse type */\n output: TOutput\n}\n\nexport abstract class Schema<\n out TInput = unknown,\n out TOutput = TInput,\n out TInternals extends SchemaInternals<TInput, TOutput> = SchemaInternals<\n TInput,\n TOutput\n >,\n> implements Validator<TInternals['input'], TInternals['output']>\n{\n declare readonly ['__lex']: TInternals\n\n abstract validateInContext(\n input: unknown,\n ctx: ValidationContext,\n ): ValidationResult\n\n /**\n * @note use {@link check}() instead of {@link assert}() if you encounter a\n * `ts(2775)` error and you are not able to fully type the validator. This\n * will typically arise in generic contexts, where the narrowed type is not\n * needed.\n */\n assert(input: unknown): asserts input is InferInput<this> {\n const result = ValidationContext.validate(input, this)\n if (!result.success) throw result.reason\n }\n\n /**\n * Alias for {@link assert}(). Most useful in generic contexts where the\n * validator is not exactly typed, allowing to avoid \"_Assertions require\n * every name in the call target to be declared with an explicit type\n * annotation. ts(2775)_\" errors.\n */\n check(input: unknown): void {\n this.assert(input)\n }\n\n /**\n * Casts the input (by validating it) to the output type if it matches the\n * schema, otherwise throws. This is the same as calling {@link parse}() with\n * `mode: \"validate\"`.\n */\n cast<I>(input: I): I & InferInput<this> {\n const result = ValidationContext.validate(input, this)\n if (result.success) return result.value\n throw result.reason\n }\n\n matches<I>(input: I): input is I & InferInput<this> {\n const result = ValidationContext.validate(input, this)\n return result.success\n }\n\n ifMatches<I>(input: I): (I & InferInput<this>) | undefined {\n return this.matches(input) ? input : undefined\n }\n\n parse(input: unknown, options?: ParseOptions): InferOutput<this> {\n const result = this.safeParse(input, options)\n if (result.success) return result.value\n throw result.reason\n }\n\n safeParse(\n input: unknown,\n options?: ParseOptions,\n ): ValidationResult<InferOutput<this>> {\n return ValidationContext.validate(input, this, {\n ...options,\n mode: 'parse',\n })\n }\n\n validate<I>(input: I, options?: ValidateOptions): I & InferInput<this> {\n const result = this.safeValidate(input, options)\n if (result.success) return result.value\n throw result.reason\n }\n\n safeValidate<I>(\n input: I,\n options?: ValidateOptions,\n ): ValidationResult<I & InferInput<this>> {\n return ValidationContext.validate(input, this, {\n ...options,\n mode: 'validate',\n })\n }\n\n // @NOTE The built lexicons namespaces will export utility functions that\n // allow accessing the schema's methods without the need to specify \".main.\"\n // as part of the namespace. This way, a utility for a particular record type\n // can be called like \"app.bsky.feed.post.<utility>()\" instead of\n // \"app.bsky.feed.post.main.<utility>()\". Because those utilities could\n // conflict with other schemas (e.g. if there is a lexicon definition at\n // \"#<utility>\"), those exported utilities will be prefixed with \"$\". In order\n // to be able to consistently call the utilities, when using the \"main\" and\n // non \"main\" definitions, we also expose the same methods with a \"$\" prefix.\n // Thanks to this, both of the following call will be possible:\n //\n // - \"app.bsky.feed.post.$parse(...)\" // calls a utility function created by \"lex build\"\n // - \"app.bsky.feed.defs.postView.$parse(...)\" // uses the alias defined below on the schema instance\n\n $assert(input: unknown): asserts input is InferInput<this> {\n return this.assert(input)\n }\n\n $check(input: unknown): void {\n return this.check(input)\n }\n\n $cast<I>(input: I): I & InferInput<this> {\n return this.cast(input)\n }\n\n $matches(input: unknown): input is InferInput<this> {\n return this.matches(input)\n }\n\n $ifMatches<I>(input: I): (I & InferInput<this>) | undefined {\n return this.ifMatches(input)\n }\n\n $parse(input: unknown, options?: ValidateOptions): InferOutput<this> {\n return this.parse(input, options)\n }\n\n $safeParse(\n input: unknown,\n options?: ValidateOptions,\n ): ValidationResult<InferOutput<this>> {\n return this.safeParse(input, options)\n }\n\n $validate<I>(input: I, options?: ValidateOptions): I & InferInput<this> {\n return this.validate(input, options)\n }\n\n $safeValidate<I>(\n input: I,\n options?: ValidateOptions,\n ): ValidationResult<I & InferInput<this>> {\n return this.safeValidate(input, options)\n }\n}\n"]}
1
+ {"version":3,"file":"schema.js","sourceRoot":"","sources":["../../src/core/schema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AAC;IAED;;;;OAIG;IACH,QAAQ,CAAC,KAAc;QACrB,OAAO,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,CAAA;IAC5B,CAAC;IAED;;;;OAIG;IACH,UAAU,CAAI,KAAQ;QACpB,OAAO,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAA;IAC9B,CAAC;IAED;;;;OAIG;IACH,MAAM,CAAC,KAAc,EAAE,OAAyB;QAC9C,OAAO,IAAI,CAAC,KAAK,CAAC,KAAK,EAAE,OAAO,CAAC,CAAA;IACnC,CAAC;IAED;;;;OAIG;IACH,UAAU,CACR,KAAc,EACd,OAAyB;QAEzB,OAAO,IAAI,CAAC,SAAS,CAAC,KAAK,EAAE,OAAO,CAAC,CAAA;IACvC,CAAC;IAED;;;;OAIG;IACH,SAAS,CAAI,KAAQ,EAAE,OAAyB;QAC9C,OAAO,IAAI,CAAC,QAAQ,CAAC,KAAK,EAAE,OAAO,CAAC,CAAA;IACtC,CAAC;IAED;;;;OAIG;IACH,aAAa,CACX,KAAQ,EACR,OAAyB;QAEzB,OAAO,IAAI,CAAC,YAAY,CAAC,KAAK,EAAE,OAAO,CAAC,CAAA;IAC1C,CAAC;CACF;AAhUD,wBAgUC","sourcesContent":["import {\n InferInput,\n InferOutput,\n ValidationContext,\n ValidationOptions,\n ValidationResult,\n Validator,\n} from './validator.js'\n\n/**\n * Options for parsing operations.\n * Excludes the `mode` option as it is implicitly set to `\"parse\"`.\n */\ntype ParseOptions = Omit<ValidationOptions, 'mode'>\n\n/**\n * Options for validation operations.\n * Excludes the `mode` option as it is implicitly set to `\"validate\"`.\n */\ntype ValidateOptions = Omit<ValidationOptions, 'mode'>\n\n/**\n * Internal type structure for schema type inference.\n *\n * This interface defines the phantom types used for compile-time type inference\n * without affecting runtime behavior. The `input` and `output` properties\n * represent the expected input type during validation and the resulting output\n * type after parsing, respectively.\n *\n * @typeParam TInput - The type accepted as input during validation\n * @typeParam TOutput - The type returned after parsing (may differ from input due to coercion)\n */\nexport interface SchemaInternals<out TInput = unknown, out TOutput = TInput> {\n input: TInput\n output: TOutput\n}\n\n/**\n * Abstract base class for all schema validators in the lexicon system.\n *\n * This class provides the standard validation interface that all schema types\n * implement. It offers multiple methods for validating and parsing data:\n *\n * - **Assertion methods**: `assert()`, `check()` - throw on invalid input\n * - **Type guard methods**: `matches()`, `ifMatches()` - return boolean or optional value\n * - **Parse methods**: `parse()`, `safeParse()` - allow value transformation/coercion\n * - **Validate methods**: `validate()`, `safeValidate()` - strict validation without coercion\n *\n * All methods are also available with a `$` prefix (e.g., `$parse()`, `$validate()`)\n * for consistent access in generated lexicon namespaces.\n *\n * @typeParam TInput - The type accepted as valid input during validation\n * @typeParam TOutput - The type returned after parsing (may include transformations)\n * @typeParam TInternals - Internal type structure for type inference\n *\n * @example\n * ```typescript\n * class MySchema extends Schema<string> {\n * validateInContext(input: unknown, ctx: ValidationContext): ValidationResult {\n * if (typeof input !== 'string') {\n * return ctx.issueInvalidType(input, 'string')\n * }\n * return ctx.success(input)\n * }\n * }\n *\n * const schema = new MySchema()\n * schema.assert('hello') // OK\n * schema.assert(123) // Throws ValidationError\n * schema.matches('hello') // true\n * schema.matches(123) // false\n * ```\n */\nexport abstract class Schema<\n out TInput = unknown,\n out TOutput = TInput,\n out TInternals extends SchemaInternals<TInput, TOutput> = SchemaInternals<\n TInput,\n TOutput\n >,\n> implements Validator<TInternals['input'], TInternals['output']>\n{\n /**\n * Internal phantom property for type inference.\n * This property does not exist at runtime.\n *\n * @internal\n */\n declare readonly ['__lex']: TInternals\n\n /**\n * Performs validation of the input value within a validation context.\n *\n * This method must be implemented by subclasses to define the actual\n * validation logic. It should not be called directly; use\n * {@link ValidationContext.validate} instead to ensure proper mode enforcement.\n *\n * @param input - The value to validate\n * @param ctx - The validation context providing path tracking and issue reporting\n * @returns A validation result indicating success with the validated value or failure with issues\n *\n * @internal\n */\n abstract validateInContext(\n input: unknown,\n ctx: ValidationContext,\n ): ValidationResult\n\n /**\n * @note use {@link check}() instead of {@link assert}() if you encounter a\n * `ts(2775)` error and you are not able to fully type the validator. This\n * will typically arise in generic contexts, where the narrowed type is not\n * needed.\n */\n assert(input: unknown): asserts input is InferInput<this> {\n const result = ValidationContext.validate(input, this)\n if (!result.success) throw result.reason\n }\n\n /**\n * Alias for {@link assert}(). Most useful in generic contexts where the\n * validator is not exactly typed, allowing to avoid \"_Assertions require\n * every name in the call target to be declared with an explicit type\n * annotation. ts(2775)_\" errors.\n */\n check(input: unknown): void {\n this.assert(input)\n }\n\n /**\n * Casts the input (by validating it) to the output type if it matches the\n * schema, otherwise throws. This is the same as calling {@link parse}() with\n * `mode: \"validate\"`.\n */\n cast<I>(input: I): I & InferInput<this> {\n const result = ValidationContext.validate(input, this)\n if (result.success) return result.value\n throw result.reason\n }\n\n /**\n * Type guard that checks if the input matches this schema.\n *\n * @param input - The value to check\n * @returns `true` if the input is valid according to this schema\n *\n * @example\n * ```typescript\n * if (schema.matches(data)) {\n * // data is narrowed to the schema's input type\n * console.log(data)\n * }\n * ```\n */\n matches<I>(input: I): input is I & InferInput<this> {\n const result = ValidationContext.validate(input, this)\n return result.success\n }\n\n /**\n * Returns the input if it matches this schema, otherwise returns `undefined`.\n *\n * This is useful for optional filtering operations where you want to\n * conditionally extract values that match a schema.\n *\n * @param input - The value to check\n * @returns The input value with narrowed type if valid, otherwise `undefined`\n *\n * @example\n * ```typescript\n * const validData = schema.ifMatches(data)\n * if (validData !== undefined) {\n * // validData is the schema's input type\n * console.log(validData)\n * }\n * ```\n */\n ifMatches<I>(input: I): (I & InferInput<this>) | undefined {\n return this.matches(input) ? input : undefined\n }\n\n /**\n * Parses the input, allowing value transformations and coercion.\n *\n * Unlike {@link validate}, this method allows the schema to transform\n * the input value (e.g., applying default values, type coercion).\n * Throws a {@link ValidationError} if the input is invalid.\n *\n * @param input - The value to parse\n * @param options - Optional parsing configuration\n * @returns The parsed and potentially transformed value\n * @throws {ValidationError} If the input fails validation\n *\n * @example\n * ```typescript\n * const result = schema.parse(rawData)\n * // result has defaults applied and is fully typed\n * ```\n */\n parse(input: unknown, options?: ParseOptions): InferOutput<this> {\n const result = this.safeParse(input, options)\n if (result.success) return result.value\n throw result.reason\n }\n\n /**\n * Safely parses the input without throwing, returning a result object.\n *\n * This method allows value transformations like {@link parse}, but\n * returns a discriminated union result instead of throwing on error.\n *\n * @param input - The value to parse\n * @param options - Optional parsing configuration\n * @returns A {@link ValidationResult} with either the parsed value or validation errors\n *\n * @example\n * ```typescript\n * const result = schema.safeParse(data)\n * if (result.success) {\n * console.log(result.value)\n * } else {\n * console.error(result.reason.issues)\n * }\n * ```\n */\n safeParse(\n input: unknown,\n options?: ParseOptions,\n ): ValidationResult<InferOutput<this>> {\n return ValidationContext.validate(input, this, {\n ...options,\n mode: 'parse',\n })\n }\n\n /**\n * Validates the input strictly without allowing transformations.\n *\n * Unlike {@link parse}, this method requires the input to exactly match\n * the schema without any transformations (no defaults applied, no coercion).\n * Throws a {@link ValidationError} if the input is invalid or would require transformation.\n *\n * @typeParam I - The input type (preserved in the return type)\n * @param input - The value to validate\n * @param options - Optional validation configuration\n * @returns The validated input with narrowed type\n * @throws {ValidationError} If the input fails validation or requires transformation\n *\n * @example\n * ```typescript\n * const validated = schema.validate(data)\n * // validated is typed as the intersection of input type and schema type\n * ```\n */\n validate<I>(input: I, options?: ValidateOptions): I & InferInput<this> {\n const result = this.safeValidate(input, options)\n if (result.success) return result.value\n throw result.reason\n }\n\n /**\n * Safely validates the input without throwing, returning a result object.\n *\n * This method performs strict validation like {@link validate}, but\n * returns a discriminated union result instead of throwing on error.\n *\n * @typeParam I - The input type (preserved in the result value type)\n * @param input - The value to validate\n * @param options - Optional validation configuration\n * @returns A {@link ValidationResult} with either the validated value or validation errors\n *\n * @example\n * ```typescript\n * const result = schema.safeValidate(data)\n * if (result.success) {\n * console.log(result.value)\n * } else {\n * console.error(result.reason.issues)\n * }\n * ```\n */\n safeValidate<I>(\n input: I,\n options?: ValidateOptions,\n ): ValidationResult<I & InferInput<this>> {\n return ValidationContext.validate(input, this, {\n ...options,\n mode: 'validate',\n })\n }\n\n // @NOTE Dollar-prefixed aliases\n //\n // The built lexicons namespaces export utility functions that allow accessing\n // the schema's methods without the need to specify \".main.\" as part of the\n // namespace. This way, a utility for a particular record type can be called\n // like \"app.bsky.feed.post.<utility>()\" instead of\n // \"app.bsky.feed.post.main.<utility>()\". Because those utilities could\n // conflict with other schemas (e.g. if there is a lexicon definition at\n // \"#<utility>\"), those exported utilities will be prefixed with \"$\". In order\n // to be able to consistently call the utilities, when using the \"main\" and\n // non \"main\" definitions, we also expose the same methods with a \"$\" prefix.\n // Thanks to this, both of the following call will be possible:\n //\n // - \"app.bsky.feed.post.$parse(...)\" // calls a utility function created by \"lex build\"\n // - \"app.bsky.feed.defs.postView.$parse(...)\" // uses the alias defined below on the schema instance\n\n /**\n * Alias for {@link assert} with `$` prefix for namespace compatibility.\n *\n * @see {@link assert}\n */\n $assert(input: unknown): asserts input is InferInput<this> {\n return this.assert(input)\n }\n\n /**\n * Alias for {@link check} with `$` prefix for namespace compatibility.\n *\n * @see {@link check}\n */\n $check(input: unknown): void {\n return this.check(input)\n }\n\n /**\n * Alias for {@link cast} with `$` prefix for namespace compatibility.\n *\n * @see {@link cast}\n */\n $cast<I>(input: I): I & InferInput<this> {\n return this.cast(input)\n }\n\n /**\n * Alias for {@link matches} with `$` prefix for namespace compatibility.\n *\n * @see {@link matches}\n */\n $matches(input: unknown): input is InferInput<this> {\n return this.matches(input)\n }\n\n /**\n * Alias for {@link ifMatches} with `$` prefix for namespace compatibility.\n *\n * @see {@link ifMatches}\n */\n $ifMatches<I>(input: I): (I & InferInput<this>) | undefined {\n return this.ifMatches(input)\n }\n\n /**\n * Alias for {@link parse} with `$` prefix for namespace compatibility.\n *\n * @see {@link parse}\n */\n $parse(input: unknown, options?: ValidateOptions): InferOutput<this> {\n return this.parse(input, options)\n }\n\n /**\n * Alias for {@link safeParse} with `$` prefix for namespace compatibility.\n *\n * @see {@link safeParse}\n */\n $safeParse(\n input: unknown,\n options?: ValidateOptions,\n ): ValidationResult<InferOutput<this>> {\n return this.safeParse(input, options)\n }\n\n /**\n * Alias for {@link validate} with `$` prefix for namespace compatibility.\n *\n * @see {@link validate}\n */\n $validate<I>(input: I, options?: ValidateOptions): I & InferInput<this> {\n return this.validate(input, options)\n }\n\n /**\n * Alias for {@link safeValidate} with `$` prefix for namespace compatibility.\n *\n * @see {@link safeValidate}\n */\n $safeValidate<I>(\n input: I,\n options?: ValidateOptions,\n ): ValidationResult<I & InferInput<this>> {\n return this.safeValidate(input, options)\n }\n}\n"]}