定制 aurabx/harmony-toml-validator 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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 present
  • required_if = "condition" - Conditional requirements (e.g., backend == 's3')
  • Missing required fields throw RequiredFieldException

Constraints

  • enum = [...] - Value must be in allowed list
  • min / max - Numeric bounds checking
  • min_items / max_items - Array length validation
  • pattern - Regex pattern matching
  • Constraint violations throw ConstraintViolationException

Pattern-Based Tables

  • Tables with pattern = true match multiple instances (e.g., network.* matches network.default, network.management)
  • Supports pattern_constraint for 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 mismatches
  • RequiredFieldException - Missing required fields
  • ConstraintViolationException - Enum, min/max, pattern violations
  • SchemaLoadException - 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

  1. SchemaLoader - Parses schema TOML files into internal representation
  2. SchemaDefinition/SchemaTable/SchemaField - Internal schema data structures
  3. ValidationContext - Tracks validation state and field paths
  4. Validator Rules - Individual validation rule implementations
  5. TomlValidator - Main orchestrator that coordinates validation

Validation Flow

  1. Load schema file using SchemaLoader
  2. Parse TOML configuration using yosymfony/toml
  3. Iterate through schema tables and fields
  4. Apply validation rules to configuration values
  5. 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 我们能提供哪些服务?
定制开发 / 二次开发

基于 aurabx/harmony-toml-validator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 37
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 31
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-11-06