-
-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
38f6b12
commit 6e88967
Showing
15 changed files
with
109 additions
and
77 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
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 |
---|---|---|
|
@@ -4,12 +4,6 @@ sidebar: | |
order: 3 | ||
--- | ||
|
||
:::caution[Under Construction] | ||
We're still working on our new docs, as evidenced by the fact that they are not done. | ||
|
||
Check back soon for more! | ||
::: | ||
|
||
TypeScript is extremely versatile for representing types like `string` or `number`, but what about `email` or `integer less than 100`? | ||
|
||
In ArkType, conditions that narrow a type beyond its **basis** are called **constraints**. | ||
|
@@ -82,28 +76,29 @@ type _Contact = typeof _contact.t | |
interface Contact extends _Contact {} | ||
|
||
export const contact: Type<Contact> = _contact | ||
|
||
const contacts = contact.array().atLeastLength(1) | ||
|
||
const extractDuplicates = (arr: typeof contacts.infer) => | ||
Object.values(Object.groupBy(arr, contact => contact.email)).flatMap( | ||
contactsWithEmail => | ||
contactsWithEmail && contactsWithEmail.length > 1 ? | ||
contactsWithEmail[0].email | ||
: [] | ||
) | ||
// ---cut--- | ||
const uniqueContacts = contacts.narrow((arr, ctx) => { | ||
// get a list of the duplicate emails | ||
const duplicates = extractDuplicates(arr) | ||
|
||
// return true if there are no duplicates | ||
if (duplicates.length === 0) return true | ||
|
||
// or false with a custom error message listing the duplicates | ||
return ctx.invalid({ | ||
expected: "an array of unique contacts", | ||
actual: `duplicated by ${duplicates}` | ||
}) | ||
const goodContact = contact.narrow((data, ctx) => { | ||
if (data.score > 75 || data.email.endsWith("@arktype.io")) { | ||
return true | ||
} | ||
// add a customizable error and return false | ||
return ctx.mustBe("a better contact") | ||
}) | ||
|
||
const out = goodContact({ | ||
email: "[email protected]", | ||
score: 60 | ||
}) | ||
|
||
if (out instanceof type.errors) { | ||
console.error(out.summary) | ||
} else { | ||
console.log(out.email) | ||
} | ||
``` | ||
|
||
You now know how to refine your types to enforce additional constraints at runtime (see the full list [here](/reference/constraints)). | ||
|
||
Once your input is fully validated, you still need to make some adjustments before it's ready to use in your code? | ||
|
||
The final section of intro will cover **morphs**, an extremely powerful tool for composing and transforming Types. |
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
--- | ||
title: Morphs & More | ||
sidebar: | ||
order: 4 | ||
--- | ||
|
||
:::caution[Under Construction] | ||
We're still working on our new docs, as evidenced by the fact that they are not done. | ||
|
||
Check back soon for more! | ||
::: |
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
--- | ||
title: Constraints | ||
--- |
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 |
---|---|---|
|
@@ -10,28 +10,22 @@ type _Contact = typeof _contact.t | |
interface Contact extends _Contact {} | ||
|
||
export const contact: Type<Contact> = _contact | ||
|
||
const contacts = contact.array().atLeastLength(1) | ||
|
||
const extractDuplicates = (arr: typeof contacts.infer) => | ||
Object.values(Object.groupBy(arr, contact => contact.email)).flatMap( | ||
contactsWithEmail => | ||
contactsWithEmail && contactsWithEmail.length > 1 ? | ||
contactsWithEmail[0].email | ||
: [] | ||
) | ||
// ---cut--- | ||
const goodContact = contact.narrow((data, ctx) => { | ||
if (data.score > 75 || data.email.endsWith("@arktype.io")) { | ||
return true | ||
} | ||
// add a customizable error and return false | ||
return ctx.mustBe("a better contact") | ||
}) | ||
|
||
const uniqueContacts = contacts.narrow((arr, ctx) => { | ||
// get a list of the duplicate emails | ||
const duplicates = extractDuplicates(arr) | ||
|
||
// return true if there are no duplicates | ||
if (duplicates.length === 0) return true | ||
|
||
// or false with a custom error message listing the duplicates | ||
return ctx.invalid({ | ||
expected: "an array of unique contacts", | ||
actual: `duplicated by ${duplicates}` | ||
}) | ||
const out = goodContact({ | ||
email: "[email protected]", | ||
score: 60 | ||
}) | ||
|
||
if (out instanceof type.errors) { | ||
console.error(out.summary) | ||
} else { | ||
console.log(`${out.email}: ${out.score}`) | ||
} |
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
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