@atproto/lex-schema 0.0.5 → 0.0.7

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 (269) hide show
  1. package/CHANGELOG.md +30 -0
  2. package/dist/core/$type.d.ts +6 -3
  3. package/dist/core/$type.d.ts.map +1 -1
  4. package/dist/core/$type.js.map +1 -1
  5. package/dist/core/property-key.d.ts.map +1 -0
  6. package/dist/core/property-key.js.map +1 -0
  7. package/dist/core/schema.d.ts.map +1 -0
  8. package/dist/core/schema.js.map +1 -0
  9. package/dist/core/validation-error.d.ts +19 -0
  10. package/dist/core/validation-error.d.ts.map +1 -0
  11. package/dist/{validation → core}/validation-error.js +12 -5
  12. package/dist/core/validation-error.js.map +1 -0
  13. package/dist/{validation → core}/validation-issue.d.ts +43 -0
  14. package/dist/core/validation-issue.d.ts.map +1 -0
  15. package/dist/{validation → core}/validation-issue.js +45 -0
  16. package/dist/core/validation-issue.js.map +1 -0
  17. package/dist/{validation → core}/validator.d.ts +1 -1
  18. package/dist/core/validator.d.ts.map +1 -0
  19. package/dist/{validation → core}/validator.js +4 -4
  20. package/dist/core/validator.js.map +1 -0
  21. package/dist/core.d.ts +5 -0
  22. package/dist/core.d.ts.map +1 -1
  23. package/dist/core.js +5 -0
  24. package/dist/core.js.map +1 -1
  25. package/dist/external.d.ts +15 -16
  26. package/dist/external.d.ts.map +1 -1
  27. package/dist/external.js +38 -55
  28. package/dist/external.js.map +1 -1
  29. package/dist/helpers.d.ts +7 -2
  30. package/dist/helpers.d.ts.map +1 -1
  31. package/dist/helpers.js +10 -0
  32. package/dist/helpers.js.map +1 -1
  33. package/dist/schema/_parameters.d.ts +1 -1
  34. package/dist/schema/_parameters.d.ts.map +1 -1
  35. package/dist/schema/_parameters.js.map +1 -1
  36. package/dist/schema/array.d.ts +1 -1
  37. package/dist/schema/array.d.ts.map +1 -1
  38. package/dist/schema/array.js +2 -2
  39. package/dist/schema/array.js.map +1 -1
  40. package/dist/schema/blob.d.ts +3 -7
  41. package/dist/schema/blob.d.ts.map +1 -1
  42. package/dist/schema/blob.js +2 -2
  43. package/dist/schema/blob.js.map +1 -1
  44. package/dist/schema/boolean.d.ts +1 -1
  45. package/dist/schema/boolean.d.ts.map +1 -1
  46. package/dist/schema/boolean.js +2 -2
  47. package/dist/schema/boolean.js.map +1 -1
  48. package/dist/schema/bytes.d.ts +1 -1
  49. package/dist/schema/bytes.d.ts.map +1 -1
  50. package/dist/schema/bytes.js +2 -2
  51. package/dist/schema/bytes.js.map +1 -1
  52. package/dist/schema/cid.d.ts +1 -1
  53. package/dist/schema/cid.d.ts.map +1 -1
  54. package/dist/schema/cid.js +2 -2
  55. package/dist/schema/cid.js.map +1 -1
  56. package/dist/schema/custom.d.ts +1 -1
  57. package/dist/schema/custom.d.ts.map +1 -1
  58. package/dist/schema/custom.js +3 -3
  59. package/dist/schema/custom.js.map +1 -1
  60. package/dist/schema/dict.d.ts +1 -1
  61. package/dist/schema/dict.d.ts.map +1 -1
  62. package/dist/schema/dict.js +2 -2
  63. package/dist/schema/dict.js.map +1 -1
  64. package/dist/schema/discriminated-union.d.ts +1 -1
  65. package/dist/schema/discriminated-union.d.ts.map +1 -1
  66. package/dist/schema/discriminated-union.js +2 -2
  67. package/dist/schema/discriminated-union.js.map +1 -1
  68. package/dist/schema/enum.d.ts +1 -1
  69. package/dist/schema/enum.d.ts.map +1 -1
  70. package/dist/schema/enum.js +2 -2
  71. package/dist/schema/enum.js.map +1 -1
  72. package/dist/schema/integer.d.ts +1 -1
  73. package/dist/schema/integer.d.ts.map +1 -1
  74. package/dist/schema/integer.js +4 -4
  75. package/dist/schema/integer.js.map +1 -1
  76. package/dist/schema/intersection.d.ts +1 -2
  77. package/dist/schema/intersection.d.ts.map +1 -1
  78. package/dist/schema/intersection.js +2 -2
  79. package/dist/schema/intersection.js.map +1 -1
  80. package/dist/schema/literal.d.ts +1 -1
  81. package/dist/schema/literal.d.ts.map +1 -1
  82. package/dist/schema/literal.js +2 -2
  83. package/dist/schema/literal.js.map +1 -1
  84. package/dist/schema/never.d.ts +1 -1
  85. package/dist/schema/never.d.ts.map +1 -1
  86. package/dist/schema/never.js +2 -2
  87. package/dist/schema/never.js.map +1 -1
  88. package/dist/schema/null.d.ts +1 -1
  89. package/dist/schema/null.d.ts.map +1 -1
  90. package/dist/schema/null.js +2 -2
  91. package/dist/schema/null.js.map +1 -1
  92. package/dist/schema/nullable.d.ts +1 -1
  93. package/dist/schema/nullable.d.ts.map +1 -1
  94. package/dist/schema/nullable.js +2 -2
  95. package/dist/schema/nullable.js.map +1 -1
  96. package/dist/schema/object.d.ts +1 -2
  97. package/dist/schema/object.d.ts.map +1 -1
  98. package/dist/schema/object.js +2 -2
  99. package/dist/schema/object.js.map +1 -1
  100. package/dist/schema/optional.d.ts +1 -1
  101. package/dist/schema/optional.d.ts.map +1 -1
  102. package/dist/schema/optional.js +2 -2
  103. package/dist/schema/optional.js.map +1 -1
  104. package/dist/schema/params.d.ts +1 -2
  105. package/dist/schema/params.d.ts.map +1 -1
  106. package/dist/schema/params.js +2 -2
  107. package/dist/schema/params.js.map +1 -1
  108. package/dist/schema/payload.d.ts +1 -1
  109. package/dist/schema/payload.d.ts.map +1 -1
  110. package/dist/schema/payload.js.map +1 -1
  111. package/dist/schema/record.d.ts +9 -14
  112. package/dist/schema/record.d.ts.map +1 -1
  113. package/dist/schema/record.js +5 -3
  114. package/dist/schema/record.js.map +1 -1
  115. package/dist/schema/ref.d.ts +1 -1
  116. package/dist/schema/ref.d.ts.map +1 -1
  117. package/dist/schema/ref.js +2 -2
  118. package/dist/schema/ref.js.map +1 -1
  119. package/dist/schema/refine.d.ts +18 -1
  120. package/dist/schema/refine.d.ts.map +1 -1
  121. package/dist/schema/refine.js +2 -2
  122. package/dist/schema/refine.js.map +1 -1
  123. package/dist/schema/regexp.d.ts +1 -1
  124. package/dist/schema/regexp.d.ts.map +1 -1
  125. package/dist/schema/regexp.js +2 -2
  126. package/dist/schema/regexp.js.map +1 -1
  127. package/dist/schema/string.d.ts +1 -2
  128. package/dist/schema/string.d.ts.map +1 -1
  129. package/dist/schema/string.js +1 -2
  130. package/dist/schema/string.js.map +1 -1
  131. package/dist/schema/subscription.d.ts +1 -2
  132. package/dist/schema/subscription.d.ts.map +1 -1
  133. package/dist/schema/subscription.js.map +1 -1
  134. package/dist/schema/token.d.ts +1 -1
  135. package/dist/schema/token.d.ts.map +1 -1
  136. package/dist/schema/token.js +2 -2
  137. package/dist/schema/token.js.map +1 -1
  138. package/dist/schema/typed-object.d.ts +8 -13
  139. package/dist/schema/typed-object.d.ts.map +1 -1
  140. package/dist/schema/typed-object.js +5 -3
  141. package/dist/schema/typed-object.js.map +1 -1
  142. package/dist/schema/typed-ref.d.ts +1 -1
  143. package/dist/schema/typed-ref.d.ts.map +1 -1
  144. package/dist/schema/typed-ref.js +2 -2
  145. package/dist/schema/typed-ref.js.map +1 -1
  146. package/dist/schema/typed-union.d.ts +2 -3
  147. package/dist/schema/typed-union.d.ts.map +1 -1
  148. package/dist/schema/typed-union.js +2 -2
  149. package/dist/schema/typed-union.js.map +1 -1
  150. package/dist/schema/union.d.ts +1 -1
  151. package/dist/schema/union.d.ts.map +1 -1
  152. package/dist/schema/union.js +3 -3
  153. package/dist/schema/union.js.map +1 -1
  154. package/dist/schema/unknown-object.d.ts +1 -2
  155. package/dist/schema/unknown-object.d.ts.map +1 -1
  156. package/dist/schema/unknown-object.js +2 -2
  157. package/dist/schema/unknown-object.js.map +1 -1
  158. package/dist/schema/unknown.d.ts +1 -1
  159. package/dist/schema/unknown.d.ts.map +1 -1
  160. package/dist/schema/unknown.js +2 -2
  161. package/dist/schema/unknown.js.map +1 -1
  162. package/dist/util/memoize.d.ts +3 -0
  163. package/dist/util/memoize.d.ts.map +1 -0
  164. package/dist/util/memoize.js +52 -0
  165. package/dist/util/memoize.js.map +1 -0
  166. package/package.json +6 -6
  167. package/src/core/$type.ts +13 -2
  168. package/src/{validation → core}/validation-error.ts +11 -3
  169. package/src/{validation → core}/validation-issue.ts +52 -0
  170. package/src/{validation → core}/validator.ts +1 -1
  171. package/src/core.ts +5 -0
  172. package/src/external.ts +67 -48
  173. package/src/helpers.test.ts +1 -0
  174. package/src/helpers.ts +16 -2
  175. package/src/schema/_parameters.test.ts +1 -0
  176. package/src/schema/_parameters.ts +1 -1
  177. package/src/schema/array.test.ts +1 -0
  178. package/src/schema/array.ts +1 -1
  179. package/src/schema/blob.test.ts +1 -0
  180. package/src/schema/blob.ts +3 -6
  181. package/src/schema/boolean.test.ts +1 -0
  182. package/src/schema/boolean.ts +1 -1
  183. package/src/schema/bytes.test.ts +1 -0
  184. package/src/schema/bytes.ts +1 -1
  185. package/src/schema/cid.test.ts +1 -0
  186. package/src/schema/cid.ts +1 -1
  187. package/src/schema/custom.test.ts +3 -2
  188. package/src/schema/custom.ts +1 -1
  189. package/src/schema/dict.test.ts +1 -0
  190. package/src/schema/dict.ts +1 -1
  191. package/src/schema/discriminated-union.test.ts +1 -0
  192. package/src/schema/discriminated-union.ts +1 -1
  193. package/src/schema/enum.test.ts +1 -0
  194. package/src/schema/enum.ts +1 -1
  195. package/src/schema/integer.test.ts +1 -0
  196. package/src/schema/integer.ts +3 -3
  197. package/src/schema/intersection.test.ts +1 -0
  198. package/src/schema/intersection.ts +2 -2
  199. package/src/schema/literal.test.ts +1 -0
  200. package/src/schema/literal.ts +1 -1
  201. package/src/schema/never.test.ts +1 -0
  202. package/src/schema/never.ts +1 -1
  203. package/src/schema/null.test.ts +1 -0
  204. package/src/schema/null.ts +1 -1
  205. package/src/schema/nullable.test.ts +1 -0
  206. package/src/schema/nullable.ts +1 -1
  207. package/src/schema/object.test.ts +1 -0
  208. package/src/schema/object.ts +3 -3
  209. package/src/schema/optional.test.ts +1 -0
  210. package/src/schema/optional.ts +1 -1
  211. package/src/schema/params.test.ts +1 -0
  212. package/src/schema/params.ts +3 -3
  213. package/src/schema/payload.test.ts +1 -0
  214. package/src/schema/payload.ts +1 -1
  215. package/src/schema/permission-set.test.ts +1 -0
  216. package/src/schema/permission.test.ts +1 -0
  217. package/src/schema/procedure.test.ts +1 -0
  218. package/src/schema/query.test.ts +1 -0
  219. package/src/schema/record.test.ts +1 -0
  220. package/src/schema/record.ts +11 -9
  221. package/src/schema/ref.test.ts +2 -1
  222. package/src/schema/ref.ts +1 -1
  223. package/src/schema/refine.test.ts +1 -0
  224. package/src/schema/refine.ts +18 -1
  225. package/src/schema/regexp.test.ts +1 -0
  226. package/src/schema/regexp.ts +1 -1
  227. package/src/schema/string.test.ts +1 -0
  228. package/src/schema/string.ts +8 -2
  229. package/src/schema/subscription.test.ts +1 -0
  230. package/src/schema/subscription.ts +1 -2
  231. package/src/schema/token.test.ts +1 -0
  232. package/src/schema/token.ts +1 -1
  233. package/src/schema/typed-object.test.ts +1 -0
  234. package/src/schema/typed-object.ts +10 -6
  235. package/src/schema/typed-ref.test.ts +1 -0
  236. package/src/schema/typed-ref.ts +1 -1
  237. package/src/schema/typed-union.test.ts +1 -0
  238. package/src/schema/typed-union.ts +4 -4
  239. package/src/schema/union.test.ts +1 -0
  240. package/src/schema/union.ts +1 -1
  241. package/src/schema/unknown-object.test.ts +1 -0
  242. package/src/schema/unknown-object.ts +1 -2
  243. package/src/schema/unknown.test.ts +1 -0
  244. package/src/schema/unknown.ts +1 -1
  245. package/src/util/array-agg.test.ts +1 -0
  246. package/src/util/memoize.ts +57 -0
  247. package/tsconfig.tests.json +1 -1
  248. package/dist/validation/property-key.d.ts.map +0 -1
  249. package/dist/validation/property-key.js.map +0 -1
  250. package/dist/validation/schema.d.ts.map +0 -1
  251. package/dist/validation/schema.js.map +0 -1
  252. package/dist/validation/validation-error.d.ts +0 -9
  253. package/dist/validation/validation-error.d.ts.map +0 -1
  254. package/dist/validation/validation-error.js.map +0 -1
  255. package/dist/validation/validation-issue.d.ts.map +0 -1
  256. package/dist/validation/validation-issue.js.map +0 -1
  257. package/dist/validation/validator.d.ts.map +0 -1
  258. package/dist/validation/validator.js.map +0 -1
  259. package/dist/validation.d.ts +0 -6
  260. package/dist/validation.d.ts.map +0 -1
  261. package/dist/validation.js +0 -9
  262. package/dist/validation.js.map +0 -1
  263. package/src/validation.ts +0 -5
  264. /package/dist/{validation → core}/property-key.d.ts +0 -0
  265. /package/dist/{validation → core}/property-key.js +0 -0
  266. /package/dist/{validation → core}/schema.d.ts +0 -0
  267. /package/dist/{validation → core}/schema.js +0 -0
  268. /package/src/{validation → core}/property-key.ts +0 -0
  269. /package/src/{validation → core}/schema.ts +0 -0
