whitlockjc / json-refs

Various utilities for JSON Pointers (http://tools.ietf.org/html/rfc6901) and JSON References (http://tools.ietf.org/html/draft-pbryan-zyp-json-ref-03).
MIT License
223 stars 63 forks source link

Bug resolving vega-lite JSON schema #207

Open NfNitLoop opened 4 months ago

NfNitLoop commented 4 months ago
> json-refs resolve https://vega.github.io/schema/vega-lite/v5.json

  error: JSON Pointer points to missing location: #/definitions/PredicateComposition/anyOf

Adding --force doesn't solve the issue and continues to return the same error.

whitlockjc commented 4 months ago

--force isn't expected to resolve this issue as there is no invalid reference that can be ignored, just an unexpected error. I'll take a peek.

whitlockjc commented 4 months ago

Alright, I've figured it out. https://vega.github.io/schema/vega-lite/v5.json is a JSON Schema file and its $ref plays by different rules than JSON References. So the many cases where $ref is adjacent to other properties, these are not valid in JSON References but are for JSON Schema. But the problematic one is the $ref on line 2 of v5.json because it tells json-refs to replace the entire content of the document with the resolved value it points to. At that point, json-refs will then continue to attempt to resolve the other references it found which now no longer point to valid places in the resolved document and therein lies the rub. Typically json-refs handles this is by using a graph of dependencies to know the order in which to resolve references. But I'm guessing this logic doesn't work with documents having a JSON Reference at the root of the document and elsewhere, as that's invalid. So I now need to figure out if I want to update json-refs to work in cases where it's working with invalid JSON References.

Just to be sure, if you remove the $ref at the root of the document, json-refs resolve works just fine but it fails later on when trying to JSON.stringify a document that is so large.

NfNitLoop commented 4 months ago

Thanks for looking into it!

I ran into this because I was going to try to generate a Zod schema validator (and type definitions) from the schema, and https://github.com/StefanTerdell/json-schema-to-zod requires that I resolve refs first (and recommends json-refs).

I'm going to open an issue over there just to let them know that json-refs has this known issue with JSON Schemas.

FWIW, I've moved on from the issue and decided not to pursue it further so it's not necessarily a pressing issue for me at the moment. Thanks for this library!