arktype 2.2.7 → 2.2.9
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/keywords/string.d.ts +22 -20
- package/out/keywords/string.js +2 -0
- package/out/keywords/ts.d.ts +2 -2
- package/out/parser/shift/operand/enclosed.d.ts +6 -6
- package/out/parser/tupleExpressions.d.ts +9 -9
- package/out/type.d.ts +4 -8
- package/package.json +6 -5
- package/skills/arktype/SKILL.md +169 -0
package/out/keywords/string.d.ts
CHANGED
|
@@ -111,39 +111,39 @@ export declare namespace lower {
|
|
|
111
111
|
export declare const normalizedForms: readonly ["NFC", "NFD", "NFKC", "NFKD"];
|
|
112
112
|
export type NormalizedForm = (typeof normalizedForms)[number];
|
|
113
113
|
export declare const NFC: Module<{
|
|
114
|
-
root: unknown;
|
|
115
114
|
preformatted: unknown;
|
|
115
|
+
root: unknown;
|
|
116
116
|
}>;
|
|
117
117
|
export declare const NFD: Module<{
|
|
118
|
-
root: unknown;
|
|
119
118
|
preformatted: unknown;
|
|
119
|
+
root: unknown;
|
|
120
120
|
}>;
|
|
121
121
|
export declare const NFKC: Module<{
|
|
122
|
-
root: unknown;
|
|
123
122
|
preformatted: unknown;
|
|
123
|
+
root: unknown;
|
|
124
124
|
}>;
|
|
125
125
|
export declare const NFKD: Module<{
|
|
126
|
-
root: unknown;
|
|
127
126
|
preformatted: unknown;
|
|
127
|
+
root: unknown;
|
|
128
128
|
}>;
|
|
129
129
|
export declare const normalize: Module<{
|
|
130
|
-
root: unknown;
|
|
131
130
|
NFC: Submodule<{
|
|
132
|
-
root: unknown;
|
|
133
131
|
preformatted: unknown;
|
|
132
|
+
root: unknown;
|
|
134
133
|
}>;
|
|
135
134
|
NFD: Submodule<{
|
|
136
|
-
root: unknown;
|
|
137
135
|
preformatted: unknown;
|
|
136
|
+
root: unknown;
|
|
138
137
|
}>;
|
|
139
138
|
NFKC: Submodule<{
|
|
140
|
-
root: unknown;
|
|
141
139
|
preformatted: unknown;
|
|
140
|
+
root: unknown;
|
|
142
141
|
}>;
|
|
143
142
|
NFKD: Submodule<{
|
|
144
|
-
root: unknown;
|
|
145
143
|
preformatted: unknown;
|
|
144
|
+
root: unknown;
|
|
146
145
|
}>;
|
|
146
|
+
root: unknown;
|
|
147
147
|
}>;
|
|
148
148
|
export declare namespace normalize {
|
|
149
149
|
type module = Module<submodule>;
|
|
@@ -221,12 +221,12 @@ export declare namespace url {
|
|
|
221
221
|
}
|
|
222
222
|
export declare const uuid: Module<{
|
|
223
223
|
root: string;
|
|
224
|
-
v4: unknown;
|
|
225
|
-
v6: unknown;
|
|
226
224
|
v1: unknown;
|
|
227
225
|
v2: unknown;
|
|
228
226
|
v3: unknown;
|
|
227
|
+
v4: unknown;
|
|
229
228
|
v5: unknown;
|
|
229
|
+
v6: unknown;
|
|
230
230
|
v7: unknown;
|
|
231
231
|
v8: unknown;
|
|
232
232
|
}>;
|
|
@@ -249,10 +249,9 @@ export declare namespace uuid {
|
|
|
249
249
|
}
|
|
250
250
|
}
|
|
251
251
|
export declare const string: Module<{
|
|
252
|
-
root: unknown;
|
|
253
252
|
alpha: unknown;
|
|
254
253
|
alphanumeric: unknown;
|
|
255
|
-
|
|
254
|
+
base58: unknown;
|
|
256
255
|
base64: Submodule<{
|
|
257
256
|
root: unknown;
|
|
258
257
|
url: unknown;
|
|
@@ -262,43 +261,45 @@ export declare const string: Module<{
|
|
|
262
261
|
date: Submodule<stringDate.submodule>;
|
|
263
262
|
digits: unknown;
|
|
264
263
|
email: unknown;
|
|
264
|
+
hex: unknown;
|
|
265
265
|
integer: Submodule<stringInteger.submodule>;
|
|
266
266
|
ip: Submodule<ip.submodule>;
|
|
267
267
|
json: Submodule<stringJson.submodule>;
|
|
268
268
|
lower: Submodule<lower.submodule>;
|
|
269
269
|
normalize: Submodule<{
|
|
270
|
-
root: unknown;
|
|
271
270
|
NFC: Submodule<{
|
|
272
|
-
root: unknown;
|
|
273
271
|
preformatted: unknown;
|
|
272
|
+
root: unknown;
|
|
274
273
|
}>;
|
|
275
274
|
NFD: Submodule<{
|
|
276
|
-
root: unknown;
|
|
277
275
|
preformatted: unknown;
|
|
276
|
+
root: unknown;
|
|
278
277
|
}>;
|
|
279
278
|
NFKC: Submodule<{
|
|
280
|
-
root: unknown;
|
|
281
279
|
preformatted: unknown;
|
|
280
|
+
root: unknown;
|
|
282
281
|
}>;
|
|
283
282
|
NFKD: Submodule<{
|
|
284
|
-
root: unknown;
|
|
285
283
|
preformatted: unknown;
|
|
284
|
+
root: unknown;
|
|
286
285
|
}>;
|
|
286
|
+
root: unknown;
|
|
287
287
|
}>;
|
|
288
288
|
numeric: Submodule<stringNumeric.submodule>;
|
|
289
289
|
regex: unknown;
|
|
290
|
+
root: unknown;
|
|
290
291
|
semver: unknown;
|
|
291
292
|
trim: Submodule<trim.submodule>;
|
|
292
293
|
upper: Submodule<upper.submodule>;
|
|
293
294
|
url: Submodule<url.submodule>;
|
|
294
295
|
uuid: Submodule<{
|
|
295
296
|
root: string;
|
|
296
|
-
v4: unknown;
|
|
297
|
-
v6: unknown;
|
|
298
297
|
v1: unknown;
|
|
299
298
|
v2: unknown;
|
|
300
299
|
v3: unknown;
|
|
300
|
+
v4: unknown;
|
|
301
301
|
v5: unknown;
|
|
302
|
+
v6: unknown;
|
|
302
303
|
v7: unknown;
|
|
303
304
|
v8: unknown;
|
|
304
305
|
}>;
|
|
@@ -311,6 +312,7 @@ export declare namespace string {
|
|
|
311
312
|
alpha: string;
|
|
312
313
|
alphanumeric: string;
|
|
313
314
|
hex: string;
|
|
315
|
+
base58: string;
|
|
314
316
|
base64: base64.submodule;
|
|
315
317
|
capitalize: capitalize.submodule;
|
|
316
318
|
creditCard: string;
|
package/out/keywords/string.js
CHANGED
|
@@ -31,6 +31,7 @@ export const stringInteger = Scope.module({
|
|
|
31
31
|
name: "string.integer"
|
|
32
32
|
});
|
|
33
33
|
const hex = regexStringNode(/^[\dA-Fa-f]+$/, "hex characters only");
|
|
34
|
+
const base58 = regexStringNode(/^[1-9A-HJ-NP-Za-km-z]+$/, "base58-encoded");
|
|
34
35
|
const base64 = Scope.module({
|
|
35
36
|
root: regexStringNode(/^(?:[\d+/A-Za-z]{4})*(?:[\d+/A-Za-z]{2}==|[\d+/A-Za-z]{3}=)?$/, "base64-encoded"),
|
|
36
37
|
url: regexStringNode(/^(?:[\w-]{4})*(?:[\w-]{2}(?:==|%3D%3D)?|[\w-]{3}(?:=|%3D)?)?$/, "base64url-encoded")
|
|
@@ -442,6 +443,7 @@ export const string = Scope.module({
|
|
|
442
443
|
alpha: regexStringNode(/^[A-Za-z]*$/, "only letters"),
|
|
443
444
|
alphanumeric: regexStringNode(/^[\dA-Za-z]*$/, "only letters and digits 0-9"),
|
|
444
445
|
hex,
|
|
446
|
+
base58,
|
|
445
447
|
base64,
|
|
446
448
|
capitalize,
|
|
447
449
|
creditCard,
|
package/out/keywords/ts.d.ts
CHANGED
|
@@ -21,8 +21,8 @@ export declare namespace arkTsKeywords {
|
|
|
21
21
|
};
|
|
22
22
|
}
|
|
23
23
|
export declare const unknown: Module<{
|
|
24
|
-
root: unknown;
|
|
25
24
|
any: unknown;
|
|
25
|
+
root: unknown;
|
|
26
26
|
}>;
|
|
27
27
|
export declare namespace unknown {
|
|
28
28
|
type submodule = Submodule<$>;
|
|
@@ -43,11 +43,11 @@ export declare namespace json {
|
|
|
43
43
|
};
|
|
44
44
|
}
|
|
45
45
|
export declare const object: Module<{
|
|
46
|
-
root: unknown;
|
|
47
46
|
json: Submodule<{
|
|
48
47
|
root: unknown;
|
|
49
48
|
stringify: unknown;
|
|
50
49
|
}>;
|
|
50
|
+
root: unknown;
|
|
51
51
|
}>;
|
|
52
52
|
export declare namespace object {
|
|
53
53
|
type submodule = Submodule<$>;
|
|
@@ -22,9 +22,9 @@ export declare const enclosingChar: {
|
|
|
22
22
|
};
|
|
23
23
|
export declare const enclosingLiteralTokens: {
|
|
24
24
|
readonly "d'": "'";
|
|
25
|
-
readonly 'd"': "
|
|
25
|
+
readonly 'd"': '"';
|
|
26
26
|
readonly "'": "'";
|
|
27
|
-
readonly '"': "
|
|
27
|
+
readonly '"': '"';
|
|
28
28
|
};
|
|
29
29
|
export type EnclosingLiteralTokens = typeof enclosingLiteralTokens;
|
|
30
30
|
export type EnclosingLiteralStartToken = keyof EnclosingLiteralTokens;
|
|
@@ -35,12 +35,12 @@ export declare const enclosingRegexTokens: {
|
|
|
35
35
|
export type EnclosingRegexTokens = typeof enclosingRegexTokens;
|
|
36
36
|
export type EnclosingRegexToken = keyof EnclosingRegexTokens;
|
|
37
37
|
export declare const enclosingTokens: {
|
|
38
|
-
readonly "/": "/";
|
|
39
|
-
readonly "x/": "/";
|
|
40
38
|
readonly "d'": "'";
|
|
41
|
-
readonly 'd"': "
|
|
39
|
+
readonly 'd"': '"';
|
|
42
40
|
readonly "'": "'";
|
|
43
|
-
readonly '"': "
|
|
41
|
+
readonly '"': '"';
|
|
42
|
+
readonly "/": "/";
|
|
43
|
+
readonly "x/": "/";
|
|
44
44
|
};
|
|
45
45
|
export type EnclosingTokens = typeof enclosingTokens;
|
|
46
46
|
export type EnclosingStartToken = keyof EnclosingTokens;
|
|
@@ -43,24 +43,24 @@ declare const postfixParsers: {
|
|
|
43
43
|
export type TuplePostfixOperator = keyof typeof postfixParsers;
|
|
44
44
|
declare const infixParsers: {
|
|
45
45
|
"&": IndexOneParser<"&">;
|
|
46
|
-
"|": IndexOneParser<"|">;
|
|
47
|
-
"|>": IndexOneParser<"|>">;
|
|
48
|
-
"=": IndexOneParser<"=">;
|
|
49
46
|
":": IndexOneParser<":">;
|
|
47
|
+
"=": IndexOneParser<"=">;
|
|
50
48
|
"=>": IndexOneParser<"=>">;
|
|
51
49
|
"@": IndexOneParser<"@">;
|
|
50
|
+
"|": IndexOneParser<"|">;
|
|
51
|
+
"|>": IndexOneParser<"|>">;
|
|
52
52
|
};
|
|
53
53
|
export type TupleInfixOperator = keyof typeof infixParsers;
|
|
54
54
|
declare const indexOneParsers: {
|
|
55
55
|
"&": IndexOneParser<"&">;
|
|
56
|
-
"|": IndexOneParser<"|">;
|
|
57
|
-
"|>": IndexOneParser<"|>">;
|
|
58
|
-
"=": IndexOneParser<"=">;
|
|
59
56
|
":": IndexOneParser<":">;
|
|
57
|
+
"=": IndexOneParser<"=">;
|
|
60
58
|
"=>": IndexOneParser<"=>">;
|
|
61
|
-
"@": IndexOneParser<"@">;
|
|
62
59
|
"?": IndexOneParser<"?">;
|
|
60
|
+
"@": IndexOneParser<"@">;
|
|
63
61
|
"[]": IndexOneParser<"[]">;
|
|
62
|
+
"|": IndexOneParser<"|">;
|
|
63
|
+
"|>": IndexOneParser<"|>">;
|
|
64
64
|
};
|
|
65
65
|
export type IndexOneOperator = keyof typeof indexOneParsers;
|
|
66
66
|
export type InfixExpression = readonly [unknown, InfixToken, ...unknown[]];
|
|
@@ -70,9 +70,9 @@ type IndexZeroExpression<token extends string = IndexZeroOperator> = readonly [
|
|
|
70
70
|
...unknown[]
|
|
71
71
|
];
|
|
72
72
|
declare const indexZeroParsers: {
|
|
73
|
-
keyof: IndexZeroParser<"keyof">;
|
|
74
|
-
instanceof: IndexZeroParser<"instanceof">;
|
|
75
73
|
"===": IndexZeroParser<"===">;
|
|
74
|
+
instanceof: IndexZeroParser<"instanceof">;
|
|
75
|
+
keyof: IndexZeroParser<"keyof">;
|
|
76
76
|
};
|
|
77
77
|
export type IndexZeroOperator = keyof typeof indexZeroParsers;
|
|
78
78
|
export declare const writeInvalidConstructorMessage: <actual extends Domain | BuiltinObjectKind>(actual: actual) => string;
|
package/out/type.d.ts
CHANGED
|
@@ -21,7 +21,9 @@ export interface TypeParser<$ = {}> extends Ark.boundTypeAttachments<$> {
|
|
|
21
21
|
*/
|
|
22
22
|
<const def, r = type.instantiate<def, $>>(def: type.validate<def, $>): r extends infer _ ? _ : never;
|
|
23
23
|
/**
|
|
24
|
-
* Create a {@link Generic} from a parameter string and body definition
|
|
24
|
+
* Create a {@link Generic} from a parameter string and body definition, or a
|
|
25
|
+
* {@link Type} from a [tuple expression](http://localhost:3000/docs/expressions)
|
|
26
|
+
* spread as this function's arguments.
|
|
25
27
|
*
|
|
26
28
|
* @param params A string like "<t, n extends number>" specifying the
|
|
27
29
|
* {@link Generic}'s parameters and any associated constraints via `extends`.
|
|
@@ -31,15 +33,9 @@ export interface TypeParser<$ = {}> extends Ark.boundTypeAttachments<$> {
|
|
|
31
33
|
* from its {@link Scope}.
|
|
32
34
|
*
|
|
33
35
|
* @example const BoxOf = type("<t extends string | number>", { contents: "t" })
|
|
34
|
-
*/
|
|
35
|
-
<const params extends ParameterString, const def, r = Generic<parseValidGenericParams<params, $>, def, $>>(params: validateParameterString<params, $>, def: type.validate<def, $, baseGenericConstraints<parseValidGenericParams<params, $>>>): r extends infer _ ? _ : never;
|
|
36
|
-
/**
|
|
37
|
-
* Create a {@link Type} from a [tuple expression](http://localhost:3000/docs/expressions)
|
|
38
|
-
* spread as this function's arguments.
|
|
39
|
-
*
|
|
40
36
|
* @example type("string", "|", { foo: "number" })
|
|
41
37
|
*/
|
|
42
|
-
<const zero, const one, const rest extends array, r = type.instantiate<[zero, one, ...rest], $>>(_0: zero extends IndexZeroOperator ? zero : type.validate<zero, $>, _1: zero extends "keyof" ? type.validate<one, $> : zero extends "instanceof" ? conform<one, Constructor> : zero extends "===" ? conform<one, unknown> : conform<one, ArgTwoOperator>, ..._2: zero extends "===" ? rest : zero extends "instanceof" ? conform<rest, readonly Constructor[]> : one extends TupleInfixOperator ? one extends ":" ? [Predicate<distill.In<type.infer<zero, $>>>] : one extends "=>" ? [Morph<distill.Out<type.infer<zero, $>>, unknown>] : one extends "|>" ? [type.validate<rest[0], $>] : one extends "@" ? [TypeMeta.MappableInput, NodeSelector?] : [type.validate<rest[0], $>] : []): r extends infer _ ? _ : never;
|
|
38
|
+
<const zero, const one, const rest extends array, r = zero extends ParameterString ? Generic<parseValidGenericParams<zero, $>, one, $> : type.instantiate<[zero, one, ...rest], $>>(_0: zero extends ParameterString ? validateParameterString<zero, $> : zero extends IndexZeroOperator ? zero : type.validate<zero, $>, _1: zero extends ParameterString ? type.validate<one, $, baseGenericConstraints<parseValidGenericParams<zero, $>>> : zero extends "keyof" ? type.validate<one, $> : zero extends "instanceof" ? conform<one, Constructor> : zero extends "===" ? conform<one, unknown> : conform<one, ArgTwoOperator>, ..._2: zero extends ParameterString ? [] : zero extends "===" ? rest : zero extends "instanceof" ? conform<rest, readonly Constructor[]> : one extends TupleInfixOperator ? one extends ":" ? [Predicate<distill.In<type.infer<zero, $>>>] : one extends "=>" ? [Morph<distill.Out<type.infer<zero, $>>, unknown>] : one extends "|>" ? [type.validate<rest[0], $>] : one extends "@" ? [TypeMeta.MappableInput, NodeSelector?] : [type.validate<rest[0], $>] : []): r extends infer _ ? _ : never;
|
|
43
39
|
/**
|
|
44
40
|
* An alias of the {@link ArkErrors} class, an instance of which is returned when a {@link Type}
|
|
45
41
|
* is invoked with invalid input.
|
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.2.
|
|
4
|
+
"version": "2.2.9",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
@@ -35,12 +35,13 @@
|
|
|
35
35
|
}
|
|
36
36
|
},
|
|
37
37
|
"files": [
|
|
38
|
-
"out"
|
|
38
|
+
"out",
|
|
39
|
+
"skills"
|
|
39
40
|
],
|
|
40
41
|
"dependencies": {
|
|
41
|
-
"@ark/util": "0.56.
|
|
42
|
-
"@ark/schema": "0.56.
|
|
43
|
-
"arkregex": "0.0.
|
|
42
|
+
"@ark/util": "0.56.8",
|
|
43
|
+
"@ark/schema": "0.56.8",
|
|
44
|
+
"arkregex": "0.0.14"
|
|
44
45
|
},
|
|
45
46
|
"publishConfig": {
|
|
46
47
|
"access": "public"
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: arktype
|
|
3
|
+
description: Write, review or debug runtime validation with ArkType (the `arktype` package). Use when code imports from "arktype", when a TypeScript project needs to validate untrusted input like request bodies, env vars, JSON, config files or form data, or when converting schemas from Zod, Yup, Valibot or io-ts to ArkType.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# ArkType
|
|
7
|
+
|
|
8
|
+
ArkType definitions are TypeScript type syntax, written as strings and objects, that also validate at runtime. If you know the TypeScript type, write that.
|
|
9
|
+
|
|
10
|
+
Full docs as Markdown: https://arktype.io/llms.txt. Any docs page is also available as Markdown by adding `.md` to its URL, e.g. https://arktype.io/docs/objects.md.
|
|
11
|
+
|
|
12
|
+
## Define, infer, validate
|
|
13
|
+
|
|
14
|
+
```ts
|
|
15
|
+
import { type } from "arktype"
|
|
16
|
+
|
|
17
|
+
const User = type({
|
|
18
|
+
name: "string",
|
|
19
|
+
email: "string.email",
|
|
20
|
+
"age?": "number.integer >= 0",
|
|
21
|
+
role: "'admin' | 'member'",
|
|
22
|
+
tags: "string[]"
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
// the TypeScript type, derived from the definition
|
|
26
|
+
type User = typeof User.infer
|
|
27
|
+
|
|
28
|
+
const out = User(JSON.parse(body))
|
|
29
|
+
|
|
30
|
+
if (out instanceof type.errors) {
|
|
31
|
+
// every problem, one per line, e.g. `email must be an email address (was "x")`
|
|
32
|
+
console.error(out.summary)
|
|
33
|
+
} else {
|
|
34
|
+
// out is narrowed to User
|
|
35
|
+
console.log(out.name)
|
|
36
|
+
}
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
- Calling a type returns the validated data or an `ArkErrors` instance. It does not throw, so check with `instanceof type.errors` rather than wrapping it in `try`/`catch`.
|
|
40
|
+
- `User.assert(data)` throws instead, for places where invalid data is a bug.
|
|
41
|
+
- Name types in PascalCase (`User`). Name types that transform their input in camelCase (`parseUser`), since they behave like functions.
|
|
42
|
+
- `typeof User.infer` is the output type. `typeof User.inferIn` is the input type, which differs once a type transforms its input.
|
|
43
|
+
|
|
44
|
+
## Syntax
|
|
45
|
+
|
|
46
|
+
Strings use TypeScript's operators. Write string literals in single quotes inside the definition.
|
|
47
|
+
|
|
48
|
+
| Want | Write |
|
|
49
|
+
| ---------------------- | --------------------------------------------------------- |
|
|
50
|
+
| optional key | `"key?": "string"` |
|
|
51
|
+
| default (objects only) | `count: "number = 0"` |
|
|
52
|
+
| union | `"string \| number"`, `"'a' \| 'b'"` |
|
|
53
|
+
| array | `"string[]"`, `"(string \| number)[]"` |
|
|
54
|
+
| tuple | `["string", "number"]` |
|
|
55
|
+
| number range | `"number > 0"`, `"0 <= number < 1"` |
|
|
56
|
+
| string or array length | `"string > 0"`, `"1 <= string <= 255"`, `"string[] >= 1"` |
|
|
57
|
+
| divisibility | `"number % 2"` |
|
|
58
|
+
| regex | `"/^[a-z]+$/"` |
|
|
59
|
+
| record | `"Record<string, number>"` or `{ "[string]": "number" }` |
|
|
60
|
+
| class instance | `"Date"`, `"Map"`, or `type.instanceOf(MyClass)` |
|
|
61
|
+
| enum from values | `type.enumerated("a", "b")` or `type.valueOf(MyEnum)` |
|
|
62
|
+
|
|
63
|
+
Common keywords: `string.email`, `string.uuid`, `string.url`, `string.ip`, `string.semver`, `string.date.iso`, `string.alphanumeric`, `number.integer`, `number.safe`, `number.epoch`. The full list is at https://arktype.io/docs/keywords.md.
|
|
64
|
+
|
|
65
|
+
## Composing types
|
|
66
|
+
|
|
67
|
+
Put an existing type anywhere a definition goes. Don't call `type()` again on nested objects; plain objects are already definitions.
|
|
68
|
+
|
|
69
|
+
```ts
|
|
70
|
+
const Address = type({ city: "string", zip: "string" })
|
|
71
|
+
|
|
72
|
+
const Customer = type({
|
|
73
|
+
name: "string",
|
|
74
|
+
address: Address,
|
|
75
|
+
"previous?": Address.array()
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
const Admin = Customer.and({ permissions: "string[]" })
|
|
79
|
+
const MaybeCustomer = Customer.or("null")
|
|
80
|
+
const CustomerName = Customer.pick("name")
|
|
81
|
+
const CustomerDraft = Customer.partial()
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
Never interpolate a type into a string, as in ``type(`${Address}[]`)``. Use `Address.array()`, `Address.or(...)` and the other chained methods, or a tuple expression like `[Address, "[]"]`.
|
|
85
|
+
|
|
86
|
+
## Parsing and transforming
|
|
87
|
+
|
|
88
|
+
Keywords ending in `.parse` transform their input. `.to()` validates the parsed result, and `.pipe()` runs your own function.
|
|
89
|
+
|
|
90
|
+
```ts
|
|
91
|
+
// "8080" -> 8080, then checked as a port
|
|
92
|
+
const parsePort = type("string.integer.parse").to("0 < number <= 65535")
|
|
93
|
+
|
|
94
|
+
// a JSON string -> a validated object
|
|
95
|
+
const parsePayload = type("string.json.parse").to({ id: "number.integer" })
|
|
96
|
+
|
|
97
|
+
// any function, after validation
|
|
98
|
+
const parseTrimmed = type("string").pipe(s => s.trim())
|
|
99
|
+
|
|
100
|
+
// env vars arrive as strings, so parse them and give defaults
|
|
101
|
+
const Env = type({
|
|
102
|
+
DATABASE_URL: "string.url",
|
|
103
|
+
PORT: type("string.integer.parse").default("3000"),
|
|
104
|
+
"DEBUG?": "'true' | 'false'"
|
|
105
|
+
})
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
Other morph keywords: `string.trim`, `string.lower`, `string.upper`, `string.numeric.parse`, `string.date.parse`, `string.date.iso.parse`, `string.url.parse`.
|
|
109
|
+
|
|
110
|
+
## Custom rules
|
|
111
|
+
|
|
112
|
+
Use `.narrow()` when a rule involves more than one value. Return `true` to accept, or `ctx.reject()` to give a message and a path.
|
|
113
|
+
|
|
114
|
+
```ts
|
|
115
|
+
const Signup = type({
|
|
116
|
+
password: "string >= 8",
|
|
117
|
+
confirmPassword: "string"
|
|
118
|
+
}).narrow(
|
|
119
|
+
(data, ctx) =>
|
|
120
|
+
data.password === data.confirmPassword ||
|
|
121
|
+
ctx.reject({
|
|
122
|
+
expected: "identical to password",
|
|
123
|
+
actual: "",
|
|
124
|
+
path: ["confirmPassword"]
|
|
125
|
+
})
|
|
126
|
+
)
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
## Recursive and cyclic types
|
|
130
|
+
|
|
131
|
+
Use a scope, where definitions can reference each other by name.
|
|
132
|
+
|
|
133
|
+
```ts
|
|
134
|
+
import { scope } from "arktype"
|
|
135
|
+
|
|
136
|
+
const types = scope({
|
|
137
|
+
TreeNode: {
|
|
138
|
+
value: "number",
|
|
139
|
+
children: "TreeNode[]"
|
|
140
|
+
}
|
|
141
|
+
}).export()
|
|
142
|
+
|
|
143
|
+
const tree = types.TreeNode({ value: 1, children: [] })
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
Don't wrap definitions inside a scope with `type()`. They're resolved within the scope, and `type()` would resolve them globally.
|
|
147
|
+
|
|
148
|
+
## Extra keys
|
|
149
|
+
|
|
150
|
+
By default, keys a definition doesn't declare are allowed and left in the output. To reject or remove them for one object, use `"+"`:
|
|
151
|
+
|
|
152
|
+
```ts
|
|
153
|
+
const Strict = type({ "+": "reject", name: "string" })
|
|
154
|
+
const Stripped = type({ "+": "delete", name: "string" })
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
## Integrations
|
|
158
|
+
|
|
159
|
+
- ArkType types are [Standard Schema](https://standardschema.dev), so they work directly with tRPC, oRPC, Hono and other libraries that accept one. react-hook-form takes them through `arktypeResolver` from `@hookform/resolvers/arktype`.
|
|
160
|
+
- `User.toJsonSchema()` returns the equivalent JSON Schema.
|
|
161
|
+
- Global configuration (error messages, `onUndeclaredKey`, and so on) goes in a file imported from `arktype/config` before anything imports `arktype`. See https://arktype.io/docs/configuration.md.
|
|
162
|
+
|
|
163
|
+
## Mistakes to avoid
|
|
164
|
+
|
|
165
|
+
- Defaults like `"number = 0"` only work as object values or tuple elements, not as a standalone `type("number = 0")`.
|
|
166
|
+
- An optional key can be missing, but not present with the value `undefined`, mirroring TypeScript's `exactOptionalPropertyTypes`. Write `"key?": "string | undefined"` to allow both.
|
|
167
|
+
- A string literal needs its own quotes inside the definition: `"'a' | 'b'"`. A bare `"a"` is read as the name of a type, and fails to resolve.
|
|
168
|
+
- Don't hand-write a TypeScript interface next to the type. Derive it with `typeof T.infer`, so the two can't drift.
|
|
169
|
+
- An invalid definition is a TypeScript error at the definition itself, with a message saying what's wrong. Fix it there rather than casting it away.
|