aurabx/harmony-toml-validator
Composer 安装命令:
composer require aurabx/harmony-toml-validator
包简介
TOML validation library for Harmony Proxy configuration files against DSL schemas
README 文档
README
PHP library for validating TOML configuration files against Harmony DSL schemas. This package provides a cross-language compatible validator that matches the schema validation implemented in harmony-proxy (Rust).
Installation
composer require aurabx/harmony-toml-validator
Requirements
- PHP ^8.3 - Uses typed properties, named parameters, and modern type system
- yosymfony/toml ^1.0 - TOML parsing library
Quick Start
Validate a Configuration File
use Runbeam\HarmonyTomlValidator\TomlValidator; use Runbeam\HarmonyTomlValidator\Exceptions\ValidationException; $validator = new TomlValidator(); try { $validator->validateFile( '/path/to/config.toml', '/path/to/harmony-config-schema.toml' ); echo "✓ Configuration is valid\n"; } catch (ValidationException $e) { echo "✗ Validation failed\n"; echo "Field: " . $e->getFieldPath() . "\n"; echo "Error: " . $e->getFormattedError() . "\n"; }
Validate TOML Content
$content = <<<'TOML' [proxy] id = "gateway-01" log_level = "info" TOML; try { $validator->validateContent($content, '/path/to/schema.toml'); echo "✓ Configuration is valid\n"; } catch (ValidationException $e) { echo "✗ Validation failed: " . $e->getMessage() . "\n"; }
Schema Format
This validator uses the Harmony DSL schema format (TOML-based) that defines:
- Tables - Configuration sections (e.g.,
[proxy],[network.default]) - Fields - Table properties with type and constraint information
- Types - string, integer, boolean, float, array, table
- Constraints - enum, min/max, array bounds, regex patterns, required fields
Example Schema
[[table]] name = "proxy" required = true description = "Core proxy configuration" [[table.field]] name = "id" type = "string" required = true description = "Unique proxy identifier" [[table.field]] name = "log_level" type = "string" required = false default = "error" enum = ["trace", "debug", "info", "warn", "error"] description = "Logging verbosity level"
Validation Coverage
Type Checking
- All TOML types: string, integer, boolean, float, array, table
- Type mismatches throw
TypeValidationException
Required Fields
required = true- Field must be presentrequired_if = "condition"- Conditional requirements (e.g.,backend == 's3')- Missing required fields throw
RequiredFieldException
Constraints
enum = [...]- Value must be in allowed listmin/max- Numeric bounds checkingmin_items/max_items- Array length validationpattern- Regex pattern matching- Constraint violations throw
ConstraintViolationException
Pattern-Based Tables
- Tables with
pattern = truematch multiple instances (e.g.,network.*matchesnetwork.default,network.management) - Supports
pattern_constraintfor regex validation of table names
Exception Hierarchy
All validation errors are exceptions extending ValidationException with detailed context:
try { $validator->validateFile($config, $schema); } catch (ValidationException $e) { $e->getFieldPath(); // Dotted path: "network.default.bind_port" $e->getRuleName(); // Rule that failed: "min", "enum", "required" $e->getActualValue(); // The value that failed validation $e->getExpectedConstraint(); // What was expected $e->getMessage(); // Short error message $e->getFormattedError(); // Formatted error with full context }
Specialized Exceptions
TypeValidationException- Type mismatchesRequiredFieldException- Missing required fieldsConstraintViolationException- Enum, min/max, pattern violationsSchemaLoadException- Schema file errors
Development
Running Tests
# Run all tests (unit + integration) composer test # Run specific test suite vendor/bin/phpunit --testsuite=Unit vendor/bin/phpunit --testsuite=Integration
Static Analysis
# Run PHPStan at level 8 composer analyse # Run both analysis and tests composer check
Code Coverage
# Generate HTML and text coverage reports composer test:coverage # HTML report available in coverage/ directory
Architecture
Core Components
- SchemaLoader - Parses schema TOML files into internal representation
- SchemaDefinition/SchemaTable/SchemaField - Internal schema data structures
- ValidationContext - Tracks validation state and field paths
- Validator Rules - Individual validation rule implementations
- TomlValidator - Main orchestrator that coordinates validation
Validation Flow
- Load schema file using
SchemaLoader - Parse TOML configuration using yosymfony/toml
- Iterate through schema tables and fields
- Apply validation rules to configuration values
- Collect errors and report first validation exception
Limitations
- Nested field names (e.g.,
options.path) require special handling not yet implemented - Currently validates table-level and field-level constraints
- For advanced conditional logic, consider extending
RequiredValidator
Related Projects
- harmony-dsl - Schema definitions and DSL specification
- harmony-proxy - Rust-based gateway that consumes validated configurations
- Runbeam Cloud API - Uses this library for configuration validation
Testing
composer test
License
MIT
aurabx/harmony-toml-validator 适用场景与选型建议
aurabx/harmony-toml-validator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 37 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 11 月 06 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「configuration」 「DSL」 「schema」 「validation」 「toml」 「Harmony」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 aurabx/harmony-toml-validator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 aurabx/harmony-toml-validator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 aurabx/harmony-toml-validator 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Configuration component
Extension for Opis JSON Schema
Feature complete, object oriented, composable, extendable Elasticsearch query DSL builder for PHP.
EAV modeling package for Eloquent and Laravel.
serialize Symfony Forms into JSON schema
Elasticsearch DSL library
统计信息
- 总下载量: 37
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 31
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-11-06