nickel-lang / json-schema-to-nickel

Convert JSON schemas into Nickel contracts
Apache License 2.0
32 stars 0 forks source link

Handle more types of internal references #74

Closed yannham closed 5 months ago

yannham commented 5 months ago

Motivation

Until now, js2n only supported internal references to either top-level definitions or JSON pointer composed only of properties, that is /properties/x/properties/y/[...].

This PR extends the handling to many other JSON schema keyword that can appear in a valid JSON pointer, for example pointer element like items/2, contains or oneOf/0. This is in particular useful to use json-schema-to-nickel together with a schema bundler to get rid of external references: the bundler can create such currently unsupported references.

Content

This change incurred a refactoring which makes the overall environment logistic simpler: there are less ad-hoc distinctions (schemas vs contracts, properties predicates versus definitions predicates, etc.).

In the end, all references are treated as a unique elements in a flat environment (as opposed to a nested structure before) whose identifier is determined by the full pointer plus its usage (contract and predicate). The only remaining special casing is for a contract usage of a property-only path, which can be referenced directly from the converted schema and doesn't need to be duplicated in the environment.

We also got rid of the JsonPointer type in favor of a more structured SchemaPointer.

Limitations

The only remaining unsupported keywords are due to schemars limitations: schemars doesn't handle properly nested definitions (or, put differently, js2n only handles references to top-level definitions) and some less frequent keywords, such as prefixItems.

Beside keywords, remaining limitations for internal references are: