-
-
Notifications
You must be signed in to change notification settings - Fork 70
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
4fe9623
commit 56f8d0b
Showing
7 changed files
with
27 additions
and
74 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
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,75 +1,28 @@ | ||
import { scope, type } from "arktype" | ||
import { Disjoint } from "../schema/shared/disjoint.ts" | ||
import { buildApi, jsDocGen } from "./jsDocGen.ts" | ||
import { type } from "arktype" | ||
|
||
// type stats on attribute removal merge 12/18/2024 | ||
// { | ||
// "checkTime": 10.98, | ||
// "types": 409252, | ||
// "instantiations": 5066185 | ||
// } | ||
|
||
const uniqueStrings = type("string[]").narrow((arr, ctx) => { | ||
const seen: Record<string, number> = {} | ||
for (let i = 0; i < arr.length; i++) { | ||
if (arr[i] in seen) { | ||
return ctx.reject({ | ||
expected: "a unique string", | ||
actual: `a duplicate of '${arr[i]}' at index ${seen[arr[i]]}`, | ||
relativePath: [i] | ||
}) | ||
} else seen[arr[i]] = i | ||
const user = type({ | ||
name: "string", | ||
projects: "string[]", | ||
nested: { | ||
nested2: { | ||
key: "string.numeric.parse | number" | ||
} | ||
} | ||
return true | ||
}) | ||
|
||
// const notFoo = type.string.narrow((s, ctx) => { | ||
// if (s !== "foo") return true | ||
// // ["names", 1] | ||
// console.warn(ctx.path) | ||
// return ctx.mustBe("not foo") | ||
// }) | ||
|
||
// const obj = type({ | ||
// names: notFoo.array() | ||
// }) | ||
|
||
// // ArkErrors: names[1] must be not foo (was "foo") | ||
// obj({ names: ["bar", "foo"] }) | ||
|
||
const closedObjectScope = scope( | ||
{ | ||
user: { | ||
name: "string" | ||
} | ||
}, | ||
{ | ||
onUndeclaredKey: "reject" | ||
} | ||
) | ||
const types = closedObjectScope.export() | ||
types.user({ name: "Alice", age: 99 }).toString() //? | ||
user.extends({ name: "string" }) //? | ||
|
||
const user = type({ | ||
password: "string >= 8" | ||
}).configure({ | ||
message: ctx => | ||
`${ctx.propString || "(root)"}: ${ctx.actual} isn't ${ctx.expected}` | ||
const out = user({ | ||
name: "Josh Goldberg", | ||
projects: ["typescript-eslint", "mocha", 999] | ||
}) | ||
// ArkErrors: (root): a string isn't an object | ||
const out1 = user("ez123") | ||
// but `.configure` only applies shallowly, so the nested error isn't changed! | ||
// ArkErrors: password must be at least length 8 (was 5) | ||
const out2 = user({ password: "ez123" }) | ||
|
||
const mod = type.module( | ||
{ isEven: "number%2" }, | ||
{ | ||
divisor: { | ||
expected: ctx => `% ${ctx.rule} !== 0`, | ||
problem: ctx => `${ctx.actual} ${ctx.expected}` | ||
} | ||
} | ||
) | ||
// ArkErrors: 3 % 2 !== 0 | ||
mod.isEven(3) | ||
if (out instanceof type.errors) { | ||
console.log(`Encountered ${out.length} errors:`) | ||
console.log(out.summary) | ||
} else { | ||
console.log(`Valid result:`) | ||
console.log(out) | ||
} | ||
|
||
console.log(user.expression) |
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