ota-meshi / eslint-plugin-json-schema-validator

ESLint plugin that validates data using JSON Schema Validator.
https://ota-meshi.github.io/eslint-plugin-json-schema-validator/
MIT License
70 stars 5 forks source link

Support for Multiple YAML Documents #344

Open Fleshgrinder opened 1 month ago

Fleshgrinder commented 1 month ago

YAML has support for multiple documents via the document start --- and (optional) document end ... markers. Trying to validate a YAML with multiple documents currently fails with:

  2:1  error  Root must be object  json-schema-validator/no-invalid
ota-meshi commented 5 days ago

Currently the plugin handles multiple documents as an array. How do popular deserializers handle multiple documents?

Fleshgrinder commented 5 days ago

The majority I know offer two functions. One to deserialize a single document and another to deserialize multiple (returning some kind of list). The problem here is that a JSON schema is meant to describe a single document, but in YAML we can have multiple documents in a single file.

I think that the library should always assume multiple documents in a single YAML file and validate each separately. A JSON schema cannot disable this YAML functionality anyway.

ota-meshi commented 5 days ago

So multiple documents are treated as a list (array), right? But does the schema expect object? Are there any popular schema definitions and use cases that use multiple documents?

Fleshgrinder commented 4 days ago

Each document has its own schema. It needs to be thought of like a tar file. One file containing many others. Hence, a schema applies per document within a YAML file, not per YAML file.

I don't think it's very widespread. Using a single file with multiple documents always results in unnecessary merge conflicts. The only one case I have at hand where it seems to be a thing is Backstage's catalog-info.yaml (example).

Each document would need to be treated like a separate file and validated individually. This begs the question of how to choose the schema. Obviously it's desirable to have the ability for a single schema to apply to all documents within. This can be achieved via the ESlint config, and via YAML comments (once implemented). Individual schemas can be achieved via a $schema in each document.

That said, I've never seen the need for individual schemas, hence, having only one would IMHO be good enough. Still, it's how YAML works...