ArkType

Check data against a Type by calling it, asserting it or testing it with allows.

Calling a Type

A Type is a function. Call it with any data and it returns either the validated output or ArkErrors:

const 
const User: Type<{
    name: string;
    age: number;
}>
User
= type({
name: "string", age: "number.integer >= 0" }) const
const out: {
    name: string;
    age: number;
} | ArkErrors
out
= User({ name: "Alan", age: 41.5 })
if (
const out: {
    name: string;
    age: number;
} | ArkErrors
out
instanceof type.errors) {
// age must be an integer console.error(const out: ArkErrorsout.summary) } else { // out is { name: string; age: number } console.log(
const out: {
    name: string;
    age: number;
}
out
.name)
}

type.errors is the ArkErrors class, so instanceof narrows the result. Invalid data never throws unless you configure onFail. See Errors for what ArkErrors contains.

The output is the data after morphs and defaults have run, so it can differ from the input:

const 
const Form: Type<{
    age: (In: string) => To<number>;
}>
Form
= type({ age: "string.numeric.parse" })
// { age: 41 } Form({ age: "41" }) // ArkErrors: age must be a well-formed numeric string Form({ age: "forty" })

assert

.assert returns the output or throws a TraversalError, for when invalid data is a bug rather than something to handle:

const 
const Form: Type<{
    age: (In: string) => To<number>;
}>
Form
= type({ age: "string.numeric.parse" })
// { age: 41 } const
const form: {
    age: number;
}
form
=
const Form: Type<{
    age: (In: string) => To<number>;
}>
Form
.assert({ age: "41" })
// TraversalError: age must be a well-formed numeric string
const Form: Type<{
    age: (In: string) => To<number>;
}>
Form
.assert({ age: "forty" })

from

.from is .assert with its argument typed as the Type's input, so TypeScript checks data you write by hand:

const 
const User: Type<{
    name: string;
    age: number;
}>
User
= type({ name: "string", age: "number" })
// { name: "David", age: 34 } const
const david: {
    name: string;
    age: number;
}
david
=
const User: Type<{
    name: string;
    age: number;
}>
User
.from({ name: "David", age: 34 })
// TypeScript: Type 'string' is not assignable to type 'number' const
const typo: {
    name: string;
    age: number;
}
typo
=
const User: Type<{
    name: string;
    age: number;
}>
User
.from({ name: "David", age: "34" })
Type 'string' is not assignable to type 'number'.

allows

.allows returns a boolean and narrows its argument, without building errors or running morphs:

const 
const User: Type<{
    name: string;
    age: number;
}>
User
= type({ name: "string", age: "number" })
const const data: unknowndata: unknown = { name: "David", age: 34 } if (
const User: Type<{
    name: string;
    age: number;
}>
User
.allows(const data: unknowndata)) {
// data is { name: string; age: number } console.log(
const data: {
    name: string;
    age: number;
}
data
.name)
} const const Numeric: Type<number | bigint>Numeric = type("number | bigint") // [0, 2n] const numerics = [0, "one", 2n].filter(const Numeric: Type<number | bigint>Numeric.allows)

On a Type with a morph, .allows checks only the input, so data can be allowed and still fail when the Type is called:

const const AtLeast6: Type<(In: string) => To<number>>AtLeast6 = type("string.numeric.parse").to("number >= 6")

// true, since "3" is a numeric string
const AtLeast6: Type<(In: string) => To<number>>AtLeast6.allows("3")

// ArkErrors: must be at least 6
AtLeast6("3")

Standard Schema

Every Type implements Standard Schema, so libraries that accept one can validate with it through ~standard.validate. ArkType validates synchronously, and a failure result is { issues }, each error in the ArkErrors, whose message leaves out its path:

const 
const User: Type<{
    name: string;
    age: number;
}>
User
= type({ name: "string", age: "number" })
const
const result: StandardSchemaV1.Result<{
    name: string;
    age: number;
}>
result
= await
const User: Type<{
    name: string;
    age: number;
}>
User
["~standard"].validate({ name: "David", age: "34" })
if (
const result: StandardSchemaV1.Result<{
    name: string;
    age: number;
}>
result
.issues) {
// must be a number console.log(const result: StandardSchemaV1.FailureResultresult.issues[0].message) } else { // { name: string; age: number } console.log(
const result: StandardSchemaV1.SuccessResult<{
    name: string;
    age: number;
}>
result
.value)
}

Custom checks

To add your own check, use .narrow. To transform data, use .pipe. Both receive a ctx for reporting errors.