arktype 2.0.0-rc.28 → 2.0.0-rc.29
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.
- package/out/index.d.ts +1 -1
- package/out/index.js +1 -1
- package/out/keywords/Array.js +2 -2
- package/out/keywords/FormData.js +2 -2
- package/out/keywords/TypedArray.js +2 -2
- package/out/keywords/builtins.js +2 -2
- package/out/keywords/constructors.js +2 -2
- package/out/keywords/number.js +2 -2
- package/out/keywords/string.js +21 -21
- package/out/keywords/ts.js +6 -6
- package/out/parser/definition.d.ts +2 -2
- package/out/parser/property.d.ts +2 -3
- package/out/parser/shift/scanner.d.ts +1 -1
- package/out/parser/shift/scanner.js +2 -1
- package/out/parser/tupleLiteral.d.ts +32 -17
- package/out/parser/tupleLiteral.js +11 -8
- package/out/scope.d.ts +0 -1
- package/out/scope.js +0 -1
- package/package.json +3 -3
- package/out/keywords/utils.d.ts +0 -1
- package/out/keywords/utils.js +0 -2
package/out/index.d.ts
CHANGED
|
@@ -5,5 +5,5 @@ export * from "./config.ts";
|
|
|
5
5
|
export { Generic } from "./generic.ts";
|
|
6
6
|
export { ark, declare, define, generic, keywords, type, type Ark } from "./keywords/keywords.ts";
|
|
7
7
|
export { Module, type BoundModule, type Submodule } from "./module.ts";
|
|
8
|
-
export {
|
|
8
|
+
export { scope, type Scope } from "./scope.ts";
|
|
9
9
|
export { Type } from "./type.ts";
|
package/out/index.js
CHANGED
|
@@ -4,5 +4,5 @@ export * from "./config.js";
|
|
|
4
4
|
export { Generic } from "./generic.js";
|
|
5
5
|
export { ark, declare, define, generic, keywords, type } from "./keywords/keywords.js";
|
|
6
6
|
export { Module } from "./module.js";
|
|
7
|
-
export {
|
|
7
|
+
export { scope } from "./scope.js";
|
|
8
8
|
export { Type } from "./type.js";
|
package/out/keywords/Array.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { genericNode, intrinsic, rootSchema } from "@ark/schema";
|
|
2
2
|
import { Hkt, liftArray } from "@ark/util";
|
|
3
|
-
import {
|
|
3
|
+
import { Scope } from "../scope.js";
|
|
4
4
|
class liftFromHkt extends Hkt {
|
|
5
5
|
}
|
|
6
6
|
const liftFrom = genericNode("element")(args => {
|
|
@@ -11,7 +11,7 @@ const liftFrom = genericNode("element")(args => {
|
|
|
11
11
|
.pipe(liftArray)
|
|
12
12
|
.distribute(branch => branch.assertHasKind("morph").declareOut(lifted), rootSchema);
|
|
13
13
|
}, liftFromHkt);
|
|
14
|
-
export const arkArray =
|
|
14
|
+
export const arkArray = Scope.module({
|
|
15
15
|
root: intrinsic.Array,
|
|
16
16
|
readonly: "root",
|
|
17
17
|
index: intrinsic.nonNegativeIntegerString,
|
package/out/keywords/FormData.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { rootSchema } from "@ark/schema";
|
|
2
2
|
import { registry } from "@ark/util";
|
|
3
|
-
import {
|
|
3
|
+
import { Scope } from "../scope.js";
|
|
4
4
|
const value = rootSchema(["string", registry.FileConstructor]);
|
|
5
5
|
const parsedFormDataValue = value.rawOr(value.array());
|
|
6
6
|
const parsed = rootSchema({
|
|
@@ -11,7 +11,7 @@ const parsed = rootSchema({
|
|
|
11
11
|
value: parsedFormDataValue
|
|
12
12
|
}
|
|
13
13
|
});
|
|
14
|
-
export const arkFormData =
|
|
14
|
+
export const arkFormData = Scope.module({
|
|
15
15
|
root: ["instanceof", FormData],
|
|
16
16
|
value,
|
|
17
17
|
parsed,
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export const TypedArray =
|
|
1
|
+
import { Scope } from "../scope.js";
|
|
2
|
+
export const TypedArray = Scope.module({
|
|
3
3
|
Int8: ["instanceof", Int8Array],
|
|
4
4
|
Uint8: ["instanceof", Uint8Array],
|
|
5
5
|
Uint8Clamped: ["instanceof", Uint8ClampedArray],
|
package/out/keywords/builtins.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { genericNode, intrinsic } from "@ark/schema";
|
|
2
2
|
import { Hkt } from "@ark/util";
|
|
3
|
-
import {
|
|
3
|
+
import { Scope } from "../scope.js";
|
|
4
4
|
class MergeHkt extends Hkt {
|
|
5
5
|
}
|
|
6
6
|
const Merge = genericNode(["base", intrinsic.object], ["props", intrinsic.object])(args => args.base.merge(args.props), MergeHkt);
|
|
7
|
-
export const arkBuiltins =
|
|
7
|
+
export const arkBuiltins = Scope.module({
|
|
8
8
|
Key: intrinsic.key,
|
|
9
9
|
Merge
|
|
10
10
|
});
|
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
import { ecmascriptConstructors, flatMorph, platformConstructors } from "@ark/util";
|
|
2
|
+
import { Scope } from "../scope.js";
|
|
2
3
|
import { arkArray } from "./Array.js";
|
|
3
4
|
import { arkFormData } from "./FormData.js";
|
|
4
5
|
import { TypedArray } from "./TypedArray.js";
|
|
5
|
-
import { arkModule } from "./utils.js";
|
|
6
6
|
const omittedPrototypes = {
|
|
7
7
|
Boolean: 1,
|
|
8
8
|
Number: 1,
|
|
9
9
|
String: 1
|
|
10
10
|
};
|
|
11
|
-
export const arkPrototypes =
|
|
11
|
+
export const arkPrototypes = Scope.module({
|
|
12
12
|
...flatMorph({ ...ecmascriptConstructors, ...platformConstructors }, (k, v) => (k in omittedPrototypes ? [] : [k, ["instanceof", v]])),
|
|
13
13
|
Array: arkArray,
|
|
14
14
|
TypedArray,
|
package/out/keywords/number.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { intrinsic, rootSchema } from "@ark/schema";
|
|
2
|
-
import {
|
|
2
|
+
import { Scope } from "../scope.js";
|
|
3
3
|
/**
|
|
4
4
|
* As per the ECMA-262 specification:
|
|
5
5
|
* A time value supports a slightly smaller range of -8,640,000,000,000,000 to 8,640,000,000,000,000 milliseconds.
|
|
@@ -29,7 +29,7 @@ export const integer = rootSchema({
|
|
|
29
29
|
domain: "number",
|
|
30
30
|
divisor: 1
|
|
31
31
|
});
|
|
32
|
-
export const number =
|
|
32
|
+
export const number = Scope.module({
|
|
33
33
|
root: intrinsic.number,
|
|
34
34
|
integer,
|
|
35
35
|
epoch,
|
package/out/keywords/string.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { ArkErrors, intrinsic, node, rootSchema } from "@ark/schema";
|
|
2
2
|
import { flatMorph, numericStringMatcher, wellFormedIntegerMatcher } from "@ark/util";
|
|
3
|
+
import { Scope } from "../scope.js";
|
|
3
4
|
import { number } from "./number.js";
|
|
4
|
-
import { arkModule } from "./utils.js";
|
|
5
5
|
// Non-trivial expressions should have an explanation or attribution
|
|
6
6
|
export const regexStringNode = (regex, description) => node("intersection", {
|
|
7
7
|
domain: "string",
|
|
@@ -12,7 +12,7 @@ export const regexStringNode = (regex, description) => node("intersection", {
|
|
|
12
12
|
}
|
|
13
13
|
});
|
|
14
14
|
const stringIntegerRoot = regexStringNode(wellFormedIntegerMatcher, "a well-formed integer string");
|
|
15
|
-
export const stringInteger =
|
|
15
|
+
export const stringInteger = Scope.module({
|
|
16
16
|
root: stringIntegerRoot,
|
|
17
17
|
parse: rootSchema({
|
|
18
18
|
in: stringIntegerRoot,
|
|
@@ -23,12 +23,12 @@ export const stringInteger = arkModule({
|
|
|
23
23
|
declaredOut: intrinsic.integer
|
|
24
24
|
})
|
|
25
25
|
});
|
|
26
|
-
const base64 =
|
|
26
|
+
const base64 = Scope.module({
|
|
27
27
|
root: regexStringNode(/^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/, "base64-encoded"),
|
|
28
28
|
url: regexStringNode(/^(?:[A-Za-z0-9_-]{4})*(?:[A-Za-z0-9_-]{2}(?:==|%3D%3D)?|[A-Za-z0-9_-]{3}(?:=|%3D)?)?$/, "base64url-encoded")
|
|
29
29
|
});
|
|
30
30
|
const preformattedCapitalize = regexStringNode(/^[A-Z].*$/, "capitalized");
|
|
31
|
-
export const capitalize =
|
|
31
|
+
export const capitalize = Scope.module({
|
|
32
32
|
root: rootSchema({
|
|
33
33
|
in: "string",
|
|
34
34
|
morphs: (s) => s.charAt(0).toUpperCase() + s.slice(1),
|
|
@@ -132,7 +132,7 @@ const epochRoot = stringInteger.root.internal
|
|
|
132
132
|
description: "an integer string representing a safe Unix timestamp"
|
|
133
133
|
})
|
|
134
134
|
.assertHasKind("intersection");
|
|
135
|
-
const epoch =
|
|
135
|
+
const epoch = Scope.module({
|
|
136
136
|
root: epochRoot,
|
|
137
137
|
parse: rootSchema({
|
|
138
138
|
in: epochRoot,
|
|
@@ -141,7 +141,7 @@ const epoch = arkModule({
|
|
|
141
141
|
})
|
|
142
142
|
});
|
|
143
143
|
const isoRoot = regexStringNode(iso8601Matcher, "an ISO 8601 (YYYY-MM-DDTHH:mm:ss.sssZ) date").internal.assertHasKind("intersection");
|
|
144
|
-
const iso =
|
|
144
|
+
const iso = Scope.module({
|
|
145
145
|
root: isoRoot,
|
|
146
146
|
parse: rootSchema({
|
|
147
147
|
in: isoRoot,
|
|
@@ -149,7 +149,7 @@ const iso = arkModule({
|
|
|
149
149
|
declaredOut: intrinsic.Date
|
|
150
150
|
})
|
|
151
151
|
});
|
|
152
|
-
export const stringDate =
|
|
152
|
+
export const stringDate = Scope.module({
|
|
153
153
|
root: parsableDate,
|
|
154
154
|
parse: rootSchema({
|
|
155
155
|
declaredIn: parsableDate,
|
|
@@ -184,7 +184,7 @@ const ipv6Matcher = new RegExp("^(" +
|
|
|
184
184
|
`(?:${ipv6Segment}:){1}(?:(:${ipv6Segment}){0,4}:${ipv4Address}|(:${ipv6Segment}){1,6}|:)|` +
|
|
185
185
|
`(?::((?::${ipv6Segment}){0,5}:${ipv4Address}|(?::${ipv6Segment}){1,7}|:))` +
|
|
186
186
|
")(%[0-9a-zA-Z.]{1,})?$");
|
|
187
|
-
export const ip =
|
|
187
|
+
export const ip = Scope.module({
|
|
188
188
|
root: ["v4 | v6", "@", "an IP address"],
|
|
189
189
|
v4: regexStringNode(ipv4Matcher, "an IPv4 address"),
|
|
190
190
|
v6: regexStringNode(ipv6Matcher, "an IPv6 address")
|
|
@@ -233,7 +233,7 @@ const parseJson = (s, ctx) => {
|
|
|
233
233
|
});
|
|
234
234
|
}
|
|
235
235
|
};
|
|
236
|
-
export const json =
|
|
236
|
+
export const json = Scope.module({
|
|
237
237
|
root: jsonRoot,
|
|
238
238
|
parse: rootSchema({
|
|
239
239
|
in: "string",
|
|
@@ -242,7 +242,7 @@ export const json = arkModule({
|
|
|
242
242
|
})
|
|
243
243
|
});
|
|
244
244
|
const preformattedLower = regexStringNode(/^[a-z]*$/, "only lowercase letters");
|
|
245
|
-
const lower =
|
|
245
|
+
const lower = Scope.module({
|
|
246
246
|
root: rootSchema({
|
|
247
247
|
in: "string",
|
|
248
248
|
morphs: (s) => s.toLowerCase(),
|
|
@@ -267,23 +267,23 @@ const normalizeNodes = flatMorph(normalizedForms, (i, form) => [
|
|
|
267
267
|
declaredOut: preformattedNodes[form]
|
|
268
268
|
})
|
|
269
269
|
]);
|
|
270
|
-
export const NFC =
|
|
270
|
+
export const NFC = Scope.module({
|
|
271
271
|
root: normalizeNodes.NFC,
|
|
272
272
|
preformatted: preformattedNodes.NFC
|
|
273
273
|
});
|
|
274
|
-
export const NFD =
|
|
274
|
+
export const NFD = Scope.module({
|
|
275
275
|
root: normalizeNodes.NFD,
|
|
276
276
|
preformatted: preformattedNodes.NFD
|
|
277
277
|
});
|
|
278
|
-
export const NFKC =
|
|
278
|
+
export const NFKC = Scope.module({
|
|
279
279
|
root: normalizeNodes.NFKC,
|
|
280
280
|
preformatted: preformattedNodes.NFKC
|
|
281
281
|
});
|
|
282
|
-
export const NFKD =
|
|
282
|
+
export const NFKD = Scope.module({
|
|
283
283
|
root: normalizeNodes.NFKD,
|
|
284
284
|
preformatted: preformattedNodes.NFKD
|
|
285
285
|
});
|
|
286
|
-
export const normalize =
|
|
286
|
+
export const normalize = Scope.module({
|
|
287
287
|
root: "NFC",
|
|
288
288
|
NFC,
|
|
289
289
|
NFD,
|
|
@@ -291,7 +291,7 @@ export const normalize = arkModule({
|
|
|
291
291
|
NFKD
|
|
292
292
|
});
|
|
293
293
|
const numericRoot = regexStringNode(numericStringMatcher, "a well-formed numeric string");
|
|
294
|
-
export const numeric =
|
|
294
|
+
export const numeric = Scope.module({
|
|
295
295
|
root: numericRoot,
|
|
296
296
|
parse: rootSchema({
|
|
297
297
|
in: numericRoot,
|
|
@@ -305,7 +305,7 @@ const semver = regexStringNode(semverMatcher, "a semantic version (see https://s
|
|
|
305
305
|
const preformattedTrim = regexStringNode(
|
|
306
306
|
// no leading or trailing whitespace
|
|
307
307
|
/^\S.*\S$|^\S?$/, "trimmed");
|
|
308
|
-
const trim =
|
|
308
|
+
const trim = Scope.module({
|
|
309
309
|
root: rootSchema({
|
|
310
310
|
in: "string",
|
|
311
311
|
morphs: (s) => s.trim(),
|
|
@@ -314,7 +314,7 @@ const trim = arkModule({
|
|
|
314
314
|
preformatted: preformattedTrim
|
|
315
315
|
});
|
|
316
316
|
const preformattedUpper = regexStringNode(/^[A-Z]*$/, "only uppercase letters");
|
|
317
|
-
const upper =
|
|
317
|
+
const upper = Scope.module({
|
|
318
318
|
root: rootSchema({
|
|
319
319
|
in: "string",
|
|
320
320
|
morphs: (s) => s.toUpperCase(),
|
|
@@ -341,7 +341,7 @@ const urlRoot = rootSchema({
|
|
|
341
341
|
predicate: isParsableUrl
|
|
342
342
|
}
|
|
343
343
|
});
|
|
344
|
-
export const url =
|
|
344
|
+
export const url = Scope.module({
|
|
345
345
|
root: urlRoot,
|
|
346
346
|
parse: rootSchema({
|
|
347
347
|
declaredIn: urlRoot,
|
|
@@ -358,7 +358,7 @@ export const url = arkModule({
|
|
|
358
358
|
})
|
|
359
359
|
});
|
|
360
360
|
// Based on https://github.com/validatorjs/validator.js/blob/master/src/lib/isUUID.js
|
|
361
|
-
export const uuid =
|
|
361
|
+
export const uuid = Scope.module({
|
|
362
362
|
// the meta tuple expression ensures the error message does not delegate
|
|
363
363
|
// to the individual branches, which are too detailed
|
|
364
364
|
root: ["versioned | nil | max", "@", "a UUID"],
|
|
@@ -374,7 +374,7 @@ export const uuid = arkModule({
|
|
|
374
374
|
v7: regexStringNode(/^[0-9a-f]{8}-[0-9a-f]{4}-7[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i, "a UUIDv7"),
|
|
375
375
|
v8: regexStringNode(/^[0-9a-f]{8}-[0-9a-f]{4}-8[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i, "a UUIDv8")
|
|
376
376
|
});
|
|
377
|
-
export const string =
|
|
377
|
+
export const string = Scope.module({
|
|
378
378
|
root: intrinsic.string,
|
|
379
379
|
alpha: regexStringNode(/^[A-Za-z]*$/, "only letters"),
|
|
380
380
|
alphanumeric: regexStringNode(/^[A-Za-z\d]*$/, "only letters and digits 0-9"),
|
package/out/keywords/ts.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { genericNode, intrinsic, node } from "@ark/schema";
|
|
2
2
|
import { Hkt } from "@ark/util";
|
|
3
|
-
import {
|
|
4
|
-
export const arkTsKeywords =
|
|
3
|
+
import { Scope } from "../scope.js";
|
|
4
|
+
export const arkTsKeywords = Scope.module({
|
|
5
5
|
bigint: intrinsic.bigint,
|
|
6
6
|
boolean: intrinsic.boolean,
|
|
7
7
|
false: intrinsic.false,
|
|
@@ -15,11 +15,11 @@ export const arkTsKeywords = arkModule({
|
|
|
15
15
|
unknown: intrinsic.unknown,
|
|
16
16
|
undefined: intrinsic.undefined
|
|
17
17
|
});
|
|
18
|
-
export const unknown =
|
|
18
|
+
export const unknown = Scope.module({
|
|
19
19
|
root: intrinsic.unknown,
|
|
20
20
|
any: intrinsic.unknown
|
|
21
21
|
});
|
|
22
|
-
export const json =
|
|
22
|
+
export const json = Scope.module({
|
|
23
23
|
root: intrinsic.json,
|
|
24
24
|
stringify: node("morph", {
|
|
25
25
|
in: intrinsic.json,
|
|
@@ -27,7 +27,7 @@ export const json = arkModule({
|
|
|
27
27
|
declaredOut: intrinsic.string
|
|
28
28
|
})
|
|
29
29
|
});
|
|
30
|
-
export const object =
|
|
30
|
+
export const object = Scope.module({
|
|
31
31
|
root: intrinsic.object,
|
|
32
32
|
json
|
|
33
33
|
});
|
|
@@ -58,7 +58,7 @@ const Exclude = genericNode("T", "U")(args => args.T.exclude(args.U), ExcludeHkt
|
|
|
58
58
|
class ExtractHkt extends Hkt {
|
|
59
59
|
}
|
|
60
60
|
const Extract = genericNode("T", "U")(args => args.T.extract(args.U), ExtractHkt);
|
|
61
|
-
export const arkTsGenerics =
|
|
61
|
+
export const arkTsGenerics = Scope.module({
|
|
62
62
|
Exclude,
|
|
63
63
|
Extract,
|
|
64
64
|
Omit,
|
|
@@ -4,7 +4,7 @@ import type { type } from "../keywords/keywords.ts";
|
|
|
4
4
|
import type { InnerParseResult } from "../scope.ts";
|
|
5
5
|
import type { shallowDefaultableMessage, shallowOptionalMessage, validateString } from "./ast/validate.ts";
|
|
6
6
|
import { type inferObjectLiteral, type validateObjectLiteral } from "./objectLiteral.ts";
|
|
7
|
-
import type {
|
|
7
|
+
import type { isDefaultable, OptionalPropertyDefinition } from "./property.ts";
|
|
8
8
|
import { type BaseCompletions, type inferString } from "./string.ts";
|
|
9
9
|
import { type inferTupleExpression, type maybeValidateTupleExpression, type TupleExpression } from "./tupleExpressions.ts";
|
|
10
10
|
import { type inferTupleLiteral, type validateTupleLiteral } from "./tupleLiteral.ts";
|
|
@@ -13,7 +13,7 @@ export declare const parseObject: (def: object, ctx: BaseParseContext) => BaseRo
|
|
|
13
13
|
export type inferDefinition<def, $, args> = [
|
|
14
14
|
def
|
|
15
15
|
] extends [anyOrNever] ? def : def extends type.cast<infer t> ? ifEmptyObjectLiteral<def, object, t> : def extends ThunkCast<infer t> ? t : def extends string ? inferString<def, $, args> : def extends array ? inferTuple<def, $, args> : def extends RegExp ? string : def extends object ? inferObjectLiteral<def, $, args> : never;
|
|
16
|
-
export type validateDefinition<def, $, args> = null extends undefined ? ErrorMessage<`'strict' or 'strictNullChecks' must be set to true in your tsconfig's 'compilerOptions'`> : [def] extends [anyOrNever] ? def : def extends OptionalPropertyDefinition ? ErrorMessage<shallowOptionalMessage> : def
|
|
16
|
+
export type validateDefinition<def, $, args> = null extends undefined ? ErrorMessage<`'strict' or 'strictNullChecks' must be set to true in your tsconfig's 'compilerOptions'`> : [def] extends [anyOrNever] ? def : def extends OptionalPropertyDefinition ? ErrorMessage<shallowOptionalMessage> : isDefaultable<def, $, args> extends true ? ErrorMessage<shallowDefaultableMessage> : validateInnerDefinition<def, $, args>;
|
|
17
17
|
export type validateInnerDefinition<def, $, args> = [
|
|
18
18
|
def
|
|
19
19
|
] 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> | {} : RegExp extends def ? def : validateObjectLiteral<def, $, args>;
|
package/out/parser/property.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import type { BaseParseContext, BaseRoot, UndeclaredKeyBehavior } from "@ark/schema";
|
|
2
2
|
import { type anyOrNever, type ErrorMessage, type ErrorType, type typeToString } from "@ark/util";
|
|
3
|
-
import type { validateString } from "./ast/validate.ts";
|
|
4
3
|
import { type inferDefinition, type validateInnerDefinition } from "./definition.ts";
|
|
5
4
|
import type { ParsedKeyKind, writeInvalidSpreadTypeMessage } from "./objectLiteral.ts";
|
|
6
5
|
import type { ParsedDefaultableProperty } from "./shift/operator/default.ts";
|
|
@@ -22,8 +21,8 @@ export type validateProperty<def, keyKind extends ParsedKeyKind, $, args> = [
|
|
|
22
21
|
* })
|
|
23
22
|
* ```
|
|
24
23
|
*/
|
|
25
|
-
def : keyKind extends "spread" ? validateSpread<def, inferDefinition<def, $, args>, $, args> : keyKind extends "undeclared" ? UndeclaredKeyBehavior : keyKind extends "required" ? validateInnerDefinition<def, $, args> : def extends OptionalPropertyDefinition ? ErrorMessage<invalidOptionalKeyKindMessage> : def
|
|
26
|
-
export type
|
|
24
|
+
def : keyKind extends "spread" ? validateSpread<def, inferDefinition<def, $, args>, $, args> : keyKind extends "undeclared" ? UndeclaredKeyBehavior : keyKind extends "required" ? validateInnerDefinition<def, $, args> : def extends OptionalPropertyDefinition ? ErrorMessage<invalidOptionalKeyKindMessage> : isDefaultable<def, $, args> extends true ? ErrorMessage<invalidDefaultableKeyKindMessage> : validateInnerDefinition<def, $, args>;
|
|
25
|
+
export type isDefaultable<def, $, args> = def extends DefaultablePropertyTuple ? true : def extends PossibleDefaultableStringDefinition ? parseString<def, $, args> extends DefaultablePropertyTuple ? true : false : false;
|
|
27
26
|
type validateSpread<def, inferredProperty, $, args> = inferredProperty extends object ? validateInnerDefinition<def, $, args> : ErrorType<writeInvalidSpreadTypeMessage<typeToString<inferredProperty>>>;
|
|
28
27
|
export type OptionalPropertyDefinition<baseDef = unknown> = OptionalPropertyTuple<baseDef> | OptionalPropertyString<baseDef & string>;
|
|
29
28
|
export type OptionalPropertyString<baseDef extends string = string> = `${baseDef}?`;
|
|
@@ -26,7 +26,7 @@ export declare class ArkTypeScanner<lookahead extends string = string> extends S
|
|
|
26
26
|
readonly "=": 1;
|
|
27
27
|
readonly "?": 1;
|
|
28
28
|
};
|
|
29
|
-
static lookaheadIsFinalizing: (lookahead: string, unscanned: string) => lookahead is ">" | "," | "=";
|
|
29
|
+
static lookaheadIsFinalizing: (lookahead: string, unscanned: string) => lookahead is ">" | "," | "=" | "?";
|
|
30
30
|
}
|
|
31
31
|
export declare namespace ArkTypeScanner {
|
|
32
32
|
type lookaheadIsFinalizing<lookahead extends string, unscanned extends string> = lookahead extends ">" ? unscanned extends `=${infer nextUnscanned}` ? nextUnscanned extends `=${string}` ? true : false : ArkTypeScanner.skipWhitespace<unscanned> extends ("" | `${TerminatingChar}${string}`) ? true : false : lookahead extends "=" ? unscanned extends `=${string}` ? false : true : lookahead extends "," | "?" ? true : false;
|
|
@@ -31,7 +31,8 @@ export class ArkTypeScanner extends Scanner {
|
|
|
31
31
|
// >== would only occur in an expression like Array<number>==5
|
|
32
32
|
// otherwise, >= would only occur as part of a bound like number>=5
|
|
33
33
|
unscanned[1] === "="
|
|
34
|
-
// if > is the end of a generic instantiation, the next token will be
|
|
34
|
+
// if > is the end of a generic instantiation, the next token will be
|
|
35
|
+
// an operator or the end of the string
|
|
35
36
|
: unscanned.trimStart() === "" ||
|
|
36
37
|
isKeyOf(unscanned.trimStart()[0], ArkTypeScanner.terminatingChars)
|
|
37
38
|
// "=" is a finalizer on its own (representing a default value),
|
|
@@ -1,24 +1,30 @@
|
|
|
1
|
-
import { type BaseParseContext, type BaseRoot, type Sequence } from "@ark/schema";
|
|
1
|
+
import { postfixAfterOptionalOrDefaultableMessage, type BaseParseContext, type BaseRoot, type Sequence } from "@ark/schema";
|
|
2
2
|
import { type array, type conform, type ErrorMessage, type satisfy } from "@ark/util";
|
|
3
3
|
import type { inferDefinition, validateInnerDefinition } from "./definition.ts";
|
|
4
|
-
import { type DefaultablePropertyTuple, type
|
|
4
|
+
import { type DefaultablePropertyTuple, type isDefaultable, type OptionalPropertyDefinition } from "./property.ts";
|
|
5
5
|
export declare const parseTupleLiteral: (def: array, ctx: BaseParseContext) => BaseRoot;
|
|
6
6
|
export type validateTupleLiteral<def extends array, $, args> = parseSequence<def, $, args> extends infer s extends SequenceParseState ? Readonly<s["validated"]> : never;
|
|
7
7
|
export type inferTupleLiteral<def extends array, $, args> = parseSequence<def, $, args> extends infer s extends SequenceParseState ? s["inferred"] : never;
|
|
8
|
-
type
|
|
8
|
+
type SequencePhase = satisfy<keyof Sequence.Inner, SequencePhase.prefix | SequencePhase.optionals | SequencePhase.defaultables | SequencePhase.postfix>;
|
|
9
|
+
declare namespace SequencePhase {
|
|
10
|
+
type prefix = "prefix";
|
|
11
|
+
type optionals = "optionals";
|
|
12
|
+
type defaultables = "defaultables";
|
|
13
|
+
type postfix = "postfix";
|
|
14
|
+
}
|
|
9
15
|
type SequenceParseState = {
|
|
10
16
|
unscanned: array;
|
|
11
17
|
inferred: array;
|
|
12
18
|
validated: array;
|
|
13
|
-
phase:
|
|
19
|
+
phase: SequencePhase;
|
|
14
20
|
};
|
|
15
21
|
type parseSequence<def extends array, $, args> = parseNextElement<{
|
|
16
22
|
unscanned: def;
|
|
17
23
|
inferred: [];
|
|
18
24
|
validated: [];
|
|
19
|
-
phase:
|
|
25
|
+
phase: SequencePhase.prefix;
|
|
20
26
|
}, $, args>;
|
|
21
|
-
type PreparsedElementKind = "required" |
|
|
27
|
+
type PreparsedElementKind = "required" | SequencePhase.optionals | SequencePhase.defaultables;
|
|
22
28
|
type PreparsedElement = {
|
|
23
29
|
head: unknown;
|
|
24
30
|
tail: array;
|
|
@@ -29,6 +35,9 @@ type PreparsedElement = {
|
|
|
29
35
|
};
|
|
30
36
|
declare namespace PreparsedElement {
|
|
31
37
|
type from<result extends PreparsedElement> = result;
|
|
38
|
+
type required = "required";
|
|
39
|
+
type optionals = "optionals";
|
|
40
|
+
type defaultables = "defaultables";
|
|
32
41
|
}
|
|
33
42
|
type preparseNextState<s extends SequenceParseState, $, args> = s["unscanned"] extends readonly ["...", infer head, ...infer tail] ? preparseNextElement<head, tail, true, $, args> : s["unscanned"] extends readonly [infer head, ...infer tail] ? preparseNextElement<head, tail, false, $, args> : null;
|
|
34
43
|
type preparseNextElement<head, tail extends array, spread extends boolean, $, args> = PreparsedElement.from<{
|
|
@@ -36,19 +45,22 @@ type preparseNextElement<head, tail extends array, spread extends boolean, $, ar
|
|
|
36
45
|
tail: tail;
|
|
37
46
|
inferred: inferDefinition<head, $, args>;
|
|
38
47
|
validated: validateInnerDefinition<head, $, args>;
|
|
39
|
-
kind: head extends OptionalPropertyDefinition ?
|
|
48
|
+
kind: head extends OptionalPropertyDefinition ? PreparsedElement.optionals : head extends DefaultablePropertyTuple ? PreparsedElement.defaultables : isDefaultable<head, $, args> extends true ? PreparsedElement.defaultables : PreparsedElement.required;
|
|
40
49
|
spread: spread;
|
|
41
50
|
}>;
|
|
42
51
|
type parseNextElement<s extends SequenceParseState, $, args> = preparseNextState<s, $, args> extends infer next extends PreparsedElement ? parseNextElement<{
|
|
43
52
|
unscanned: next["tail"];
|
|
44
53
|
inferred: nextInferred<s, next>;
|
|
45
54
|
validated: nextValidated<s, next>;
|
|
46
|
-
phase: next["kind"] extends
|
|
55
|
+
phase: next["kind"] extends (SequencePhase.optionals | SequencePhase.defaultables) ? next["kind"] : number extends nextInferred<s, next>["length"] ? s["phase"] : SequencePhase.prefix;
|
|
47
56
|
}, $, args> : s;
|
|
48
57
|
type nextInferred<s extends SequenceParseState, next extends PreparsedElement> = next["spread"] extends true ? [
|
|
49
58
|
...s["inferred"],
|
|
50
59
|
...conform<next["inferred"], array>
|
|
51
|
-
] : next["kind"] extends
|
|
60
|
+
] : next["kind"] extends SequencePhase.optionals ? [
|
|
61
|
+
...s["inferred"],
|
|
62
|
+
next["inferred"]?
|
|
63
|
+
] : [...s["inferred"], next["inferred"]];
|
|
52
64
|
type nextValidated<s extends SequenceParseState, next extends PreparsedElement> = [
|
|
53
65
|
...s["validated"],
|
|
54
66
|
...nextValidatedSpreadOperatorIfPresent<s, next>,
|
|
@@ -56,25 +68,28 @@ type nextValidated<s extends SequenceParseState, next extends PreparsedElement>
|
|
|
56
68
|
];
|
|
57
69
|
type nextValidatedSpreadOperatorIfPresent<s extends SequenceParseState, next extends PreparsedElement> = next["spread"] extends true ? [
|
|
58
70
|
next["inferred"] extends infer spreadOperand extends array ? [
|
|
59
|
-
|
|
71
|
+
number,
|
|
60
72
|
number
|
|
61
|
-
] extends [
|
|
73
|
+
] extends ([
|
|
74
|
+
s["inferred"]["length"],
|
|
75
|
+
spreadOperand["length"]
|
|
76
|
+
]) ? ErrorMessage<multipleVariadicMessage> : "..." : ErrorMessage<writeNonArraySpreadMessage<next["head"]>>
|
|
62
77
|
] : [];
|
|
63
|
-
type nextValidatedElement<s extends SequenceParseState, next extends PreparsedElement> = next["kind"] extends
|
|
78
|
+
type nextValidatedElement<s extends SequenceParseState, next extends PreparsedElement> = next["kind"] extends SequencePhase.optionals ? next["spread"] extends true ? ErrorMessage<spreadOptionalMessage> : s["phase"] extends SequencePhase.postfix ? ErrorMessage<optionalOrDefaultableAfterVariadicMessage> : next["validated"] : next["kind"] extends SequencePhase.defaultables ? next["spread"] extends true ? ErrorMessage<spreadDefaultableMessage> : s["phase"] extends SequencePhase.optionals ? ErrorMessage<defaultablePostOptionalMessage> : s["phase"] extends SequencePhase.postfix ? ErrorMessage<optionalOrDefaultableAfterVariadicMessage> : next["validated"] : [s["phase"], next["spread"]] extends ([
|
|
79
|
+
SequencePhase.optionals | SequencePhase.defaultables,
|
|
80
|
+
false
|
|
81
|
+
]) ? ErrorMessage<postfixAfterOptionalOrDefaultableMessage> : next["validated"];
|
|
64
82
|
export declare const writeNonArraySpreadMessage: <operand extends string>(operand: operand) => writeNonArraySpreadMessage<operand>;
|
|
65
83
|
type writeNonArraySpreadMessage<operand> = `Spread element must be an array${operand extends string ? ` (was ${operand})` : ""}`;
|
|
66
84
|
export declare const multipleVariadicMesage = "A tuple may have at most one variadic element";
|
|
67
85
|
type multipleVariadicMessage = typeof multipleVariadicMesage;
|
|
68
86
|
export declare const requiredPostOptionalMessage = "A required element may not follow an optional element";
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
type optionalPostVariadicMessage = typeof optionalPostVariadicMessage;
|
|
87
|
+
export declare const optionalOrDefaultableAfterVariadicMessage = "An optional element may not follow a variadic element";
|
|
88
|
+
type optionalOrDefaultableAfterVariadicMessage = typeof optionalOrDefaultableAfterVariadicMessage;
|
|
72
89
|
export declare const spreadOptionalMessage = "A spread element cannot be optional";
|
|
73
90
|
type spreadOptionalMessage = typeof spreadOptionalMessage;
|
|
74
91
|
export declare const spreadDefaultableMessage = "A spread element cannot have a default";
|
|
75
92
|
type spreadDefaultableMessage = typeof spreadDefaultableMessage;
|
|
76
|
-
export declare const defaultablePostVariadicMessage = "A defaultable element may not follow a variadic element";
|
|
77
|
-
type defaultablePostVariadicMessage = typeof defaultablePostVariadicMessage;
|
|
78
93
|
export declare const defaultablePostOptionalMessage = "A defaultable element may not follow an optional element without a default";
|
|
79
94
|
type defaultablePostOptionalMessage = typeof defaultablePostOptionalMessage;
|
|
80
95
|
export {};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { $ark, makeRootAndArrayPropertiesMutable } from "@ark/schema";
|
|
1
|
+
import { $ark, makeRootAndArrayPropertiesMutable, postfixAfterOptionalOrDefaultableMessage } from "@ark/schema";
|
|
2
2
|
import { append, isArray, isEmptyObject, throwParseError } from "@ark/util";
|
|
3
3
|
import { parseProperty } from "./property.js";
|
|
4
4
|
export const parseTupleLiteral = (def, ctx) => {
|
|
@@ -44,9 +44,13 @@ export const parseTupleLiteral = (def, ctx) => {
|
|
|
44
44
|
}));
|
|
45
45
|
};
|
|
46
46
|
const appendRequiredElement = (base, element) => {
|
|
47
|
-
if (base.optionals)
|
|
48
|
-
|
|
49
|
-
|
|
47
|
+
if (base.defaultables || base.optionals) {
|
|
48
|
+
return throwParseError(base.variadic ?
|
|
49
|
+
// e.g. [boolean = true, ...string[], number]
|
|
50
|
+
postfixAfterOptionalOrDefaultableMessage
|
|
51
|
+
// e.g. [string?, number]
|
|
52
|
+
: requiredPostOptionalMessage);
|
|
53
|
+
}
|
|
50
54
|
if (base.variadic) {
|
|
51
55
|
// e.g. [...string[], number]
|
|
52
56
|
base.postfix = append(base.postfix, element);
|
|
@@ -60,7 +64,7 @@ const appendRequiredElement = (base, element) => {
|
|
|
60
64
|
const appendOptionalElement = (base, element) => {
|
|
61
65
|
if (base.variadic)
|
|
62
66
|
// e.g. [...string[], number?]
|
|
63
|
-
return throwParseError(
|
|
67
|
+
return throwParseError(optionalOrDefaultableAfterVariadicMessage);
|
|
64
68
|
// e.g. [string, number?]
|
|
65
69
|
base.optionals = append(base.optionals, element);
|
|
66
70
|
return base;
|
|
@@ -68,7 +72,7 @@ const appendOptionalElement = (base, element) => {
|
|
|
68
72
|
const appendDefaultableElement = (base, element, value) => {
|
|
69
73
|
if (base.variadic)
|
|
70
74
|
// e.g. [...string[], number = 0]
|
|
71
|
-
return throwParseError(
|
|
75
|
+
return throwParseError(optionalOrDefaultableAfterVariadicMessage);
|
|
72
76
|
if (base.optionals)
|
|
73
77
|
// e.g. [string?, number = 0]
|
|
74
78
|
return throwParseError(defaultablePostOptionalMessage);
|
|
@@ -112,8 +116,7 @@ const appendSpreadBranch = (base, branch) => {
|
|
|
112
116
|
export const writeNonArraySpreadMessage = (operand) => `Spread element must be an array (was ${operand})`;
|
|
113
117
|
export const multipleVariadicMesage = "A tuple may have at most one variadic element";
|
|
114
118
|
export const requiredPostOptionalMessage = "A required element may not follow an optional element";
|
|
115
|
-
export const
|
|
119
|
+
export const optionalOrDefaultableAfterVariadicMessage = "An optional element may not follow a variadic element";
|
|
116
120
|
export const spreadOptionalMessage = "A spread element cannot be optional";
|
|
117
121
|
export const spreadDefaultableMessage = "A spread element cannot have a default";
|
|
118
|
-
export const defaultablePostVariadicMessage = "A defaultable element may not follow a variadic element";
|
|
119
122
|
export const defaultablePostOptionalMessage = "A defaultable element may not follow an optional element without a default";
|
package/out/scope.d.ts
CHANGED
package/out/scope.js
CHANGED
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "arktype",
|
|
3
3
|
"description": "TypeScript's 1:1 validator, optimized from editor to runtime",
|
|
4
|
-
"version": "2.0.0-rc.
|
|
4
|
+
"version": "2.0.0-rc.29",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": {
|
|
7
7
|
"name": "David Blass",
|
|
@@ -34,8 +34,8 @@
|
|
|
34
34
|
"out"
|
|
35
35
|
],
|
|
36
36
|
"dependencies": {
|
|
37
|
-
"@ark/util": "0.
|
|
38
|
-
"@ark/schema": "0.
|
|
37
|
+
"@ark/util": "0.29.0",
|
|
38
|
+
"@ark/schema": "0.29.0"
|
|
39
39
|
},
|
|
40
40
|
"publishConfig": {
|
|
41
41
|
"access": "public"
|
package/out/keywords/utils.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const arkModule: import("../scope.ts").ModuleParser;
|
package/out/keywords/utils.js
DELETED