arktype 2.0.0-dev.9 → 2.0.0-rc.1

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 (263) hide show
  1. package/LICENSE +7 -0
  2. package/out/config.d.ts +25 -0
  3. package/out/config.js +2 -0
  4. package/out/generic.d.ts +157 -18
  5. package/out/generic.js +37 -23
  6. package/out/index.d.ts +7 -0
  7. package/out/index.js +7 -0
  8. package/out/intersect.d.ts +28 -0
  9. package/out/intersect.js +1 -0
  10. package/out/keys.d.ts +16 -0
  11. package/out/keys.js +1 -0
  12. package/out/keywords/ark.d.ts +39 -0
  13. package/out/keywords/ark.js +24 -0
  14. package/out/keywords/ast.d.ts +193 -0
  15. package/out/keywords/ast.js +2 -0
  16. package/out/keywords/builtins.d.ts +17 -0
  17. package/out/keywords/builtins.js +10 -0
  18. package/out/keywords/constructors/Array.d.ts +20 -0
  19. package/out/keywords/constructors/Array.js +19 -0
  20. package/out/keywords/constructors/FormData.d.ts +15 -0
  21. package/out/keywords/constructors/FormData.js +38 -0
  22. package/out/keywords/constructors/TypedArray.d.ts +19 -0
  23. package/out/keywords/constructors/TypedArray.js +14 -0
  24. package/out/keywords/constructors/constructors.d.ts +31 -0
  25. package/out/keywords/constructors/constructors.js +16 -0
  26. package/out/keywords/number/epoch.d.ts +10 -0
  27. package/out/keywords/number/epoch.js +26 -0
  28. package/out/keywords/number/integer.d.ts +3 -0
  29. package/out/keywords/number/integer.js +5 -0
  30. package/out/keywords/number/number.d.ts +28 -0
  31. package/out/keywords/number/number.js +9 -0
  32. package/out/keywords/string/alpha.d.ts +7 -0
  33. package/out/keywords/string/alpha.js +2 -0
  34. package/out/keywords/string/alphanumeric.d.ts +7 -0
  35. package/out/keywords/string/alphanumeric.js +2 -0
  36. package/out/keywords/string/capitalize.d.ts +15 -0
  37. package/out/keywords/string/capitalize.js +12 -0
  38. package/out/keywords/string/creditCard.d.ts +8 -0
  39. package/out/keywords/string/creditCard.js +37 -0
  40. package/out/keywords/string/date.d.ts +59 -0
  41. package/out/keywords/string/date.js +98 -0
  42. package/out/keywords/string/digits.d.ts +7 -0
  43. package/out/keywords/string/digits.js +2 -0
  44. package/out/keywords/string/email.d.ts +7 -0
  45. package/out/keywords/string/email.js +4 -0
  46. package/out/keywords/string/integer.d.ts +16 -0
  47. package/out/keywords/string/integer.js +16 -0
  48. package/out/keywords/string/ip.d.ts +20 -0
  49. package/out/keywords/string/ip.js +23 -0
  50. package/out/keywords/string/json.d.ts +15 -0
  51. package/out/keywords/string/json.js +45 -0
  52. package/out/keywords/string/lower.d.ts +15 -0
  53. package/out/keywords/string/lower.js +12 -0
  54. package/out/keywords/string/normalize.d.ts +92 -0
  55. package/out/keywords/string/normalize.js +43 -0
  56. package/out/keywords/string/numeric.d.ts +15 -0
  57. package/out/keywords/string/numeric.js +13 -0
  58. package/out/keywords/string/semver.d.ts +7 -0
  59. package/out/keywords/string/semver.js +4 -0
  60. package/out/keywords/string/string.d.ts +112 -0
  61. package/out/keywords/string/string.js +41 -0
  62. package/out/keywords/string/trim.d.ts +15 -0
  63. package/out/keywords/string/trim.js +14 -0
  64. package/out/keywords/string/upper.d.ts +15 -0
  65. package/out/keywords/string/upper.js +12 -0
  66. package/out/keywords/string/url.d.ts +15 -0
  67. package/out/keywords/string/url.js +37 -0
  68. package/out/keywords/string/utils.d.ts +2 -0
  69. package/out/keywords/string/utils.js +10 -0
  70. package/out/keywords/string/uuid.d.ts +42 -0
  71. package/out/keywords/string/uuid.js +19 -0
  72. package/out/keywords/ts.d.ts +64 -0
  73. package/out/keywords/ts.js +53 -0
  74. package/out/keywords/utils.d.ts +1 -0
  75. package/out/keywords/utils.js +2 -0
  76. package/out/module.d.ts +21 -9
  77. package/out/module.js +3 -3
  78. package/out/parser/definition.d.ts +18 -15
  79. package/out/parser/definition.js +5 -5
  80. package/out/parser/objectLiteral.d.ts +65 -35
  81. package/out/parser/objectLiteral.js +75 -50
  82. package/out/parser/semantic/bounds.d.ts +12 -9
  83. package/out/parser/semantic/default.d.ts +9 -0
  84. package/out/parser/semantic/default.js +1 -0
  85. package/out/parser/semantic/divisor.d.ts +5 -5
  86. package/out/parser/semantic/divisor.js +1 -0
  87. package/out/parser/semantic/infer.d.ts +29 -14
  88. package/out/parser/semantic/infer.js +1 -0
  89. package/out/parser/semantic/keyof.d.ts +7 -0
  90. package/out/parser/semantic/keyof.js +1 -0
  91. package/out/parser/semantic/utils.d.ts +4 -4
  92. package/out/parser/semantic/utils.js +1 -0
  93. package/out/parser/semantic/validate.d.ts +19 -18
  94. package/out/parser/semantic/validate.js +0 -1
  95. package/out/parser/string/reduce/dynamic.d.ts +16 -14
  96. package/out/parser/string/reduce/dynamic.js +8 -7
  97. package/out/parser/string/reduce/shared.d.ts +15 -15
  98. package/out/parser/string/reduce/shared.js +4 -2
  99. package/out/parser/string/reduce/static.d.ts +22 -23
  100. package/out/parser/string/reduce/static.js +1 -0
  101. package/out/parser/string/shift/operand/date.d.ts +2 -4
  102. package/out/parser/string/shift/operand/date.js +1 -1
  103. package/out/parser/string/shift/operand/enclosed.d.ts +13 -7
  104. package/out/parser/string/shift/operand/enclosed.js +5 -4
  105. package/out/parser/string/shift/operand/genericArgs.d.ts +13 -13
  106. package/out/parser/string/shift/operand/genericArgs.js +7 -12
  107. package/out/parser/string/shift/operand/operand.d.ts +8 -7
  108. package/out/parser/string/shift/operand/operand.js +5 -6
  109. package/out/parser/string/shift/operand/unenclosed.d.ts +28 -16
  110. package/out/parser/string/shift/operand/unenclosed.js +9 -12
  111. package/out/parser/string/shift/operator/bounds.d.ts +18 -22
  112. package/out/parser/string/shift/operator/bounds.js +11 -16
  113. package/out/parser/string/shift/operator/default.d.ts +19 -0
  114. package/out/parser/string/shift/operator/default.js +17 -0
  115. package/out/parser/string/shift/operator/divisor.d.ts +3 -3
  116. package/out/parser/string/shift/operator/divisor.js +1 -1
  117. package/out/parser/string/shift/operator/operator.d.ts +7 -6
  118. package/out/parser/string/shift/operator/operator.js +2 -2
  119. package/out/parser/string/shift/scanner.d.ts +29 -31
  120. package/out/parser/string/shift/scanner.js +24 -21
  121. package/out/parser/string/string.d.ts +17 -13
  122. package/out/parser/string/string.js +5 -2
  123. package/out/parser/tuple.d.ts +21 -19
  124. package/out/parser/tuple.js +12 -23
  125. package/out/scope.d.ts +116 -55
  126. package/out/scope.js +73 -59
  127. package/out/subtypes/array.d.ts +13 -0
  128. package/out/subtypes/array.js +1 -0
  129. package/out/subtypes/base.d.ts +85 -0
  130. package/out/subtypes/base.js +1 -0
  131. package/out/subtypes/date.d.ts +11 -0
  132. package/out/subtypes/date.js +1 -0
  133. package/out/subtypes/instantiate.d.ts +16 -0
  134. package/out/subtypes/instantiate.js +1 -0
  135. package/out/subtypes/morph.d.ts +13 -0
  136. package/out/subtypes/morph.js +1 -0
  137. package/out/subtypes/number.d.ts +12 -0
  138. package/out/subtypes/number.js +1 -0
  139. package/out/subtypes/object.d.ts +33 -0
  140. package/out/subtypes/object.js +1 -0
  141. package/out/subtypes/string.d.ts +13 -0
  142. package/out/subtypes/string.js +1 -0
  143. package/out/subtypes/validator.d.ts +11 -0
  144. package/out/subtypes/validator.js +1 -0
  145. package/out/type.d.ts +32 -49
  146. package/out/type.js +24 -14
  147. package/package.json +46 -45
  148. package/__tests__/array.test.ts +0 -391
  149. package/__tests__/badDefinitionType.test.ts +0 -58
  150. package/__tests__/basis.test.ts +0 -50
  151. package/__tests__/bounds.test.ts +0 -318
  152. package/__tests__/cast.test.ts +0 -37
  153. package/__tests__/completions.test.ts +0 -51
  154. package/__tests__/config.test.ts +0 -96
  155. package/__tests__/dateLiteral.test.ts +0 -36
  156. package/__tests__/declared.test.ts +0 -124
  157. package/__tests__/discrimination.test.ts +0 -74
  158. package/__tests__/divisor.test.ts +0 -114
  159. package/__tests__/enclosed.test.ts +0 -107
  160. package/__tests__/examples.test.ts +0 -56
  161. package/__tests__/expressions.test.ts +0 -210
  162. package/__tests__/generateBenchData.ts +0 -89
  163. package/__tests__/generated/cyclic.ts +0 -3058
  164. package/__tests__/generic.test.ts +0 -313
  165. package/__tests__/group.test.ts +0 -85
  166. package/__tests__/hkt.test.ts +0 -22
  167. package/__tests__/imports.test.ts +0 -126
  168. package/__tests__/instanceof.test.ts +0 -104
  169. package/__tests__/intersection.test.ts +0 -108
  170. package/__tests__/keyTraversal.test.ts +0 -77
  171. package/__tests__/keyof.test.ts +0 -150
  172. package/__tests__/keywords.test.ts +0 -214
  173. package/__tests__/literal.test.ts +0 -90
  174. package/__tests__/match.bench.ts +0 -31
  175. package/__tests__/match.test.ts +0 -234
  176. package/__tests__/methods.bench.ts +0 -22
  177. package/__tests__/narrow.test.ts +0 -100
  178. package/__tests__/object.bench.ts +0 -28
  179. package/__tests__/objectLiteral.test.ts +0 -442
  180. package/__tests__/operand.bench.ts +0 -45
  181. package/__tests__/operator.bench.ts +0 -130
  182. package/__tests__/pipe.test.ts +0 -501
  183. package/__tests__/realWorld.test.ts +0 -63
  184. package/__tests__/regex.test.ts +0 -68
  185. package/__tests__/scope.bench.ts +0 -39
  186. package/__tests__/scope.test.ts +0 -374
  187. package/__tests__/serialization.test.ts +0 -19
  188. package/__tests__/string.test.ts +0 -52
  189. package/__tests__/submodule.test.ts +0 -86
  190. package/__tests__/this.test.ts +0 -83
  191. package/__tests__/thunk.test.ts +0 -86
  192. package/__tests__/traverse.test.ts +0 -141
  193. package/__tests__/tsGenerics.test.ts +0 -12
  194. package/__tests__/type.test.ts +0 -36
  195. package/__tests__/typeReference.test.ts +0 -39
  196. package/__tests__/unenclosed.test.ts +0 -136
  197. package/__tests__/union.test.ts +0 -214
  198. package/ark.ts +0 -47
  199. package/generic.ts +0 -69
  200. package/main.ts +0 -6
  201. package/match.ts +0 -218
  202. package/module.ts +0 -15
  203. package/out/ark.d.ts +0 -28
  204. package/out/ark.js +0 -10
  205. package/out/ark.js.map +0 -1
  206. package/out/generic.js.map +0 -1
  207. package/out/main.d.ts +0 -6
  208. package/out/main.js +0 -5
  209. package/out/main.js.map +0 -1
  210. package/out/match.d.ts +0 -71
  211. package/out/match.js +0 -52
  212. package/out/match.js.map +0 -1
  213. package/out/module.js.map +0 -1
  214. package/out/parser/definition.js.map +0 -1
  215. package/out/parser/generic.d.ts +0 -16
  216. package/out/parser/generic.js +0 -15
  217. package/out/parser/generic.js.map +0 -1
  218. package/out/parser/objectLiteral.js.map +0 -1
  219. package/out/parser/semantic/bounds.js.map +0 -1
  220. package/out/parser/semantic/divisor.js.map +0 -1
  221. package/out/parser/semantic/infer.js.map +0 -1
  222. package/out/parser/semantic/utils.js.map +0 -1
  223. package/out/parser/semantic/validate.js.map +0 -1
  224. package/out/parser/string/reduce/dynamic.js.map +0 -1
  225. package/out/parser/string/reduce/shared.js.map +0 -1
  226. package/out/parser/string/reduce/static.js.map +0 -1
  227. package/out/parser/string/shift/operand/date.js.map +0 -1
  228. package/out/parser/string/shift/operand/enclosed.js.map +0 -1
  229. package/out/parser/string/shift/operand/genericArgs.js.map +0 -1
  230. package/out/parser/string/shift/operand/operand.js.map +0 -1
  231. package/out/parser/string/shift/operand/unenclosed.js.map +0 -1
  232. package/out/parser/string/shift/operator/bounds.js.map +0 -1
  233. package/out/parser/string/shift/operator/divisor.js.map +0 -1
  234. package/out/parser/string/shift/operator/operator.js.map +0 -1
  235. package/out/parser/string/shift/scanner.js.map +0 -1
  236. package/out/parser/string/string.js.map +0 -1
  237. package/out/parser/tuple.js.map +0 -1
  238. package/out/scope.js.map +0 -1
  239. package/out/type.js.map +0 -1
  240. package/parser/definition.ts +0 -143
  241. package/parser/generic.ts +0 -66
  242. package/parser/objectLiteral.ts +0 -233
  243. package/parser/semantic/bounds.ts +0 -62
  244. package/parser/semantic/divisor.ts +0 -11
  245. package/parser/semantic/infer.ts +0 -141
  246. package/parser/semantic/utils.ts +0 -15
  247. package/parser/semantic/validate.ts +0 -117
  248. package/parser/string/reduce/dynamic.ts +0 -208
  249. package/parser/string/reduce/shared.ts +0 -99
  250. package/parser/string/reduce/static.ts +0 -296
  251. package/parser/string/shift/operand/date.ts +0 -55
  252. package/parser/string/shift/operand/enclosed.ts +0 -123
  253. package/parser/string/shift/operand/genericArgs.ts +0 -128
  254. package/parser/string/shift/operand/operand.ts +0 -42
  255. package/parser/string/shift/operand/unenclosed.ts +0 -217
  256. package/parser/string/shift/operator/bounds.ts +0 -270
  257. package/parser/string/shift/operator/divisor.ts +0 -33
  258. package/parser/string/shift/operator/operator.ts +0 -71
  259. package/parser/string/shift/scanner.ts +0 -210
  260. package/parser/string/string.ts +0 -85
  261. package/parser/tuple.ts +0 -542
  262. package/scope.ts +0 -291
  263. package/type.ts +0 -266
@@ -0,0 +1,64 @@
1
+ import { Hkt, type Key, type omit, type pick, type show } from "@ark/util";
2
+ import type { Module, Submodule } from "../module.ts";
3
+ export declare const arkTsKeywords: arkTsKeywords;
4
+ export type arkTsKeywords = Module<arkTsKeywords.$>;
5
+ export declare namespace arkTsKeywords {
6
+ type submodule = Submodule<$>;
7
+ type $ = {
8
+ bigint: bigint;
9
+ boolean: boolean;
10
+ false: false;
11
+ never: never;
12
+ null: null;
13
+ number: number;
14
+ object: object;
15
+ string: string;
16
+ symbol: symbol;
17
+ true: true;
18
+ unknown: unknown;
19
+ undefined: undefined;
20
+ };
21
+ }
22
+ declare class RecordHkt extends Hkt<[Key, unknown]> {
23
+ body: Record<this[0], this[1]>;
24
+ }
25
+ declare const Record: import("@ark/schema").GenericRoot<readonly [["K", Key], ["V", unknown]], RecordHkt>;
26
+ declare class PickHkt extends Hkt<[object, Key]> {
27
+ body: pick<this[0], this[1] & keyof this[0]>;
28
+ }
29
+ declare const Pick: import("@ark/schema").GenericRoot<readonly [["T", object], ["K", Key]], PickHkt>;
30
+ declare class OmitHkt extends Hkt<[object, Key]> {
31
+ body: omit<this[0], this[1] & keyof this[0]>;
32
+ }
33
+ declare const Omit: import("@ark/schema").GenericRoot<readonly [["T", object], ["K", Key]], OmitHkt>;
34
+ declare class PartialHkt extends Hkt<[object]> {
35
+ body: show<Partial<this[0]>>;
36
+ }
37
+ declare const Partial: import("@ark/schema").GenericRoot<readonly [["T", object]], PartialHkt>;
38
+ declare class RequiredHkt extends Hkt<[object]> {
39
+ body: show<Required<this[0]>>;
40
+ }
41
+ declare const Required: import("@ark/schema").GenericRoot<readonly [["T", object]], RequiredHkt>;
42
+ declare class ExcludeHkt extends Hkt<[unknown, unknown]> {
43
+ body: Exclude<this[0], this[1]>;
44
+ }
45
+ declare const Exclude: import("@ark/schema").GenericRoot<readonly [["T", unknown], ["U", unknown]], ExcludeHkt>;
46
+ declare class ExtractHkt extends Hkt<[unknown, unknown]> {
47
+ body: Extract<this[0], this[1]>;
48
+ }
49
+ declare const Extract: import("@ark/schema").GenericRoot<readonly [["T", unknown], ["U", unknown]], ExtractHkt>;
50
+ export declare const arkTsGenerics: arkTsGenerics.module;
51
+ export declare namespace arkTsGenerics {
52
+ type module = Module<arkTsGenerics.$>;
53
+ type submodule = Submodule<$>;
54
+ type $ = {
55
+ Record: typeof Record.t;
56
+ Pick: typeof Pick.t;
57
+ Omit: typeof Omit.t;
58
+ Exclude: typeof Exclude.t;
59
+ Extract: typeof Extract.t;
60
+ Partial: typeof Partial.t;
61
+ Required: typeof Required.t;
62
+ };
63
+ }
64
+ export {};
@@ -0,0 +1,53 @@
1
+ import { genericNode, intrinsic } from "@ark/schema";
2
+ import { Hkt } from "@ark/util";
3
+ import { submodule } from "./utils.js";
4
+ export const arkTsKeywords = submodule({
5
+ bigint: intrinsic.bigint,
6
+ boolean: intrinsic.boolean,
7
+ false: intrinsic.false,
8
+ never: intrinsic.never,
9
+ null: intrinsic.null,
10
+ number: intrinsic.number,
11
+ object: intrinsic.object,
12
+ string: intrinsic.string,
13
+ symbol: intrinsic.symbol,
14
+ true: intrinsic.true,
15
+ unknown: intrinsic.unknown,
16
+ undefined: intrinsic.undefined
17
+ });
18
+ class RecordHkt extends Hkt {
19
+ }
20
+ const Record = genericNode(["K", intrinsic.key], "V")(args => ({
21
+ domain: "object",
22
+ index: {
23
+ signature: args.K,
24
+ value: args.V
25
+ }
26
+ }), RecordHkt);
27
+ class PickHkt extends Hkt {
28
+ }
29
+ const Pick = genericNode(["T", intrinsic.object], ["K", intrinsic.key])(args => args.T.pick(args.K), PickHkt);
30
+ class OmitHkt extends Hkt {
31
+ }
32
+ const Omit = genericNode(["T", intrinsic.object], ["K", intrinsic.key])(args => args.T.omit(args.K), OmitHkt);
33
+ class PartialHkt extends Hkt {
34
+ }
35
+ const Partial = genericNode(["T", intrinsic.object])(args => args.T.partial(), PartialHkt);
36
+ class RequiredHkt extends Hkt {
37
+ }
38
+ const Required = genericNode(["T", intrinsic.object])(args => args.T.required(), RequiredHkt);
39
+ class ExcludeHkt extends Hkt {
40
+ }
41
+ const Exclude = genericNode("T", "U")(args => args.T.exclude(args.U), ExcludeHkt);
42
+ class ExtractHkt extends Hkt {
43
+ }
44
+ const Extract = genericNode("T", "U")(args => args.T.extract(args.U), ExtractHkt);
45
+ export const arkTsGenerics = submodule({
46
+ Record,
47
+ Pick,
48
+ Omit,
49
+ Exclude,
50
+ Extract,
51
+ Partial,
52
+ Required
53
+ });
@@ -0,0 +1 @@
1
+ export declare const submodule: import("../scope.ts").ModuleParser;
@@ -0,0 +1,2 @@
1
+ import { module } from "../scope.js";
2
+ export const submodule = module;
package/out/module.d.ts CHANGED
@@ -1,10 +1,22 @@
1
- import type { PreparsedNodeResolution, arkKind } from "@arktype/schema";
2
- import { DynamicBase, type isAnyOrNever } from "@arktype/util";
3
- import type { Type } from "./type.js";
4
- type exportScope<$> = {
5
- [k in keyof $]: $[k] extends PreparsedNodeResolution ? isAnyOrNever<$[k]> extends true ? Type<$[k], $> : $[k] : Type<$[k], $>;
6
- };
7
- export declare class Module<$ = any> extends DynamicBase<exportScope<$>> {
8
- readonly [arkKind]: "module";
1
+ import { RootModule, type GenericAst, type PreparsedNodeResolution } from "@ark/schema";
2
+ import type { anyOrNever, inferred } from "@ark/util";
3
+ import type { Generic } from "./generic.ts";
4
+ import type { Type } from "./type.ts";
5
+ export declare const Module: new <$ extends {}>(exports: exportScope<$>) => Module<$>;
6
+ export interface Module<$ extends {} = {}> extends RootModule<exportScope<$>> {
9
7
  }
10
- export {};
8
+ export type exportScope<$> = bindExportsToScope<$, $>;
9
+ export declare const BoundModule: new <exports extends {}, $ extends {}>(exports: bindExportsToScope<exports, $>, $: $) => BoundModule<exports, $>;
10
+ export interface BoundModule<exports extends {}, $> extends RootModule<bindExportsToScope<exports, $>> {
11
+ }
12
+ export type bindExportsToScope<exports, $> = {
13
+ [k in keyof exports]: instantiateExport<exports[k], $>;
14
+ } & unknown;
15
+ export type Submodule<exports extends {}> = RootModule<exports & ("$root" extends keyof exports ? {
16
+ [inferred]: exports["$root"];
17
+ } : {})>;
18
+ export type instantiateExport<t, $> = [
19
+ t
20
+ ] extends [PreparsedNodeResolution] ? [
21
+ t
22
+ ] extends [anyOrNever] ? Type<t, $> : t extends GenericAst<infer params, infer body, infer body$> ? Generic<params, body, body$, $> : t extends Submodule<infer exports> ? BoundModule<exports, $> : never : Type<t, $>;
package/out/module.js CHANGED
@@ -1,3 +1,3 @@
1
- import { DynamicBase } from "@arktype/util";
2
- export class Module extends DynamicBase {
3
- }
1
+ import { RootModule } from "@ark/schema";
2
+ export const Module = RootModule;
3
+ export const BoundModule = RootModule;
@@ -1,28 +1,31 @@
1
- import { type RawSchema, type string } from "@arktype/schema";
2
- import { type ErrorMessage, type Primitive, type array, type defined, type equals, type isAny, type isUnknown, type objectKindOrDomainOf, type optionalKeyOf, type requiredKeyOf, type show } from "@arktype/util";
3
- import type { type } from "../ark.js";
4
- import type { ParseContext } from "../scope.js";
5
- import { type inferObjectLiteral, type validateObjectLiteral } from "./objectLiteral.js";
6
- import type { validateString } from "./semantic/validate.js";
7
- import type { BaseCompletions, inferString } from "./string/string.js";
8
- import { type TupleExpression, type inferTuple, type validateTuple } from "./tuple.js";
9
- export declare const parseObject: (def: object, ctx: ParseContext) => RawSchema;
10
- export type inferDefinition<def, $, args> = isAny<def> extends true ? never : def extends type.cast<infer t> | ThunkCast<infer t> ? t : def extends string ? inferString<def, $, args> : def extends array ? inferTuple<def, $, args> : def extends RegExp ? string.matching<string> : def extends object ? inferObjectLiteral<def, $, args> : never;
11
- export type validateDefinition<def, $, args> = null extends undefined ? ErrorMessage<`'strict' or 'strictNullChecks' must be set to true in your tsconfig's 'compilerOptions'`> : [def] extends [Terminal] ? def : def extends string ? validateString<def, $, args> : def extends array ? validateTuple<def, $, args> : def extends BadDefinitionType ? writeBadDefinitionTypeMessage<objectKindOrDomainOf<def>> : isUnknown<def> extends true ? BaseCompletions<$, args> | {} : validateObjectLiteral<def, $, args>;
1
+ import { type BaseRoot } from "@ark/schema";
2
+ import { type ErrorMessage, type Fn, type Primitive, type anyOrNever, type array, type defined, type equals, type ifEmptyObjectLiteral, type objectKindOrDomainOf, type optionalKeyOf, type requiredKeyOf, type show } from "@ark/util";
3
+ import type { type } from "../keywords/ark.ts";
4
+ import type { string } from "../keywords/string/string.ts";
5
+ import type { ParseContext } from "../scope.ts";
6
+ import { type inferObjectLiteral, type validateObjectLiteral } from "./objectLiteral.ts";
7
+ import type { validateString } from "./semantic/validate.ts";
8
+ import type { BaseCompletions, inferString } from "./string/string.ts";
9
+ import { type TupleExpression, type inferTuple, type validateTuple } from "./tuple.ts";
10
+ export declare const parseObject: (def: object, ctx: ParseContext) => BaseRoot;
11
+ export type inferDefinition<def, $, args> = [
12
+ def
13
+ ] extends [anyOrNever] ? def : def extends type.cast<infer t> ? ifEmptyObjectLiteral<t, object, t> : def extends ThunkCast<infer t> ? t : def extends string ? inferString<def, $, args> : def extends array ? inferTuple<def, $, args> : def extends RegExp ? string.matching<string> : def extends object ? inferObjectLiteral<def, $, args> : never;
14
+ export type validateDefinition<def, $, args> = null extends undefined ? ErrorMessage<`'strict' or 'strictNullChecks' must be set to true in your tsconfig's 'compilerOptions'`> : [def] extends [Terminal] ? def : def extends string ? validateString<def, $, args> : def extends array ? validateTuple<def, $, args> : def extends BadDefinitionType ? ErrorMessage<writeBadDefinitionTypeMessage<objectKindOrDomainOf<def>>> : unknown extends def ? BaseCompletions<$, args> | {} : validateObjectLiteral<def, $, args>;
12
15
  export type validateDeclared<declared, def, $, args> = def extends validateDefinition<def, $, args> ? validateInference<def, declared, $, args> : validateDefinition<def, $, args>;
13
16
  type validateInference<def, declared, $, args> = def extends RegExp | type.cast<unknown> | ThunkCast | TupleExpression ? validateShallowInference<def, declared, $, args> : def extends array ? declared extends array ? {
14
- [i in keyof declared]: i extends keyof def ? validateInference<def[i], declared[i], $, args> : unknown;
17
+ [i in keyof declared]: i extends keyof def ? validateInference<def[i], declared[i], $, args> : declared[i];
15
18
  } : show<declarationMismatch<def, declared, $, args>> : def extends object ? show<{
16
- [k in requiredKeyOf<declared>]: k extends keyof def ? validateInference<def[k], declared[k], $, args> : unknown;
19
+ [k in requiredKeyOf<declared>]: k extends keyof def ? validateInference<def[k], declared[k], $, args> : declared[k];
17
20
  } & {
18
- [k in optionalKeyOf<declared> & string as `${k}?`]: `${k}?` extends (keyof def) ? validateInference<def[`${k}?`], defined<declared[k]>, $, args> : unknown;
21
+ [k in optionalKeyOf<declared> & string as `${k}?`]: `${k}?` extends (keyof def) ? validateInference<def[`${k}?`], defined<declared[k]>, $, args> : declared[k];
19
22
  }> : validateShallowInference<def, declared, $, args>;
20
23
  type validateShallowInference<def, declared, $, args> = equals<inferDefinition<def, $, args>, declared> extends true ? def : show<declarationMismatch<def, declared, $, args>>;
21
24
  type declarationMismatch<def, declared, $, args> = {
22
25
  declared: declared;
23
26
  inferred: inferDefinition<def, $, args>;
24
27
  };
25
- type Terminal = RegExp | type.cast<unknown> | ((...args: never[]) => unknown);
28
+ type Terminal = RegExp | type.cast<unknown> | Fn;
26
29
  export type ThunkCast<t = unknown> = () => type.cast<t>;
27
30
  type BadDefinitionType = Exclude<Primitive, string>;
28
31
  export declare const writeBadDefinitionTypeMessage: <actual extends string>(actual: actual) => writeBadDefinitionTypeMessage<actual>;
@@ -1,12 +1,12 @@
1
- import { hasArkKind } from "@arktype/schema";
2
- import { isThunk, objectKindOf, printable, throwParseError } from "@arktype/util";
1
+ import { hasArkKind } from "@ark/schema";
2
+ import { isThunk, objectKindOf, printable, throwParseError } from "@ark/util";
3
3
  import { parseObjectLiteral } from "./objectLiteral.js";
4
4
  import { parseTuple } from "./tuple.js";
5
5
  export const parseObject = (def, ctx) => {
6
6
  const objectKind = objectKindOf(def);
7
7
  switch (objectKind) {
8
8
  case undefined:
9
- if (hasArkKind(def, "schema"))
9
+ if (hasArkKind(def, "root"))
10
10
  return def;
11
11
  return parseObjectLiteral(def, ctx);
12
12
  case "Array":
@@ -14,11 +14,11 @@ export const parseObject = (def, ctx) => {
14
14
  case "RegExp":
15
15
  return ctx.$.node("intersection", {
16
16
  domain: "string",
17
- regex: def
17
+ pattern: def
18
18
  }, { prereduced: true });
19
19
  case "Function": {
20
20
  const resolvedDef = isThunk(def) ? def() : def;
21
- if (hasArkKind(resolvedDef, "schema"))
21
+ if (hasArkKind(resolvedDef, "root"))
22
22
  return resolvedDef;
23
23
  return throwParseError(writeBadDefinitionTypeMessage("Function"));
24
24
  }
@@ -1,64 +1,94 @@
1
- import { type RawSchema, type writeInvalidPropertyKeyMessage } from "@arktype/schema";
2
- import { type Dict, type ErrorMessage, type Key, type merge, type show } from "@arktype/util";
3
- import type { ParseContext } from "../scope.js";
4
- import type { inferDefinition, validateDefinition } from "./definition.js";
5
- import type { astToString } from "./semantic/utils.js";
6
- import type { validateString } from "./semantic/validate.js";
7
- import { Scanner } from "./string/shift/scanner.js";
8
- export declare const parseObjectLiteral: (def: Dict, ctx: ParseContext) => RawSchema;
1
+ import { type BaseRoot, type Index, type Optional, type Required, type UndeclaredKeyBehavior, type writeInvalidPropertyKeyMessage } from "@ark/schema";
2
+ import { type anyOrNever, type conform, type Dict, type ErrorMessage, type ErrorType, type EscapeToken, type Key, type listable, type merge, type show } from "@ark/util";
3
+ import type { constrain, Default } from "../keywords/ast.ts";
4
+ import type { ParseContext } from "../scope.ts";
5
+ import type { inferDefinition, validateDefinition } from "./definition.ts";
6
+ import type { astToString } from "./semantic/utils.ts";
7
+ import type { validateString } from "./semantic/validate.ts";
8
+ export declare const parseObjectLiteral: (def: Dict, ctx: ParseContext) => BaseRoot;
9
+ export declare const writeInvalidUndeclaredBehaviorMessage: (actual: unknown) => string;
9
10
  export declare const nonLeadingSpreadError = "Spread operator may only be used as the first key in an object";
11
+ export type inferObjectLiteral<def extends object, $, args> = show<"..." extends keyof def ? merge<inferDefinition<def["..."], $, args>, _inferObjectLiteral<def, $, args>> : _inferObjectLiteral<def, $, args>>;
10
12
  /**
11
13
  * Infers the contents of an object literal, ignoring a spread definition
12
- * You probably want to use {@link inferObjectLiteral} instead.
13
14
  */
14
- type inferObjectLiteralInner<def extends object, $, args> = {
15
- -readonly [k in keyof def as nonOptionalKeyFrom<k, $, args>]: inferDefinition<def[k], $, args>;
15
+ type _inferObjectLiteral<def extends object, $, args> = {
16
+ -readonly [k in keyof def as nonOptionalKeyFrom<k, $, args>]: def[k] extends (DefaultValueTuple<infer baseDef, infer defaultValue>) ? [
17
+ def[k]
18
+ ] extends [anyOrNever] ? def[k] : (In?: inferDefinition<baseDef, $, args>) => Default<defaultValue> : inferDefinition<def[k], $, args>;
16
19
  } & {
17
20
  -readonly [k in keyof def as optionalKeyFrom<k>]?: inferDefinition<def[k], $, args>;
18
21
  };
19
- export type inferObjectLiteral<def extends object, $, args> = show<"..." extends keyof def ? merge<inferDefinition<def["..."], $, args>, inferObjectLiteralInner<def, $, args>> : inferObjectLiteralInner<def, $, args>>;
20
22
  export type validateObjectLiteral<def, $, args> = {
21
- [k in keyof def]: k extends IndexKey<infer indexDef> ? validateString<indexDef, $, args> extends ErrorMessage<infer message> ? indexParseError<message> : inferDefinition<indexDef, $, args> extends PropertyKey ? validateDefinition<def[k], $, args> : indexParseError<writeInvalidPropertyKeyMessage<indexDef>> : k extends "..." ? inferDefinition<def[k], $, args> extends object ? validateDefinition<def[k], $, args> : indexParseError<writeInvalidSpreadTypeMessage<astToString<def[k]>>> : validateDefinition<def[k], $, args>;
23
+ [k in keyof def]: k extends IndexKey<infer indexDef> ? validateString<indexDef, $, args> extends ErrorMessage<infer message> ? ErrorType<message> : inferDefinition<indexDef, $, args> extends Key | constrain<Key, {}> ? validateDefinition<def[k], $, args> : ErrorType<writeInvalidPropertyKeyMessage<indexDef>> : k extends "..." ? inferDefinition<def[k], $, args> extends object ? validateDefinition<def[k], $, args> : ErrorType<writeInvalidSpreadTypeMessage<astToString<def[k]>>> : k extends "+" ? UndeclaredKeyBehavior : validateDefaultableValue<def, k, $, args>;
22
24
  };
23
- type nonOptionalKeyFrom<k, $, args> = parseKey<k> extends PreparsedKey<"required", infer inner> ? inner : parseKey<k> extends PreparsedKey<"index", infer inner> ? inferDefinition<inner, $, args> & Key : never;
25
+ type validateDefaultableValue<def, k extends keyof def, $, args> = def[k] extends DefaultValueTuple ? [
26
+ def[k]
27
+ ] extends [anyOrNever] ?
28
+ /** this extra [anyOrNever] check is required to ensure that nested `type` invocations
29
+ * like the following are not prematurely validated by the outer call:
30
+ *
31
+ * ```ts
32
+ * type({
33
+ * "test?": type("string").pipe(x => x === "true")
34
+ * })
35
+ * ```
36
+ */
37
+ def[k] : validateDefaultValueTuple<def[k], k, $, args> : validateDefinition<def[k], $, args>;
38
+ type DefaultValueTuple<baseDef = unknown, defaultValue = unknown> = readonly [
39
+ baseDef,
40
+ "=",
41
+ defaultValue
42
+ ];
43
+ type validateDefaultValueTuple<def extends DefaultValueTuple, k extends PropertyKey, $, args> = parseKey<k>["kind"] extends "required" ? conform<def, readonly [
44
+ validateDefinition<def[0], $, args>,
45
+ "=",
46
+ inferDefinition<def[0], $, args>
47
+ ]> : ErrorMessage<invalidDefaultKeyKindMessage>;
48
+ type nonOptionalKeyFrom<k, $, args> = parseKey<k> extends PreparsedKey<"required", infer inner> ? inner : parseKey<k> extends PreparsedKey<"index", infer inner> ? inferDefinition<inner, $, args> extends infer t extends Key ? t : never : never;
24
49
  type optionalKeyFrom<k> = parseKey<k> extends PreparsedKey<"optional", infer inner> ? inner : never;
25
- type PreparsedKey<kind extends ParsedKeyKind = ParsedKeyKind, inner extends Key = Key> = {
50
+ type PreparsedKey<kind extends ParsedKeyKind = ParsedKeyKind, key extends Key = Key> = {
26
51
  kind: kind;
27
- inner: inner;
52
+ key: key;
28
53
  };
29
54
  declare namespace PreparsedKey {
30
55
  type from<t extends PreparsedKey> = t;
31
56
  }
32
- type ParsedKeyKind = "required" | "optional" | "index" | "spread";
57
+ type ParsedKeyKind = "required" | "optional" | "index" | MetaKey;
58
+ export type MetaKey = "..." | "+";
33
59
  export type IndexKey<def extends string = string> = `[${def}]`;
34
- type PreparsedEntry = PreparsedKey & {
35
- value: unknown;
60
+ export type ParsedEntry = ParsedUndeclaredEntry | ParsedSpreadEntry | Required.Node | Optional.Node | Index.Node;
61
+ export type ParsedUndeclaredEntry = {
62
+ kind: "undeclared";
63
+ behavior: UndeclaredKeyBehavior;
64
+ };
65
+ export type ParsedSpreadEntry = {
66
+ kind: "spread";
67
+ node: BaseRoot;
36
68
  };
37
- export declare const parseEntry: (entry: readonly [Key, unknown]) => PreparsedEntry;
38
- declare const parseKey: (key: Key) => PreparsedKey<ParsedKeyKind, Key>;
39
- type parseKey<k> = k extends `${infer inner}?` ? inner extends `${infer baseName}${Scanner.EscapeToken}` ? PreparsedKey.from<{
69
+ export declare const parseEntry: (key: Key, value: unknown, ctx: ParseContext) => listable<ParsedEntry>;
70
+ export declare const invalidDefaultKeyKindMessage = "Only required keys may specify default values, e.g. { value: 'number = 0' }";
71
+ export type invalidDefaultKeyKindMessage = typeof invalidDefaultKeyKindMessage;
72
+ declare const parseKey: (key: Key) => PreparsedKey;
73
+ type parseKey<k> = k extends `${infer inner}?` ? inner extends `${infer baseName}${EscapeToken}` ? PreparsedKey.from<{
40
74
  kind: "required";
41
- inner: `${baseName}?`;
75
+ key: `${baseName}?`;
42
76
  }> : PreparsedKey.from<{
43
77
  kind: "optional";
44
- inner: inner;
45
- }> : k extends "..." ? PreparsedKey.from<{
46
- kind: "spread";
47
- inner: "...";
48
- }> : k extends `${Scanner.EscapeToken}...` ? PreparsedKey.from<{
78
+ key: inner;
79
+ }> : k extends MetaKey ? PreparsedKey.from<{
80
+ kind: k;
81
+ key: k;
82
+ }> : k extends `${EscapeToken}${infer escapedMeta extends MetaKey}` ? PreparsedKey.from<{
49
83
  kind: "required";
50
- inner: "...";
84
+ key: escapedMeta;
51
85
  }> : k extends IndexKey<infer def> ? PreparsedKey.from<{
52
86
  kind: "index";
53
- inner: def;
87
+ key: def;
54
88
  }> : PreparsedKey.from<{
55
89
  kind: "required";
56
- inner: k extends (`${Scanner.EscapeToken}${infer escapedIndexKey extends IndexKey}`) ? escapedIndexKey : k extends Key ? k : `${k & number}`;
90
+ key: k extends `${EscapeToken}${infer escapedIndexKey extends IndexKey}` ? escapedIndexKey : k extends Key ? k : `${k & number}`;
57
91
  }>;
58
- declare const indexParseSymbol: unique symbol;
59
- export type indexParseError<message extends string = string> = {
60
- [indexParseSymbol]: message;
61
- };
62
92
  export declare const writeInvalidSpreadTypeMessage: <def extends string>(def: def) => writeInvalidSpreadTypeMessage<def>;
63
93
  type writeInvalidSpreadTypeMessage<def extends string> = `Spread operand must resolve to an object literal type (was ${def})`;
64
94
  export {};
@@ -1,75 +1,100 @@
1
- import { tsKeywords } from "@arktype/schema";
2
- import { printable, spliterate, stringAndSymbolicEntriesOf, throwParseError } from "@arktype/util";
3
- import { Scanner } from "./string/shift/scanner.js";
1
+ import { ArkErrors, normalizeIndex } from "@ark/schema";
2
+ import { append, escapeToken, isArray, printable, stringAndSymbolicEntriesOf, throwParseError } from "@ark/util";
3
+ import { writeUnassignableDefaultValueMessage } from "./semantic/default.js";
4
4
  export const parseObjectLiteral = (def, ctx) => {
5
- const propNodes = [];
6
- const indexNodes = [];
5
+ let spread;
6
+ const structure = {};
7
7
  // We only allow a spread operator to be used as the first key in an object
8
8
  // because to match JS behavior any keys before the spread are overwritten
9
9
  // by the values in the target object, so there'd be no useful purpose in having it
10
10
  // anywhere except for the beginning.
11
- const parsedEntries = stringAndSymbolicEntriesOf(def).map(parseEntry);
11
+ const parsedEntries = stringAndSymbolicEntriesOf(def).flatMap(entry => parseEntry(entry[0], entry[1], ctx));
12
12
  if (parsedEntries[0]?.kind === "spread") {
13
13
  // remove the spread entry so we can iterate over the remaining entries
14
14
  // expecting non-spread entries
15
15
  const spreadEntry = parsedEntries.shift();
16
- const spreadNode = ctx.$.parse(spreadEntry.value, ctx);
17
- if (!spreadNode.hasKind("intersection") ||
18
- !spreadNode.extends(tsKeywords.object)) {
19
- return throwParseError(writeInvalidSpreadTypeMessage(printable(spreadEntry.value)));
16
+ if (!spreadEntry.node.hasKind("intersection") ||
17
+ !spreadEntry.node.structure) {
18
+ return throwParseError(writeInvalidSpreadTypeMessage(typeof spreadEntry.node.expression));
20
19
  }
21
- // TODO: move to props group merge in schema
22
- // For each key on spreadNode, add it to our object.
23
- // We filter out keys from the spreadNode that will be defined later on this same object
24
- // because the currently parsed definition will overwrite them.
25
- spreadNode.prop?.forEach(spreadRequired => !parsedEntries.some(({ inner: innerKey }) => innerKey === spreadRequired.key) && propNodes.push(spreadRequired));
20
+ spread = spreadEntry.node.structure;
26
21
  }
27
22
  for (const entry of parsedEntries) {
28
23
  if (entry.kind === "spread")
29
24
  return throwParseError(nonLeadingSpreadError);
30
- if (entry.kind === "index") {
31
- // handle key parsing first to match type behavior
32
- const key = ctx.$.parse(entry.inner, ctx);
33
- const value = ctx.$.parse(entry.value, ctx);
34
- // extract enumerable named props from the index signature
35
- const [enumerable, nonEnumerable] = spliterate(key.branches, k => k.hasKind("unit"));
36
- if (enumerable.length) {
37
- propNodes.push(...enumerable.map(k => ctx.$.node("prop", { key: k.unit, value })));
38
- if (nonEnumerable.length)
39
- indexNodes.push(ctx.$.node("index", { key: nonEnumerable, value }));
40
- }
41
- else
42
- indexNodes.push(ctx.$.node("index", { key, value }));
43
- }
44
- else {
45
- const value = ctx.$.parse(entry.value, ctx);
46
- propNodes.push({
47
- key: entry.inner,
48
- value,
49
- optional: entry.kind === "optional"
50
- });
25
+ if (entry.kind === "undeclared") {
26
+ structure.undeclared = entry.behavior;
27
+ continue;
51
28
  }
29
+ structure[entry.kind] = append(structure[entry.kind], entry);
52
30
  }
53
- return ctx.$.schema({
31
+ const structureNode = ctx.$.node("structure", structure);
32
+ return ctx.$.rootNode({
54
33
  domain: "object",
55
- prop: propNodes,
56
- index: indexNodes
34
+ structure: spread?.merge(structureNode) ?? structureNode
57
35
  });
58
36
  };
37
+ export const writeInvalidUndeclaredBehaviorMessage = (actual) => `Value of '+' key must be 'reject', 'delete', or 'ignore' (was ${printable(actual)})`;
59
38
  export const nonLeadingSpreadError = "Spread operator may only be used as the first key in an object";
60
- export const parseEntry = (entry) => Object.assign(parseKey(entry[0]), { value: entry[1] });
61
- const parseKey = (key) => typeof key === "symbol" ? { inner: key, kind: "required" }
39
+ export const parseEntry = (key, value, ctx) => {
40
+ const parsedKey = parseKey(key);
41
+ if (parsedKey.kind === "+") {
42
+ if (value !== "reject" && value !== "delete" && value !== "ignore")
43
+ throwParseError(writeInvalidUndeclaredBehaviorMessage(value));
44
+ return { kind: "undeclared", behavior: value };
45
+ }
46
+ if (parsedKey.kind === "...")
47
+ return { kind: "spread", node: ctx.$.parse(value, ctx) };
48
+ const parsedValue = isArray(value) && value[1] === "=" ?
49
+ [ctx.$.parse(value[0], ctx), "=", value[2]]
50
+ : ctx.$.parse(value, ctx, true);
51
+ if (isArray(parsedValue)) {
52
+ if (parsedKey.kind !== "required")
53
+ throwParseError(invalidDefaultKeyKindMessage);
54
+ const out = parsedValue[0].traverse(parsedValue[2]);
55
+ if (out instanceof ArkErrors) {
56
+ throwParseError(writeUnassignableDefaultValueMessage(printable(parsedKey.key), out.message));
57
+ }
58
+ return ctx.$.node("optional", {
59
+ key: parsedKey.key,
60
+ value: parsedValue[0],
61
+ default: parsedValue[2]
62
+ });
63
+ }
64
+ if (parsedKey.kind === "index") {
65
+ const signature = ctx.$.parse(parsedKey.key, ctx);
66
+ const normalized = normalizeIndex(signature, parsedValue, ctx.$);
67
+ return (normalized.index ?
68
+ normalized.required ?
69
+ [normalized.index, ...normalized.required]
70
+ : normalized.index
71
+ : (normalized.required ?? []));
72
+ }
73
+ return ctx.$.node(parsedKey.kind, {
74
+ key: parsedKey.key,
75
+ value: parsedValue
76
+ });
77
+ };
78
+ // single quote use here is better for TypeScript's inlined error to avoid escapes
79
+ export const invalidDefaultKeyKindMessage = `Only required keys may specify default values, e.g. { value: 'number = 0' }`;
80
+ const parseKey = (key) => typeof key === "symbol" ? { kind: "required", key }
62
81
  : key.at(-1) === "?" ?
63
- key.at(-2) === Scanner.escapeToken ?
64
- { inner: `${key.slice(0, -2)}?`, kind: "required" }
82
+ key.at(-2) === escapeToken ?
83
+ { kind: "required", key: `${key.slice(0, -2)}?` }
65
84
  : {
66
- inner: key.slice(0, -1),
67
- kind: "optional"
85
+ kind: "optional",
86
+ key: key.slice(0, -1)
68
87
  }
69
88
  : key[0] === "[" && key.at(-1) === "]" ?
70
- { inner: key.slice(1, -1), kind: "index" }
71
- : key[0] === Scanner.escapeToken && key[1] === "[" && key.at(-1) === "]" ?
72
- { inner: key.slice(1), kind: "required" }
73
- : key === "..." ? { inner: "...", kind: "spread" }
74
- : { inner: key === "\\..." ? "..." : key, kind: "required" };
89
+ { kind: "index", key: key.slice(1, -1) }
90
+ : key[0] === escapeToken && key[1] === "[" && key.at(-1) === "]" ?
91
+ { kind: "required", key: key.slice(1) }
92
+ : key === "..." ? { kind: key, key }
93
+ : key === "+" ? { kind: key, key }
94
+ : {
95
+ kind: "required",
96
+ key: key === "\\..." ? "..."
97
+ : key === "\\+" ? "+"
98
+ : key
99
+ };
75
100
  export const writeInvalidSpreadTypeMessage = (def) => `Spread operand must resolve to an object literal type (was ${def})`;
@@ -1,12 +1,15 @@
1
- import type { LimitLiteral, writeUnboundableMessage } from "@arktype/schema";
2
- import type { ErrorMessage, array } from "@arktype/util";
3
- import type { Comparator, InvertedComparators } from "../string/reduce/shared.js";
4
- import type { BoundExpressionKind, writeInvalidLimitMessage } from "../string/shift/operator/bounds.js";
5
- import type { inferAstIn } from "./infer.js";
6
- import type { astToString } from "./utils.js";
7
- import type { validateAst } from "./validate.js";
8
- export type validateRange<l, comparator extends Comparator, r, $, args> = l extends LimitLiteral ? validateBound<r, InvertedComparators[comparator], l, "left", $, args> : l extends [infer leftAst, Comparator, unknown] ? ErrorMessage<writeDoubleRightBoundMessage<astToString<leftAst>>> : validateBound<l, comparator, r & LimitLiteral, "right", $, args>;
9
- export type validateBound<boundedAst, comparator extends Comparator, limit extends LimitLiteral, boundKind extends BoundExpressionKind, $, args> = inferAstIn<boundedAst, $, args> extends infer bounded ? isNumericallyBoundable<bounded> extends true ? limit extends number ? validateAst<boundedAst, $, args> : ErrorMessage<writeInvalidLimitMessage<comparator, limit, boundKind>> : bounded extends Date ? validateAst<boundedAst, $, args> : ErrorMessage<writeUnboundableMessage<astToString<boundKind extends "left" ? boundedAst[0 & keyof boundedAst] : boundedAst>>> : never;
1
+ import type { writeUnboundableMessage } from "@ark/schema";
2
+ import type { ErrorMessage, array, typeToString } from "@ark/util";
3
+ import type { LimitLiteral } from "../../keywords/ast.ts";
4
+ import type { Comparator } from "../string/reduce/shared.ts";
5
+ import type { BoundExpressionKind, writeInvalidLimitMessage } from "../string/shift/operator/bounds.ts";
6
+ import type { inferAstIn } from "./infer.ts";
7
+ import type { astToString } from "./utils.ts";
8
+ import type { validateAst } from "./validate.ts";
9
+ export type validateRange<l, comparator extends Comparator, r, $, args> = [
10
+ l
11
+ ] extends [LimitLiteral] ? validateBound<r, comparator, l, "left", $, args> : [l] extends [[infer leftAst, Comparator, unknown]] ? ErrorMessage<writeDoubleRightBoundMessage<astToString<leftAst>>> : validateBound<l, comparator, r & LimitLiteral, "right", $, args>;
12
+ export type validateBound<boundedAst, comparator extends Comparator, limit extends LimitLiteral, boundKind extends BoundExpressionKind, $, args> = inferAstIn<boundedAst, $, args> extends infer bounded ? isNumericallyBoundable<bounded> extends true ? limit extends number ? validateAst<boundedAst, $, args> : ErrorMessage<writeInvalidLimitMessage<comparator, limit, boundKind>> : [bounded] extends [Date] ? validateAst<boundedAst, $, args> : ErrorMessage<writeUnboundableMessage<typeToString<bounded>>> : never;
10
13
  type isNumericallyBoundable<bounded> = [
11
14
  bounded
12
15
  ] extends [number] ? true : [bounded] extends [string] ? true : [bounded] extends [array] ? true : false;
@@ -0,0 +1,9 @@
1
+ import type { ErrorMessage } from "@ark/util";
2
+ import type { inferAmbient } from "../../type.ts";
3
+ import type { UnitLiteral } from "../string/shift/operator/default.ts";
4
+ import type { inferAstOut } from "./infer.ts";
5
+ import type { astToString } from "./utils.ts";
6
+ import type { validateAst } from "./validate.ts";
7
+ export type validateDefault<baseAst, unitLiteral extends UnitLiteral, $, args> = validateAst<baseAst, $, args> extends infer e extends ErrorMessage ? e : inferAmbient<unitLiteral> extends inferAstOut<baseAst, $, args> ? undefined : ErrorMessage<writeUnassignableDefaultValueMessage<astToString<baseAst>, unitLiteral>>;
8
+ export declare const writeUnassignableDefaultValueMessage: <key extends string, message extends string>(key: key, message: message) => string;
9
+ export type writeUnassignableDefaultValueMessage<baseDef extends string, defaultValue extends string> = `Default value ${defaultValue} is not assignable to ${baseDef}`;
@@ -0,0 +1 @@
1
+ export const writeUnassignableDefaultValueMessage = (key, message) => `Default value at ${key} ${message}`;
@@ -1,7 +1,7 @@
1
- import type { Schema, writeIndivisibleMessage } from "@arktype/schema";
2
- import type { ErrorMessage } from "@arktype/util";
3
- import type { inferAstIn } from "./infer.js";
4
- import type { validateAst } from "./validate.js";
1
+ import type { writeIndivisibleMessage } from "@ark/schema";
2
+ import type { ErrorMessage } from "@ark/util";
3
+ import type { inferAstIn } from "./infer.ts";
4
+ import type { validateAst } from "./validate.ts";
5
5
  export type validateDivisor<l, $, args> = inferAstIn<l, $, args> extends infer data ? [
6
6
  data
7
- ] extends [number] ? validateAst<l, $, args> : ErrorMessage<writeIndivisibleMessage<Schema<data>>> : never;
7
+ ] extends [number] ? validateAst<l, $, args> : ErrorMessage<writeIndivisibleMessage<data>> : never;
@@ -0,0 +1 @@
1
+ export {};