验证器
具有模型上下文协议(MCP)集成的TypeScript验证库,旨在通过编程API和MCP服务器/客户端接口提供全面的数据验证功能。
特性
当前验证器
- JSON验证:使用验证JSON字符串格式
validateJaon() - YAML验证:全面的YAML验证,包括结构和模式验证
运输选项
验证器可以通过多种传输机制访问:
- STDIN/STDOUT:MCP集成的传统命令行界面
- HTTP与SSE:用于浏览器和HTTP客户端集成的基于Web的服务器发送事件
安装
# Install dependencies
pnpm install
# Build the project
pnpm run build快速开始
API直接使用
JSON验证
import { validateJaon } from "./dist/validators/json.mjs";
const isValid = validateJaon('{"name": "John", "age": 30}');
console.log(isValid); // true
const isInvalid = validateJaon('{invalid json}');
console.log(isInvalid); // falseYAML验证
import {
validateYamlStructure,
validateYaml,
validateYamlWithSchemaDetection
} from "./dist/validators/yaml.mjs";
// Basic structural validation
const isStructurallyValid = validateYamlStructure(`
name: John
age: 30
`);
console.log(isStructurallyValid); // true
// Validation with JSON schema
const schema = {
type: "object",
properties: {
name: { type: "string" },
age: { type: "number", minimum: 0 }
},
required: ["name", "age"]
};
const result = validateYaml(`
name: John
age: 30
`, { schema });
console.log(result.structurallyValid); // true
console.log(result.schemaValid); // true
// Auto-detection from schema reference in YAML
const yamlWithSchema = `
$schema: "person-schema"
name: John
age: 30
`;
const schemaRegistry = {
"person-schema": schema
};
const autoResult = validateYamlWithSchemaDetection(yamlWithSchema, schemaRegistry);
console.log(autoResult.schemaValid); // trueMCP客户端使用情况
STDIN/STDOUT运输
import { ValidatorClient } from "./dist/mcp/client.mjs";
const client = new ValidatorClient({
transport: "stdio",
command: "node",
args: ["dist/mcp/server.mjs"]
});
await client.connect();
// JSON validation
const jsonResult = await client.validateJson('{"test": true}');
console.log(jsonResult); // { valid: true, input: '{"test": true}' }
// YAML structural validation
const yamlStructureResult = await client.validateYamlStructure(`
name: John
age: 30
`);
console.log(yamlStructureResult); // { structurallyValid: true, input: "..." }
// YAML with schema validation
const yamlResult = await client.validateYaml(`
name: John
age: 30
`, {
schema: {
type: "object",
properties: {
name: { type: "string" },
age: { type: "number" }
},
required: ["name", "age"]
}
});
console.log(yamlResult.schemaValid); // true
// YAML with auto schema detection
const autoResult = await client.validateYamlWithSchemaDetection(`
$schema: "person-schema"
name: John
age: 30
`, {
"person-schema": {
type: "object",
properties: {
name: { type: "string" },
age: { type: "number" }
}
}
});
await client.disconnect();HTTP与SSE传输
import { ValidatorClient } from "./dist/mcp/client.mjs";
const client = new ValidatorClient({
transport: "sse",
url: "http://localhost:3000/mcp"
});
await client.connect();
// Same API as STDIO transport
const result = await client.validateJson('{"test": true}');
const yamlResult = await client.validateYamlStructure('name: value');
await client.disconnect();运行服务器
STDIN/STDOUT MCP服务器
pnpm run start:stdio带SSE的HTTP MCP服务器
pnpm run start:httpHTTP服务器提供以下端点:
GET http://localhost:3000/mcp-建立SSE连接POST http://localhost:3000/mcp/message-向服务器发送消息GET http://localhost:3000/health-健康检查端点
发展
命令
# Run tests
pnpm test
# Run tests in watch mode
pnpm run test:watch
# Type checking
pnpm run typecheck
# Linting
pnpm run lint
# Build project
pnpm run build项目结构
src/
├── validators/ # All validation functionality
│ ├── json.ts # JSON validation (validateJaon)
│ └── yaml.ts # YAML validation (validateYaml*, extractYamlSchemaReference)
├── mcp/ # MCP server and client implementations
│ ├── server.ts # STDIN/STDOUT MCP server
│ ├── http-server.ts # HTTP MCP server with SSE
│ ├── client.ts # Universal MCP client
│ └── example.ts # Usage examples
tests/
├── json/ # JSON validation tests
├── yaml/ # YAML validation tests
└── mcp/ # MCP integration tests未来的验证器
这个库是为可扩展性而设计的。未来的验证类型将作为单独的模块添加:
- 电子邮件验证:验证电子邮件地址格式
- URL验证:验证URL结构和协议
- 架构验证:根据JSON模式验证数据
- 日期验证:验证日期格式和范围
- 自定义验证:支持用户定义的验证规则
每个验证器将遵循相同的模式:
- API直接进入
src/validators/{validator-type}.ts具有指定出口 - 通过现有的服务器/客户端基础设施进行MCP集成
- 全面的测试覆盖率
tests/{validator-type}/
贡献
- 安装依赖项:
pnpm install - 进行更改
- 运行测试:
pnpm test - 运行类型检查:
pnpm run typecheck - 运行linting:
pnpm run lint - 构建:
pnpm run build
许可证
国际协调委员会
