Skip to content
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

feat: Async retriever #703

Merged
merged 1 commit into from
Feb 8, 2025
Merged

feat: Async retriever #703

merged 1 commit into from
Feb 8, 2025

Conversation

Stranger6667
Copy link
Owner

@Stranger6667 Stranger6667 commented Feb 7, 2025

Resolves #385

let validator = jsonschema::async_validator_for(&schema).await?;

Copy link

codecov bot commented Feb 7, 2025

Codecov Report

Attention: Patch coverage is 96.38672% with 37 lines in your changes missing coverage. Please review.

Project coverage is 85.99%. Comparing base (6a529dd) to head (e2c36be).
Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
crates/jsonschema/src/options.rs 83.33% 18 Missing ⚠️
crates/jsonschema-py/src/lib.rs 0.00% 10 Missing ⚠️
crates/jsonschema-py/src/retriever.rs 0.00% 4 Missing ⚠️
crates/jsonschema-py/src/registry.rs 0.00% 2 Missing ⚠️
crates/jsonschema/src/compiler.rs 96.87% 2 Missing ⚠️
crates/jsonschema/src/retriever.rs 99.36% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #703      +/-   ##
==========================================
+ Coverage   85.31%   85.99%   +0.67%     
==========================================
  Files          94       94              
  Lines       14448    15220     +772     
==========================================
+ Hits        12327    13089     +762     
- Misses       2121     2131      +10     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link

codspeed-hq bot commented Feb 7, 2025

CodSpeed Performance Report

Merging #703 will not alter performance

Comparing dd/async-retrieve (e2c36be) with master (6a529dd)

Summary

✅ 47 untouched benchmarks

@Stranger6667 Stranger6667 force-pushed the dd/async-retrieve branch 3 times, most recently from bd74f0f to 10baec1 Compare February 7, 2025 22:10
@Stranger6667
Copy link
Owner Author

Tagging @uncenter @gadomski because of your recent PRs (in taplo and stac-rs respectively) which may benefit from this feature in jsonschema.

This is a very early iteration for async retrieval of external schemas, but it works (the original message with an example above)! If you have any feedback on the API or think that I've missed something here, please, let me know :)

I plan to work more on this feature so it covers your use cases and has the least amount of rough edges in terms of usability and ergonomics.

@Stranger6667 Stranger6667 force-pushed the dd/async-retrieve branch 3 times, most recently from 29fe45c to 0ec3e10 Compare February 8, 2025 16:06
@Stranger6667 Stranger6667 marked this pull request as ready for review February 8, 2025 16:12
@Stranger6667 Stranger6667 force-pushed the dd/async-retrieve branch 2 times, most recently from 57710cf to a45cdcb Compare February 8, 2025 18:16
@Stranger6667
Copy link
Owner Author

Stranger6667 commented Feb 8, 2025

With the latest update, the following would work for the simplest case:

let validator = jsonschema::async_validator_for(&schema).await?;

And custom non-blocking retrievers are also possible:

use serde_json::{Value, json};
use jsonschema::{Draft, AsyncRetrieve, Uri};

// Custom async retriever
struct MyRetriever;

#[async_trait::async_trait]
impl AsyncRetrieve for MyRetriever {
    async fn retrieve(&self, uri: &Uri<String>) -> Result<Value, Box<dyn std::error::Error + Send + Sync>> {
        // Custom retrieval logic
        Ok(json!({}))
    }
}

let schema = json!({
    "$ref": "https://example.com/user.json"
});
let validator = jsonschema::async_options()
    .with_draft(Draft::Draft202012)
    .with_retriever(MyRetriever)
    .build(&schema)
    .await?;

I hope it should be enough for the majority of use cases

@Stranger6667 Stranger6667 force-pushed the dd/async-retrieve branch 2 times, most recently from 9f4fcb8 to a842dad Compare February 8, 2025 18:27
Signed-off-by: Dmitry Dygalo <[email protected]>
@Stranger6667 Stranger6667 merged commit d69a5d3 into master Feb 8, 2025
38 of 39 checks passed
@Stranger6667 Stranger6667 deleted the dd/async-retrieve branch February 8, 2025 18:49
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.

Async SchemaResolver
1 participant