json-schema-validator/no-invalid
validate object with JSON Schema.
- ⚙️ This rule is included in
configs.recommended.
📖 Rule Details
This rule validates the file with JSON Schema and reports errors.
// File name is ".eslintrc.json"
/* eslint json-schema-validator/no-invalid: 'error' */
{
overrides: [
{
files: ["good"],
/* ✓ GOOD */
extends: ["foo"],
},
{
files: ["bad"],
/* ✗ BAD */
extends: [42],
},
],
}The format keyword (for example email, uri, date-time) is validated using ajv-formats.
🔧 Options
{
"json-schema-validator/no-invalid": [
"error",
{
schemas: [
{
fileMatch: [".eslintrc.json"],
schema: {
/* JSON Schema Definition */
}, // or string
},
],
useSchemastoreCatalog: true,
mergeSchemas: true, // or ["$schema", "options", "catalog"]
mostSpecificErrorsOnly: true,
},
],
}schemas... Define an array of any JSON Schema.fileMatch... A list of known file names (or globs) that match the schema.schema... An object that defines a JSON schema. Or the path of the JSON schema file or URL.
useSchemastoreCatalog... Iftrue, it will automatically configure some schemas defined in https://www.schemastore.org/api/json/catalog.json. DefaulttruemergeSchemas... Iftrue, it will merge all schemas defined inschemas, at the$schemafield within files, and the catalogue. If an array is given, it will merge only schemas from the given sources. DefaultfalsemostSpecificErrorsOnly... Iftrue, collapses cascadingoneOf/anyOferrors: when every alternative fails the same way the duplicate/umbrella errors are removed, otherwise only the closest-matching alternative is reported (alongside amust match one of the allowed schemasmessage). Defaultfalse
This option can also be given a JSON schema file or URL. This is useful for configuring with the /* eslint */ directive comments.
/* eslint json-schema-validator/no-invalid: [
"error",
"https://www.schemastore.org/eslintrc"
]
*/
module.exports = {
overrides: [
{
files: ["good"],
/* ✓ GOOD */
extends: ["foo"],
},
{
files: ["bad"],
/* ✗ BAD */
extends: [42],
},
],
};Specify a schema with a yaml-language-server comment
For YAML files, you can specify the JSON Schema inline using a modeline comment in the same syntax accepted by the YAML Language Server or JetBrains IDEs:
# yaml-language-server: $schema=<url-or-path>
# $schema: <url-or-path>
foo: barThe schema location may be a URL, or a path relative to the YAML file.
Only a modeline in the document's header comment block is used, and the first such modeline wins. If a file has both a modeline comment and a $schema property, the modeline comment takes precedence.
Setting the schema to none disables schema validation for the file:
# yaml-language-server: $schema=none
foo: barUse with .vue
This rule supports .vue custom blocks.
Example:
<i18n>
{
"en": {
"hello": "Hello"
}
}
</i18n>You must also install eslint-plugin-vue to enable .vue files validation. See here for details.
To match a custom block, use a glob like this:
{
// If you want to match the <i18n> block.
fileMatch: ["**/*blockType=i18n*"],
schema: { type: "object" /* JSON Schema Definition */ },
}The following custom blocks will try to test if it matches with the virtual filenames.
<i18n lang="yaml">
# path/to/foo.vue/i18n.yaml?vue&type=custom&blockType=i18n&lang=yaml
foo: bar
</i18n>
<i18n lang="json">
// path/to/foo.vue/i18n.json?vue&type=custom&blockType=i18n&lang=json
{ "foo": "bar" }
</i18n>
<i18n>
// path/to/foo.vue/i18n.json?vue&type=custom&blockType=i18n
{ "foo": "bar"}
</i18n>📚 Further reading
🚀 Version
This rule was introduced in eslint-plugin-json-schema-validator v0.1.0