@@ -14,32 +14,29 @@ fn jsonb_matches_schema(schema: Json, instance: JsonB) -> bool {
14
14
15
15
#[ pg_extern( immutable, strict, parallel_safe) ]
16
16
fn jsonschema_is_valid ( schema : Json ) -> bool {
17
- match jsonschema:: JSONSchema :: compile ( & schema. 0 ) {
18
- Ok ( _) => true ,
19
- Err ( e) => {
20
- // Only call notice! for a non empty instance_path
21
- if e. instance_path . last ( ) . is_some ( ) {
22
- notice ! (
23
- "Invalid JSON schema at path: {}" ,
24
- e. instance_path. to_string( )
25
- ) ;
26
- }
17
+ match jsonschema:: meta:: try_validate ( & schema. 0 ) {
18
+ Ok ( Ok ( _) ) => true ,
19
+ Ok ( Err ( err) ) => {
20
+ notice ! ( "Invalid JSON schema at path: {}" , err. instance_path) ;
21
+ false
22
+ }
23
+ Err ( err) => {
24
+ notice ! ( "{err}" ) ;
27
25
false
28
26
}
29
27
}
30
28
}
31
29
32
30
#[ pg_extern( immutable, strict, parallel_safe) ]
33
31
fn jsonschema_validation_errors ( schema : Json , instance : Json ) -> Vec < String > {
34
- let schema = match jsonschema:: JSONSchema :: compile ( & schema. 0 ) {
35
- Ok ( s ) => s ,
36
- Err ( e ) => return vec ! [ e . to_string( ) ] ,
32
+ let validator = match jsonschema:: validator_for ( & schema. 0 ) {
33
+ Ok ( v ) => v ,
34
+ Err ( err ) => return vec ! [ err . to_string( ) ] ,
37
35
} ;
38
- let errors = match schema. validate ( & instance. 0 ) {
39
- Ok ( _) => vec ! [ ] ,
40
- Err ( e) => e. into_iter ( ) . map ( |e| e. to_string ( ) ) . collect ( ) ,
41
- } ;
42
- errors
36
+ validator
37
+ . iter_errors ( & instance. 0 )
38
+ . map ( |err| err. to_string ( ) )
39
+ . collect ( )
43
40
}
44
41
45
42
#[ pg_schema]
@@ -146,6 +143,13 @@ mod tests {
146
143
} ) ) ) ) ;
147
144
}
148
145
146
+ #[ pg_test]
147
+ fn test_jsonschema_unknown_specification ( ) {
148
+ assert ! ( !crate :: jsonschema_is_valid( Json ( json!( {
149
+ "$schema" : "invalid-uri" , "type" : "string"
150
+ } ) ) ) ) ;
151
+ }
152
+
149
153
#[ pg_test]
150
154
fn test_jsonschema_validation_errors_none ( ) {
151
155
let errors = crate :: jsonschema_validation_errors (
0 commit comments