@@ -1,3 +1,4 @@
1
+ import { describe, test } from 'vitest'
1
2
  import * as l from './external.js'
2
3
 
3
4
  class BinaryValue {
package/src/helpers.ts CHANGED
@@ -1,14 +1,23 @@
1
- import { Restricted } from './core.js'
1
+ import { LexErrorData } from '@atproto/lex-data'
2
+ import { Infer, Restricted, Schema } from './core.js'
2
3
  import {
3
4
  InferPayload,
4
5
  InferPayloadBody,
5
6
  InferPayloadEncoding,
7
+ ObjectSchema,
8
+ OptionalSchema,
6
9
  Payload,
7
10
  Procedure,
8
11
  Query,
12
+ StringSchema,
9
13
  Subscription,
10
14
  } from './schema.js'
11
- import { Infer, Schema } from './validation.js'
15
+
16
+ export type Main<T> = T | { main: T }
17
+
18
+ export function getMain<T extends object>(ns: Main<T>): T {
19
+ return 'main' in ns ? ns.main : ns
20
+ }
12
21
 
13
22
  /**
14
23
  * Every XRPC implementation should translate `application/json` and `text/*`
@@ -59,3 +68,8 @@ export type InferMethodMessage<
59
68
  //
60
69
  M extends Procedure | Query | Subscription,
61
70
  > = M extends { message: Schema } ? Infer<M['message']> : undefined
71
+
72
+ export const lexErrorData: Schema<LexErrorData> = new ObjectSchema({
73
+ error: new StringSchema({ minLength: 1 }),
74
+ message: new OptionalSchema(new StringSchema({})),
75
+ })
@@ -1,3 +1,4 @@
1
+ import { describe, expect, it } from 'vitest'
1
2
  import { paramSchema, paramsSchema } from './_parameters.js'
2
3
 
3
4
  describe('paramSchema', () => {
@@ -1,4 +1,4 @@
1
- import { Infer, Validator } from '../validation.js'
1
+ import { Infer, Validator } from '../core.js'
2
2
  import { ArraySchema } from './array.js'
3
3
  import { BooleanSchema } from './boolean.js'
4
4
  import { DictSchema } from './dict.js'
@@ -1,3 +1,4 @@
1
+ import { describe, expect, it } from 'vitest'
1
2
  import { ArraySchema } from './array.js'
2
3
  import { IntegerSchema } from './integer.js'
3
4
  import { ObjectSchema } from './object.js'
@@ -4,7 +4,7 @@ import {
4
4
  ValidationResult,
5
5
  Validator,
6
6
  ValidatorContext,
7
- } from '../validation.js'
7
+ } from '../core.js'
8
8
 
9
9
  export type ArraySchemaOptions = {
10
10
  minLength?: number
@@ -1,3 +1,4 @@
1
+ import { describe, expect, it } from 'vitest'
1
2
  import { parseCid } from '@atproto/lex-data'
2
3
  import { BlobSchema } from './blob.js'
3
4
 
@@ -1,21 +1,18 @@
1
1
  import {
2
2
  BlobRef,
3
+ BlobRefValidationOptions,
3
4
  LegacyBlobRef,
4
5
  isBlobRef,
5
6
  isLegacyBlobRef,
6
7
  } from '@atproto/lex-data'
7
- import { Schema, ValidationResult, ValidatorContext } from '../validation.js'
8
+ import { Schema, ValidationResult, ValidatorContext } from '../core.js'
8
9
 
9
- export type BlobSchemaOptions = {
10
+ export type BlobSchemaOptions = BlobRefValidationOptions & {
10
11
  /**
11
12
  * Whether to allow legacy blob references format
12
13
  * @see {@link LegacyBlobRef}
13
14
  */
14
15
  allowLegacy?: boolean
15
- /**
16
- * Whether to enforce strict validation on the blob reference (CID version, codec, hash function)
17
- */
18
- strict?: boolean
19
16
  /**
20
17
  * List of accepted mime types
21
18
  */
@@ -1,3 +1,4 @@
1
+ import { describe, expect, it } from 'vitest'
1
2
  import { BooleanSchema } from './boolean.js'
2
3
 
3
4
  describe('BooleanSchema', () => {
@@ -1,4 +1,4 @@
1
- import { Schema, ValidationResult, ValidatorContext } from '../validation.js'
1
+ import { Schema, ValidationResult, ValidatorContext } from '../core.js'
2
2
 
3
3
  export type BooleanSchemaOptions = {
4
4
  default?: boolean
@@ -1,3 +1,4 @@
1
+ import { describe, expect, it } from 'vitest'
1
2
  import { BytesSchema } from './bytes.js'
2
3
 
3
4
  describe('BytesSchema', () => {
@@ -1,5 +1,5 @@
1
1
  import { asUint8Array } from '@atproto/lex-data'
2
- import { Schema, ValidationResult, ValidatorContext } from '../validation.js'
2
+ import { Schema, ValidationResult, ValidatorContext } from '../core.js'
3
3
 
4
4
  export type BytesSchemaOptions = {
5
5
  minLength?: number
@@ -1,3 +1,4 @@
1
+ import { describe, expect, it } from 'vitest'
1
2
  import { parseCid } from '@atproto/lex-data'
2
3
  import { CidSchema } from './cid.js'
3
4
 
package/src/schema/cid.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { Cid, isCid } from '@atproto/lex-data'
2
- import { Schema, ValidationResult, ValidatorContext } from '../validation.js'
2
+ import { Schema, ValidationResult, ValidatorContext } from '../core.js'
3
3
 
4
4
  export type { Cid }
5
5
 
@@ -1,4 +1,5 @@
1
- import { IssueCustom } from '../validation.js'
1
+ import { describe, expect, it, vi } from 'vitest'
2
+ import { IssueCustom } from '../core.js'
2
3
  import { CustomSchema } from './custom.js'
3
4
 
4
5
  describe('CustomSchema', () => {
@@ -371,7 +372,7 @@ describe('CustomSchema', () => {
371
372
 
372
373
  describe('assertion context behavior', () => {
373
374
  it('calls assertion with null as this', () => {
374
- const assertion = jest.fn(function (
375
+ const assertion = vi.fn(function (
375
376
  this: unknown,
376
377
  input: unknown,
377
378
  ): input is string {
@@ -5,7 +5,7 @@ import {
5
5
  Schema,
6
6
  ValidationResult,
7
7
  ValidatorContext,
8
- } from '../validation.js'
8
+ } from '../core.js'
9
9
 
10
10
  export type CustomAssertionContext = {
11
11
  path: PropertyKey[]
@@ -1,3 +1,4 @@
1
+ import { describe, expect, it } from 'vitest'
1
2
  import { BooleanSchema } from './boolean.js'
2
3
  import { DictSchema } from './dict.js'
3
4
  import { EnumSchema } from './enum.js'
@@ -5,7 +5,7 @@ import {
5
5
  ValidationResult,
6
6
  Validator,
7
7
  ValidatorContext,
8
- } from '../validation.js'
8
+ } from '../core.js'
9
9
 
10
10
  export type DictSchemaOutput<
11
11
  KeySchema extends Validator<string>,
@@ -1,3 +1,4 @@
1
+ import { describe, expect, it } from 'vitest'
1
2
  import { DiscriminatedUnionSchema } from './discriminated-union.js'
2
3
  import { EnumSchema } from './enum.js'
3
4
  import { IntegerSchema } from './integer.js'
@@ -5,7 +5,7 @@ import {
5
5
  ValidationResult,
6
6
  Validator,
7
7
  ValidatorContext,
8
- } from '../validation.js'
8
+ } from '../core.js'
9
9
  import { EnumSchema } from './enum.js'
10
10
  import { LiteralSchema } from './literal.js'
11
11
  import { ObjectSchema } from './object.js'
@@ -1,3 +1,4 @@
1
+ import { describe, expect, it } from 'vitest'
1
2
  import { EnumSchema } from './enum.js'
2
3
 
3
4
  describe('EnumSchema', () => {
@@ -1,4 +1,4 @@
1
- import { Schema, ValidationResult, ValidatorContext } from '../validation.js'
1
+ import { Schema, ValidationResult, ValidatorContext } from '../core.js'
2
2
 
3
3
  export type EnumSchemaOptions<T extends null | string | number | boolean> = {
4
4
  default?: T
@@ -1,3 +1,4 @@
1
+ import { describe, expect, it } from 'vitest'
1
2
  import { IntegerSchema } from './integer.js'
2
3
 
3
4
  describe('IntegerSchema', () => {
@@ -1,4 +1,4 @@
1
- import { Schema, ValidationResult, ValidatorContext } from '../validation.js'
1
+ import { Schema, ValidationResult, ValidatorContext } from '../core.js'
2
2
 
3
3
  export type IntegerSchemaOptions = {
4
4
  default?: number
@@ -32,8 +32,8 @@ export class IntegerSchema extends Schema<number> {
32
32
  }
33
33
 
34
34
  /**
35
- * Simple wrapper around {@link Number.isInteger} that acts as a type guard.
35
+ * Simple wrapper around {@link Number.isSafeInteger} that acts as a type guard.
36
36
  */
37
37
  function isInteger(input: unknown): input is number {
38
- return Number.isInteger(input) && Number.isSafeInteger(input)
38
+ return Number.isSafeInteger(input)
39
39
  }
@@ -1,3 +1,4 @@
1
+ import { describe, expect, it } from 'vitest'
1
2
  import { BooleanSchema } from './boolean.js'
2
3
  import { DictSchema } from './dict.js'
3
4
  import { EnumSchema } from './enum.js'
@@ -1,10 +1,10 @@
1
- import { Simplify } from '../core.js'
2
1
  import {
3
2
  Infer,
4
3
  Schema,
4
+ Simplify,
5
5
  ValidationResult,
6
6
  ValidatorContext,
7
- } from '../validation.js'
7
+ } from '../core.js'
8
8
  import { DictSchema } from './dict.js'
9
9
  import { ObjectSchema } from './object.js'
10
10
 
@@ -1,3 +1,4 @@
1
+ import { describe, expect, it } from 'vitest'
1
2
  import { LiteralSchema } from './literal.js'
2
3
 
3
4
  describe('LiteralSchema', () => {
@@ -1,4 +1,4 @@
1
- import { Schema, ValidationResult, ValidatorContext } from '../validation.js'
1
+ import { Schema, ValidationResult, ValidatorContext } from '../core.js'
2
2
 
3
3
  export type LiteralSchemaOptions<T extends null | string | number | boolean> = {
4
4
  default?: T
@@ -1,3 +1,4 @@
1
+ import { describe, expect, it } from 'vitest'
1
2
  import { NeverSchema } from './never.js'
2
3
 
3
4
  describe('NeverSchema', () => {
@@ -1,4 +1,4 @@
1
- import { Schema, ValidationFailure, ValidatorContext } from '../validation.js'
1
+ import { Schema, ValidationFailure, ValidatorContext } from '../core.js'
2
2
 
3
3
  export class NeverSchema extends Schema<never> {
4
4
  validateInContext(input: unknown, ctx: ValidatorContext): ValidationFailure {
@@ -1,3 +1,4 @@
1
+ import { describe, expect, it } from 'vitest'
1
2
  import { NullSchema } from './null.js'
2
3
 
3
4
  describe('NullSchema', () => {
@@ -1,4 +1,4 @@
1
- import { Schema, ValidationResult, ValidatorContext } from '../validation.js'
1
+ import { Schema, ValidationResult, ValidatorContext } from '../core.js'
2
2
 
3
3
  export class NullSchema extends Schema<null> {
4
4
  constructor() {
@@ -1,3 +1,4 @@
1
+ import { describe, expect, it } from 'vitest'
1
2
  import { EnumSchema } from './enum.js'
2
3
  import { IntegerSchema } from './integer.js'
3
4
  import { NullableSchema } from './nullable.js'
@@ -3,7 +3,7 @@ import {
3
3
  ValidationResult,
4
4
  Validator,
5
5
  ValidatorContext,
6
- } from '../validation.js'
6
+ } from '../core.js'
7
7
 
8
8
  export class NullableSchema<V> extends Schema<V | null> {
9
9
  constructor(readonly schema: Validator<V>) {
@@ -1,3 +1,4 @@
1
+ import { describe, expect, it } from 'vitest'
1
2
  import { EnumSchema } from './enum.js'
2
3
  import { IntegerSchema } from './integer.js'
3
4
  import { NullableSchema } from './nullable.js'
@@ -1,13 +1,13 @@
1
1
  import { isPlainObject } from '@atproto/lex-data'
2
- import { WithOptionalProperties } from '../core.js'
3
- import { lazyProperty } from '../util/lazy-property.js'
4
2
  import {
5
3
  Infer,
6
4
  Schema,
7
5
  ValidationResult,
8
6
  Validator,
9
7
  ValidatorContext,
10
- } from '../validation.js'
8
+ WithOptionalProperties,
9
+ } from '../core.js'
10
+ import { lazyProperty } from '../util/lazy-property.js'
11
11
 
12
12
  export type ObjectSchemaShape = Record<string, Validator>
13
13
 
@@ -1,3 +1,4 @@
1
+ import { describe, expect, it } from 'vitest'
1
2
  import { BooleanSchema } from './boolean.js'
2
3
  import { IntegerSchema } from './integer.js'
3
4
  import { OptionalSchema } from './optional.js'
@@ -3,7 +3,7 @@ import {
3
3
  ValidationResult,
4
4
  Validator,
5
5
  ValidatorContext,
6
- } from '../validation.js'
6
+ } from '../core.js'
7
7
 
8
8
  export class OptionalSchema<V> extends Schema<V | undefined> {
9
9
  constructor(readonly schema: Validator<V>) {
@@ -1,3 +1,4 @@
1
+ import { describe, expect, it } from 'vitest'
1
2
  import { ArraySchema } from './array.js'
2
3
  import { BooleanSchema } from './boolean.js'
3
4
  import { IntegerSchema } from './integer.js'
@@ -1,13 +1,13 @@
1
1
  import { isPlainObject } from '@atproto/lex-data'
2
- import { WithOptionalProperties } from '../core.js'
3
- import { lazyProperty } from '../util/lazy-property.js'
4
2
  import {
5
3
  Infer,
6
4
  Schema,
7
5
  ValidationResult,
8
6
  Validator,
9
7
  ValidatorContext,
10
- } from '../validation.js'
8
+ WithOptionalProperties,
9
+ } from '../core.js'
10
+ import { lazyProperty } from '../util/lazy-property.js'
11
11
  import { Param, ParamScalar, paramSchema } from './_parameters.js'
12
12
  import { StringSchema } from './string.js'
13
13
 
@@ -1,3 +1,4 @@
1
+ import { describe, expect, it } from 'vitest'
1
2
  import { IntegerSchema } from './integer.js'
2
3
  import { ObjectSchema } from './object.js'
3
4
  import { Payload } from './payload.js'
@@ -1,5 +1,5 @@
1
1
  import { LexValue } from '@atproto/lex-data'
2
- import { Infer, Schema } from '../validation.js'
2
+ import { Infer, Schema } from '../core.js'
3
3
 
4
4
  export type InferPayload<
5
5
  P extends Payload,
@@ -1,3 +1,4 @@
1
+ import { describe, expect, it } from 'vitest'
1
2
  import { asNsidString } from '../core.js'
2
3
  import { PermissionSet } from './permission-set.js'
3
4
  import { Permission } from './permission.js'
@@ -1,3 +1,4 @@
1
+ import { describe, expect, it } from 'vitest'
1
2
  import { Permission } from './permission.js'
2
3
 
3
4
  describe('Permission', () => {
@@ -1,3 +1,4 @@
1
+ import { describe, expect, it } from 'vitest'
1
2
  import { ObjectSchema } from './object.js'
2
3
  import { ParamsSchema } from './params.js'
3
4
  import { Payload } from './payload.js'
@@ -1,3 +1,4 @@
1
+ import { describe, expect, it } from 'vitest'
1
2
  import { asNsidString } from '../core.js'
2
3
  import { IntegerSchema } from './integer.js'
3
4
  import { ObjectSchema } from './object.js'
@@ -1,3 +1,4 @@
1
+ import { describe, expect, it } from 'vitest'
1
2
  import { ObjectSchema } from './object.js'
2
3
  import { RecordSchema } from './record.js'
3
4
  import { StringSchema } from './string.js'
@@ -1,11 +1,14 @@
1
- import { LexiconRecordKey, NsidString, Simplify, TidString } from '../core.js'
2
1
  import {
2
+ $Typed,
3
3
  Infer,
4
+ LexiconRecordKey,
5
+ NsidString,
4
6
  Schema,
7
+ TidString,
5
8
  ValidationResult,
6
9
  Validator,
7
10
  ValidatorContext,
8
- } from '../validation.js'
11
+ } from '../core.js'
9
12
  import { LiteralSchema } from './literal.js'
10
13
  import { StringSchema } from './string.js'
11
14
  import { TypedObject } from './typed-union.js'
@@ -16,7 +19,7 @@ export type InferRecordKey<R extends RecordSchema> =
16
19
  export type RecordSchemaOutput<
17
20
  T extends NsidString,
18
21
  S extends Validator<{ [k: string]: unknown }>,
19
- > = Simplify<{ $type: T } & Omit<Infer<S>, '$type'>>
22
+ > = $Typed<Infer<S>, T>
20
23
 
21
24
  export class RecordSchema<
22
25
  K extends LexiconRecordKey = any,
@@ -36,17 +39,16 @@ export class RecordSchema<
36
39
 
37
40
  isTypeOf<X extends { $type?: unknown }>(
38
41
  value: X,
39
- ): value is Exclude<
40
- X extends { $type?: T } ? X : X & { $type?: T },
41
- TypedObject
42
- > {
42
+ ): value is Exclude<X extends { $type: T } ? X : $Typed<X, T>, TypedObject> {
43
43
  return value.$type === this.$type
44
44
  }
45
45
 
46
46
  build<X extends Omit<Infer<S>, '$type'>>(
47
47
  input: X,
48
- ): Simplify<Omit<X, '$type'> & { $type: T }> {
49
- return { ...input, $type: this.$type }
48
+ ): $Typed<Omit<X, '$type'>, T> {
49
+ return input.$type === this.$type
50
+ ? (input as $Typed<X, T>)
51
+ : { ...input, $type: this.$type }
50
52
  }
51
53
 
52
54
  $isTypeOf<X extends { $type?: unknown }>(value: X) {
@@ -1,4 +1,5 @@
1
- import { Schema } from '../validation.js'
1
+ import { describe, expect, it } from 'vitest'
2
+ import { Schema } from '../core.js'
2
3
  import { IntegerSchema } from './integer.js'
3
4
  import { ObjectSchema } from './object.js'
4
5
  import { OptionalSchema } from './optional.js'
package/src/schema/ref.ts CHANGED
@@ -3,7 +3,7 @@ import {
3
3
  ValidationResult,
4
4
  Validator,
5
5
  ValidatorContext,
6
- } from '../validation.js'
6
+ } from '../core.js'
7
7
 
8
8
  export type RefSchemaGetter<V> = () => Validator<V>
9
9
 
@@ -1,3 +1,4 @@
1
+ import { describe, expect, it } from 'vitest'
1
2
  import { IntegerSchema } from './integer.js'
2
3
  import { ObjectSchema } from './object.js'
3
4
  import { refine } from './refine.js'
@@ -5,7 +5,7 @@ import {
5
5
  ValidationResult,
6
6
  Validator,
7
7
  ValidatorContext,
8
- } from '../validation.js'
8
+ } from '../core.js'
9
9
  import { CustomAssertionContext } from './custom.js'
10
10
 
11
11
  export type RefinementCheck<T> = {
@@ -31,6 +31,23 @@ export type Refinement<T = any, Out extends T = T> =
31
31
  | RefinementCheck<T>
32
32
  | RefinementAssertion<T, Out>
33
33
 
34
+ /**
35
+ * Create a refined schema based on an existing schema and a refinement check.
36
+ *
37
+ * @param schema - The base schema to refine.
38
+ * @param refinement - The refinement check to apply.
39
+ * @returns A new schema that includes the refinement.
40
+ * @example
41
+ *
42
+ * ```ts
43
+ * const PositiveInt = refine(l.integer(), {
44
+ * check: (value) => value > 0,
45
+ * message: 'Value must be a positive integer',
46
+ * })
47
+ * const result = PositiveInt.validate(-5)
48
+ * // result.success === false
49
+ * ```
50
+ */
34
51
  export function refine<S extends Validator, Out extends Infer<S>>(
35
52
  schema: S,
36
53
  refinement: RefinementAssertion<Infer<S>, Out>,
@@ -1,3 +1,4 @@
1
+ import { describe, expect, it } from 'vitest'
1
2
  import { RegexpSchema } from './regexp.js'
2
3
 
3
4
  describe('RegexpSchema', () => {
@@ -1,4 +1,4 @@
1
- import { Schema, ValidationResult, ValidatorContext } from '../validation'
1
+ import { Schema, ValidationResult, ValidatorContext } from '../core.js'
2
2
 
3
3
  export class RegexpSchema<T extends string> extends Schema<T> {
4
4
  constructor(public readonly pattern: RegExp) {
@@ -1,3 +1,4 @@
1
+ import { describe, expect, it } from 'vitest'
1
2
  import { StringSchema } from './string.js'
2
3
  import { TokenSchema } from './token.js'
3
4
 
@@ -1,6 +1,12 @@
1
1
  import { asCid, graphemeLen, utf8Len } from '@atproto/lex-data'
2
- import { InferStringFormat, StringFormat, assertStringFormat } from '../core.js'
3
- import { Schema, ValidationResult, ValidatorContext } from '../validation.js'
2
+ import {
3
+ InferStringFormat,
4
+ Schema,
5
+ StringFormat,
6
+ ValidationResult,
7
+ ValidatorContext,
8
+ assertStringFormat,
9
+ } from '../core.js'
4
10
  import { TokenSchema } from './token.js'
5
11
 
6
12
  export type StringSchemaOptions = {
@@ -1,3 +1,4 @@
1
+ import { describe, expect, it } from 'vitest'
1
2
  import { asNsidString } from '../core.js'
2
3
  import { IntegerSchema } from './integer.js'
3
4
  import { ObjectSchema } from './object.js'
@@ -1,5 +1,4 @@
1
- import { NsidString } from '../core.js'
2
- import { Infer, Schema } from '../validation.js'
1
+ import { Infer, NsidString, Schema } from '../core.js'
3
2
  import { ParamsSchema } from './params.js'
4
3
 
5
4
  export type InferSubscriptionParameters<S extends Subscription> = Infer<
@@ -1,3 +1,4 @@
1
+ import { describe, expect, it } from 'vitest'
1
2
  import { TokenSchema } from './token.js'
2
3
 
3
4
  describe('TokenSchema', () => {
@@ -1,4 +1,4 @@
1
- import { Schema, ValidationResult, ValidatorContext } from '../validation.js'
1
+ import { Schema, ValidationResult, ValidatorContext } from '../core.js'
2
2
 
3
3
  export class TokenSchema<V extends string = any> extends Schema<V> {
4
4
  constructor(protected readonly value: V) {
@@ -1,3 +1,4 @@
1
+ import { describe, expect, it } from 'vitest'
1
2
  import { EnumSchema } from './enum.js'
2
3
  import { IntegerSchema } from './integer.js'
3
4
  import { NullableSchema } from './nullable.js'