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

Check $schema key inside the document to know what schema to validate against #574

Open
Zegnat opened this issue Mar 2, 2025 · 3 comments

Comments

@Zegnat
Copy link

Zegnat commented Mar 2, 2025

Sometimes there are files where multiple schemas from the catalogue might match, e.g. a manifest.json file:

ℹ Processing ./src/manifest.json
ℹ Cache hit: using cached response from https://www.schemastore.org/api/json/catalog.json
ℹ Cache hit: using cached response from https://json.schemastore.org/schema-catalog.json
ℹ Searching for schema in https://www.schemastore.org/api/json/catalog.json ...
ℹ Found multiple possible matches for ./src/manifest.json. Possible matches:

  Foxx Manifest
  ArangoDB Foxx service manifest file
  https://json.schemastore.org/foxx-manifest.json

  UI5 Manifest
  UI5 Manifest (manifest.json)
  https://raw.githubusercontent.com/SAP/ui5-manifest/master/schema.json

  WebExtensions
  WebExtension manifest files
  https://json.schemastore.org/webextension.json

  Web App Manifest
  Web Application manifest file
  https://json.schemastore.org/web-manifest-combined.json

✖ Found multiple possible schemas to validate ./src/manifest.json

As I also depend on JSON schemas when editing these type of files, I often supply a specific $schema within the file so VSCode can manage. See the following screenshots showing how VSCode is warning me about all the keys before I add $schema:

Screenshot of VSCode showing the first three lines of a web extension manifest JSON file, all JSON members have yellow squigly lines because of warnings.

Screenshot of VSCode showing the same three lines of a web extension manifest JSON file with an explicit schema link prepended., all JSON members have yellow squigly lines because of warnings.

  1. Can v8r break ties based on the $schema member?
  2. Should v8r go as far as to prefer the explicit $schema member over the catalogue?

In case of 2, it could save the entire round trip to schemastore.org. If all files in a repository include $schema it will not need to check a catalogue at all.

@Zegnat
Copy link
Author

Zegnat commented Mar 2, 2025

As an aside to this, it would be really nice if $schema was allowed by the v8r config schema. This would then allow the schema to be used by the code editor by doing something like the following:

{
    "$schema": "./node_modules/v8r/config-schema.json",
    "customCatalog": {
        "schemas": [
            {
                "name": "JSON schema for WebExtensions manifest files",
                "fileMatch": [
                    "manifest.json"
                ],
                "location": "https://json.schemastore.org/webextension.json"
            }
        ]
    }
}

Currently this cannot be done as v8r (correctly) validates its config file before using it, leading to an additional properties error because of $schema:

/Users/Zegnat/path/.v8rrc.json# must NOT have additional properties

@chris48s
Copy link
Owner

chris48s commented Mar 3, 2025

If you've got files where SchemaStore doesn't know how to validate them, you can use a custom catalog to tell v8r which schemas to use for which files.
This can either be a standalone catalog
https://chris48s.github.io/v8r/usage-examples/#using-a-custom-catlog
or you can do it in the config file
https://chris48s.github.io/v8r/configuration/
So there is a way to solve the case where there are multiple possible matches in schemastore.

This idea of files that contain a $schema key has come up once before a few years ago in #129
I asked some follow up questions but the original poster never responded so it never went anywhere. Maybe you have some info on how widespread this is or resources I could look at.

@Zegnat
Copy link
Author

Zegnat commented Mar 5, 2025

Thanks for linking the closed PR. I had looked through the repo before, but clearly still managed to somehow miss that one.

I am not sure how wide-spread the usage is. Like I described above, I mostly do it because it makes VSCode’s linting work and delivers auto-complete. It would not surprise me if this is how most people use it, which might be hard to track.

I will do some discovery work this weekend and see if I can find any projects that explicitly use or call out the use of $schema and then report back. Probably better to have this as an issue for discussing than immediately jump to a PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants