@atproto/lex-schema 0.0.4 → 0.0.6

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 (299) hide show
  1. package/CHANGELOG.md +58 -0
  2. package/dist/core/$type.d.ts +7 -0
  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/result.d.ts +7 -6
  8. package/dist/core/result.d.ts.map +1 -1
  9. package/dist/core/result.js +9 -8
  10. package/dist/core/result.js.map +1 -1
  11. package/dist/{validation → core}/schema.d.ts +21 -2
  12. package/dist/core/schema.d.ts.map +1 -0
  13. package/dist/{validation → core}/schema.js +25 -2
  14. package/dist/core/schema.js.map +1 -0
  15. package/dist/core/string-format.d.ts +37 -26
  16. package/dist/core/string-format.d.ts.map +1 -1
  17. package/dist/core/string-format.js +66 -59
  18. package/dist/core/string-format.js.map +1 -1
  19. package/dist/core/types.d.ts +3 -0
  20. package/dist/core/types.d.ts.map +1 -1
  21. package/dist/core/types.js.map +1 -1
  22. package/dist/core/validation-error.d.ts +19 -0
  23. package/dist/core/validation-error.d.ts.map +1 -0
  24. package/dist/{validation → core}/validation-error.js +13 -6
  25. package/dist/core/validation-error.js.map +1 -0
  26. package/dist/{validation → core}/validation-issue.d.ts +43 -0
  27. package/dist/core/validation-issue.d.ts.map +1 -0
  28. package/dist/{validation → core}/validation-issue.js +55 -2
  29. package/dist/core/validation-issue.js.map +1 -0
  30. package/dist/{validation → core}/validator.d.ts +5 -4
  31. package/dist/core/validator.d.ts.map +1 -0
  32. package/dist/{validation → core}/validator.js +16 -13
  33. package/dist/core/validator.js.map +1 -0
  34. package/dist/core.d.ts +5 -0
  35. package/dist/core.d.ts.map +1 -1
  36. package/dist/core.js +5 -0
  37. package/dist/core.js.map +1 -1
  38. package/dist/external.d.ts +21 -21
  39. package/dist/external.d.ts.map +1 -1
  40. package/dist/external.js +39 -55
  41. package/dist/external.js.map +1 -1
  42. package/dist/helpers.d.ts +41 -0
  43. package/dist/helpers.d.ts.map +1 -0
  44. package/dist/helpers.js +13 -0
  45. package/dist/helpers.js.map +1 -0
  46. package/dist/schema/_parameters.d.ts +1 -1
  47. package/dist/schema/_parameters.d.ts.map +1 -1
  48. package/dist/schema/_parameters.js.map +1 -1
  49. package/dist/schema/array.d.ts +1 -1
  50. package/dist/schema/array.d.ts.map +1 -1
  51. package/dist/schema/array.js +2 -2
  52. package/dist/schema/array.js.map +1 -1
  53. package/dist/schema/blob.d.ts +2 -1
  54. package/dist/schema/blob.d.ts.map +1 -1
  55. package/dist/schema/blob.js +34 -20
  56. package/dist/schema/blob.js.map +1 -1
  57. package/dist/schema/boolean.d.ts +1 -1
  58. package/dist/schema/boolean.d.ts.map +1 -1
  59. package/dist/schema/boolean.js +2 -2
  60. package/dist/schema/boolean.js.map +1 -1
  61. package/dist/schema/bytes.d.ts +1 -1
  62. package/dist/schema/bytes.d.ts.map +1 -1
  63. package/dist/schema/bytes.js +2 -2
  64. package/dist/schema/bytes.js.map +1 -1
  65. package/dist/schema/cid.d.ts +1 -1
  66. package/dist/schema/cid.d.ts.map +1 -1
  67. package/dist/schema/cid.js +2 -2
  68. package/dist/schema/cid.js.map +1 -1
  69. package/dist/schema/custom.d.ts +1 -1
  70. package/dist/schema/custom.d.ts.map +1 -1
  71. package/dist/schema/custom.js +3 -3
  72. package/dist/schema/custom.js.map +1 -1
  73. package/dist/schema/dict.d.ts +1 -1
  74. package/dist/schema/dict.d.ts.map +1 -1
  75. package/dist/schema/dict.js +2 -2
  76. package/dist/schema/dict.js.map +1 -1
  77. package/dist/schema/discriminated-union.d.ts +1 -1
  78. package/dist/schema/discriminated-union.d.ts.map +1 -1
  79. package/dist/schema/discriminated-union.js +2 -2
  80. package/dist/schema/discriminated-union.js.map +1 -1
  81. package/dist/schema/enum.d.ts +1 -1
  82. package/dist/schema/enum.d.ts.map +1 -1
  83. package/dist/schema/enum.js +2 -2
  84. package/dist/schema/enum.js.map +1 -1
  85. package/dist/schema/integer.d.ts +1 -1
  86. package/dist/schema/integer.d.ts.map +1 -1
  87. package/dist/schema/integer.js +4 -4
  88. package/dist/schema/integer.js.map +1 -1
  89. package/dist/schema/intersection.d.ts +1 -2
  90. package/dist/schema/intersection.d.ts.map +1 -1
  91. package/dist/schema/intersection.js +2 -2
  92. package/dist/schema/intersection.js.map +1 -1
  93. package/dist/schema/literal.d.ts +1 -1
  94. package/dist/schema/literal.d.ts.map +1 -1
  95. package/dist/schema/literal.js +2 -2
  96. package/dist/schema/literal.js.map +1 -1
  97. package/dist/schema/never.d.ts +1 -1
  98. package/dist/schema/never.d.ts.map +1 -1
  99. package/dist/schema/never.js +2 -2
  100. package/dist/schema/never.js.map +1 -1
  101. package/dist/schema/null.d.ts +1 -1
  102. package/dist/schema/null.d.ts.map +1 -1
  103. package/dist/schema/null.js +2 -2
  104. package/dist/schema/null.js.map +1 -1
  105. package/dist/schema/nullable.d.ts +1 -1
  106. package/dist/schema/nullable.d.ts.map +1 -1
  107. package/dist/schema/nullable.js +2 -2
  108. package/dist/schema/nullable.js.map +1 -1
  109. package/dist/schema/object.d.ts +1 -2
  110. package/dist/schema/object.d.ts.map +1 -1
  111. package/dist/schema/object.js +2 -2
  112. package/dist/schema/object.js.map +1 -1
  113. package/dist/schema/optional.d.ts +1 -1
  114. package/dist/schema/optional.d.ts.map +1 -1
  115. package/dist/schema/optional.js +2 -2
  116. package/dist/schema/optional.js.map +1 -1
  117. package/dist/schema/params.d.ts +1 -3
  118. package/dist/schema/params.d.ts.map +1 -1
  119. package/dist/schema/params.js +2 -2
  120. package/dist/schema/params.js.map +1 -1
  121. package/dist/schema/payload.d.ts +17 -15
  122. package/dist/schema/payload.d.ts.map +1 -1
  123. package/dist/schema/payload.js +28 -0
  124. package/dist/schema/payload.js.map +1 -1
  125. package/dist/schema/procedure.d.ts +3 -6
  126. package/dist/schema/procedure.d.ts.map +1 -1
  127. package/dist/schema/procedure.js +1 -0
  128. package/dist/schema/procedure.js.map +1 -1
  129. package/dist/schema/query.d.ts +3 -5
  130. package/dist/schema/query.d.ts.map +1 -1
  131. package/dist/schema/query.js +1 -0
  132. package/dist/schema/query.js.map +1 -1
  133. package/dist/schema/record.d.ts +14 -14
  134. package/dist/schema/record.d.ts.map +1 -1
  135. package/dist/schema/record.js +2 -2
  136. package/dist/schema/record.js.map +1 -1
  137. package/dist/schema/ref.d.ts +1 -1
  138. package/dist/schema/ref.d.ts.map +1 -1
  139. package/dist/schema/ref.js +2 -2
  140. package/dist/schema/ref.js.map +1 -1
  141. package/dist/schema/refine.d.ts +18 -1
  142. package/dist/schema/refine.d.ts.map +1 -1
  143. package/dist/schema/refine.js +2 -2
  144. package/dist/schema/refine.js.map +1 -1
  145. package/dist/schema/regexp.d.ts +1 -1
  146. package/dist/schema/regexp.d.ts.map +1 -1
  147. package/dist/schema/regexp.js +2 -2
  148. package/dist/schema/regexp.js.map +1 -1
  149. package/dist/schema/string.d.ts +1 -2
  150. package/dist/schema/string.d.ts.map +1 -1
  151. package/dist/schema/string.js +1 -2
  152. package/dist/schema/string.js.map +1 -1
  153. package/dist/schema/subscription.d.ts +4 -8
  154. package/dist/schema/subscription.d.ts.map +1 -1
  155. package/dist/schema/subscription.js.map +1 -1
  156. package/dist/schema/token.d.ts +1 -1
  157. package/dist/schema/token.d.ts.map +1 -1
  158. package/dist/schema/token.js +2 -2
  159. package/dist/schema/token.js.map +1 -1
  160. package/dist/schema/typed-object.d.ts +8 -8
  161. package/dist/schema/typed-object.d.ts.map +1 -1
  162. package/dist/schema/typed-object.js +2 -2
  163. package/dist/schema/typed-object.js.map +1 -1
  164. package/dist/schema/typed-ref.d.ts +1 -1
  165. package/dist/schema/typed-ref.d.ts.map +1 -1
  166. package/dist/schema/typed-ref.js +2 -2
  167. package/dist/schema/typed-ref.js.map +1 -1
  168. package/dist/schema/typed-union.d.ts +2 -3
  169. package/dist/schema/typed-union.d.ts.map +1 -1
  170. package/dist/schema/typed-union.js +2 -2
  171. package/dist/schema/typed-union.js.map +1 -1
  172. package/dist/schema/union.d.ts +1 -1
  173. package/dist/schema/union.d.ts.map +1 -1
  174. package/dist/schema/union.js +3 -6
  175. package/dist/schema/union.js.map +1 -1
  176. package/dist/schema/unknown-object.d.ts +1 -2
  177. package/dist/schema/unknown-object.d.ts.map +1 -1
  178. package/dist/schema/unknown-object.js +2 -2
  179. package/dist/schema/unknown-object.js.map +1 -1
  180. package/dist/schema/unknown.d.ts +1 -1
  181. package/dist/schema/unknown.d.ts.map +1 -1
  182. package/dist/schema/unknown.js +2 -2
  183. package/dist/schema/unknown.js.map +1 -1
  184. package/dist/util/assertion-util.d.ts +8 -0
  185. package/dist/util/assertion-util.d.ts.map +1 -0
  186. package/dist/util/assertion-util.js +31 -0
  187. package/dist/util/assertion-util.js.map +1 -0
  188. package/dist/util/memoize.d.ts +3 -0
  189. package/dist/util/memoize.d.ts.map +1 -0
  190. package/dist/util/memoize.js +52 -0
  191. package/dist/util/memoize.js.map +1 -0
  192. package/package.json +6 -6
  193. package/src/core/$type.ts +4 -0
  194. package/src/core/result.ts +9 -8
  195. package/src/{validation → core}/schema.ts +29 -4
  196. package/src/core/string-format.ts +88 -68
  197. package/src/core/types.ts +4 -0
  198. package/src/{validation → core}/validation-error.ts +14 -6
  199. package/src/{validation → core}/validation-issue.ts +64 -2
  200. package/src/{validation → core}/validator.ts +17 -13
  201. package/src/core.ts +5 -0
  202. package/src/external.ts +75 -55
  203. package/src/helpers.test.ts +487 -0
  204. package/src/helpers.ts +75 -0
  205. package/src/schema/_parameters.test.ts +1 -0
  206. package/src/schema/_parameters.ts +1 -1
  207. package/src/schema/array.test.ts +1 -0
  208. package/src/schema/array.ts +1 -1
  209. package/src/schema/blob.test.ts +3 -4
  210. package/src/schema/blob.ts +32 -24
  211. package/src/schema/boolean.test.ts +1 -0
  212. package/src/schema/boolean.ts +1 -1
  213. package/src/schema/bytes.test.ts +1 -0
  214. package/src/schema/bytes.ts +1 -1
  215. package/src/schema/cid.test.ts +1 -0
  216. package/src/schema/cid.ts +1 -1
  217. package/src/schema/custom.test.ts +8 -7
  218. package/src/schema/custom.ts +2 -2
  219. package/src/schema/dict.test.ts +1 -0
  220. package/src/schema/dict.ts +1 -1
  221. package/src/schema/discriminated-union.test.ts +1 -0
  222. package/src/schema/discriminated-union.ts +1 -1
  223. package/src/schema/enum.test.ts +1 -0
  224. package/src/schema/enum.ts +1 -1
  225. package/src/schema/integer.test.ts +1 -0
  226. package/src/schema/integer.ts +3 -3
  227. package/src/schema/intersection.test.ts +1 -0
  228. package/src/schema/intersection.ts +2 -2
  229. package/src/schema/literal.test.ts +1 -0
  230. package/src/schema/literal.ts +1 -1
  231. package/src/schema/never.test.ts +1 -0
  232. package/src/schema/never.ts +1 -1
  233. package/src/schema/null.test.ts +1 -0
  234. package/src/schema/null.ts +1 -1
  235. package/src/schema/nullable.test.ts +1 -0
  236. package/src/schema/nullable.ts +1 -1
  237. package/src/schema/object.test.ts +1 -0
  238. package/src/schema/object.ts +3 -3
  239. package/src/schema/optional.test.ts +1 -0
  240. package/src/schema/optional.ts +1 -1
  241. package/src/schema/params.test.ts +1 -0
  242. package/src/schema/params.ts +3 -10
  243. package/src/schema/payload.test.ts +1 -0
  244. package/src/schema/payload.ts +67 -34
  245. package/src/schema/permission-set.test.ts +37 -36
  246. package/src/schema/permission.test.ts +1 -0
  247. package/src/schema/procedure.test.ts +2 -62
  248. package/src/schema/procedure.ts +8 -20
  249. package/src/schema/query.test.ts +23 -69
  250. package/src/schema/query.ts +7 -14
  251. package/src/schema/record.test.ts +1 -0
  252. package/src/schema/record.ts +13 -6
  253. package/src/schema/ref.test.ts +2 -1
  254. package/src/schema/ref.ts +1 -1
  255. package/src/schema/refine.test.ts +1 -0
  256. package/src/schema/refine.ts +19 -2
  257. package/src/schema/regexp.test.ts +1 -0
  258. package/src/schema/regexp.ts +1 -1
  259. package/src/schema/string.test.ts +1 -0
  260. package/src/schema/string.ts +8 -2
  261. package/src/schema/subscription.test.ts +31 -93
  262. package/src/schema/subscription.ts +11 -25
  263. package/src/schema/token.test.ts +1 -0
  264. package/src/schema/token.ts +1 -1
  265. package/src/schema/typed-object.test.ts +1 -0
  266. package/src/schema/typed-object.ts +10 -5
  267. package/src/schema/typed-ref.test.ts +1 -0
  268. package/src/schema/typed-ref.ts +1 -1
  269. package/src/schema/typed-union.test.ts +1 -0
  270. package/src/schema/typed-union.ts +4 -4
  271. package/src/schema/union.test.ts +1 -0
  272. package/src/schema/union.ts +2 -5
  273. package/src/schema/unknown-object.test.ts +1 -0
  274. package/src/schema/unknown-object.ts +1 -2
  275. package/src/schema/unknown.test.ts +1 -0
  276. package/src/schema/unknown.ts +1 -1
  277. package/src/util/array-agg.test.ts +1 -0
  278. package/src/util/assertion-util.ts +40 -0
  279. package/src/util/memoize.ts +57 -0
  280. package/tsconfig.tests.json +2 -2
  281. package/dist/validation/property-key.d.ts.map +0 -1
  282. package/dist/validation/property-key.js.map +0 -1
  283. package/dist/validation/schema.d.ts.map +0 -1
  284. package/dist/validation/schema.js.map +0 -1
  285. package/dist/validation/validation-error.d.ts +0 -9
  286. package/dist/validation/validation-error.d.ts.map +0 -1
  287. package/dist/validation/validation-error.js.map +0 -1
  288. package/dist/validation/validation-issue.d.ts.map +0 -1
  289. package/dist/validation/validation-issue.js.map +0 -1
  290. package/dist/validation/validator.d.ts.map +0 -1
  291. package/dist/validation/validator.js.map +0 -1
  292. package/dist/validation.d.ts +0 -6
  293. package/dist/validation.d.ts.map +0 -1
  294. package/dist/validation.js +0 -9
  295. package/dist/validation.js.map +0 -1
  296. package/src/validation.ts +0 -5
  297. /package/dist/{validation → core}/property-key.d.ts +0 -0
  298. /package/dist/{validation → core}/property-key.js +0 -0
  299. /package/src/{validation → core}/property-key.ts +0 -0
