Skip to content

Commit

Permalink
feat: add the validate utils (#32)
Browse files Browse the repository at this point in the history
add jsonschema_validation_errors function for displaying validation errors
  • Loading branch information
duguorong009 committed Jul 20, 2023
1 parent 47dc3e9 commit 0d286ed
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,19 @@ fn jsonschema_is_valid(schema: Json) -> bool {
}
}

#[pg_extern(immutable, strict)]
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 errors = match schema.validate(&instance.0) {
Ok(_) => vec![],
Err(e) => e.into_iter().map(|e| e.to_string()).collect(),
};
errors
}

#[pg_schema]
#[cfg(any(test, feature = "pg_test"))]
mod tests {
Expand Down Expand Up @@ -132,6 +145,53 @@ mod tests {
"type": "obj"
}))));
}

#[pg_test]
fn test_jsonschema_validation_errors_none() {
let errors = crate::jsonschema_validation_errors(
Json(json!({ "maxLength": 4 })),
Json(json!("foo")),
);
assert!(errors.len() == 0);
}

#[pg_test]
fn test_jsonschema_validation_erros_one() {
let errors = crate::jsonschema_validation_errors(
Json(json!({ "maxLength": 4 })),
Json(json!("123456789")),
);
assert!(errors.len() == 1);
assert!(errors[0] == "\"123456789\" is longer than 4 characters".to_string());
}

#[pg_test]
fn test_jsonschema_validation_errors_multiple() {
let errors = crate::jsonschema_validation_errors(
Json(json!(
{
"type": "object",
"properties": {
"foo": {
"type": "string"
},
"bar": {
"type": "number"
},
"baz": {
"type": "boolean"
},
"additionalProperties": false,
}
})),
Json(json!({"foo": 1, "bar": [], "baz": "1"})),
);

assert!(errors.len() == 3);
assert!(errors[0] == "[] is not of type \"number\"".to_string());
assert!(errors[1] == "\"1\" is not of type \"boolean\"".to_string());
assert!(errors[2] == "1 is not of type \"string\"".to_string());
}
}

#[cfg(test)]
Expand Down

0 comments on commit 0d286ed

Please sign in to comment.