Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions crates/validate/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
use stac::Version;
use thiserror::Error;

#[derive(Error, Debug)]
#[non_exhaustive]
pub enum Error {
/// Cannot validate the given STAC version.
#[error("cannot validate version: {0}")]
CannotValidateVersion(Version),

/// [fluent_uri::ParseError]
#[error(transparent)]
FluentUriParse(#[from] fluent_uri::ParseError),
Expand Down
28 changes: 17 additions & 11 deletions crates/validate/src/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ impl Validator {
.unwrap()
.ok_or(stac::Error::MissingField("stac_version"))?;

let uri = build_uri(r#type, &version);
let uri =
build_uri(r#type, &version).ok_or_else(|| Error::CannotValidateVersion(version))?;
let validator = self.validator(uri).await?;
let value = Value::Object(object);
let errors: Vec<_> = validator.iter_errors(&value).collect();
Expand Down Expand Up @@ -255,15 +256,10 @@ impl AsyncRetrieve for Retriever {
}
}

fn build_uri(r#type: Type, version: &Version) -> Uri<String> {
Uri::parse(format!(
"{}{}",
SCHEMA_BASE,
r#type
.spec_path(version)
.expect("we shouldn't get here with an item collection")
))
.unwrap()
fn build_uri(r#type: Type, version: &Version) -> Option<Uri<String>> {
r#type
.spec_path(version)
.and_then(|path| Uri::parse(format!("{}{}", SCHEMA_BASE, path)).ok())
}

async fn prebuild_validators(
Expand All @@ -277,7 +273,7 @@ async fn prebuild_validators(

macro_rules! schema {
($t:expr_2021, $v:expr_2021, $path:expr_2021, $schemas:expr_2021) => {
let url = build_uri($t, &$v);
let url = build_uri($t, &$v).unwrap();
let value = serde_json::from_str(include_str!($path)).unwrap();
let validator = jsonschema::async_options()
.with_registry(registry)
Expand Down Expand Up @@ -439,4 +435,14 @@ mod tests {
});
collections.validate().await.unwrap();
}

#[tokio::test]
async fn validate_invalid_version() {
// https://github.com/stac-utils/rustac/issues/1168
let mut validator = Validator::new().await.unwrap();
validator
.validate_value(serde_json::json!({"type": "Feature", "stac_version": "not a version"}))
.await
.unwrap_err();
}
}
Loading