arktype 1.0.4-alpha → 1.0.6-alpha
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/README.md +96 -18
- package/dist/cjs/nodes/compose.js +3 -3
- package/dist/cjs/nodes/discriminate.js +2 -1
- package/dist/cjs/nodes/node.js +3 -3
- package/dist/cjs/nodes/rules/class.js +1 -3
- package/dist/cjs/parse/ast/keyof.js +3 -2
- package/dist/cjs/parse/ast/tuple.js +1 -1
- package/dist/cjs/parse/definition.js +1 -1
- package/dist/cjs/parse/string/shift/operand/operand.js +1 -1
- package/dist/cjs/parse/string/shift/operator/bounds.js +1 -1
- package/dist/cjs/parse/string/shift/operator/operator.js +1 -1
- package/dist/cjs/scopes/jsObjects.js +14 -14
- package/dist/cjs/scopes/type.js +4 -12
- package/dist/cjs/traverse/problems.js +3 -1
- package/dist/cjs/traverse/traverse.js +53 -10
- package/dist/cjs/utils/generics.js +0 -5
- package/dist/mjs/nodes/compose.js +2 -2
- package/dist/mjs/nodes/discriminate.js +3 -2
- package/dist/mjs/nodes/node.js +5 -5
- package/dist/mjs/nodes/rules/class.js +2 -4
- package/dist/mjs/parse/ast/keyof.js +3 -2
- package/dist/mjs/parse/ast/tuple.js +1 -1
- package/dist/mjs/parse/definition.js +1 -1
- package/dist/mjs/parse/string/shift/operand/operand.js +1 -1
- package/dist/mjs/parse/string/shift/operator/bounds.js +1 -1
- package/dist/mjs/parse/string/shift/operator/operator.js +3 -1
- package/dist/mjs/scopes/jsObjects.js +14 -14
- package/dist/mjs/scopes/type.js +5 -12
- package/dist/mjs/traverse/problems.js +3 -1
- package/dist/mjs/traverse/traverse.js +52 -10
- package/dist/mjs/utils/generics.js +0 -4
- package/dist/tsconfig.temp.tsbuildinfo +1 -1
- package/dist/types/main.d.ts +1 -0
- package/dist/types/nodes/compose.d.ts +1 -1
- package/dist/types/nodes/rules/class.d.ts +1 -1
- package/dist/types/nodes/rules/rules.d.ts +2 -3
- package/dist/types/parse/ast/morph.d.ts +2 -1
- package/dist/types/parse/ast/node.d.ts +5 -8
- package/dist/types/parse/definition.d.ts +5 -5
- package/dist/types/parse/string/shift/operand/operand.d.ts +1 -1
- package/dist/types/scopes/ark.d.ts +4 -12
- package/dist/types/scopes/jsObjects.d.ts +2 -6
- package/dist/types/scopes/type.d.ts +5 -10
- package/dist/types/traverse/problems.d.ts +2 -2
- package/dist/types/traverse/traverse.d.ts +8 -1
- package/dist/types/utils/generics.d.ts +2 -3
- package/dist/types/utils/numericLiterals.d.ts +2 -2
- package/package.json +11 -7
package/README.md
CHANGED
|
@@ -1,21 +1,36 @@
|
|
|
1
1
|
<h1 align="center">ArkType <sub><sup>Isomorphic types for TS/JS</sup></sub></h1>
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
<sub
|
|
3
|
+
[<img src="./dev/arktype.io/static/img/arktype.gif">](https://arktype.io/try)
|
|
4
|
+
<sub>
|
|
5
|
+
<i>`typescript@4.9.5` in VS Code— no extensions or plugins required (<a href="#how">how?</a>) (<a href="https://arktype.io/try">try in-browser</a>)</i>
|
|
6
|
+
</sub>
|
|
7
|
+
<br />
|
|
5
8
|
|
|
6
|
-
|
|
9
|
+
<!-- @snipStart:intro -->
|
|
10
|
+
|
|
11
|
+
ArkType is a type-safe runtime validator that understands TypeScript syntax.
|
|
12
|
+
<br />
|
|
13
|
+
<br />
|
|
14
|
+
Each character you type is statically parsed so you'll know exactly what to expect from editor to runtime ⛵
|
|
15
|
+
|
|
16
|
+
<!-- @snipEnd -->
|
|
7
17
|
|
|
8
|
-
|
|
18
|
+
<!-- @snipStart:install -->
|
|
9
19
|
|
|
10
|
-
|
|
20
|
+
## Install <sub><sub>📦`4KB` gzipped, `0` dependencies</sub></sub>
|
|
21
|
+
|
|
22
|
+
<img src="./dev/arktype.io/static/img/npm.svg" alt="Npm Icon" height="16px" /> <code>npm install arktype</code>
|
|
23
|
+
<sub>(or whatever package manager you prefer)</sub>
|
|
24
|
+
<br />
|
|
11
25
|
|
|
12
|
-
<img src="./dev/arktype.io/static/img/
|
|
13
|
-
your IDE)</sub>
|
|
26
|
+
<img src="./dev/arktype.io/static/img/deno.svg" alt="Deno Icon" height="16px" /> <code>import { type } from "https://deno.land/x/arktype/main.ts"</code>
|
|
14
27
|
|
|
15
28
|
Our types are tested in [strict-mode](https://www.typescriptlang.org/tsconfig#strict) with TypeScript versions `4.8`, `4.9`, and `5.0`.
|
|
16
29
|
|
|
17
30
|
_Our APIs have mostly stabilized, but details may still change during the alpha/beta stages of our 1.0 release. If you have suggestions that may require a breaking change, now is the time to let us know!_ ⛵
|
|
18
31
|
|
|
32
|
+
<!-- @snipEnd -->
|
|
33
|
+
|
|
19
34
|
## Types
|
|
20
35
|
|
|
21
36
|
[Try it in-browser.](https://arktype.io/docs/#your-first-type)
|
|
@@ -23,7 +38,7 @@ _Our APIs have mostly stabilized, but details may still change during the alpha/
|
|
|
23
38
|
```ts @blockFrom:dev/examples/type.ts
|
|
24
39
|
import { type } from "arktype"
|
|
25
40
|
|
|
26
|
-
// Define
|
|
41
|
+
// Define your type...
|
|
27
42
|
export const user = type({
|
|
28
43
|
name: "string",
|
|
29
44
|
device: {
|
|
@@ -35,7 +50,7 @@ export const user = type({
|
|
|
35
50
|
// Infer it...
|
|
36
51
|
export type User = typeof user.infer
|
|
37
52
|
|
|
38
|
-
//
|
|
53
|
+
// Get validated data or clear, customizable error messages.
|
|
39
54
|
export const { data, problems } = user({
|
|
40
55
|
name: "Alan Turing",
|
|
41
56
|
device: {
|
|
@@ -84,22 +99,85 @@ packageData.dependencies![0].dependencies = [packageData]
|
|
|
84
99
|
export const { data, problems } = types.package(packageData)
|
|
85
100
|
```
|
|
86
101
|
|
|
87
|
-
|
|
102
|
+
### API
|
|
103
|
+
|
|
104
|
+
<!--@snipStart:api -->
|
|
105
|
+
|
|
106
|
+
ArkType supports many of TypeScript's built-in types and operators, as well as some new ones dedicated exclusively to runtime validation. In fact, we got a little ahead of ourselves and built a ton of cool features, but we're still working on getting caught up syntax and API docs. Keep an eye out for more in the next couple weeks ⛵
|
|
107
|
+
|
|
108
|
+
In the meantime, check out the examples here and use the type hints you get to learn how you can customize your types and scopes. If you want to explore some of the more advanced features, take a look at [our unit tests](./dev/test) and don't hesitate to reach out on our [Discord channel](https://discord.gg/WSNF3Kc4xh)!
|
|
109
|
+
|
|
110
|
+
<!--@snipEnd -->
|
|
111
|
+
|
|
112
|
+
## How
|
|
113
|
+
|
|
114
|
+
ArkType's isomorphic parser has parallel static and dynamic implementations. This means as soon as you type a definition in your editor, you'll know the eventual result at runtime.
|
|
115
|
+
|
|
116
|
+
If you're curious, below is an example of what that looks like under the hood. If not, close that hood back up, `npm install arktype` and enjoy top-notch developer experience 🧑💻
|
|
117
|
+
|
|
118
|
+
```ts @blockFrom:src/parse/string/shift/operator/operator.ts:parseOperator
|
|
119
|
+
export const parseOperator = (s: DynamicState): void => {
|
|
120
|
+
const lookahead = s.scanner.shift()
|
|
121
|
+
return lookahead === ""
|
|
122
|
+
? s.finalize()
|
|
123
|
+
: lookahead === "["
|
|
124
|
+
? s.scanner.shift() === "]"
|
|
125
|
+
? s.rootToArray()
|
|
126
|
+
: s.error(incompleteArrayTokenMessage)
|
|
127
|
+
: isKeyOf(lookahead, Scanner.branchTokens)
|
|
128
|
+
? s.pushRootToBranch(lookahead)
|
|
129
|
+
: lookahead === ")"
|
|
130
|
+
? s.finalizeGroup()
|
|
131
|
+
: isKeyOf(lookahead, Scanner.comparatorStartChars)
|
|
132
|
+
? parseBound(s, lookahead)
|
|
133
|
+
: lookahead === "%"
|
|
134
|
+
? parseDivisor(s)
|
|
135
|
+
: lookahead === " "
|
|
136
|
+
? parseOperator(s)
|
|
137
|
+
: throwInternalError(writeUnexpectedCharacterMessage(lookahead))
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
export type parseOperator<s extends StaticState> =
|
|
141
|
+
s["unscanned"] extends Scanner.shift<infer lookahead, infer unscanned>
|
|
142
|
+
? lookahead extends "["
|
|
143
|
+
? unscanned extends Scanner.shift<"]", infer nextUnscanned>
|
|
144
|
+
? state.setRoot<s, [s["root"], "[]"], nextUnscanned>
|
|
145
|
+
: error<incompleteArrayTokenMessage>
|
|
146
|
+
: lookahead extends Scanner.BranchToken
|
|
147
|
+
? state.reduceBranch<s, lookahead, unscanned>
|
|
148
|
+
: lookahead extends ")"
|
|
149
|
+
? state.finalizeGroup<s, unscanned>
|
|
150
|
+
: lookahead extends Scanner.ComparatorStartChar
|
|
151
|
+
? parseBound<s, lookahead, unscanned>
|
|
152
|
+
: lookahead extends "%"
|
|
153
|
+
? parseDivisor<s, unscanned>
|
|
154
|
+
: lookahead extends " "
|
|
155
|
+
? parseOperator<state.scanTo<s, unscanned>>
|
|
156
|
+
: error<writeUnexpectedCharacterMessage<lookahead>>
|
|
157
|
+
: state.finalize<s>
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
## Contributions
|
|
161
|
+
|
|
162
|
+
We accept and encourage pull requests from outside ArkType.
|
|
163
|
+
|
|
164
|
+
Depending on your level of familiarity with type systems and TS generics, some parts of the codebase may be hard to jump into. That said, there's plenty of opportunities for more straightforward contributions.
|
|
165
|
+
|
|
166
|
+
If you're planning on submitting a non-trivial fix or a new feature, please [create an issue first](https://github.com/arktypeio/arktype/issues/new) so everyone's on the same page. The last thing we want is for you to spend time on a submission we're unable to merge.
|
|
88
167
|
|
|
89
|
-
|
|
168
|
+
When you're ready, check out our [guide](./dev/configs/CONTRIBUTING.md) to get started!
|
|
90
169
|
|
|
91
|
-
##
|
|
170
|
+
## Sponsorship
|
|
92
171
|
|
|
93
|
-
|
|
172
|
+
We've been working full-time on this project for over a year and it means a lot to have the community behind us.
|
|
94
173
|
|
|
95
|
-
|
|
96
|
-
2. Check out our [guide](./dev/configs/CONTRIBUTING.md) to get started!
|
|
174
|
+
If the project has been useful to you and you are in a financial position to do so, please feel free to chip in [via our Patreon](https://www.patreon.com/ArkType240).
|
|
97
175
|
|
|
98
|
-
|
|
176
|
+
Otherwise, consider sending me an email (david@arktype.io) or [message me on Discord](https://discord.gg/WSNF3Kc4xh) to let me know you're a fan of ArkType. Either would make my day! 😊
|
|
99
177
|
|
|
100
|
-
|
|
178
|
+
## Collaboration
|
|
101
179
|
|
|
102
|
-
|
|
180
|
+
I'd love to hear about what you're working on and how ArkType can help. Please reach out to david@arktype.io.
|
|
103
181
|
|
|
104
182
|
## License
|
|
105
183
|
|
|
@@ -10,7 +10,7 @@ function _export(target, all) {
|
|
|
10
10
|
}
|
|
11
11
|
_export(exports, {
|
|
12
12
|
composeIntersection: ()=>composeIntersection,
|
|
13
|
-
|
|
13
|
+
undefinedOperandsMessage: ()=>undefinedOperandsMessage,
|
|
14
14
|
disjointDescriptionWriters: ()=>disjointDescriptionWriters,
|
|
15
15
|
stringifyRange: ()=>stringifyRange,
|
|
16
16
|
IntersectionState: ()=>IntersectionState,
|
|
@@ -64,8 +64,8 @@ function _classPrivateFieldSet(receiver, privateMap, value) {
|
|
|
64
64
|
_classApplyDescriptorSet(receiver, descriptor, value);
|
|
65
65
|
return value;
|
|
66
66
|
}
|
|
67
|
-
const composeIntersection = (reducer)=>(l, r, state)=>l === undefined ? r === undefined ?
|
|
68
|
-
const
|
|
67
|
+
const composeIntersection = (reducer)=>(l, r, state)=>l === undefined ? r === undefined ? (0, _errorsTs.throwInternalError)(undefinedOperandsMessage) : r : r === undefined ? l : reducer(l, r, state);
|
|
68
|
+
const undefinedOperandsMessage = `Unexpected operation two undefined operands`;
|
|
69
69
|
const disjointDescriptionWriters = {
|
|
70
70
|
domain: ({ l , r })=>`${l.join(", ")} and ${r.join(", ")}`,
|
|
71
71
|
range: ({ l , r })=>`${stringifyRange(l)} and ${stringifyRange(r)}`,
|
|
@@ -243,7 +243,8 @@ const serializeDefinitionIfAllowed = (kind, definition)=>{
|
|
|
243
243
|
case "domain":
|
|
244
244
|
return definition;
|
|
245
245
|
case "class":
|
|
246
|
-
|
|
246
|
+
const constructorName = Object(definition).name;
|
|
247
|
+
return constructorName && (0, _genericsTs.isKeyOf)(constructorName, _objectKindsTs.defaultObjectKinds) && _objectKindsTs.defaultObjectKinds[constructorName] === definition ? constructorName : undefined;
|
|
247
248
|
default:
|
|
248
249
|
return;
|
|
249
250
|
}
|
package/dist/cjs/nodes/node.js
CHANGED
|
@@ -40,7 +40,7 @@ const nodeIntersection = (l, r, state)=>{
|
|
|
40
40
|
};
|
|
41
41
|
const typeNodeIntersection = (0, _composeTs.composeKeyedIntersection)((domain, l, r, context)=>{
|
|
42
42
|
if (l === undefined) {
|
|
43
|
-
return r === undefined ? (0,
|
|
43
|
+
return r === undefined ? (0, _errorsTs.throwInternalError)(_composeTs.undefinedOperandsMessage) : undefined;
|
|
44
44
|
}
|
|
45
45
|
if (r === undefined) {
|
|
46
46
|
return undefined;
|
|
@@ -63,7 +63,7 @@ const rootUnion = (l, r, type)=>{
|
|
|
63
63
|
...rDomains
|
|
64
64
|
});
|
|
65
65
|
for (const domain of domains){
|
|
66
|
-
result[domain] = (0, _genericsTs.hasKey)(lDomains, domain) ? (0, _genericsTs.hasKey)(rDomains, domain) ? (0, _predicateTs.predicateUnion)(domain, lDomains[domain], rDomains[domain], type) : lDomains[domain] : (0, _genericsTs.hasKey)(rDomains, domain) ? rDomains[domain] : (0,
|
|
66
|
+
result[domain] = (0, _genericsTs.hasKey)(lDomains, domain) ? (0, _genericsTs.hasKey)(rDomains, domain) ? (0, _predicateTs.predicateUnion)(domain, lDomains[domain], rDomains[domain], type) : lDomains[domain] : (0, _genericsTs.hasKey)(rDomains, domain) ? rDomains[domain] : (0, _errorsTs.throwInternalError)(_composeTs.undefinedOperandsMessage);
|
|
67
67
|
}
|
|
68
68
|
return result;
|
|
69
69
|
};
|
|
@@ -131,7 +131,7 @@ const resolutionExtendsDomain = (resolution, domain)=>{
|
|
|
131
131
|
};
|
|
132
132
|
const toArrayNode = (node)=>({
|
|
133
133
|
object: {
|
|
134
|
-
class:
|
|
134
|
+
class: Array,
|
|
135
135
|
props: {
|
|
136
136
|
[_propsTs.mappedKeys.index]: node
|
|
137
137
|
}
|
|
@@ -15,9 +15,7 @@ _export(exports, {
|
|
|
15
15
|
const _objectKindsTs = require("../../utils/objectKinds.js");
|
|
16
16
|
const _composeTs = require("../compose.js");
|
|
17
17
|
const classIntersection = (0, _composeTs.composeIntersection)((l, r, state)=>{
|
|
18
|
-
|
|
19
|
-
const rClass = typeof r === "string" ? _objectKindsTs.defaultObjectKinds[r] : r;
|
|
20
|
-
return lClass === rClass ? (0, _composeTs.equality)() : lClass instanceof rClass ? l : rClass instanceof lClass ? r : state.addDisjoint("class", l, r);
|
|
18
|
+
return l === r ? (0, _composeTs.equality)() : l instanceof r ? l : r instanceof l ? r : state.addDisjoint("class", l, r);
|
|
21
19
|
});
|
|
22
20
|
const checkClass = (expectedClass, state)=>{
|
|
23
21
|
if (typeof expectedClass === "string") {
|
|
@@ -23,7 +23,7 @@ const arrayIndexNumberBranch = (0, _freezeTs.deepFreeze)({
|
|
|
23
23
|
range: {
|
|
24
24
|
min: {
|
|
25
25
|
comparator: ">=",
|
|
26
|
-
limit:
|
|
26
|
+
limit: 0
|
|
27
27
|
}
|
|
28
28
|
},
|
|
29
29
|
divisor: 1
|
|
@@ -51,7 +51,7 @@ const parseKeyOfTuple = (def, ctx)=>{
|
|
|
51
51
|
} else {
|
|
52
52
|
return (0, _errorsTs.throwInternalError)(`Unexpected keyof key '${(0, _serializeTs.stringify)(key)}'`);
|
|
53
53
|
}
|
|
54
|
-
}
|
|
54
|
+
/* c8 ignore stop*/ }
|
|
55
55
|
return Object.fromEntries(Object.entries(keyNode).map(([domain, branches])=>[
|
|
56
56
|
domain,
|
|
57
57
|
branches.length === 1 ? branches[0] : branches
|
|
@@ -87,6 +87,7 @@ const keysOfObjectBranch = (branch)=>{
|
|
|
87
87
|
if ("props" in branch) {
|
|
88
88
|
for (const key of Object.keys(branch.props)){
|
|
89
89
|
if (key === _propsTs.mappedKeys.index) {
|
|
90
|
+
// if any number is a valid key push this RegExp
|
|
90
91
|
result.push(_numericLiteralsTs.wellFormedNonNegativeIntegerMatcher);
|
|
91
92
|
} else if (!result.includes(key)) {
|
|
92
93
|
result.push(key);
|
|
@@ -58,6 +58,6 @@ const parseDefinition = (def, ctx)=>{
|
|
|
58
58
|
}
|
|
59
59
|
};
|
|
60
60
|
const as = Symbol("as");
|
|
61
|
-
const unknownDefinitionMessage = "Cannot statically parse a definition inferred as unknown. Consider using 'as
|
|
61
|
+
const unknownDefinitionMessage = "Cannot statically parse a definition inferred as unknown. Consider using 'as Infer<...>' to cast it.";
|
|
62
62
|
const isThunk = (def)=>typeof def === "function" && def.length === 0;
|
|
63
63
|
const writeBadDefinitionTypeMessage = (actual)=>`Type definitions must be strings or objects (was ${actual})`;
|
|
@@ -8,4 +8,4 @@ Object.defineProperty(exports, "parseOperand", {
|
|
|
8
8
|
});
|
|
9
9
|
const _enclosedTs = require("./enclosed.js");
|
|
10
10
|
const _unenclosedTs = require("./unenclosed.js");
|
|
11
|
-
const parseOperand = (s)=>s.scanner.lookahead === "" ? s.error((0, _unenclosedTs.writeMissingOperandMessage)(s)) : s.scanner.lookahead === "(" ? s.shiftedByOne().reduceGroupOpen() : s.scanner.lookaheadIsIn(_enclosedTs.enclosingChar) ? (0, _enclosedTs.parseEnclosed)(s, s.scanner.shift()) : (0, _unenclosedTs.parseUnenclosed)(s);
|
|
11
|
+
const parseOperand = (s)=>s.scanner.lookahead === "" ? s.error((0, _unenclosedTs.writeMissingOperandMessage)(s)) : s.scanner.lookahead === "(" ? s.shiftedByOne().reduceGroupOpen() : s.scanner.lookaheadIsIn(_enclosedTs.enclosingChar) ? (0, _enclosedTs.parseEnclosed)(s, s.scanner.shift()) : s.scanner.lookahead === " " ? parseOperand(s.shiftedByOne()) : (0, _unenclosedTs.parseUnenclosed)(s);
|
|
@@ -71,7 +71,7 @@ const distributeRange = (range, s)=>{
|
|
|
71
71
|
if (resolution.object === true) {
|
|
72
72
|
return false;
|
|
73
73
|
}
|
|
74
|
-
return (0, _genericsTs.listFrom)(resolution.object).every((branch)=>"class" in branch && branch.class ===
|
|
74
|
+
return (0, _genericsTs.listFrom)(resolution.object).every((branch)=>"class" in branch && branch.class === Array);
|
|
75
75
|
default:
|
|
76
76
|
return false;
|
|
77
77
|
}
|
|
@@ -20,7 +20,7 @@ const _boundsTs = require("./bounds.js");
|
|
|
20
20
|
const _divisorTs = require("./divisor.js");
|
|
21
21
|
const parseOperator = (s)=>{
|
|
22
22
|
const lookahead = s.scanner.shift();
|
|
23
|
-
return lookahead === "" ? s.finalize() : lookahead === "[" ? s.scanner.shift() === "]" ? s.rootToArray() : s.error(incompleteArrayTokenMessage) : (0, _genericsTs.isKeyOf)(lookahead, _scannerTs.Scanner.branchTokens) ? s.pushRootToBranch(lookahead) : lookahead === ")" ? s.finalizeGroup() : (0, _genericsTs.isKeyOf)(lookahead, _scannerTs.Scanner.comparatorStartChars) ? (0, _boundsTs.parseBound)(s, lookahead) : lookahead === "%" ? (0, _divisorTs.parseDivisor)(s) : lookahead === " " ? parseOperator(s) :
|
|
23
|
+
return lookahead === "" ? s.finalize() : lookahead === "[" ? s.scanner.shift() === "]" ? s.rootToArray() : s.error(incompleteArrayTokenMessage) : (0, _genericsTs.isKeyOf)(lookahead, _scannerTs.Scanner.branchTokens) ? s.pushRootToBranch(lookahead) : lookahead === ")" ? s.finalizeGroup() : (0, _genericsTs.isKeyOf)(lookahead, _scannerTs.Scanner.comparatorStartChars) ? (0, _boundsTs.parseBound)(s, lookahead) : lookahead === "%" ? (0, _divisorTs.parseDivisor)(s) : lookahead === " " ? parseOperator(s) : (0, _errorsTs.throwInternalError)(writeUnexpectedCharacterMessage(lookahead));
|
|
24
24
|
};
|
|
25
25
|
const writeUnexpectedCharacterMessage = (char)=>`Unexpected character '${char}'`;
|
|
26
26
|
const incompleteArrayTokenMessage = `Missing expected ']'`;
|
|
@@ -18,7 +18,7 @@ const jsObjectsScope = (0, _scopeTs.scope)({
|
|
|
18
18
|
"node",
|
|
19
19
|
{
|
|
20
20
|
object: {
|
|
21
|
-
class:
|
|
21
|
+
class: Function
|
|
22
22
|
}
|
|
23
23
|
}
|
|
24
24
|
],
|
|
@@ -26,7 +26,7 @@ const jsObjectsScope = (0, _scopeTs.scope)({
|
|
|
26
26
|
"node",
|
|
27
27
|
{
|
|
28
28
|
object: {
|
|
29
|
-
class:
|
|
29
|
+
class: Array
|
|
30
30
|
}
|
|
31
31
|
}
|
|
32
32
|
],
|
|
@@ -34,7 +34,7 @@ const jsObjectsScope = (0, _scopeTs.scope)({
|
|
|
34
34
|
"node",
|
|
35
35
|
{
|
|
36
36
|
object: {
|
|
37
|
-
class:
|
|
37
|
+
class: Date
|
|
38
38
|
}
|
|
39
39
|
}
|
|
40
40
|
],
|
|
@@ -42,7 +42,7 @@ const jsObjectsScope = (0, _scopeTs.scope)({
|
|
|
42
42
|
"node",
|
|
43
43
|
{
|
|
44
44
|
object: {
|
|
45
|
-
class:
|
|
45
|
+
class: Error
|
|
46
46
|
}
|
|
47
47
|
}
|
|
48
48
|
],
|
|
@@ -50,7 +50,7 @@ const jsObjectsScope = (0, _scopeTs.scope)({
|
|
|
50
50
|
"node",
|
|
51
51
|
{
|
|
52
52
|
object: {
|
|
53
|
-
class:
|
|
53
|
+
class: Map
|
|
54
54
|
}
|
|
55
55
|
}
|
|
56
56
|
],
|
|
@@ -58,7 +58,7 @@ const jsObjectsScope = (0, _scopeTs.scope)({
|
|
|
58
58
|
"node",
|
|
59
59
|
{
|
|
60
60
|
object: {
|
|
61
|
-
class:
|
|
61
|
+
class: RegExp
|
|
62
62
|
}
|
|
63
63
|
}
|
|
64
64
|
],
|
|
@@ -66,7 +66,7 @@ const jsObjectsScope = (0, _scopeTs.scope)({
|
|
|
66
66
|
"node",
|
|
67
67
|
{
|
|
68
68
|
object: {
|
|
69
|
-
class:
|
|
69
|
+
class: Set
|
|
70
70
|
}
|
|
71
71
|
}
|
|
72
72
|
],
|
|
@@ -74,7 +74,7 @@ const jsObjectsScope = (0, _scopeTs.scope)({
|
|
|
74
74
|
"node",
|
|
75
75
|
{
|
|
76
76
|
object: {
|
|
77
|
-
class:
|
|
77
|
+
class: Object
|
|
78
78
|
}
|
|
79
79
|
}
|
|
80
80
|
],
|
|
@@ -82,7 +82,7 @@ const jsObjectsScope = (0, _scopeTs.scope)({
|
|
|
82
82
|
"node",
|
|
83
83
|
{
|
|
84
84
|
object: {
|
|
85
|
-
class:
|
|
85
|
+
class: String
|
|
86
86
|
}
|
|
87
87
|
}
|
|
88
88
|
],
|
|
@@ -90,7 +90,7 @@ const jsObjectsScope = (0, _scopeTs.scope)({
|
|
|
90
90
|
"node",
|
|
91
91
|
{
|
|
92
92
|
object: {
|
|
93
|
-
class:
|
|
93
|
+
class: Number
|
|
94
94
|
}
|
|
95
95
|
}
|
|
96
96
|
],
|
|
@@ -98,7 +98,7 @@ const jsObjectsScope = (0, _scopeTs.scope)({
|
|
|
98
98
|
"node",
|
|
99
99
|
{
|
|
100
100
|
object: {
|
|
101
|
-
class:
|
|
101
|
+
class: Boolean
|
|
102
102
|
}
|
|
103
103
|
}
|
|
104
104
|
],
|
|
@@ -106,7 +106,7 @@ const jsObjectsScope = (0, _scopeTs.scope)({
|
|
|
106
106
|
"node",
|
|
107
107
|
{
|
|
108
108
|
object: {
|
|
109
|
-
class:
|
|
109
|
+
class: WeakMap
|
|
110
110
|
}
|
|
111
111
|
}
|
|
112
112
|
],
|
|
@@ -114,7 +114,7 @@ const jsObjectsScope = (0, _scopeTs.scope)({
|
|
|
114
114
|
"node",
|
|
115
115
|
{
|
|
116
116
|
object: {
|
|
117
|
-
class:
|
|
117
|
+
class: WeakSet
|
|
118
118
|
}
|
|
119
119
|
}
|
|
120
120
|
],
|
|
@@ -122,7 +122,7 @@ const jsObjectsScope = (0, _scopeTs.scope)({
|
|
|
122
122
|
"node",
|
|
123
123
|
{
|
|
124
124
|
object: {
|
|
125
|
-
class:
|
|
125
|
+
class: Promise
|
|
126
126
|
}
|
|
127
127
|
}
|
|
128
128
|
]
|
package/dist/cjs/scopes/type.js
CHANGED
|
@@ -11,22 +11,11 @@ function _export(target, all) {
|
|
|
11
11
|
_export(exports, {
|
|
12
12
|
initializeType: ()=>initializeType,
|
|
13
13
|
isType: ()=>isType,
|
|
14
|
-
isCheckResult: ()=>isCheckResult,
|
|
15
14
|
isAnonymousName: ()=>isAnonymousName
|
|
16
15
|
});
|
|
17
16
|
const _traverseTs = require("../traverse/traverse.js");
|
|
18
17
|
const _chainableNoOpProxyTs = require("../utils/chainableNoOpProxy.js");
|
|
19
|
-
const _genericsTs = require("../utils/generics.js");
|
|
20
18
|
const initializeType = (name, definition, config, scope)=>{
|
|
21
|
-
const namedTraverse = (0, _genericsTs.composeNamed)((data)=>{
|
|
22
|
-
const state = new _traverseTs.TraversalState(data, t);
|
|
23
|
-
(0, _traverseTs.traverse)(t.flat, state);
|
|
24
|
-
return state.problems.count ? {
|
|
25
|
-
problems: state.problems
|
|
26
|
-
} : {
|
|
27
|
-
data: state.data
|
|
28
|
-
};
|
|
29
|
-
}, name);
|
|
30
19
|
const root = {
|
|
31
20
|
// temporarily initialize node/flat to aliases that will be included in
|
|
32
21
|
// the final type in case of cyclic resolutions
|
|
@@ -45,9 +34,12 @@ const initializeType = (name, definition, config, scope)=>{
|
|
|
45
34
|
includesMorph: false,
|
|
46
35
|
config
|
|
47
36
|
};
|
|
37
|
+
// define within a key to dynamically assign a name to the function
|
|
38
|
+
const namedTraverse = {
|
|
39
|
+
[name]: (data)=>(0, _traverseTs.traverseRoot)(namedTraverse, data)
|
|
40
|
+
}[name];
|
|
48
41
|
const t = Object.assign(namedTraverse, root);
|
|
49
42
|
return t;
|
|
50
43
|
};
|
|
51
44
|
const isType = (value)=>value?.infer === _chainableNoOpProxyTs.chainableNoOpProxy;
|
|
52
|
-
const isCheckResult = (value)=>(0, _genericsTs.getNominalId)(value) !== undefined;
|
|
53
45
|
const isAnonymousName = (name)=>name[0] === "λ";
|
|
@@ -108,7 +108,9 @@ class ProblemArray extends Array {
|
|
|
108
108
|
const data = // we have to check for the presence of the key explicitly since the
|
|
109
109
|
// data could be undefined or null
|
|
110
110
|
opts && "data" in opts ? opts.data : _classPrivateFieldGet(this, _state).data;
|
|
111
|
-
const problem = new Problem(
|
|
111
|
+
const problem = new Problem(// avoid a bunch of errors from TS trying to discriminate the
|
|
112
|
+
// problem input based on the code
|
|
113
|
+
code, path, data, source, _classPrivateFieldGet(this, _state).getProblemConfig(code));
|
|
112
114
|
this.addProblem(problem);
|
|
113
115
|
return problem;
|
|
114
116
|
}
|
|
@@ -9,6 +9,7 @@ function _export(target, all) {
|
|
|
9
9
|
});
|
|
10
10
|
}
|
|
11
11
|
_export(exports, {
|
|
12
|
+
traverseRoot: ()=>traverseRoot,
|
|
12
13
|
TraversalState: ()=>TraversalState,
|
|
13
14
|
traverse: ()=>traverse,
|
|
14
15
|
checkEntries: ()=>checkEntries,
|
|
@@ -20,7 +21,6 @@ const _divisorTs = require("../nodes/rules/divisor.js");
|
|
|
20
21
|
const _rangeTs = require("../nodes/rules/range.js");
|
|
21
22
|
const _regexTs = require("../nodes/rules/regex.js");
|
|
22
23
|
const _rulesTs = require("../nodes/rules/rules.js");
|
|
23
|
-
const _typeTs = require("../scopes/type.js");
|
|
24
24
|
const _domainsTs = require("../utils/domains.js");
|
|
25
25
|
const _errorsTs = require("../utils/errors.js");
|
|
26
26
|
const _genericsTs = require("../utils/generics.js");
|
|
@@ -77,6 +77,22 @@ const problemWriterKeys = [
|
|
|
77
77
|
"writeReason",
|
|
78
78
|
"addContext"
|
|
79
79
|
];
|
|
80
|
+
const traverseRoot = (t, data)=>{
|
|
81
|
+
const state = new TraversalState(data, t);
|
|
82
|
+
traverse(t.flat, state);
|
|
83
|
+
const result = new CheckResult(state);
|
|
84
|
+
if (state.problems.count) {
|
|
85
|
+
result.problems = state.problems;
|
|
86
|
+
} else {
|
|
87
|
+
for (const [o, k] of state.entriesToPrune){
|
|
88
|
+
delete o[k];
|
|
89
|
+
}
|
|
90
|
+
result.data = state.data;
|
|
91
|
+
}
|
|
92
|
+
return result;
|
|
93
|
+
};
|
|
94
|
+
const CheckResult = class {
|
|
95
|
+
};
|
|
80
96
|
var _seen = /*#__PURE__*/ new WeakMap();
|
|
81
97
|
class TraversalState {
|
|
82
98
|
getProblemConfig(code) {
|
|
@@ -149,15 +165,19 @@ class TraversalState {
|
|
|
149
165
|
const branchProblems = new _problemsTs.Problems(this);
|
|
150
166
|
this.problems = branchProblems;
|
|
151
167
|
const lastPath = this.path;
|
|
168
|
+
const lastKeysToPrune = this.entriesToPrune;
|
|
152
169
|
let hasValidBranch = false;
|
|
153
170
|
for (const branch of branches){
|
|
154
171
|
this.path = new _pathsTs.Path();
|
|
172
|
+
this.entriesToPrune = [];
|
|
155
173
|
if (checkEntries(branch, this)) {
|
|
156
174
|
hasValidBranch = true;
|
|
175
|
+
lastKeysToPrune.push(...this.entriesToPrune);
|
|
157
176
|
break;
|
|
158
177
|
}
|
|
159
178
|
}
|
|
160
179
|
this.path = lastPath;
|
|
180
|
+
this.entriesToPrune = lastKeysToPrune;
|
|
161
181
|
this.problems = lastProblems;
|
|
162
182
|
this.failFast = lastFailFast;
|
|
163
183
|
return hasValidBranch || !this.problems.add("branches", branchProblems);
|
|
@@ -171,6 +191,7 @@ class TraversalState {
|
|
|
171
191
|
this.type = type;
|
|
172
192
|
this.path = new _pathsTs.Path();
|
|
173
193
|
this.problems = new _problemsTs.Problems(this);
|
|
194
|
+
this.entriesToPrune = [];
|
|
174
195
|
this.failFast = false;
|
|
175
196
|
this.traversalConfig = initializeTraversalConfig();
|
|
176
197
|
_classPrivateFieldSet(this, _seen, {});
|
|
@@ -210,27 +231,49 @@ const checkRequiredProp = (prop, state)=>{
|
|
|
210
231
|
};
|
|
211
232
|
const createPropChecker = (kind)=>(props, state)=>{
|
|
212
233
|
let isValid = true;
|
|
213
|
-
const
|
|
234
|
+
const remainingUnseenRequired = {
|
|
214
235
|
...props.required
|
|
215
236
|
};
|
|
216
237
|
for(const k in state.data){
|
|
217
238
|
if (props.required[k]) {
|
|
218
|
-
isValid
|
|
219
|
-
delete
|
|
239
|
+
isValid && (isValid = state.traverseKey(k, props.required[k]));
|
|
240
|
+
delete remainingUnseenRequired[k];
|
|
220
241
|
} else if (props.optional[k]) {
|
|
221
|
-
isValid
|
|
242
|
+
isValid && (isValid = state.traverseKey(k, props.optional[k]));
|
|
222
243
|
} else if (kind === "distilledProps") {
|
|
223
|
-
|
|
244
|
+
if (state.failFast) {
|
|
245
|
+
// If we're in a union (i.e. failFast is enabled) in
|
|
246
|
+
// distilled mode, we need to wait to prune distilled keys
|
|
247
|
+
// to avoid mutating data based on a branch that will not be
|
|
248
|
+
// included in the final result. Instead, we push the object
|
|
249
|
+
// and key to state to handle after traversal is complete.
|
|
250
|
+
state.entriesToPrune.push([
|
|
251
|
+
state.data,
|
|
252
|
+
k
|
|
253
|
+
]);
|
|
254
|
+
} else {
|
|
255
|
+
// If we're not in a union, we can safely distill right away
|
|
256
|
+
delete state.data[k];
|
|
257
|
+
}
|
|
224
258
|
} else {
|
|
259
|
+
isValid = false;
|
|
225
260
|
state.problems.add("extraneous", state.data[k], {
|
|
226
261
|
path: state.path.concat(k)
|
|
227
262
|
});
|
|
228
|
-
isValid = false;
|
|
229
263
|
}
|
|
230
264
|
if (!isValid && state.failFast) {
|
|
231
265
|
return false;
|
|
232
266
|
}
|
|
233
267
|
}
|
|
268
|
+
const unseenRequired = Object.keys(remainingUnseenRequired);
|
|
269
|
+
if (unseenRequired.length) {
|
|
270
|
+
for (const k of unseenRequired){
|
|
271
|
+
state.problems.add("missing", undefined, {
|
|
272
|
+
path: state.path.concat(k)
|
|
273
|
+
});
|
|
274
|
+
}
|
|
275
|
+
return false;
|
|
276
|
+
}
|
|
234
277
|
return isValid;
|
|
235
278
|
};
|
|
236
279
|
const entryCheckers = {
|
|
@@ -275,8 +318,8 @@ const entryCheckers = {
|
|
|
275
318
|
}
|
|
276
319
|
const caseKeys = (0, _genericsTs.objectKeysOf)(rule.cases);
|
|
277
320
|
const missingCasePath = state.path.concat(rule.path);
|
|
278
|
-
const caseDescriptions = rule.kind === "value" ? caseKeys : rule.kind === "domain" ? (0, _problemsTs.domainsToDescriptions)(caseKeys) : rule.kind === "class" ? (0, _problemsTs.objectKindsToDescriptions)(caseKeys) : (0, _errorsTs.throwInternalError)(`Unexpectedly encountered rule kind '${rule.kind}' during traversal`);
|
|
279
|
-
state.problems.add("cases", caseDescriptions, {
|
|
321
|
+
const caseDescriptions = rule.kind === "value" ? caseKeys : rule.kind === "domain" ? (0, _problemsTs.domainsToDescriptions)(caseKeys) : rule.kind === "class" ? (0, _problemsTs.objectKindsToDescriptions)(caseKeys) : /* c8 ignore start*/ (0, _errorsTs.throwInternalError)(`Unexpectedly encountered rule kind '${rule.kind}' during traversal`);
|
|
322
|
+
/* c8 ignore stop*/ state.problems.add("cases", caseDescriptions, {
|
|
280
323
|
path: missingCasePath,
|
|
281
324
|
data: dataAtPath
|
|
282
325
|
});
|
|
@@ -304,7 +347,7 @@ const entryCheckers = {
|
|
|
304
347
|
state.problems.addProblem(out);
|
|
305
348
|
return false;
|
|
306
349
|
}
|
|
307
|
-
if (
|
|
350
|
+
if (out instanceof CheckResult) {
|
|
308
351
|
if (out.problems) {
|
|
309
352
|
for (const problem of out.problems){
|
|
310
353
|
state.problems.addProblem(problem);
|
|
@@ -19,7 +19,6 @@ _export(exports, {
|
|
|
19
19
|
keyCount: ()=>keyCount,
|
|
20
20
|
hasKeys: ()=>hasKeys,
|
|
21
21
|
nominal: ()=>nominal,
|
|
22
|
-
composeNamed: ()=>composeNamed,
|
|
23
22
|
getNominalId: ()=>getNominalId,
|
|
24
23
|
hasNominalId: ()=>hasNominalId,
|
|
25
24
|
listFrom: ()=>listFrom
|
|
@@ -57,10 +56,6 @@ const id = Symbol("id");
|
|
|
57
56
|
const nominal = (o, name)=>Object.assign(o, {
|
|
58
57
|
[id]: name
|
|
59
58
|
});
|
|
60
|
-
const composeNamed = (f, name)=>// define within a key to dynamically assign a name to the function
|
|
61
|
-
({
|
|
62
|
-
[name]: (...args)=>nominal(f(...args), name)
|
|
63
|
-
})[name];
|
|
64
59
|
const getNominalId = (data)=>(0, _domainsTs.hasDomain)(data, "object") && id in data ? data[id] : undefined;
|
|
65
60
|
const hasNominalId = (data, name)=>getNominalId(data) === name;
|
|
66
61
|
const listFrom = (data)=>Array.isArray(data) ? data : [
|
|
@@ -42,8 +42,8 @@ import { throwInternalError } from "../utils/errors.js";
|
|
|
42
42
|
import { objectKeysOf } from "../utils/generics.js";
|
|
43
43
|
import { Path } from "../utils/paths.js";
|
|
44
44
|
import { stringify } from "../utils/serialize.js";
|
|
45
|
-
export const composeIntersection = (reducer)=>(l, r, state)=>l === undefined ? r === undefined ?
|
|
46
|
-
export const
|
|
45
|
+
export const composeIntersection = (reducer)=>(l, r, state)=>l === undefined ? r === undefined ? throwInternalError(undefinedOperandsMessage) : r : r === undefined ? l : reducer(l, r, state);
|
|
46
|
+
export const undefinedOperandsMessage = `Unexpected operation two undefined operands`;
|
|
47
47
|
export const disjointDescriptionWriters = {
|
|
48
48
|
domain: ({ l , r })=>`${l.join(", ")} and ${r.join(", ")}`,
|
|
49
49
|
range: ({ l , r })=>`${stringifyRange(l)} and ${stringifyRange(r)}`,
|