pub trait SchemaResolver: Send + Sync {
    fn resolve(
        &self,
        root_schema: &Value,
        url: &Url,
        original_reference: &str
    ) -> Result<Arc<Value>, SchemaResolverError>; }
Expand description

A resolver that resolves external schema references. Internal references such as #/definitions and JSON pointers are handled internally.

All operations are blocking and it is not possible to return futures. As a workaround, errors can be returned that will contain the schema URLs to resolve and can be resolved outside the validation process if needed.

Example


struct MyCustomResolver;

impl SchemaResolver for MyCustomResolver {
    fn resolve(&self, root_schema: &Value, url: &Url, _original_reference: &str) -> Result<Arc<Value>, SchemaResolverError> {
        match url.scheme() {
            "json-schema" => {
                Err(anyhow!("cannot resolve schema without root schema ID"))
            },
            "http" | "https" => {
                Ok(Arc::new(json!({ "description": "an external schema" })))
            }
            _ => Err(anyhow!("scheme is not supported"))
        }
    }
}

Required Methods

Resolve an external schema via an URL.

Relative URLs are resolved based on the root schema’s ID, if there is no root schema ID available, the scheme json-schema is used and any relative paths are turned into absolutes.

Additionally the original reference string is also passed, in most cases it should not be needed, but it preserves some information, such as relative paths that are lost when the URL is built.

Implementors