跳转到内容

Validator

validate 函数用于通用数据校验,validateBodyvalidateQuery 是开箱即用的中间件。

import { validate } from "@ventostack/core";
const schema = {
name: { type: "string", required: true, min: 1, max: 50 },
age: { type: "number", min: 0, max: 200 },
role: { type: "string", enum: ["admin", "user"] },
};
const result = validate({ name: "Alice", age: 25, role: "admin" }, schema);
// { valid: true, errors: [] }
import { validateBody } from "@ventostack/core";
app.post("/users", validateBody({
name: { type: "string", required: true, min: 1 },
email: { type: "string", required: true, pattern: /^.+@.+$/ },
}), (ctx) => {
return ctx.json({ ok: true });
});

校验失败自动返回 400,响应体包含 errors 数组。

import { validateQuery } from "@ventostack/core";
app.get("/search", validateQuery({
q: { type: "string", required: true },
page: { type: "number", min: 1 },
}), (ctx) => {
return ctx.json({ query: ctx.query });
});
const schema = {
tags: { type: "array", required: true, min: 1, items: { type: "string" } },
address: {
type: "object",
properties: {
city: { type: "string", required: true },
zip: { type: "string", pattern: /^\d{6}$/ },
},
},
};
const schema = {
email: {
type: "string",
required: true,
custom(value) {
return value.includes("@") ? null : "邮箱格式不正确";
},
},
};
属性类型说明
type"string" | "number" | "boolean" | "array" | "object"字段类型(必填)
requiredboolean是否必填
minnumber最小值(数字)/ 最小长度(字符串)/ 最少元素(数组)
maxnumber最大值 / 最大长度 / 最多元素
patternRegExp正则匹配
enumunknown[]枚举值列表
itemsFieldRule数组元素规则
propertiesRecord<string, FieldRule>对象属性规则
custom(value) => string | null自定义校验,返回错误字符串或 null