Skip to content

Commit

Permalink
feat: Add ValidationOptions::with_registry
Browse files Browse the repository at this point in the history
Signed-off-by: Dmitry Dygalo <[email protected]>
  • Loading branch information
Stranger6667 committed Jan 31, 2025
1 parent e1fb6fe commit 7081758
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## [Unreleased]

### Added

- Re-export `referencing::Registry` as `jsonschema::Registry`.
- `ValidationOptions::with_registry` that allows for providing a predefined `referencing::Registry`. [#682](https://github.com/Stranger6667/jsonschema/issues/682)

## [0.28.3] - 2025-01-24

### Fixed
Expand Down
6 changes: 6 additions & 0 deletions crates/jsonschema-referencing/src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,12 @@ impl Registry {
_ => unreachable!(),
}
}

pub fn resources(&self) -> impl Iterator<Item = (&Uri<String>, Arc<Resource>)> {
self.resources
.iter()
.map(|(uri, resource)| (uri, Arc::clone(resource)))
}
}

fn process_resources(
Expand Down
2 changes: 1 addition & 1 deletion crates/jsonschema/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ pub use error::{ErrorIterator, MaskedValidationError, ValidationError};
pub use keywords::custom::Keyword;
pub use options::ValidationOptions;
pub use output::BasicOutput;
pub use referencing::{Draft, Error as ReferencingError, Resource, Retrieve, Uri};
pub use referencing::{Draft, Error as ReferencingError, Registry, Resource, Retrieve, Uri};
pub use validator::Validator;

use serde_json::Value;
Expand Down
36 changes: 36 additions & 0 deletions crates/jsonschema/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,42 @@ impl ValidationOptions {
}
self
}

/// Use external schema resources from the registry, making them accessible via references
/// during validation.
///
/// # Example
///
/// ```rust
/// # use serde_json::json;
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// use jsonschema::{Registry, Resource};
///
/// let mut registry = Registry::try_new(
/// "urn:name-schema",
/// Resource::from_contents(json!({"type": "string"}))?
/// )?;
/// let schema = json!({
/// "properties": {
/// "name": { "$ref": "urn:name-schema" }
/// }
/// });
/// let validator = jsonschema::options()
/// .with_registry(registry)
/// .build(&schema)?;
/// assert!(validator.is_valid(&json!({ "name": "Valid String" })));
/// assert!(!validator.is_valid(&json!({ "name": 123 })));
/// # Ok(())
/// # }
/// ```
pub fn with_registry(&mut self, registry: referencing::Registry) -> &mut Self {
// NOTE: This is not particularly efficient, and should be changed in the future
for (uri, resource) in registry.resources() {
self.resources
.insert(uri.as_str().to_string(), (&*resource).clone());
}
self
}
/// Register a custom format validator.
///
/// # Example
Expand Down

0 comments on commit 7081758

Please sign in to comment.