Skip to content

Commit

Permalink
fix(validate): stuff was deprecated in v0.20
Browse files Browse the repository at this point in the history
  • Loading branch information
gadomski committed Sep 19, 2024
1 parent 35a5211 commit 17006f0
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions validate/src/validator.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::{Error, Result};
use jsonschema::{CompilationOptions, JSONSchema, SchemaResolver, SchemaResolverError};
use jsonschema::{
SchemaResolver, SchemaResolverError, ValidationOptions, Validator as JsonschemaValidator,
};
use reqwest::Client;
use serde::Serialize;
use serde_json::{Map, Value};
Expand All @@ -26,9 +28,9 @@ const BUFFER: usize = 10;
/// A cloneable structure for validating STAC.
#[derive(Clone, Debug)]
pub struct Validator {
compilation_options: CompilationOptions,
validation_options: ValidationOptions,
cache: Arc<std::sync::RwLock<HashMap<Url, Arc<Value>>>>,
schemas: Arc<RwLock<HashMap<Url, Arc<JSONSchema>>>>,
schemas: Arc<RwLock<HashMap<Url, Arc<JsonschemaValidator>>>>,
urls: Arc<Mutex<HashSet<Url>>>,
sender: Sender<(Url, OneshotSender<Result<Arc<Value>>>)>,
}
Expand Down Expand Up @@ -57,13 +59,13 @@ impl Validator {
cache: cache.clone(),
urls: urls.clone(),
};
let mut compilation_options = JSONSchema::options();
let _ = compilation_options.with_resolver(resolver);
let mut validation_options = JsonschemaValidator::options();
let _ = validation_options.with_resolver(resolver);
let (sender, receiver) = tokio::sync::mpsc::channel(BUFFER);
let _ = tokio::spawn(async move { get_urls(receiver).await });
let validator = Validator {
schemas: Arc::new(RwLock::new(schemas(&compilation_options))),
compilation_options,
schemas: Arc::new(RwLock::new(schemas(&validation_options))),
validation_options,
cache,
urls,
sender,
Expand Down Expand Up @@ -221,7 +223,7 @@ impl Validator {
}
}

async fn schema(&self, url: Url) -> Result<Arc<JSONSchema>> {
async fn schema(&self, url: Url) -> Result<Arc<JsonschemaValidator>> {
{
let schemas = self.schemas.read().await;
if let Some(schema) = schemas.get(&url) {
Expand All @@ -232,8 +234,8 @@ impl Validator {
self.sender.send((url.clone(), sender)).await?;
let value = receiver.await??;
let schema = self
.compilation_options
.compile(&value)
.validation_options
.build(&value)
.map_err(|err| Error::from_validation_errors([err].into_iter()))?;
let schema = Arc::new(schema);
{
Expand Down Expand Up @@ -287,7 +289,7 @@ fn build_schema_url(r#type: Type, version: &Version) -> Url {
.unwrap()
}

fn schemas(compilation_options: &CompilationOptions) -> HashMap<Url, Arc<JSONSchema>> {
fn schemas(validation_options: &ValidationOptions) -> HashMap<Url, Arc<JsonschemaValidator>> {
use Type::*;
use Version::*;

Expand All @@ -297,7 +299,7 @@ fn schemas(compilation_options: &CompilationOptions) -> HashMap<Url, Arc<JSONSch
($t:expr, $v:expr, $path:expr, $schemas:expr) => {
let url = build_schema_url($t, &$v);
let schema = serde_json::from_str(include_str!($path)).unwrap();
let schema = compilation_options.compile(&schema).unwrap();
let schema = validation_options.build(&schema).unwrap();
let _ = schemas.insert(url, Arc::new(schema));
};
}
Expand Down

0 comments on commit 17006f0

Please sign in to comment.