From c5643c974d7d57e93422dcf7cd621487592b15ec Mon Sep 17 00:00:00 2001 From: Dmitry Dygalo Date: Sun, 29 Sep 2024 00:08:49 +0200 Subject: [PATCH] docs: Update docs Signed-off-by: Dmitry Dygalo --- crates/jsonschema/src/lib.rs | 35 +++++++++++++++--------------- crates/jsonschema/src/validator.rs | 6 ++++- 2 files changed, 22 insertions(+), 19 deletions(-) diff --git a/crates/jsonschema/src/lib.rs b/crates/jsonschema/src/lib.rs index a53dcbde..abafd993 100644 --- a/crates/jsonschema/src/lib.rs +++ b/crates/jsonschema/src/lib.rs @@ -156,7 +156,7 @@ //! # } //! ``` //! -//! # Reference Resolving +//! # External References //! //! By default, `jsonschema` resolves HTTP references using `reqwest` and file references from the local file system. //! @@ -172,55 +172,54 @@ //! - Disable file resolving: `default-features = false, features = ["resolve-http"]` //! - Disable both: `default-features = false` //! -//! You can implement a custom resolver to handle external references. Here's an example that uses a static map of schemas: +//! You can implement a custom retriever to handle external references. Here's an example that uses a static map of schemas: //! //! ```rust //! # fn main() -> Result<(), Box> { //! use std::{collections::HashMap, sync::Arc}; //! use anyhow::anyhow; -//! use jsonschema::{SchemaResolver, SchemaResolverError}; +//! use jsonschema::{Retrieve, UriRef}; //! use serde_json::{json, Value}; //! use url::Url; //! -//! struct StaticSchemaResolver { -//! schemas: HashMap>, +//! struct InMemoryRetriever { +//! schemas: HashMap, //! } //! -//! impl SchemaResolver for StaticSchemaResolver { -//! fn resolve( -//! &self, -//! schema: &Value, -//! url: &Url, -//! reference: &str -//! ) -> Result, SchemaResolverError> { +//! impl Retrieve for InMemoryRetriever { +//! +//! fn retrieve( +//! &self, +//! uri: &UriRef<'_>, +//! ) -> Result> { //! self.schemas -//! .get(url.as_str()) +//! .get(uri.as_str()) //! .cloned() -//! .ok_or_else(|| anyhow!("Schema not found: {}", url)) +//! .ok_or_else(|| format!("Schema not found: {uri}").into()) //! } //! } //! //! let mut schemas = HashMap::new(); //! schemas.insert( //! "https://example.com/person.json".to_string(), -//! Arc::new(json!({ +//! json!({ //! "type": "object", //! "properties": { //! "name": { "type": "string" }, //! "age": { "type": "integer" } //! }, //! "required": ["name", "age"] -//! })), +//! }), //! ); //! -//! let resolver = StaticSchemaResolver { schemas }; +//! let retriever = InMemoryRetriever { schemas }; //! //! let schema = json!({ //! "$ref": "https://example.com/person.json" //! }); //! //! let validator = jsonschema::options() -//! .with_resolver(resolver) +//! .with_retriever(retriever) //! .build(&schema)?; //! //! assert!(validator.is_valid(&json!({ diff --git a/crates/jsonschema/src/validator.rs b/crates/jsonschema/src/validator.rs index 1e3f0e6b..f2e2fc47 100644 --- a/crates/jsonschema/src/validator.rs +++ b/crates/jsonschema/src/validator.rs @@ -166,7 +166,11 @@ impl<'a> PartialApplication<'a> { } } -/// The structure that holds a JSON Schema nodes. +/// A compiled JSON Schema validator. +/// +/// This structure represents a JSON Schema that has been parsed and compiled into +/// an efficient internal representation for validation. It contains the root node +/// of the schema tree and the configuration options used during compilation. #[derive(Debug)] pub struct Validator { pub(crate) root: SchemaNode,