Skip to main content

Getting Started

Install ESLint and the plugin:

npm install --save-dev eslint eslint-plugin-json-schema-validator-2

Enable the recommended Flat Config preset:

import jsonSchemaValidator from "eslint-plugin-json-schema-validator-2";

export default [...jsonSchemaValidator.configs.recommended];

The recommended preset registers parsers for JSON, JSONC, JSON5, YAML, and TOML files and enables json-schema-validator-2/no-invalid as a warning for those files.

The complete option reference is in the no-invalid rule docs. The broader README is available in the repository root.

Custom schemasโ€‹

Pass schemas directly to the rule when a file does not advertise a $schema property or when you need to override SchemaStore detection:

export default [
...jsonSchemaValidator.configs.base,
{
rules: {
"json-schema-validator-2/no-invalid": [
"error",
{
schemas: [
{
fileMatch: ["config/*.json"],
schema: "./schemas/config.schema.json",
},
],
useSchemastoreCatalog: false,
},
],
},
},
];

YAML schema commentsโ€‹

YAML files can use a language-server directive when the validated data cannot include a $schema property:

# yaml-language-server: $schema=./schemas/config.schema.json
enabled: true

The rule prefers a normal YAML $schema property when one exists, then falls back to the directive comment.

Reporting and cache settingsโ€‹

Use reportMode: "most-specific" to hide broad parent errors when deeper errors already identify the failing value:

export default [
...jsonSchemaValidator.configs.base,
{
rules: {
"json-schema-validator-2/no-invalid": [
"error",
{
reportMode: "most-specific",
},
],
},
},
];

Remote schemas are cached automatically for 30 days. The default cache uses node_modules/.cache/eslint-plugin-json-schema-validator-2 when the plugin is installed under node_modules, and falls back to .cache/eslint-plugin-json-schema-validator-2 in the ESLint current working directory. Configure the cache from shared settings when CI or local tooling needs a specific location:

export default [
{
settings: {
"json-schema-validator-2": {
cache: {
directory: ".cache/json-schema-validator-2",
ttl: 1000 * 60 * 60 * 24 * 30,
},
},
},
},
];

Set ttl: false if you want to keep using cached remote schemas without scheduling background refreshes.

Markdown frontmatterโ€‹

Use configs.frontmatter to validate leading YAML frontmatter in Markdown, MDX, or MDC files. The processor exposes the frontmatter as a virtual *.frontmatter.yaml file:

export default [
...jsonSchemaValidator.configs.frontmatter,
{
rules: {
"json-schema-validator-2/no-invalid": [
"error",
{
schemas: [
{
fileMatch: ["**/*.frontmatter.yaml"],
schema: "./schemas/frontmatter.schema.json",
},
],
useSchemastoreCatalog: false,
},
],
},
},
];