Skip to content

Commit

Permalink
docs: Update docs
Browse files Browse the repository at this point in the history
Signed-off-by: Dmitry Dygalo <dmitry@dygalo.dev>
  • Loading branch information
Stranger6667 committed Sep 28, 2024
1 parent 25811ce commit c5643c9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 19 deletions.
35 changes: 17 additions & 18 deletions crates/jsonschema/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@
//! # }
//! ```
//!
//! # Reference Resolving
//! # External References
//!
//! By default, `jsonschema` resolves HTTP references using `reqwest` and file references from the local file system.
//!
Expand All @@ -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<dyn std::error::Error>> {
//! 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<String, Arc<Value>>,
//! struct InMemoryRetriever {
//! schemas: HashMap<String, Value>,
//! }
//!
//! impl SchemaResolver for StaticSchemaResolver {
//! fn resolve(
//! &self,
//! schema: &Value,
//! url: &Url,
//! reference: &str
//! ) -> Result<Arc<Value>, SchemaResolverError> {
//! impl Retrieve for InMemoryRetriever {
//!
//! fn retrieve(
//! &self,
//! uri: &UriRef<'_>,
//! ) -> Result<Value, Box<dyn std::error::Error + Send + Sync>> {
//! 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!({
Expand Down
6 changes: 5 additions & 1 deletion crates/jsonschema/src/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit c5643c9

Please sign in to comment.