pub struct Registry { /* private fields */ }Expand description
A registry of JSON Schema resources, each identified by their canonical URIs.
Registries store a collection of in-memory resources and their anchors. They eagerly process all added resources, including their subresources and anchors. This means that subresources contained within any added resources are immediately discoverable and retrievable via their own IDs.
§Resource Retrieval
Registry supports both blocking and non-blocking retrieval of external resources.
§Blocking Retrieval
use referencing::{Registry, Resource, Retrieve, Uri};
use serde_json::{json, Value};
struct ExampleRetriever;
impl Retrieve for ExampleRetriever {
fn retrieve(
&self,
uri: &Uri<String>
) -> Result<Value, Box<dyn std::error::Error + Send + Sync>> {
// Always return the same value for brevity
Ok(json!({"type": "string"}))
}
}
let registry = Registry::options()
.retriever(ExampleRetriever)
.build([
// Initial schema that might reference external schemas
(
"https://example.com/user.json",
Resource::from_contents(json!({
"type": "object",
"properties": {
// Should be retrieved by `ExampleRetriever`
"role": {"$ref": "https://example.com/role.json"}
}
}))?
)
])?;§Non-blocking Retrieval
use referencing::{Registry, Resource, AsyncRetrieve, Uri};
use serde_json::{json, Value};
struct ExampleRetriever;
#[async_trait::async_trait]
impl AsyncRetrieve for ExampleRetriever {
async fn retrieve(
&self,
uri: &Uri<String>
) -> Result<Value, Box<dyn std::error::Error + Send + Sync>> {
// Always return the same value for brevity
Ok(json!({"type": "string"}))
}
}
let registry = Registry::options()
.async_retriever(ExampleRetriever)
.build([
(
"https://example.com/user.json",
Resource::from_contents(json!({
// Should be retrieved by `ExampleRetriever`
"$ref": "https://example.com/common/user.json"
}))?
)
])
.await?;The registry will automatically:
- Resolve external references
- Cache retrieved schemas
- Handle nested references
- Process JSON Schema anchors
Implementations§
Source§impl Registry
impl Registry
Sourcepub fn options() -> RegistryOptions<Arc<dyn Retrieve>>
pub fn options() -> RegistryOptions<Arc<dyn Retrieve>>
Get RegistryOptions for configuring a new Registry.
Sourcepub fn try_from_resources(
pairs: impl IntoIterator<Item = (impl AsRef<str>, Resource)>,
) -> Result<Registry, Error>
pub fn try_from_resources( pairs: impl IntoIterator<Item = (impl AsRef<str>, Resource)>, ) -> Result<Registry, Error>
Sourcepub fn try_with_resource(
self,
uri: impl AsRef<str>,
resource: Resource,
) -> Result<Registry, Error>
pub fn try_with_resource( self, uri: impl AsRef<str>, resource: Resource, ) -> Result<Registry, Error>
Create a new registry with a new resource.
§Errors
Returns an error if the URI is invalid or if there’s an issue processing the resource.
Sourcepub fn try_with_resources(
self,
pairs: impl IntoIterator<Item = (impl AsRef<str>, Resource)>,
draft: Draft,
) -> Result<Registry, Error>
pub fn try_with_resources( self, pairs: impl IntoIterator<Item = (impl AsRef<str>, Resource)>, draft: Draft, ) -> Result<Registry, Error>
Create a new registry with new resources.
§Errors
Returns an error if any URI is invalid or if there’s an issue processing the resources.
Sourcepub fn try_with_resources_and_retriever(
self,
pairs: impl IntoIterator<Item = (impl AsRef<str>, Resource)>,
retriever: &dyn Retrieve,
draft: Draft,
) -> Result<Registry, Error>
pub fn try_with_resources_and_retriever( self, pairs: impl IntoIterator<Item = (impl AsRef<str>, Resource)>, retriever: &dyn Retrieve, draft: Draft, ) -> Result<Registry, Error>
Create a new registry with new resources and using the given retriever.
§Errors
Returns an error if any URI is invalid or if there’s an issue processing the resources.
Sourcepub fn resolver(&self, base_uri: Uri<String>) -> Resolver<'_>
pub fn resolver(&self, base_uri: Uri<String>) -> Resolver<'_>
Create a new Resolver for this registry with a known valid base URI.
pub fn resolver_from_raw_parts( &self, base_uri: Arc<Uri<String>>, scopes: List<Uri<String>>, ) -> Resolver<'_>
Sourcepub fn resolve_against(
&self,
base: &Uri<&str>,
uri: &str,
) -> Result<Arc<Uri<String>>, Error>
pub fn resolve_against( &self, base: &Uri<&str>, uri: &str, ) -> Result<Arc<Uri<String>>, Error>
Resolves a reference URI against a base URI using registry’s cache.
§Errors
Returns an error if base has not schema or there is a fragment.
Sourcepub fn find_vocabularies(&self, draft: Draft, contents: &Value) -> VocabularySet
pub fn find_vocabularies(&self, draft: Draft, contents: &Value) -> VocabularySet
Returns vocabulary set configured for given draft and contents.