集成

标准模式

ArkType 自豪地支持并与 ValibotZod 共同编写新的 Standard Schema API。

Standard Schema 允许您和您的依赖项集成与库无关的验证逻辑。如果您正在构建或维护一个具有对 ArkType 和/或其他验证库的 peer 依赖的库,我们推荐尽可能通过 Standard Schema 的 API 来使用它,这样您的用户可以选择最适合他们需求的解决方案!

tRPC

ArkType 可以轻松与 tRPC 一起使用:

// trpc >= 11 accepts a Type directly
t.procedure.input(
	({
		name: "string",
		"age?": "number"
	})
)

// tRPC < 11 accepts the `.assert` prop
t.procedure.input(
	({
		name: "string",
		"age?": "number"
	}).assert
)

drizzle

Drizzle 维护着一个官方的 drizzle-arktype,它可以用于为您的 Drizzle 模式创建类型。

import { pgTable, text, integer } from "drizzle-orm/pg-core"
import { createSelectSchema } from "drizzle-arktype"

const  = pgTable("users", {
	id: integer().generatedAlwaysAsIdentity().primaryKey(),
	name: text().notNull(),
	age: integer().notNull()
})

// Type<{ id: number; name: string; age: number }>
const  = createSelectSchema()

react-hook-form

react-hook-form 通过 @hookform/resolvers 内置支持 ArkType:

import { useForm } from "react-hook-form"
import { arktypeResolver } from "@hookform/resolvers/arktype"
import { type } from "arktype"

const  = type({
	firstName: "string",
	age: "number.integer > 0"
})

// in your component
const {
	,
	,
	formState: {  }
} = useForm({
	resolver: arktypeResolver()
})

对于自定义的受控输入,您可以将推断的类型传递到钩子本身:

useForm<typeof User.infer>(/*...*/)

hono

Hono 通过 @hono/arktype-validator 内置支持 ArkType:

const  = ({
	name: "string",
	age: "number"
})

app.post("/author", arktypeValidator("json", user), c => {
	const  = c.req.valid("json")
	return c.json({
		success: true,
		message: `${.name} is ${.age}`
	})
})

hono-openapi 还提供了对 OpenAPI 文档生成的实验性支持。

oRPC

oRPC 内置支持 Standard Schema,因此 ArkType 可以开箱即用地无缝工作:

os.input(
	({
		name: "string",
		"age?": "number"
	})
)

On this page