Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Multiple YAML Documents #344

Open
Fleshgrinder opened this issue Aug 15, 2024 · 5 comments
Open

Support for Multiple YAML Documents #344

Fleshgrinder opened this issue Aug 15, 2024 · 5 comments
Labels
help wanted Extra attention is needed

Comments

@Fleshgrinder
Copy link

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
Copy link
Owner

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

@Fleshgrinder
Copy link
Author

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
Copy link
Owner

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
Copy link
Author

Fleshgrinder commented Sep 15, 2024

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...

@ota-meshi
Copy link
Owner

Sorry for the late reply.
Thank you for sharing the example.
I think it would be nice to change the rule or add option 😄

@ota-meshi ota-meshi added help wanted Extra attention is needed and removed needs more info labels Nov 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants