Auto throw if invalid

I have been looking through the docs and the API manually, but I haven't found something elegant that I am looking for. Is there a way to strictly throw an error if the validation doesn't pass? While I don't normally mind the explicitness of instanceof type.errors, I kinda prefer the method opt-in from Zod (parse vs safeParse) to make that choice. To be clear this would purely be a DX improvement to prevent typing out the error catching manually for type inference.
8 Replies
natedunn
natedunnOP3w ago
And forgive me if I missed something obivious here
natedunn
natedunnOP3w ago
ArkType
ArkType Docs
TypeScript's 1:1 validator, optimized from editor to runtime
natedunn
natedunnOP3w ago
i see the onFail configuration but that appears to be global and effect all validations instead of a safe/strict method opt-in
Travis
Travis3w ago
.assert
import { type } from "arktype"

const Thing = type({
name: "string",
"versions?": "(number | string)[]"
})

const out = Thing.assert({
name: "TypeScript",
versions: ["5.8.2", 6, 7n]
})
import { type } from "arktype"

const Thing = type({
name: "string",
"versions?": "(number | string)[]"
})

const out = Thing.assert({
name: "TypeScript",
versions: ["5.8.2", 6, 7n]
})
natedunn
natedunnOP3w ago
ah dang thats it. fwiw .assert() doesn't seem to appear in the docs. nor anything based on my original question. maybe something worth updating/adding in the docs. appreciate the help @Travis
TizzySaurus
TizzySaurus3w ago
It's mentioned within https://arktype.io/docs/type-api
ArkType
ArkType Docs
TypeScript's 1:1 validator, optimized from editor to runtime
TizzySaurus
TizzySaurus3w ago
But yeah, could be clearer I suppose
natedunn
natedunnOP3w ago
You are right, thank you for pointing it out. search doesn't seem to catch it though. maybe tables are omitted from search?

Did you find this page helpful?