Skip to content

Commit

Permalink
chore: Make return types more flexible
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 18, 2024
1 parent 1b6e25a commit 6c26605
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 57 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
### Changed

- Make `Debug` implementation for `SchemaNode` opaque.
- Make `jsonschema::validator_for` and related functions return `ValidationError<'static>` in their `Err` variant.
This change makes possible to use the `?` operator to return errors from functions where the input schema is defined.

### Deprecated

Expand Down
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
assert!(jsonschema::is_valid(&schema, &instance));

// Build & reuse (faster)
let validator = jsonschema::validator_for(&schema)
.expect("Invalid schema");
let validator = jsonschema::validator_for(&schema)?;

// Iterate over errors
if let Err(errors) = validator.validate(&instance) {
Expand Down
4 changes: 2 additions & 2 deletions crates/jsonschema/src/compilation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ impl Validator {
ValidationOptions::default()
}
/// Create a validator using the default options.
pub fn new(schema: &Value) -> Result<Validator, ValidationError> {
pub fn new(schema: &Value) -> Result<Validator, ValidationError<'static>> {
Self::options().build(schema)
}
/// Create a validator using the default options.
///
/// **DEPRECATED**: Use [`Validator::new`] instead.
#[deprecated(since = "0.20.0", note = "Use `Validator::new` instead")]
pub fn compile(schema: &Value) -> Result<Validator, ValidationError> {
pub fn compile(schema: &Value) -> Result<Validator, ValidationError<'static>> {
Self::new(schema)
}
/// Run validation against `instance` and return an iterator over [`ValidationError`] in the error case.
Expand Down
12 changes: 6 additions & 6 deletions crates/jsonschema/src/compilation/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,10 +309,7 @@ impl ValidationOptions {
/// assert!(validator.is_valid(&json!("Hello")));
/// assert!(!validator.is_valid(&json!(42)));
/// ```
pub fn build<'a>(
&self,
schema: &'a serde_json::Value,
) -> Result<Validator, ValidationError<'a>> {
pub fn build(&self, schema: &serde_json::Value) -> Result<Validator, ValidationError<'static>> {
// Draft is detected in the following precedence order:
// - Explicitly specified;
// - $schema field in the document;
Expand Down Expand Up @@ -348,11 +345,14 @@ impl ValidationOptions {
.validate(schema)
.err()
{
return Err(errors.next().expect("Should have at least one element"));
return Err(errors
.next()
.expect("Should have at least one element")
.into_owned());
}
}

let node = compile_validators(schema, &context)?;
let node = compile_validators(schema, &context).map_err(|err| err.into_owned())?;

Ok(Validator { node, config })
}
Expand Down
Loading

0 comments on commit 6c26605

Please sign in to comment.