File tree Expand file tree Collapse file tree 5 files changed +114
-57
lines changed Expand file tree Collapse file tree 5 files changed +114
-57
lines changed Original file line number Diff line number Diff line change 1818### Changed
1919
2020- Make ` Debug ` implementation for ` SchemaNode ` opaque.
21+ - Make ` jsonschema::validator_for ` and related functions return ` ValidationError<'static> ` in their ` Err ` variant.
22+ This change makes possible to use the ` ? ` operator to return errors from functions where the input schema is defined.
2123
2224### Deprecated
2325
Original file line number Diff line number Diff line change @@ -19,8 +19,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
1919 assert! (jsonschema :: is_valid (& schema , & instance ));
2020
2121 // Build & reuse (faster)
22- let validator = jsonschema :: validator_for (& schema )
23- . expect (" Invalid schema" );
22+ let validator = jsonschema :: validator_for (& schema )? ;
2423
2524 // Iterate over errors
2625 if let Err (errors ) = validator . validate (& instance ) {
Original file line number Diff line number Diff line change @@ -65,14 +65,14 @@ impl Validator {
6565 ValidationOptions :: default ( )
6666 }
6767 /// Create a validator using the default options.
68- pub fn new ( schema : & Value ) -> Result < Validator , ValidationError > {
68+ pub fn new ( schema : & Value ) -> Result < Validator , ValidationError < ' static > > {
6969 Self :: options ( ) . build ( schema)
7070 }
7171 /// Create a validator using the default options.
7272 ///
7373 /// **DEPRECATED**: Use [`Validator::new`] instead.
7474 #[ deprecated( since = "0.20.0" , note = "Use `Validator::new` instead" ) ]
75- pub fn compile ( schema : & Value ) -> Result < Validator , ValidationError > {
75+ pub fn compile ( schema : & Value ) -> Result < Validator , ValidationError < ' static > > {
7676 Self :: new ( schema)
7777 }
7878 /// Run validation against `instance` and return an iterator over [`ValidationError`] in the error case.
Original file line number Diff line number Diff line change @@ -309,10 +309,7 @@ impl ValidationOptions {
309309 /// assert!(validator.is_valid(&json!("Hello")));
310310 /// assert!(!validator.is_valid(&json!(42)));
311311 /// ```
312- pub fn build < ' a > (
313- & self ,
314- schema : & ' a serde_json:: Value ,
315- ) -> Result < Validator , ValidationError < ' a > > {
312+ pub fn build ( & self , schema : & serde_json:: Value ) -> Result < Validator , ValidationError < ' static > > {
316313 // Draft is detected in the following precedence order:
317314 // - Explicitly specified;
318315 // - $schema field in the document;
@@ -348,11 +345,14 @@ impl ValidationOptions {
348345 . validate ( schema)
349346 . err ( )
350347 {
351- return Err ( errors. next ( ) . expect ( "Should have at least one element" ) ) ;
348+ return Err ( errors
349+ . next ( )
350+ . expect ( "Should have at least one element" )
351+ . into_owned ( ) ) ;
352352 }
353353 }
354354
355- let node = compile_validators ( schema, & context) ?;
355+ let node = compile_validators ( schema, & context) . map_err ( |err| err . into_owned ( ) ) ?;
356356
357357 Ok ( Validator { node, config } )
358358 }
You can’t perform that action at this time.
0 commit comments