redhat-developer / yaml-language-server

Language Server for YAML Files
MIT License
1.06k stars 260 forks source link

Detect common schemas #61

Open keegancsmith opened 6 years ago

keegancsmith commented 6 years ago

Is it possible to add some automatic detection of the schema type for a yaml file (for popular schemas)? This is to allow zero configuration in many cases. For example when looking at kubernetes resources the presence of apiVersion, spec and type is a really good indicator.

sqs commented 6 years ago

Also can possibly use the filename, full path name, or other sibling files.

JPinkney commented 6 years ago

We autodetect schemas from http://schemastore.org/json/ based off of filename but other then that there isn't a way to decide the schema based off of context yet. I believe @andxu was working on something like that for https://github.com/Azure/vscode-kubernetes-tools but I could be wrong.

apupier commented 6 years ago

for information seems another YAML LS is providing schemas: https://github.com/adamvoss/vscode-yaml-languageservice

JPinkney commented 6 years ago

This LS has all the same features as that one. When the creator of that one was closing his vscode extension he wanted to make sure that we supported all his features so his users could transition. Both do provide schemes but neither of them have an automatic way to detect schemas

oetiker commented 5 years ago

how about following the convention vsc uses for json files: https://code.visualstudio.com/docs/languages/json#_json-schemas-settings

basically, the idea is that the yaml file can use the $schema key in the same way it is used in json schema definitions.

$schema: http://json.schemastore.org/coffeelint
line_endings: unix

This syntax is VS Code-specific and not part of the JSON Schema specification but I think it makes a lot of sense and thus would be worth supporting here too.

rvansa commented 5 years ago

What's the status of the last request? I would also appreciate being able to use $schema. An alternative approach that does not require changes on the consumer side is to add a shebang comment:

#!schema: http://json.schemastore.org/foobar
foo:
- bar
JPinkney commented 5 years ago

Currently hasn't been worked on. It is not a hard thing to add I just haven't had the time.

fbricon commented 5 years ago

I think the shebang approach is safer than using a $schema field. If someone uses $schema with a different semantic value, you have a conflict.

You also need to determine what takes precedence, #!schema or the schema association in the preferences. I think the former, since the scope is smaller.

Maybe @NikolasKomonen can work on that

felixfbecker commented 5 years ago

I don't think $schema meaning something else is something to worry about. It is specced in the JSON schema standard, which the YAML language server is built upon. https://json-schema.org/latest/json-schema-core.html#rfc.section.7

SamB commented 5 years ago

Actually, after reading carefully, I've noticed that JSON Schema spec doesn't actually seem to say anything about using $schema outside JSON schemas. (They don't say you may do it, but they also don't say you may not.)

So, that tells us nothing about whether it's okay to use $schema in this way for YAML, sadly :-(.

However, what could go wrong?

  1. $schema could be an absolute URI identifying a JSON Schema: In my opinion, anyone setting such a value who doesn't want us to validate against that schema is some kind of an idiot.
  2. $schema could be an absolute URI identifying something that merely resembles a JSON Schema: This is tricky.
  3. $schema is something else: I guess the user gets some squiggly underlines they don't need? Doesn't seem all that terrible.

However, there are some other issues with $schema:

  1. As the vscode docs point out, consumers might not be prepared for this; for instance, the schema itself might not permit the $schema property.
  2. If we're just generalizing the notation from the JSON Schema spec for use in other files, we're still stuck with the absolute URI requirement, so we can't directly reference a schema in our working tree.

These aren't necessarily showstoppers: Users can arrange that the schema permit $schema, certainly. The other issue is a bit more annoying, but apparently JSON Schema implementations aren't supposed to blindly dereference such URIs anyway, so it would be perfectly reasonable to use a mechanism similar in nature to OASIS XML Catalogs to allow this.

felixfbecker commented 5 years ago

The user is still not forced to add $schema to the document if the consumer of the document will reject it.

we're still stuck with the absolute URI requirement

Where in the spec does it say the URI must be absolute?

In the end, it doesn't look like any of this was a problem for JSON, so I don't see why it would be a problem for YAML.

oetiker commented 5 years ago

Since VC support the use of $schema in JSON, why not simply support it in YAML too? This will not compel anyone to use follow this convention, they can continue using the current config system, but for those who work with schemas that a) require the $schema keyword (these exist) or b) don't care this would make life so much simpler.

an example schema requiring the use of $schema in a normal json data structure.

https://github.com/qooxdoo/qooxdoo-compiler/blob/master/source/resource/qx/tool/schema/v1/compile.json