From 441597e604fd004a96d8503d2970799b0481bba8 Mon Sep 17 00:00:00 2001 From: Alex Groleau Date: Fri, 4 Jul 2025 04:23:06 -0400 Subject: [PATCH] need to allow empty strings when a string property has a format --- src/lib.rs | 13 ++++++++++++- src/tests.rs | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 1eeb1fb..c13a8b6 100644 --- a/src/lib.rs +++ b/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); // Filter out FALSE_SCHEMA errors if there are other validation 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 { } fn handle_format_error(base_path: &str, want: &str, got: &Cow, err: &Box) -> Vec { + // 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 { path: base_path.to_string(), code: "FORMAT_INVALID".to_string(), diff --git a/src/tests.rs b/src/tests.rs index 4231c93..bccdb7f 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -1237,3 +1237,53 @@ fn test_unevaluated_properties_errors() { let valid_result = validate_json_schema(schema_id, jsonb(valid_instance)); 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"); +}