backend-schema
暂无简介
- License 许可
- MIT
- 最近更新
- 2026-11-11
- Stars
- ★ 3
- Forks
- 0
03 SKILL.md 正文
Backend Schema (Express Validator)
Overview
This skill defines the convention for validating incoming HTTP request data using express-validator's Schema objects.
Trigger
When creating or updating validation schemas for backend API routes.
Do's
- Validate Data Strictly: Ensure all incoming data is strictly validated before reaching the controller. Check for data types (
isString,isNumeric), formats (isEmail,isUUID,isDate), and lengths (isLength). Do not let invalid data pass to the controller. - Validate Authorization Header: If the route requires authentication, ALWAYS include the
authorizationheader validation in the schema using regex/^Bearer\s[\w-]+\.[\w-]+\.[\w-]+$/. - Use
SchemaObject: Always importtype { Schema } from "express-validator"and define schemas as plain objects. - Export Indiviual Schemas: Export a separate constant for each endpoint (e.g.,
RegisterSchema,LoginSchema). - Specify Location (
in): Always define exactly where the field is expected using theinproperty (e.g.,in: ["body"],in: ["params"],in: ["query"],in: ["headers"]). - Explicit Error Messages: Provide a specific
errorMessagefor every validation rule. - Sanitize Data: Use built-in sanitizers like
trim: true,toLowerCase: true, ornormalizeEmail: trueon relevant string inputs. - Custom Validators: Use
custom: { options: (value, { req }) => ... }for interdependent fields (like matching passwords). - Format Consistency: Group validations logically:
in,exists,isString/isNumeric,isLength,matches, followed by sanitizers.
Don'ts
- Don't use array-based chain validation: Avoid
body('field').isString(). Stick exclusively to the Schema object approach. - Don't omit the
inproperty: Omitting it forces express-validator to check all locations (body, query, params, headers), which is a security/performance risk. - Don't place schemas in routers or controllers: Keep them isolated in a
*.schema.tsfile next to their respective feature.
Example Structure
import type { Schema } from "express-validator";
export const ExampleSchema: Schema = {
field: {
in: ["body"],
exists: { errorMessage: "Field is required" },
isString: { errorMessage: "Field must be a string" },
isLength: {
options: { min: 1, max: 50 },
errorMessage: "Field must be between 1 and 50 characters",
},
trim: true,
}
};
04 同仓库更多 Skills
skill-sync
Synchronizes skill metadata to AGENTS.md Auto-invoke sections. Trigger: When syncing skill metadata to AGENTS.md.
skill-creator
Creates new AI agent skills following the Agent Skills spec. Trigger: When user asks to create a new skill, add agent instructions, or document patterns for AI.
skill-commit
Creates professional git commits following conventional-commits format. Trigger: When creating commits, after completing code changes, when user asks to commit.
project-info
General monorepo structure and component overview. Trigger: When AI needs project context or component locations.
frontend-context
Frontend architecture, patterns and conventions for the Next.js 15 app. Trigger: When working on frontend code, components, pages, or API integration.
express-mocks-testing
Unit testing pattern for Express 5 controllers using node-mocks-http and @prisma/client mocks. Trigger: When writing unit tests for backend controllers, testing Express routes, or avoiding Supertest/Server instantiation.