chris48s / v8r

✔️ A command-line JSON, YAML and TOML validator that's on your wavelength
https://chris48s.github.io/v8r/
MIT License
28 stars 5 forks source link

RFC: Multidoc YAML support #378

Closed aaronnickovich closed 3 days ago

aaronnickovich commented 10 months ago

I fell in love with LSPs using schemastore so I am trying to convince my devops team to use this project in their CI/CD for schema validation.

My target files was almost exclusively multidoc YAML files so I modified the cli.js and parser.js to support multidoc YAML files.

Would you want this functionality contributed back? If so, I can discuss further and open a PR.

npm run v8r catalog-info.yaml

> v8r@2.1.0 v8r
> src/index.js catalog-info.yaml

ℹ No config file found
ℹ Processing ./catalog-info.yaml
ℹ Found schema in https://www.schemastore.org/api/json/catalog.json ...
ℹ Validating ./catalog-info.yaml against schema from https://json.schemastore.org/catalog-info.json ...
ℹ parsed yaml file as a multi-yaml doc
✔ ./catalog-info.yaml[0] is valid

✔ ./catalog-info.yaml[1] is valid
chris48s commented 10 months ago

It is not a use-case I am super familiar with, but happy to look at a PR.

Just thinking through some considerations: We would have to assume that all of the yaml documents in a given file conform to the same schema. I don't think we could deal with a situation where a file contains multiple yaml documents and they need to be validated against different schemas. At the moment everything is based on one schema being used to validate a file. I think that's a reasonable limitation.

aaronnickovich commented 10 months ago

Yes, customizing per-doc in a YAML file doesnt fit the philosophy of the schemastore which is based on the filename, path, and extension.

Also, most of the yaml schemas I see in the schemastore are supposed to be a single doc so it's an edge-case, of course.

For context, I am piloting an instance of https://backstage.io which is just a catalog of people, teams, software, and apis. Each team just maintains their own catalog-info.yaml file and each can contain somewhere between 1 - 6 docs. An example is here: https://github.com/backstage/backstage/blob/master/packages/catalog-model/examples/acme/org.yaml

Anways, I'll work on it and open a PR in a few days.

chris48s commented 1 week ago

I've just released v8r 4.0.0.

This release introduces some features relevant to this issue:

  1. It is now possible to extend the functionality of v8r by installing or writing plugins. This includes adding new input file parsers. https://chris48s.github.io/v8r/category/plugins/
  2. The JSON output format is no longer a map of filename to ValidationResult. It is just an array of ValidationResult objects https://github.com/chris48s/v8r/blob/e3f23b4d9364c4a9cd6e240d35e279e48a258016/CHANGELOG.md?plain=1#L5-L6 Crucially for this issue, this change relaxes the constraint that there was a one-to-one mapping between filename and result of validation.

At the moment, it would still not be possible to implement this as a plugin. There are still changes to the internals required to implement support for formats like multi-doc yaml or ndjson which include multiple documents in a single file. However, in implementing the plugin system I have at least thought about this issue and designed it in a way that those changes would not require another backwards-compatibility break.

From where we are now, the extra changes required to the internals would be:

Then if those modifications to internals are made, it would then be possible to write plugins (either userspace or core plugins) that return an array of document objects and v8r would correctly handle it.

chris48s commented 3 days ago

I just released v8r v4.1.0 which includes this feature https://www.npmjs.com/package/v8r/v/4.1.0 https://chris48s.github.io/v8r/usage-examples/#files-containing-multiple-documents