need to allow empty strings when a string property has a format
This commit is contained in:
13
src/lib.rs
13
src/lib.rs
@ -157,7 +157,11 @@ fn validate_json_schema(schema_id: &str, instance: JsonB) -> JsonB {
|
|||||||
let errors = format_errors(error_list, &instance_value, schema_id);
|
let errors = format_errors(error_list, &instance_value, schema_id);
|
||||||
// Filter out FALSE_SCHEMA errors if there are other validation errors
|
// Filter out FALSE_SCHEMA errors if there are other validation errors
|
||||||
let filtered_errors = filter_false_schema_errors(errors);
|
let filtered_errors = filter_false_schema_errors(errors);
|
||||||
JsonB(json!({ "errors": filtered_errors }))
|
if filtered_errors.is_empty() {
|
||||||
|
JsonB(json!({ "response": "success" }))
|
||||||
|
} else {
|
||||||
|
JsonB(json!({ "errors": filtered_errors }))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -486,6 +490,13 @@ fn handle_additional_items_error(base_path: &str, got: usize) -> Vec<Error> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn handle_format_error(base_path: &str, want: &str, got: &Cow<Value>, err: &Box<dyn std::error::Error>) -> Vec<Error> {
|
fn handle_format_error(base_path: &str, want: &str, got: &Cow<Value>, err: &Box<dyn std::error::Error>) -> Vec<Error> {
|
||||||
|
// If the value is an empty string, skip format validation.
|
||||||
|
if let Value::String(s) = got.as_ref() {
|
||||||
|
if s.is_empty() {
|
||||||
|
return vec![];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
vec![Error {
|
vec![Error {
|
||||||
path: base_path.to_string(),
|
path: base_path.to_string(),
|
||||||
code: "FORMAT_INVALID".to_string(),
|
code: "FORMAT_INVALID".to_string(),
|
||||||
|
|||||||
50
src/tests.rs
50
src/tests.rs
@ -1237,3 +1237,53 @@ fn test_unevaluated_properties_errors() {
|
|||||||
let valid_result = validate_json_schema(schema_id, jsonb(valid_instance));
|
let valid_result = validate_json_schema(schema_id, jsonb(valid_instance));
|
||||||
assert_success_with_json!(valid_result, "All properties are evaluated, should pass");
|
assert_success_with_json!(valid_result, "All properties are evaluated, should pass");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[pg_test]
|
||||||
|
fn test_format_validation_allows_empty_string() {
|
||||||
|
clear_json_schemas();
|
||||||
|
let schema_id = "format_schema_empty";
|
||||||
|
let schema = json!({
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"uuid": { "type": "string", "format": "uuid" },
|
||||||
|
"date_time": { "type": "string", "format": "date-time" },
|
||||||
|
"email": { "type": "string", "format": "email" }
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
let _ = cache_json_schema(schema_id, jsonb(schema), false);
|
||||||
|
|
||||||
|
// Test with empty strings for all formatted fields
|
||||||
|
let instance = json!({
|
||||||
|
"uuid": "",
|
||||||
|
"date_time": "",
|
||||||
|
"email": ""
|
||||||
|
});
|
||||||
|
|
||||||
|
let result = validate_json_schema(schema_id, jsonb(instance));
|
||||||
|
|
||||||
|
// This is the test that should fail before the change and pass after
|
||||||
|
assert_success_with_json!(result, "Empty strings should be allowed for format validation");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[pg_test]
|
||||||
|
fn test_non_empty_string_format_validation_still_fails() {
|
||||||
|
clear_json_schemas();
|
||||||
|
let schema_id = "non_empty_fail_schema";
|
||||||
|
let schema = json!({
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"date_time": { "type": "string", "format": "date-time" }
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
let _ = cache_json_schema(schema_id, jsonb(schema), false);
|
||||||
|
|
||||||
|
// A non-empty but invalid string should still fail
|
||||||
|
let instance = json!({
|
||||||
|
"date_time": "not-a-date"
|
||||||
|
});
|
||||||
|
|
||||||
|
let result = validate_json_schema(schema_id, jsonb(instance));
|
||||||
|
assert_failure_with_json!(result, 1, "Value \"not-a-date\" is not a valid date-time format");
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user