Skip to content

feat: Custom Resolvers for external references - #341

Merged
Stranger6667 merged 18 commits into
Stranger6667:masterfrom
tamasfe:feat-resolver
Jan 31, 2022
Merged

feat: Custom Resolvers for external references#341
Stranger6667 merged 18 commits into
Stranger6667:masterfrom
tamasfe:feat-resolver

Conversation

@tamasfe

@tamasfe tamasfe commented Jan 28, 2022

Copy link
Copy Markdown
Contributor

A PR for #246. It does not touch on the async feature, but makes the library build on wasm32-unknown-unknown with default-features = false and enables my use-case in taplo.

Added

The SchemaResolver trait that is supposed to resolve external schemas by given URLs along with a DefaultResolver that does what was already implemented, and additionally also resolves file schemes via std.

I decided to just use Arc<dyn SchemaResolver> as it is only used by ref validators currently, and adding generics everywhere would've added too much complexity.

A ValidationError::Resolver variant was also added accordingly to report if any of the external resolutions failed.

A compiler option to set a custom resolver was also added.

The following features were added to customize the default resolver:

  • resolve-http: blocking reqwest
  • resolve-file: resolution via std::fs

Both of them are enabled by default so there should be no breaking changes for current users.

Removed

The reqwest error variant.

All reqwest-* features, the users of the library can add reqwest as a dependency itself and enable the features they wish, this is a breaking change, although I don't know if anyone used these features.

Additional Dependencies

  • anyhow: It's very commonly used, and is somewhat better than bare Box<dyn std::error::Error>, although it's not strictly required for this PR.

TODO

More documentation and tests perhaps?

Also I haven't yet ran or compared the benchmarks, but I don't think that there would be any visible performance hits.

@codecov

codecov Bot commented Jan 28, 2022

Copy link
Copy Markdown

Codecov Report

Merging #341 (fbef7c5) into master (edbccd6) will decrease coverage by 0.01%.
The diff coverage is 62.50%.

❗ Current head fbef7c5 differs from pull request most recent head a71f639. Consider uploading reports for the commit a71f639 to get more accurate results
Impacted file tree graph

@@            Coverage Diff             @@
##           master     #341      +/-   ##
==========================================
- Coverage   82.72%   82.70%   -0.02%     
==========================================
  Files          57       57              
  Lines        5516     5533      +17     
==========================================
+ Hits         4563     4576      +13     
- Misses        953      957       +4     
Impacted Files Coverage Δ
jsonschema/src/error.rs 64.95% <0.00%> (+1.09%) ⬆️
jsonschema/src/lib.rs 100.00% <ø> (ø)
jsonschema/src/resolver.rs 86.95% <59.45%> (-4.63%) ⬇️
jsonschema/src/compilation/options.rs 87.12% <100.00%> (+0.11%) ⬆️
jsonschema/src/keywords/pattern.rs 80.43% <100.00%> (+0.21%) ⬆️
jsonschema/src/keywords/ref_.rs 87.93% <100.00%> (+2.47%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update edbccd6...a71f639. Read the comment docs.

@Stranger6667 Stranger6667 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Amazing! Thank you so much @tamasfe for putting this PR with such a great level of details :)
I added a few minor comments, and here are a few more general things:

  • I am good with adding anyhow, it is indeed quite convenient :)
  • Re: benchmarks - I don't think there will be notable changes + caching will reduce it even more
  • A changelog entry would be nice
  • There are failing tests in Python bindings, but it should be solved by adding new resolve-* features

Otherwise, looks great to me!

Comment thread jsonschema/Cargo.toml
Comment thread jsonschema/src/error.rs
Comment thread jsonschema/src/resolver.rs Outdated
Comment thread jsonschema/src/resolver.rs Outdated
Comment thread jsonschema/src/resolver.rs Outdated
@tamasfe

tamasfe commented Jan 31, 2022

Copy link
Copy Markdown
Contributor Author

Thanks @Stranger6667!

I've also added the original schema reference string to be passed to the resolver so that relative references are always preserved.

The tests should pass now as well. I also started building on jsonschema along with this PR in my project with great results so far.

@Stranger6667

Copy link
Copy Markdown
Owner

Thank you @tamasfe for your contribution! :) Great PR!

I also started building on jsonschema along with this PR in my project with great results so far.

Glad to hear about it!

@Stranger6667

Copy link
Copy Markdown
Owner

Could you, please, rebase the branch?

@Stranger6667
Stranger6667 merged commit b43b0e9 into Stranger6667:master Jan 31, 2022
@Stranger6667

Copy link
Copy Markdown
Owner

Strange, that after rebase the UI still showed some conflicts, so I squashed it :) Will make a new release soon

@tamasfe

tamasfe commented Jan 31, 2022

Copy link
Copy Markdown
Contributor Author

Awesome, thanks!

Github also showed that my branch was based on master HEAD, so I'm not entirely sure why the rebase was needed (but it was). It was probably due to making edits on the GUI here.

Sorry about the awful graph, I don't think I'll ever figure git out in this lifetime.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants