j13k/yaml-lint
Laravel-friendly YAML linter powered by yamllint. Validate YAML files in your project or CI, catch syntax and style issues early, and fail builds on invalid configuration. Simple command integration for consistent YAML across environments.
Installation Add the package via Composer:
composer require --dev j13k/yaml-lint
Register the service provider in config/app.php (if not auto-discovered):
'providers' => [
// ...
J13k\YamlLint\YamlLintServiceProvider::class,
],
Basic Usage Run the linter directly via Artisan:
php artisan yaml:lint path/to/file.yaml
Or integrate it into your CI pipeline (e.g., GitHub Actions, GitLab CI).
First Use Case
Validate a single YAML file (e.g., config/services.yaml):
php artisan yaml:lint config/services.yaml
Output will show syntax errors, indentation issues, or invalid keys.
CI/CD Pipeline
Add to phpunit.xml or .github/workflows/lint.yml:
<php>
<env name="YAML_LINT" value="1"/>
<file name="config/services.yaml"/>
</php>
Or via GitHub Actions:
- name: Lint YAML
run: php artisan yaml:lint config/*.yaml
Pre-Commit Hook
Use php artisan yaml:lint in a pre-commit script (e.g., with Laravel Pint or custom hooks):
# .git/hooks/pre-commit
#!/bin/bash
php artisan yaml:lint --changed-only
Laravel Task Scheduling Schedule nightly YAML validation for critical files:
// app/Console/Kernel.php
protected function schedule(Schedule $schedule)
{
$schedule->command('yaml:lint config/*.yaml')
->dailyAt('23:00');
}
config/yaml-lint.php:
'exclude' => [
'vendor/**',
'storage/logs/**',
],
YamlLint::extend() in a service provider:
YamlLint::extend(function ($errors, $yaml, $path) {
if (str_contains($yaml, 'deprecated_key')) {
$errors[] = "Key 'deprecated_key' is obsolete. Use 'new_key' instead.";
}
});
.yaml-lint.yml in the repo root for team-wide rules:
extends: default
rules:
indentation: error
line-length: disable
False Positives
#) or anchors (&) may trigger false errors.config/yaml-lint.php:
'ignore_patterns' => [
'/#.*/',
'/&[a-zA-Z_]+/',
],
Performance
database/migrations/*.yaml) can be slow.--parallel flag (if supported) or split files into smaller chunks.Indentation Sensitivity
.editorconfig:
[*.{yaml,yml}]
indent_style = space
indent_size = 4
-v for detailed error context:
php artisan yaml:lint --verbose config/services.yaml
php artisan yaml:lint --dry-run
Custom Error Formatting Override the error renderer:
YamlLint::setErrorRenderer(function ($errors) {
return collect($errors)->map(function ($error) {
return "[ERROR] Line {$error['line']}: {$error['message']}";
})->implode(PHP_EOL);
});
Integration with Laravel Mix Add a Webpack plugin to lint YAML during build:
const YamlLintPlugin = require('yaml-lint-webpack-plugin');
mix.webpackConfig({
plugins: [
new YamlLintPlugin({
configFile: '.yaml-lint.yml',
}),
],
});
API Usage Use the underlying parser programmatically:
$validator = app(YamlLint::class);
$errors = $validator->lintString('key: value', 'inline.yaml');
key-names are case-sensitive in .yaml-lint.yml.extends: default
rules:
document-start: disable # Disable if your YAML lacks `---`
How can I help you explore Laravel packages today?