2

Say I want to validate a YAML file against a JSON schema in Intellij IDEA. The file's structure would be like:

foo:
  command: touch /tmp/a.txt  # I know I don't need this but it's an example
bar:
  command: echo "Hello World!" > /tmp/a.txt
baz:
  command: cat /tmp/a.txt
  dependencies:
    - foo
    - bar

So the property names can be any string, but the dependencies should only be keys/property names of the root object. Ideally I would specify an enum, but this question suggests it's not possible Use object property keys as enum in JSON schema (unless the answer is obsolete).

Still, I have noticed that when you write a schema in Intellij and you add a "required" = [...] it autocompletes the required fields with the property names of the "property" object (even though it doesn't use them to validate, but close enough for my purpose). I have checked out the schema for it http://json-schema.org/draft-07/schema# but haven't been able to understand how it does that.

Is there a way that I can define my schema so Intellij autocompletes based on another properties' keys like it does when you define a schema?

user3146897
  • 541
  • 1
  • 5
  • 15

1 Answers1

2

There is nothing in the schema itself that indicates possible values from data. There's actually no requirement that items in the required array also be defined in properties.

This sort of functionality is defined by the IDE only. IntelliJ IDEA documents the ability to add custom schemas:

Besides schemas from JSON Schema Store, IntelliJ IDEA lets you configure and use custom schemas from other storages. You can download the required schema and store it under the project root or specify the URL of the resource so IntelliJ IDEA can download the schema automatically.

To configure a custom JSON Schema:

In the Settings/Preferences dialog ⌘,, go to Languages and Frameworks | Schemas and DTDs | JSON Schema Mappings.

https://www.jetbrains.com/help/idea/json.html#ws_json_schema_add_custom

It also details later how to make the intelesense provide a rich preview:

Using HTML descriptions in JSON schema #

By default, IntelliJ IDEA escapes HTML characters when displaying documentation for JSON schema definitions in documentation popups. To get nice looking documentation with rich HTML markup, store the HTML description in the x-intellij-html-description extension property instead of description.

https://www.jetbrains.com/help/idea/json.html#ws_json_show_doc_in_html

However,

autocompletes based on another properties' keys

sounds like custom functionality specifically designed for writing JSON Schema. JSON Schema itself cannot reference data dynamically like that (which I assume is what you were thinking).

Relequestual
  • 11,631
  • 6
  • 47
  • 83
  • Thanks @Relequestual, I'm aware of all these, but still Intellij given the schema http://json-schema.org/draft-07/schema# somehow knows how to autocomplete the required array. So there must be something about that specific schema that enables this. It seems like there should be a way to take advantage of this in my schemas to achieve autocomplete, even if it doesn't serve to validate. – user3146897 Aug 19 '19 at 14:09
  • 1
    I would bet this is custom code they have written to make this happen. `required` is not the same class of keyword, as `required` is an array of strings while `dependencies` is an object where the values are schemas. – Relequestual Aug 19 '19 at 15:05
  • 1
    There is nothing in the schema itself that indicates such. There's actually no requirement that items in the `required` array also be defined in `properties`. – Relequestual Aug 19 '19 at 15:07
  • 1
    Thanks, can you update your answer with this your last comments as they actually answer the question? I'll accept the answer. – user3146897 Aug 21 '19 at 07:53