hyperjump-io / json-schema-language-tools

JSON Schema Langauge Server
MIT License
2 stars 13 forks source link

Server - Support custom dialects #29

Open jdesrosiers opened 4 months ago

jdesrosiers commented 4 months ago

Add support for custom dialects.

A schema can be used as a meta-schema if it has a $vocabulary keyword. Only custom dialects using supported vocabularies will be supported, but errors should be handled appropriately when an unsupported vocabulary is encountered. $schema completion suggestions (#9) should include custom dialects in the workspace.

The registerSchema function (@hyperjump/json-schema) will automatically load the schema as a dialect if it includes $vocabulary, but unregisterSchema doesn't unload the dialect (this is bug that needs to be addressed).

Example usage:

/schemas/my-dialect.schema.json

{
  "$id": "https://example.com/my-dialect",
  "$schema": "https://json-schema.org/draft/2020-12/schema",

  "$vocabulary": {
    "https://json-schema.org/draft/2020-12/vocab/core": true,
    "https://json-schema.org/draft/2020-12/vocab/applicator": true,
    "https://json-schema.org/draft/2020-12/vocab/validation": true
  },
  ...
}

/schemas/my-schema.schema.json

{
  "$schema": "https://example.com/my-dialect",
  ...
}
lmgyuan commented 4 months ago

I am happy to work on this issue!

lmgyuan commented 4 months ago

Thank you for assigning me to this task! Should I still look for another teammate or could I just communicate with you? : )

jdesrosiers commented 4 months ago

I assigned you assuming @LordRona was going to be your partner. If they don't follow through I'll be your partner.

lmgyuan commented 4 months ago

I hope that it's OK for me to start working now since I have not heard back from @LordRona and there is not much time before the application period ends. It seems that there are two tasks: handle errors for unsupported vocabularies when validating custom dialects; fixed unregisterSchema so it would unload the dialect. Am I supposed to do both? If so, I have a few questions:

  1. How do we know what vocabularies are supported or unsupported? Is there a documentation that I can reference to?
  2. Should I edit the validation.js to handle errors for unsupported vocabularies?
  3. the registerSchema and unregisterSchema seem to be defined in the @hyperjump/json-schema. Should I make changes to those codes to fix unregisterSchema?

Thanks in advance! Also, I attempted to join the Slack for JSON-Schema org, but it does not let me. May I ask whether it's possible for you to send me an invite or guide me to find the right one? Thanks a lot in advance!

Best, Yuan

LordRona commented 4 months ago

I am excited to work on this issue

LordRona commented 4 months ago

Hello @lmgyuan I will like to collaborate with you on this issue

jdesrosiers commented 4 months ago

It seems that there are two tasks: handle errors for unsupported vocabularies when validating custom dialects; fixed unregisterSchema so it would unload the dialect. Am I supposed to do both?

The main task will be registering schemas in the workspace so custom dialects get loaded. Once you do that, most of the rest should just work. Unloading dialects that got removed from the workspace and handling errors are just edge cases you'll need to properly handle.

  1. How do we know what vocabularies are supported or unsupported? Is there a documentation that I can reference to?
  2. Should I edit the validation.js to handle errors for unsupported vocabularies?

An error will be thrown @hyperjump/json-schema if you try to validate against a schema that includes an unsupported vocabulary. I'm pretty sure all you'll have to do is catch that error in validateSchema in server.js and return a diagnostic.

the registerSchema and unregisterSchema seem to be defined in the @hyperjump/json-schema. Should I make changes to those codes to fix unregisterSchema?

Yes.

I attempted to join the Slack for JSON-Schema org, but it does not let me.

Did you use https://json-schema.org/slack? I checked and that should be working.

lmgyuan commented 4 months ago

@jdesrosiers Great! Do you have any thoughts on how to solve this? : )

jdesrosiers commented 4 months ago

I'm not sure exactly. You could register the schema when getSchemaResources is called. That would cover loading dialects, but not unloading them when a schema is deleted from the workspace. There isn't an event that you can use to easily handle that case.

Whenever something changes on the filesystem, the validateWorkspace function is called. That calls workspaceSchemas which provides a list of all the current schemas in the workspace. I imagine you might have to keep track of which schemas are in the workspace from one call to validateWorkspace to another and use the diff to determine that a schema was deleted and you need to unregister that schema. Hopefully you can find a better approach than that.

jdesrosiers commented 4 months ago

I went ahead and handled the part where unregisterSchema needs to unload the dialect. So, you only need to do the part where you have to register and unregister schemas as they are changed/added/removed in the workspace.

https://github.com/hyperjump-io/json-schema/commit/dc91a6db02883fae640205494adb11921f0c59a0

lmgyuan commented 3 months ago

I went ahead and handled the part where unregisterSchema needs to unload the dialect. So, you only need to do the part where you have to register and unregister schemas as they are changed/added/removed in the workspace.

hyperjump-io/json-schema@dc91a6d

Thanks!!! That helps a ton! I am assuming that the "changed/added/removed in the workspace" refers to these codes, right? If so, I should be able to open a PR on Friday.

Because the deadline is approaching, I have also drafted this proposal for your review, if possible. Any comments will be greatly appreciated! Thanks in advance!

jdesrosiers commented 3 months ago

Those are utility functions. You shouldn't be adding any language server specific code there, but one of those functions is being used where you'll need to do your work. This is where I expect your changes to be, https://github.com/hyperjump-io/json-schema-language-tools/blob/main/language-server/src/server.js#L124-L142