-
-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: fix twoslash hover styling, improve error examples
- Loading branch information
1 parent
a2fe439
commit 8865337
Showing
10 changed files
with
97 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -64,33 +64,63 @@ const contacts = contact.array().atLeastLength(1) | |
Structured constraints like divisors and ranges will only take us so far. Luckily, they integrate seamlessly with whatever custom validation logic you need. | ||
|
||
```ts | ||
import { type, type Type } from "arktype" | ||
import { type } from "arktype" | ||
|
||
const _contact = type({ | ||
email: "email", | ||
// ---cut--- | ||
const palindromicEmail = type("email").narrow((address, ctx) => { | ||
if (address === [...address].reverse().join("")) { | ||
// congratulations! your email is somehow a palindrome | ||
return true | ||
} | ||
// add a customizable error and return false | ||
return ctx.mustBe("a palindrome") | ||
}) | ||
|
||
const palindromicContact = type({ | ||
email: palindromicEmail, | ||
score: "integer < 100" | ||
}) | ||
``` | ||
|
||
type _Contact = typeof _contact.t | ||
We can invoke `palindromicContact` anywhere to get validated data or a list of errors with a user-friendly summary. | ||
|
||
interface Contact extends _Contact {} | ||
```ts | ||
import { ArkErrors, type } from "arktype" | ||
|
||
export const contact: Type<Contact> = _contact | ||
// ---cut--- | ||
const goodContact = contact.narrow((data, ctx) => { | ||
if (data.score > 75 || data.email.endsWith("@arktype.io")) { | ||
const palindromicEmail = type("email").narrow((address, ctx) => { | ||
if (address === [...address].reverse().join("")) { | ||
// congratulations! your email is somehow a palindrome | ||
return true | ||
} | ||
// add a customizable error and return false | ||
return ctx.mustBe("a better contact") | ||
return ctx.mustBe("a palindrome") | ||
}) | ||
|
||
const out = goodContact({ | ||
email: "[email protected]", | ||
score: 60 | ||
const palindromicContact = type({ | ||
email: palindromicEmail, | ||
score: "integer < 100" | ||
}) | ||
|
||
interface RuntimeErrors extends ArkErrors { | ||
/**email must be a palindrome (was "[email protected]") | ||
score (133.7) must be... | ||
• an integer | ||
• less than 100*/ | ||
summary: string | ||
} | ||
|
||
const narrowMessage = (e: ArkErrors): e is RuntimeErrors => true | ||
// ---cut--- | ||
const out = palindromicContact({ | ||
email: "[email protected]", | ||
score: 133.7 | ||
}) | ||
|
||
if (out instanceof type.errors) { | ||
// ---cut-start--- | ||
if (!narrowMessage(out)) throw new Error() | ||
// ---cut-end--- | ||
// hover summary to see validation errors | ||
console.error(out.summary) | ||
} else { | ||
console.log(out.email) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,43 @@ | ||
import { type, type Type } from "arktype" | ||
import { ArkErrors, type } from "arktype" | ||
|
||
const _contact = type({ | ||
email: "email", | ||
score: "integer < 100" | ||
}) | ||
|
||
type _Contact = typeof _contact.t | ||
interface RuntimeErrors extends ArkErrors { | ||
/** | ||
``` | ||
email must be a palindrome (was "[email protected]") | ||
score (133.7) must be... | ||
• an integer | ||
• less than 100 | ||
```*/ | ||
summary: string | ||
} | ||
|
||
interface Contact extends _Contact {} | ||
const narrowMessage = (e: ArkErrors): e is RuntimeErrors => true | ||
|
||
export const contact: Type<Contact> = _contact | ||
// ---cut--- | ||
const goodContact = contact.narrow((data, ctx) => { | ||
if (data.score > 75 || data.email.endsWith("@arktype.io")) { | ||
const palindromicEmail = type("email").narrow((address, ctx) => { | ||
if (address === [...address].reverse().join("")) { | ||
// congratulations! your email is somehow a palindrome | ||
return true | ||
} | ||
// add a customizable error and return false | ||
return ctx.mustBe("a better contact") | ||
return ctx.mustBe("a palindrome") | ||
}) | ||
|
||
const palindromicContact = type({ | ||
email: palindromicEmail, | ||
score: "integer < 100" | ||
}) | ||
|
||
const out = goodContact({ | ||
email: "david@gmail.com", | ||
score: 60 | ||
const out = palindromicContact({ | ||
email: "david@arktype.io", | ||
score: 133.7 | ||
}) | ||
|
||
if (out instanceof type.errors) { | ||
// ---cut-start--- | ||
if (!narrowMessage(out)) throw new Error() | ||
// ---cut-end--- | ||
console.error(out.summary) | ||
} else { | ||
console.log(`${out.email}: ${out.score}`) | ||
console.log(out.email) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,10 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"rootDir": "." | ||
} | ||
"rootDir": ".", | ||
"outDir": "out", | ||
"noEmit": false, | ||
"paths": {} | ||
}, | ||
"exclude": ["out", "__tests__"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.