Skip to content

Commit

Permalink
Merge pull request #66 from Stranger6667/dd/update-jsonschema
Browse files Browse the repository at this point in the history
chore: update jsonschema to 0.28.0
  • Loading branch information
imor authored Dec 30, 2024
2 parents dcbe58e + c91a53e commit 624a764
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pg_test = []
pgrx = "0.12.6"
serde = "1.0"
serde_json = "1.0"
jsonschema = {version = "0.17.1", default-features = false, features = []}
jsonschema = {version = "0.28.0", default-features = false}

[dev-dependencies]
pgrx-tests = "0.12.6"
Expand Down
40 changes: 22 additions & 18 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,29 @@ fn jsonb_matches_schema(schema: Json, instance: JsonB) -> bool {

#[pg_extern(immutable, strict, parallel_safe)]
fn jsonschema_is_valid(schema: Json) -> bool {
match jsonschema::JSONSchema::compile(&schema.0) {
Ok(_) => true,
Err(e) => {
// Only call notice! for a non empty instance_path
if e.instance_path.last().is_some() {
notice!(
"Invalid JSON schema at path: {}",
e.instance_path.to_string()
);
}
match jsonschema::meta::try_validate(&schema.0) {
Ok(Ok(_)) => true,
Ok(Err(err)) => {
notice!("Invalid JSON schema at path: {}", err.instance_path);
false
}
Err(err) => {
notice!("{err}");
false
}
}
}

#[pg_extern(immutable, strict, parallel_safe)]
fn jsonschema_validation_errors(schema: Json, instance: Json) -> Vec<String> {
let schema = match jsonschema::JSONSchema::compile(&schema.0) {
Ok(s) => s,
Err(e) => return vec![e.to_string()],
let validator = match jsonschema::validator_for(&schema.0) {
Ok(v) => v,
Err(err) => return vec![err.to_string()],
};
let errors = match schema.validate(&instance.0) {
Ok(_) => vec![],
Err(e) => e.into_iter().map(|e| e.to_string()).collect(),
};
errors
validator
.iter_errors(&instance.0)
.map(|err| err.to_string())
.collect()
}

#[pg_schema]
Expand Down Expand Up @@ -146,6 +143,13 @@ mod tests {
}))));
}

#[pg_test]
fn test_jsonschema_unknown_specification() {
assert!(!crate::jsonschema_is_valid(Json(json!({
"$schema": "invalid-uri", "type": "string"
}))));
}

#[pg_test]
fn test_jsonschema_validation_errors_none() {
let errors = crate::jsonschema_validation_errors(
Expand Down

0 comments on commit 624a764

Please sign in to comment.