How to do local file bundling #30
Replies: 7 comments 6 replies
-
I should add, for clarity, there isn't always one root schema in our case. Some schemas reference others as mixins for example, but each schema we have is useable in isolation as part of a tree of schemas where the path tree is about the concept the schema belongs to. |
Beta Was this translation helpful? Give feedback.
-
After a little playing around I figured out how to do it. Example here: https://github.com/micron-research/bundle-example My main complaint about this is that the bundle process is removing IDs of the bundled files. Why would it do this? If I ever did decide to deliver these bundled schema over the web, I can't identify them any more. Is there a way of preventing this behaviour? Another observation at least is that the IDs added to the Would it not be better in both cases not to mess with existing values? |
Beta Was this translation helpful? Give feedback.
-
I know I'm dumping loads into this thread, sorry... I've added a branch I expected a |
Beta Was this translation helpful? Give feedback.
-
Let's cover some fundamentals first. I'll get to actually answering the question of bundling from files next. (It's way easier than you think)
It's 100% normal and expected that your schemas aren't hosted on the web at the URI that identifies them. Don't think of it as "faking it", that's the intended way to use JSON Schema.
If I understand you correctly, you have that right. But, let me add that every schema needs to be identified by a non-relative URI.
{
"$id": "/schema/main",
"$ref": "/schema/foo",
"$defs": {
"foo": {
"$id": "foo",
"type": "string"
}
}
} In this example, the schema is retrieved from the schemaStore with the URI So, JSON Schema is always working with non-relative URIs. Relative URIs are allowed almost everywhere for convenience, but they have to be resolved before they can be used. |
Beta Was this translation helpful? Give feedback.
-
Especially when bundling schemas from files or schemas that are hosted on the web, I strongly recommend not using
{
"$ref": "components/foo.schema.json"
}
{ "type": "string" } const bundledSchema = bundle("file:///path/to/schemas/main.schema.json"); That's it. You don't need to load schemas with
It sounds like you want to define a given path that all of your reference paths append onto. URIs don't work that way. They can only be relative to the current document. If relative paths aren't good enough for some reason, you can go back to using |
Beta Was this translation helpful? Give feedback.
-
Thanks @jdesrosiers, for all your answers! I'll have to digest them all to make sure I understand correctly, but I'm sure I'll need more clarification 😄 |
Beta Was this translation helpful? Give feedback.
-
Ok, so after all your informative answers, I've given up on the idea of local file paths as retrieval URIs and have decided to go with a dynamic domain name so I can run it as a local server to bundle files on the fly, depending on what URI is requested. I guess the last outstanding thing is about OpenAPI 3.1 support. I know you're a busy chap; will there be support for this spec in the future? |
Beta Was this translation helpful? Give feedback.
-
Firstly, sorry if this is documented and I can't see it (wood for the trees problem).
I'm struggling to understand how to properly bundle and validate with local files.
Our API schema documents are never going to live at a real URL as they are part of a proprietary system. According to the spec, they don't have to be a real URL as the
$id
is not meant to be network addressable.I wanted to use
$id
values that were simply path based, which I assume is allowed as a "relative uri" by the spec, and would resolve to the most parent document.The issue I've run into is that I can't
addSchema
using these IDs as they are always rejected as unreachable over the network. I've tried to use theretrievalUri
property but I don't understand how to.This isn't an issue so I've raised this as a discussion, but I would like to understand how I am supposed to bundle and validate a path based tree of schemas that are filesystem only.
Here are some things I want to be able to do:
$id: /my/path/based/schema
and be referenced as$ref: /my/path/based/schema
from any nested path level./my/path/based/schema
it would be able to find other schemas that I have added based on that root level.I don't even know if this is achievable, and would love some advice on how this should be done. There is no
https://
based root level for our setup, and I'm averse to faking one just for the sake of it.Beta Was this translation helpful? Give feedback.
All reactions