Skip to content

Commit c6d3563

Browse files
committed
chore: Improve error message for unrecognized formats
Signed-off-by: Dmitry Dygalo <dmitry@dygalo.dev>
1 parent fd61fa0 commit c6d3563

File tree

4 files changed

+28
-12
lines changed

4 files changed

+28
-12
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## [Unreleased]
44

5+
### Changed
6+
7+
- Improved error message for unknown formats.
8+
59
## [0.26.2] - 2024-12-16
610

711
### Documentation

crates/jsonschema-py/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## [Unreleased]
44

5+
### Changed
6+
7+
- Improved error message for unknown formats.
8+
59
## [0.26.1] - 2024-10-29
610

711
### Fixed

crates/jsonschema-py/README.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,17 @@ validator.is_valid("not a date") # False
144144

145145
# With ignore_unknown_formats=False, using an unknown format will raise an error
146146
invalid_schema = {"type": "string", "format": "unknown"}
147-
jsonschema_rs.Draft202012Validator(
148-
invalid_schema, validate_formats=True, ignore_unknown_formats=False
149-
) # Raises an error
147+
try:
148+
jsonschema_rs.Draft202012Validator(
149+
invalid_schema, validate_formats=True, ignore_unknown_formats=False
150+
)
151+
except jsonschema_rs.ValidationError as exc:
152+
assert str(exc) == '''Unknown format: 'unknown'. Adjust configuration to ignore unrecognized formats
153+
154+
Failed validating "format" in schema
155+
156+
On instance:
157+
"unknown"'''
150158
```
151159

152160
## Performance

crates/jsonschema/src/keywords/format.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -793,15 +793,15 @@ pub(crate) fn compile<'a>(
793793
"uri-reference" if draft >= Draft::Draft6 => Some(UriReferenceValidator::compile(ctx)),
794794
"uri-template" if draft >= Draft::Draft6 => Some(UriTemplateValidator::compile(ctx)),
795795
"uuid" if draft >= Draft::Draft201909 => Some(UuidValidator::compile(ctx)),
796-
_ => {
796+
name => {
797797
if ctx.are_unknown_formats_ignored() {
798798
None
799799
} else {
800-
Some(Err(ValidationError::format(
801-
Location::new(),
800+
Some(Err(ValidationError::custom(
801+
Location::new().join("format"),
802802
ctx.location().clone(),
803803
schema,
804-
"unknown format",
804+
format!("Unknown format: '{name}'. Adjust configuration to ignore unrecognized formats"),
805805
)))
806806
}
807807
}
@@ -822,7 +822,7 @@ mod tests {
822822
use serde_json::json;
823823
use test_case::test_case;
824824

825-
use crate::{error::ValidationErrorKind, tests_util};
825+
use crate::tests_util;
826826

827827
use super::*;
828828

@@ -906,16 +906,16 @@ mod tests {
906906
#[test]
907907
fn unknown_formats_should_not_be_ignored() {
908908
let schema = json!({ "format": "custom", "type": "string"});
909-
let validation_error = crate::options()
909+
let error = crate::options()
910910
.should_validate_formats(true)
911911
.should_ignore_unknown_formats(false)
912912
.build(&schema)
913913
.expect_err("the validation error should be returned");
914914

915-
assert!(
916-
matches!(validation_error.kind, ValidationErrorKind::Format { format } if format == "unknown format")
915+
assert_eq!(
916+
error.to_string(),
917+
"Unknown format: 'custom'. Adjust configuration to ignore unrecognized formats"
917918
);
918-
assert_eq!("\"custom\"", validation_error.instance.to_string())
919919
}
920920

921921
#[test_case("2023-01-01", true; "valid regular date")]

0 commit comments

Comments
 (0)