karenetheridge / JSON-Schema-Modern

Validate data against a schema using a JSON Schema
https://metacpan.org/release/JSON-Schema-Modern/
Other
10 stars 1 forks source link

bundle a schema together with all of its references #16

Open karenetheridge opened 4 years ago

karenetheridge commented 4 years ago

If one schema has $refs to another in another document, it might be desirable to package all the referenced schemas up as definitions in the first schema as a single point of reference.

see https://json-schema.org/draft/2019-09/json-schema-core.html#rfc.appendix.B.1

karenetheridge commented 4 years ago

Injecting $id keywords into each referenced subschema in the bundle won't work if the $ref contains a json pointer fragment. In that case we would have to rewrite the $ref to refer to a local definition instead.

karenetheridge commented 4 years ago

This requires a more sophisticated traversal mechanism, as we need to walk a schema in order to identify all its $ref keywords, without any false positives that might be embedded under default, examples, const or enum.

karenetheridge commented 2 years ago

This is now possible, using the callback hooks in 'traverse'. We can use a hook on $ref to compile a list of json pointer locations where $refs live. Then, if one wants a document containing path /x/y/z and all of the locations referenced (recursively) by anything under /x/y/z, we can walk this location list recursively to find everything that can possibly be referenced by /x/y/z and its subschemas.

This code would belong in JSON::Schema::Modern::Document, and be heavily used by JSON::Schema::Modern::Document::OpenAPI for things like "give me the openapi document, but only things suitable to show to users with authentication role foo" or "...only paths with tag bar".