lts/strict-openapi-validator
Composer 安装命令:
composer require lts/strict-openapi-validator
包简介
High-performance, strict OpenAPI validator for PHP 8.4+ with LLM-optimized error output
README 文档
README
Ultra-strict OpenAPI request/response validator for PHP 8.4+ with LLM-optimized error output.
Purpose
Designed to catch API contract violations. Enforces absolute adherence to OpenAPI specifications - rejecting anything remotely invalid. Collects all validation errors before throwing to provide complete failure context.
Core Philosophy
- Collect all errors: Gathers every validation issue before throwing, never stops at first failure
- Detect, don't fix: Precisely identify violations, never attempt automatic correction
- Complete validation: Nothing incomplete, nothing extra, nothing unexpected
- Strict types: No implicit coercion -
"123"≠123 - Helpful hints: Common issues (snake/camel case confusion, type mismatches) get additional guidance
Requirements
- PHP ^8.4
Installation
composer require lts/strict-openapi-validator
Validation Modes
Three modes control what gets validated and how strictly:
Client Mode
For consuming third-party APIs where you don't control the spec quality.
- Requests: Validated strictly with errors thrown (catch your mistakes before sending)
- Responses: Validated with warnings only (log, don't throw - the API may return unexpected data)
- Spec structure: NOT validated (third-party specs are often sloppy)
use LongTermSupport\StrictOpenApiValidator\Spec; use LongTermSupport\StrictOpenApiValidator\ValidationMode; $spec = Spec::createFromFile('/path/to/third-party-api.json', ValidationMode::Client);
Server Mode
For APIs you own where you want to enforce spec quality.
- Responses: Validated strictly with errors thrown
- Spec structure: Validated strictly
- Requests: Validated with a view to generating helpful, public-safe error messages
$spec = Spec::createFromFile('/path/to/your-api.json', ValidationMode::Server);
Both Mode (default)
Full strict validation on everything - spec, requests, and responses all throw on error.
// Default - validates everything strictly $spec = Spec::createFromFile('/path/to/openapi.json'); // Explicit $spec = Spec::createFromFile('/path/to/openapi.json', ValidationMode::Both);
API
Spec Loading
use LongTermSupport\StrictOpenApiValidator\Spec; use LongTermSupport\StrictOpenApiValidator\ValidationMode; // Full strict validation (default - Both mode) $spec = Spec::createFromFile('/path/to/openapi.json'); // Client mode - skip spec structure validation $spec = Spec::createFromFile('/path/to/openapi.json', ValidationMode::Client); // From array $spec = Spec::createFromArray($specArray, ValidationMode::Client);
JSON String Validation
use LongTermSupport\StrictOpenApiValidator\Validator; // Validate request body against spec for specific path/method Validator::validateRequest(string $json, Spec $spec, string $path, string $method): void; // Validate response body Validator::validateResponse(string $json, Spec $spec, string $path, string $method, int $statusCode): void;
Symfony Request/Response Validation
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; // Middleware-style validation Validator::validate(Request $request, Spec $spec): void; Validator::validateSymfonyResponse(Response $response, Spec $spec, string $path, string $method): void;
Error Output
The validator collects all validation errors and throws a single exception containing the complete list. Errors are verbose, precise, and LLM-optimized:
Validation failed with 3 errors:
[1] unexpected string at request.body.user.age, breaking openapi.yml line 142 expectations
expected: integer
received: "25"
hint: this looks like a type confusion issue - received string "25" but spec requires integer 25
[2] unexpected property at request.body.user_name, breaking openapi.yml line 156 expectations
expected: userName
reason: additionalProperties not allowed
hint: this looks like a snake_case/camelCase confusion - did you mean "userName"?
[3] invalid format at response.body.email, breaking openapi.yml line 89 expectations
expected: email format (RFC 5322)
received: "not-an-email"
Each error includes:
- Exact location: JSONPath to problematic field
- Spec reference: Line number in OpenAPI spec
- Expected vs Received: Clear comparison
- Reason: Why validation failed
- Hint (when applicable): Suggestions for common issues
Validation Strictness
The validator enforces:
- Complete structure: All required fields must be present
- No extra fields: Additional properties rejected unless explicitly allowed
- Exact types: No type coercion (
"123"is not123) - Valid formats: Email, URI, date-time, UUID etc. must be strictly valid
- Schema adherence: Structure must match spec exactly
- No missing data: Nothing incomplete
Common Issue Detection
The validator provides helpful hints for common problems:
- snake_case/camelCase confusion: Detects similar field names with different casing
- Type confusion: String numbers vs integers/floats
- Format issues: Invalid but close-to-valid formats
- Missing required fields: Lists what's missing and where spec defines it
Note: Hints are suggestions only - the validator never attempts to fix issues.
Development
This project uses lts/php-qa-ci for quality assurance.
License
MIT
Author
lts/strict-openapi-validator 适用场景与选型建议
lts/strict-openapi-validator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 75 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 11 月 17 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「api」 「validator」 「strict」 「openapi」 「high-performance」 「llm」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 lts/strict-openapi-validator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 lts/strict-openapi-validator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 lts/strict-openapi-validator 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Base object, checking access to undefined properties and methods
Runn Me! Validation and Sanitization Library
Extension for Opis JSON Schema
Strict Typing for local variables in PHP
Package to improve Laravel framework with features to support more strict typed Laravel.
Free service to identify primitive type of a variable
统计信息
- 总下载量: 75
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 22
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-11-17