YAML Linter CLI and API

1.7.0 · active · verified Wed Apr 22

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

Warnings

Install

Imports

Quickstart

Demonstrates both direct string linting and how to lint the content of a file using the programmatic API.

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));
}

view raw JSON →