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

[json] Add support for extensions associating JSON schemas with documents dynamically (via API) #230136

Open
Kosta-Github opened this issue Sep 30, 2024 · 5 comments · May be fixed by #239701
Open
Assignees
Labels
feature-request Request for new features or functionality json JSON support issues
Milestone

Comments

@Kosta-Github
Copy link

Add support for extensions associating JSON schemas with documents dynamically (via API).

Right now, it is only possible to associate a JSON schema with a text document by:

  1. using schemas directly built-in into VSCode, e.g., package.json, .babelrc , ...

  2. explicitly mapping it in the user settings

  3. explicitly mapping it in the workspace

  4. explicitly mapping it in an extension via a file-match pattern

But all these associations are static and working only on a folder path and filename matching.

I would like to be able to dynamically/programmatically associate a JSON schema with a document within an extension.

E.g., the extension could first inspect the content of the file and check for specific well-known conditions in order to decide, which JSON schema (or if at all) should be associated with a given text document.

E.g., within our organization we have a lot of different file formats with existing JSON schemas, but we don't have canonical/standardized folders and filenames for the files that would need validation against these JSON schemas, with most of them just ending on *.json somewhere in the folder tree.

I could imagine a mechanism like this to associate a JSON schema with a JSON document doc:

registerJsonSchema(doc, schemaId, schemaSrc)

with a unique schemaId provided by the extension and schemaSrc either be URL or a local file reference.

If the same schemaId is provided for a doc with no schemaSrc the JSON schema association is removed again from that document.

Theoretically schemaSrc could also be a JSON schema object (similar to a schema directly defined in the settings), which would allow you to dynamically build up a schema by the extension and provide this for validation.

And since a lot of configuration files are not just JSON files but also YAML files, some bonus points for allowing the association of JSON schemas also to YAML files, not just JSON files.

Note, that there is also another related feature request: Support for contentMatch in addition to fileMatch for extension contributes.jsonValidation #229696

@CicDiplomacy

This comment has been minimized.

1 similar comment
@CicDiplomacy

This comment has been minimized.

@aeschli
Copy link
Contributor

aeschli commented Oct 1, 2024

@CicDiplomacy why did you add that comment?

@aeschli aeschli changed the title Add support for extensions associating JSON schemas with documents dynamically (via API) [josn] Add support for extensions associating JSON schemas with documents dynamically (via API) Dec 9, 2024
@aeschli aeschli changed the title [josn] Add support for extensions associating JSON schemas with documents dynamically (via API) [json] Add support for extensions associating JSON schemas with documents dynamically (via API) Dec 9, 2024
@aeschli aeschli added json JSON support issues feature-request Request for new features or functionality labels Dec 9, 2024
@aeschli aeschli added this to the Backlog milestone Dec 9, 2024
@Dampfwalze
Copy link

Dampfwalze commented Feb 5, 2025

I am interested in this feature too. I need to dynamically assign schemas, based on conditions that cannot be statically described using glob patterns. Moreover, I need to apply changes to the schemas before use. So I am interested in a solution, where I can not only control which document gets associated with which schema, but also proxy the schemas, so I can modify them minimally.

My proposed API

Extensions should be able to supply providers for two different purposes. One provider for providing a schema Uri for a given TextDocument, and one provider to provide the contents of a given schema uri:

interface JsonApi {
	/**
	 * Register a provider to associate a json schema with a requested
	 * `TextDocument`.
	 *
	 * @param provider The schema association provider.
	 */
	registerSchemaAssociationProvider(provider: SchemaAssociationProvider): Disposable;

	/**
	 * Notify the language server that the schema association has changed for a
	 * given uri.
	 *
	 * @param uri The uri of the document whose schema association has changed.
	 */
	schemaAssociationChanged(uri: Uri): void;

	/**
	 * Register a provider to handle json schema requests for a specific uri
	 * schema.
	 *
	 * @param schema The uri schema to register for.
	 * @param provider The provider to handle the schema request.
	 */
	registerUriSchemaProvider(schema: string, provider: UriSchemaProvider): Disposable;

	/**
	 * Notify the language server that the content of a specific schema, or
	 * multiple schemas has changed.
	 *
	 * @param uri The uri of the schema whose content has changed.
	 */
	schemaContentChanged(uri: Uri | Uri[]): void;
}

interface SchemaAssociationProvider {
	provideSchemaAssociation(document: TextDocument): Uri | null | undefined;
}

interface UriSchemaProvider {
	provideSchemaContent(uri: Uri): string;
}

Naming can be improved.

This api could be used like this:

const api: JsonApi = vscode.extensions.getExtension('vscode.json-language-features').exports;

api.registerSchemaAssociationProvider({
	provideSchemaAssociation(document) {
		if (/.../.test(document.getText())) {
			return vscode.Uri.parse('myExtenionSchema://mySchema');
		}
	},
});

api.registerUriSchemaProvider('myExtenionSchema', {
	provideSchemaContent(uri) {
		return mySchemas[uri.path];
	},
});

@Kosta-Github
Copy link
Author

BTW, this is very similar to the SchemaExtensionAPI.registerContributor() of the popular vscode-yaml extension:

https://github.com/redhat-developer/vscode-yaml/blob/1f14ee6ba1ec0a6529841ceaf661a66e9d390162/src/schema-extension-api.ts#L65-L70

@Dampfwalze Dampfwalze linked a pull request Feb 5, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request Request for new features or functionality json JSON support issues
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants