YAML Linter CLI and API
yaml-lint is a straightforward command-line interface (CLI) tool and programmatic API for validating YAML files against a specified schema or for general syntax correctness. It's currently stable at version 1.7.0. The package has seen consistent maintenance releases, with the latest significant update fixing glob pattern issues on Windows and migrating to TypeScript in version 1.3.0. Its key differentiators include the ability to specify different YAML schemas (DEFAULT, FAILSAFE, JSON, CORE), flexible configuration via JSON files, environment variables, or CLI arguments, and support for ignoring files using glob patterns. It processes multi-document YAML sources and provides clear error reporting for invalid files.
Common errors
-
Error: YAMLLINT_SCHEMA environment variable value is not a valid schema type.
cause An unsupported or misspelled schema name was provided via the `YAMLLINT_SCHEMA` environment variable.fixSet `YAMLLINT_SCHEMA` to one of the valid schema types: `DEFAULT_SCHEMA`, `FAILSAFE_SCHEMA`, `JSON_SCHEMA`, or `CORE_SCHEMA`. -
Cannot find module 'yaml-lint'
cause The `yaml-lint` package is not installed or not accessible in the current project's `node_modules`.fixRun `npm install --save-dev yaml-lint` for local project usage or `npm install -g yaml-lint` for global CLI use. -
SyntaxError: Unexpected token 'export'
cause Attempting to use `require()` on a module that primarily uses ES Modules syntax in an environment that doesn't correctly transpile or resolve ESM.fixEnsure your Node.js environment supports ESM (Node.js 12+ with 'type: module' in package.json, or file extension .mjs). Alternatively, use `import yamlLint from 'yaml-lint';` in an ESM context.
Warnings
- breaking Node.js 0.x is no longer supported. Users on older Node.js versions must upgrade to at least Node.js 8 or newer.
- gotcha When configuring ignore patterns, always use forward slashes (e.g., `dir/*.yaml`) regardless of the operating system. Backslashes on Windows might lead to patterns not being correctly applied.
- deprecated The `nocase` rule for glob patterns was removed in an earlier version (1.2.3) to prevent issues on Windows. This might affect how case-insensitive file matching works for ignore patterns.
- gotcha By default, the CLI will only display the first failing file it encounters. To see all failing files, ensure you are using a version that supports displaying multiple errors.
Install
-
npm install yaml-lint -
yarn add yaml-lint -
pnpm add yaml-lint
Imports
- yamlLint
const yamlLint = require('yaml-lint');import yamlLint from 'yaml-lint';
- lint
import yamlLint from 'yaml-lint'; yamlLint.lint('...'); - Types
import type { LintOptions } from 'yaml-lint';
Quickstart
import yamlLint from 'yaml-lint';
import { readFileSync } from 'fs';
import { join } from 'path';
// Example 1: Linting a string directly
yamlLint
.lint('key: value\narray:\n - item1\n - item2')
.then(() => {
console.log('String content is valid YAML.');
})
.catch((error) => {
console.error('String content is invalid YAML:', error.message);
});
// Example 2: Linting a file (assuming a test.yaml exists for this example)
const filePath = join(process.cwd(), 'test.yaml');
// To make this runnable, create a dummy test.yaml for demonstration:
// fs.writeFileSync(filePath, 'foo: bar\nversion: 1.0');
// For a real scenario, ensure 'test.yaml' exists and contains valid YAML
try {
const fileContent = readFileSync(filePath, 'utf8');
yamlLint.lint(fileContent)
.then(() => console.log(`${filePath} is valid YAML.`))
.catch(error => console.error(`${filePath} is invalid YAML:`, error.message));
} catch (err) {
console.warn(`Could not read ${filePath}. Please create it for file linting demo.`);
// Example of how you'd explicitly fail a file lint for a broken file:
// yamlLint.lint('invalid: - \n - yaml')
// .catch(error => console.error('Intentional invalid file content error:', error.message));
}