-
-
Notifications
You must be signed in to change notification settings - Fork 228
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
Reference local files, by schema/property id #22
Comments
I'm not 100% sure what you mean. Can you provide an example? There's no need to pre-load the schemas, because JSON Schema $Ref Parser automatically finds the $referenced schemas and loads them itself. Does that answer your question? |
I use ids for $ref instead of //common.json
{
"$schema": "http://json-schema.org/draft-04/schema#",
"id": "common",
"definitions": {
"email": {
"id": "email",
"type": "string",
"format": "email"
}
}
//user.json
{
"$schema": "http://json-schema.org/draft-04/schema#",
"id": "user",
"type": "object",
"properties": {
"email": {
"$ref": "email"
}
}
} |
Ah, I see what you mean. Couple of problems though:
All that said... I might be willing to add support for the |
Sorry for co-opting, but if I have understood correctly; It would be very nice though, to use the My scenario is that that the schemas come out of a db mirroring the fs, but the Perhaps to make it optional and off by default, as an extension or an alternative parser/resolver would be viable, and not confuse too much on part of the specifications? So this is my +1 (for someone) to make it possible. Thanks for an awesome module. |
ajv has this sort of functionality. You can add any number of schemas to the instance of the validator after specifying the subject schema, and then references will resolve based on the The use of $id and $ref in the above schema is not exactly how it's supposed to work. The $id must be a URI, and those $ids are not. URI resolution is HARD to understand! I've tried. It's easier to think about in terms of a link on a web page. If the URL of the page you are on X, and a link on that page is Y, how does that apply? It's the same with $ref resolution in a schema file. What's needed to resolve this issue?
Based on the above and some initial digging, the code for loading and storing an index of JSON Schema files could PROBABLY be lifted from ajv, and resolution COULD be achieved by creating a new resolver which is always run first if additional files have been loaded this way. This also applies schemas which are defined with URNs. Uncommon, but in use. If you want a sample set of schemas to work with, feel free to use these. A note on termonology: Including one schema in another schema is called "transclusion". Helpful distinction when you're trying to talk about two similar but subtily different events. |
Like @brettstack, I have a set of schema files that are not accessible through http but only accessible from a file directory. These schema are unfortunately referenced by their @James-Messinger Any pointer would be very much appreciated. |
@5amfung It looks like custom resolvers are supported: https://github.com/James-Messinger/json-schema-ref-parser/blob/master/docs/plugins/resolvers.md If you're willing to write a resolver plugin which replaces the HTTP resolver, please do, and make a PR for it. An issue with that solution is that schemas which use a mixture of addressable and non addressable URLs would run into problem. Maybe you could add provision in your resolver to only resolve specific domains, otherwise pass through the URL to the standard resolver. |
If you have time to spend on this, then I'm happy to test and confirm my end, as I have a need, but currently no resource to apply to the issue. |
@5amfung have you resolved your problem in some way? Having schemas stored locally is such a natural usage scenario, that I wonder how it hasn't been solved one way or another until now. I was trying to use them in NodeJS app, so simply planned to load them from local dir and validate some data structures. |
I think this is part of the greater $id rewrites needed to get them up to date with the latest changes in 2019-09: #145 |
Let's combine efforts on $id resolution on #145 to avoid duplicating any conversations. I'll take some of Ben's comments over. |
The problem for older drafts is different to 2019-09 and above. I'm in the process of writing a blog post about this. |
I'm currently using the ajv lib and some custom logic to dereference my schemas, and then I found this lib which will probably do a better job. Is there a way to add schemas so this lib is aware of them? My JSON schemas reference by id, so for example in ajv I'm loading all my schemas
schemas.forEach(schema => ajv.addSchema(schema))
and then trying to parse the $refs. Is there something similar I can do for this lib? Or is the example in the docs the only way"$ref": "schemas/people/Bruce-Wayne.json"
?Thanks
The text was updated successfully, but these errors were encountered: