Skip to content

Commit 624a764

Browse files
authored
Merge pull request #66 from Stranger6667/dd/update-jsonschema
chore: update jsonschema to 0.28.0
2 parents dcbe58e + c91a53e commit 624a764

File tree

2 files changed

+23
-19
lines changed

2 files changed

+23
-19
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pg_test = []
2424
pgrx = "0.12.6"
2525
serde = "1.0"
2626
serde_json = "1.0"
27-
jsonschema = {version = "0.17.1", default-features = false, features = []}
27+
jsonschema = {version = "0.28.0", default-features = false}
2828

2929
[dev-dependencies]
3030
pgrx-tests = "0.12.6"

src/lib.rs

+22-18
Original file line numberDiff line numberDiff line change
@@ -14,32 +14,29 @@ fn jsonb_matches_schema(schema: Json, instance: JsonB) -> bool {
1414

1515
#[pg_extern(immutable, strict, parallel_safe)]
1616
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}");
2725
false
2826
}
2927
}
3028
}
3129

3230
#[pg_extern(immutable, strict, parallel_safe)]
3331
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()],
3735
};
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()
4340
}
4441

4542
#[pg_schema]
@@ -146,6 +143,13 @@ mod tests {
146143
}))));
147144
}
148145

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+
149153
#[pg_test]
150154
fn test_jsonschema_validation_errors_none() {
151155
let errors = crate::jsonschema_validation_errors(

0 commit comments

Comments
 (0)