@@ -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', () => {
@@ -32,7 +33,7 @@ describe('CustomSchema', () => {
32
33
  const result = schema.safeParse(123)
33
34
  expect(result.success).toBe(false)
34
35
  if (!result.success) {
35
- expect(result.error.message).toContain('Custom error message')
36
+ expect(result.reason.message).toContain('Custom error message')
36
37
  }
37
38
  })
38
39
  })
@@ -143,7 +144,7 @@ describe('CustomSchema', () => {
143
144
  const result = schema.safeParse(123)
144
145
  expect(result.success).toBe(false)
145
146
  if (!result.success) {
146
- expect(result.error.message).toContain('customField')
147
+ expect(result.reason.message).toContain('customField')
147
148
  }
148
149
  })
149
150
 
@@ -157,8 +158,8 @@ describe('CustomSchema', () => {
157
158
  const result = schema.safeParse(123)
158
159
  expect(result.success).toBe(false)
159
160
  if (!result.success) {
160
- expect(result.error.message).toContain('nested')
161
- expect(result.error.message).toContain('field')
161
+ expect(result.reason.message).toContain('nested')
162
+ expect(result.reason.message).toContain('field')
162
163
  }
163
164
  })
164
165
  })
@@ -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 {
@@ -392,7 +393,7 @@ describe('CustomSchema', () => {
392
393
 
393
394
  expect(schema.safeParse('test')).toMatchObject({
394
395
  success: false,
395
- error: {
396
+ reason: {
396
397
  issues: [
397
398
  { message: 'This is a custom issue' },
398
399
  { message: 'Must be a 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[]
@@ -33,6 +33,6 @@ export class CustomSchema<T = unknown> extends Schema<T> {
33
33
  ): ValidationResult<T> {
34
34
  if (this.assertion.call(null, input, ctx)) return ctx.success(input as T)
35
35
  const path = ctx.concatPath(this.path)
36
- return ctx.failure(new IssueCustom(path, input, this.message))
36
+ return ctx.issue(new IssueCustom(path, input, this.message))
37
37
  }
38
38
  }
@@ -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)
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
 
@@ -18,13 +18,6 @@ export type ParamsSchemaOutput<Shape extends ParamsSchemaShape> =
18
18
  [K in keyof Shape]: Infer<Shape[K]>
19
19
  }>
20
20
 
21
- export type InferParamsSchema<T> =
22
- T extends ParamsSchema<infer P>
23
- ? NonNullable<unknown> extends ParamsSchemaOutput<P>
24
- ? ParamsSchemaOutput<P> | undefined // Allow undefined if all params are optional
25
- : ParamsSchemaOutput<P>
26
- : never
27
-
28
21
  export class ParamsSchema<
29
22
  const Shape extends ParamsSchemaShape = ParamsSchemaShape,
30
23
  > extends Schema<ParamsSchemaOutput<Shape>> {
@@ -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,53 +1,86 @@
1
1
  import { LexValue } from '@atproto/lex-data'
2
- import { Infer, Validator } from '../validation.js'
2
+ import { Infer, Schema } from '../core.js'
3
3
 
4
- export type LexBody<E extends string = any> = E extends `text/${string}`
5
- ? string // Text encodings always yield string bodies
6
- : E extends 'application/json'
7
- ? LexValue
8
- : Uint8Array
4
+ export type InferPayload<
5
+ P extends Payload,
6
+ B,
7
+ > = P['encoding'] extends infer E extends string
8
+ ? {
9
+ encoding: SchemaEncodingToDataEncoding<E>
10
+ body: InferPayloadBody<P, B>
11
+ }
12
+ : void | undefined
13
+
14
+ export type SchemaEncodingToDataEncoding<E extends string> = E extends '*/*'
15
+ ? `${string}/${string}`
16
+ : E extends `${infer T extends string}/*`
17
+ ? `${T}/${string}`
18
+ : E
9
19
 
10
20
  export type InferPayloadEncoding<P extends Payload> =
11
- P extends Payload<infer E, any> ? E : undefined
12
-
13
- export type InferPayloadBody<P extends Payload> =
14
- P extends Payload<any, infer S>
15
- ? S extends Validator
16
- ? Infer<S>
17
- : P extends Payload<infer E extends string>
18
- ? LexBody<E>
19
- : undefined
21
+ P['encoding'] extends string
22
+ ? SchemaEncodingToDataEncoding<P['encoding']>
20
23
  : undefined
21
24
 
22
- export type PayloadOutput<
23
- E extends string | undefined = any,
24
- S extends Validator | undefined = any,
25
- > = E extends string
26
- ? S extends Validator
27
- ? {
28
- encoding: E
29
- body: Infer<S>
30
- }
31
- : {
32
- encoding: E
33
- body: LexBody<E>
34
- }
35
- : void
36
-
37
- export type PayloadBody<E extends string | undefined> = E extends undefined
25
+ export type InferPayloadBody<
26
+ P extends Payload,
27
+ B,
28
+ > = P['encoding'] extends undefined
29
+ ? undefined // No encoding, no payload
30
+ : P['schema'] extends Schema
31
+ ? Infer<P['schema']>
32
+ : P['encoding'] extends `application/json`
33
+ ? LexValue
34
+ : B
35
+
36
+ export type PayloadSchema<E extends string | undefined> = E extends undefined
38
37
  ? undefined
39
- : Validator | undefined
38
+ : Schema | undefined
40
39
 
41
40
  export class Payload<
42
41
  const Encoding extends string | undefined = string | undefined,
43
- const Body extends PayloadBody<Encoding> = PayloadBody<Encoding>,
42
+ const Schema extends PayloadSchema<Encoding> = PayloadSchema<Encoding>,
44
43
  > {
45
44
  constructor(
46
45
  readonly encoding: Encoding,
47
- readonly schema: Body,
46
+ readonly schema: Schema,
48
47
  ) {
49
48
  if (encoding === undefined && schema !== undefined) {
50
49
  throw new TypeError('schema cannot be defined when encoding is undefined')
51
50
  }
52
51
  }
52
+
53
+ /**
54
+ * Checks whether the given content-type matches the expected payload schema's
55
+ * encoding.
56
+ */
57
+ matchesEncoding(contentType: string | undefined): boolean {
58
+ const mime = contentType?.split(';', 1)[0].trim()
59
+
60
+ const { encoding } = this
61
+
62
+ // Handle undefined cases
63
+ if (encoding === undefined) {
64
+ // Expecting no body
65
+ return mime === undefined
66
+ } else if (mime === undefined) {
67
+ // Expecting a body, but got no content-type
68
+ return false
69
+ }
70
+
71
+ if (encoding === '*/*') {
72
+ return true
73
+ }
74
+
75
+ if (encoding.endsWith('/*')) {
76
+ return mime.startsWith(encoding.slice(0, -1))
77
+ }
78
+
79
+ // Invalid: Lexicon can only specify "*/*" or "type/*" wildcards
80
+ if (encoding.includes('*')) {
81
+ return false
82
+ }
83
+
84
+ return encoding === mime
85
+ }
53
86
  }