Skip to content

Commit

Permalink
feat: Add ValidationError::to_owned
Browse files Browse the repository at this point in the history
Signed-off-by: Dmitry Dygalo <[email protected]>
  • Loading branch information
Stranger6667 committed Dec 29, 2024
1 parent 059fc22 commit 40e41f3
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

- Implement `IntoIterator` for `Location` to iterate over `LocationSegment`.
- Implement `FromIter` for `Location` to build a `Location` from an iterator of `LocationSegment`.
- `ValidationError::to_owned` method for converting errors into owned versions.

## [0.27.1] - 2024-12-24

Expand Down
6 changes: 3 additions & 3 deletions crates/jsonschema/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,12 +327,12 @@ pub(crate) fn build_validator(
.expect("Existing draft")
.validate(schema)
{
return Err(error.into_owned());
return Err(error.to_owned());
}
}

// Finally, compile the validator
let root = compile(&ctx, resource_ref).map_err(|err| err.into_owned())?;
let root = compile(&ctx, resource_ref).map_err(|err| err.to_owned())?;
Ok(Validator { root, config })
}

Expand Down Expand Up @@ -406,7 +406,7 @@ pub(crate) fn compile_with<'a>(
} else if let Some((keyword, validator)) = keywords::get_for_draft(ctx, keyword)
.and_then(|(keyword, f)| f(ctx, schema, value).map(|v| (keyword, v)))
{
validators.push((keyword, validator.map_err(|err| err.into_owned())?));
validators.push((keyword, validator.map_err(|err| err.to_owned())?));
} else if !ctx.is_known_keyword(keyword) {
// Treat all non-validation keywords as annotations
annotations.insert(keyword.to_string(), value.clone());
Expand Down
5 changes: 3 additions & 2 deletions crates/jsonschema/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,8 @@ impl<'a> ValidationError<'a> {
placeholder: placeholder.into(),
}
}
pub(crate) fn into_owned(self) -> ValidationError<'static> {
/// Converts the `ValidationError` into an owned version with `'static` lifetime.
pub fn to_owned(self) -> ValidationError<'static> {
ValidationError {
instance_path: self.instance_path.clone(),
instance: Cow::Owned(self.instance.into_owned()),
Expand Down Expand Up @@ -636,7 +637,7 @@ impl<'a> ValidationError<'a> {
instance_path,
instance: Cow::Borrowed(instance),
kind: ValidationErrorKind::PropertyNames {
error: Box::new(error.into_owned()),
error: Box::new(error.to_owned()),
},
schema_path: location,
}
Expand Down
4 changes: 2 additions & 2 deletions crates/jsonschema/src/keywords/property_names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl Validate for PropertyNamesObjectValidator {
error.schema_path.clone(),
location.into(),
instance,
error.into_owned(),
error.to_owned(),
)
})
.collect();
Expand Down Expand Up @@ -77,7 +77,7 @@ impl Validate for PropertyNamesObjectValidator {
error.schema_path.clone(),
location.into(),
instance,
error.into_owned(),
error.to_owned(),
))
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/jsonschema/src/keywords/ref_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl RefValidator {
location,
);
let inner = match compiler::compile_with(&ctx, resource_ref)
.map_err(|err| err.into_owned())
.map_err(|err| err.to_owned())
{
Ok(inner) => inner,
Err(error) => return Some(Err(error)),
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 @@ -1244,7 +1244,7 @@ pub(crate) mod tests_util {
let err = validator
.validate(instance)
.expect_err("Should be an error")
.into_owned();
.to_owned();
err
}

Expand Down

0 comments on commit 40e41f3

Please sign in to comment.