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).
composer require aurabx/harmony-toml-validator- PHP ^8.3 - Uses typed properties, named parameters, and modern type system
- yosymfony/toml ^1.0 - TOML parsing library
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";
}$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";
}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
[[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"- All TOML types: string, integer, boolean, float, array, table
- Type mismatches throw
TypeValidationException
required = true- Field must be presentrequired_if = "condition"- Conditional requirements (e.g.,backend == 's3')- Missing required fields throw
RequiredFieldException
enum = [...]- Value must be in allowed listmin/max- Numeric bounds checkingmin_items/max_items- Array length validationpattern- Regex pattern matching- Constraint violations throw
ConstraintViolationException
- Tables with
pattern = truematch multiple instances (e.g.,network.*matchesnetwork.default,network.management) - Supports
pattern_constraintfor regex validation of table names
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
}TypeValidationException- Type mismatchesRequiredFieldException- Missing required fieldsConstraintViolationException- Enum, min/max, pattern violationsSchemaLoadException- Schema file errors
# Run all tests (unit + integration)
composer test
# Run specific test suite
vendor/bin/phpunit --testsuite=Unit
vendor/bin/phpunit --testsuite=Integration# Run PHPStan at level 8
composer analyse
# Run both analysis and tests
composer check# Generate HTML and text coverage reports
composer test:coverage
# HTML report available in coverage/ directory- 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
- 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
- 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
- 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
composer testMIT