feat: improved error messages clarity (#430)

* feat: improved error messages clarity

* fix: edited test cases to match the new wording

* squash! feat: improved error messages clarity
This commit is contained in:
Iliia Maleki 2023-06-09 09:35:52 +02:00 committed by GitHub
parent 67e0e4e871
commit 066a0da459
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 9 deletions

View File

@ -68,7 +68,7 @@ pub enum ValidationErrorKind {
AdditionalItems { limit: usize },
/// Unexpected properties.
AdditionalProperties { unexpected: Vec<String> },
/// The input value is not valid under any of the given schemas.
/// The input value is not valid under any of the schemas listed in the 'anyOf' keyword.
AnyOf,
/// Results from a [`fancy_regex::Error::BacktrackLimitExceeded`] variant when matching
BacktrackLimitExceeded { error: fancy_regex::Error },
@ -122,9 +122,9 @@ pub enum ValidationErrorKind {
MultipleOf { multiple_of: f64 },
/// Negated schema failed validation.
Not { schema: Value },
/// The given schema is valid under more than one of the given schemas.
/// The given schema is valid under more than one of the schemas listed in the 'oneOf' keyword.
OneOfMultipleValid,
/// The given schema is not valid under any on the given schemas.
/// The given schema is not valid under any of the schemas listed in the 'oneOf' keyword.
OneOfNotValid,
/// When the input doesn't match to a pattern.
Pattern { pattern: String },
@ -836,9 +836,14 @@ impl fmt::Display for ValidationError<'_> {
verb
)
}
ValidationErrorKind::AnyOf | ValidationErrorKind::OneOfNotValid => write!(
ValidationErrorKind::AnyOf => write!(
f,
"{} is not valid under any of the given schemas",
"{} is not valid under any of the schemas listed in the 'anyOf' keyword",
self.instance
),
ValidationErrorKind::OneOfNotValid => write!(
f,
"{} is not valid under any of the schemas listed in the 'oneOf' keyword",
self.instance
),
ValidationErrorKind::Contains => write!(
@ -939,7 +944,7 @@ impl fmt::Display for ValidationError<'_> {
}
ValidationErrorKind::OneOfMultipleValid => write!(
f,
"{} is valid under more than one of the given schemas",
"{} is valid under more than one of the schemas listed in the 'oneOf' keyword",
self.instance
),
ValidationErrorKind::Pattern { pattern } => {

View File

@ -135,7 +135,7 @@ mod tests {
#[test_case(&json!({"items": [{}, {}, {}], "additionalItems": false}), &json!([ 1, 2, 3, 4 ]), r#"Additional items are not allowed (4 was unexpected)"#)]
#[test_case(&json!({"items": [{}, {}, {}], "additionalItems": false}), &json!([ 1, 2, 3, 4, 5 ]), r#"Additional items are not allowed (4, 5 were unexpected)"#)]
#[test_case(&json!({"properties": {"foo": {}, "bar": {}}, "patternProperties": { "^v": {} }, "additionalProperties": false}), &json!({"foo" : 1, "bar" : 2, "quux" : "boom"}), r#"Additional properties are not allowed ('quux' was unexpected)"#)]
#[test_case(&json!({"anyOf": [{"type": "integer"}, {"minimum": 2}]}), &json!(1.5), r#"1.5 is not valid under any of the given schemas"#)]
#[test_case(&json!({"anyOf": [{"type": "integer"}, {"minimum": 2}]}), &json!(1.5), r#"1.5 is not valid under any of the schemas listed in the 'anyOf' keyword"#)]
#[test_case(&json!({"const": 2}), &json!(5), r#"2 was expected"#)]
#[test_case(&json!({"contains": {"minimum": 5}}), &json!([2, 3, 4]), r#"None of [2,3,4] are valid under the given schema"#)]
#[test_case(&json!({"enum": [1, 2, 3]}), &json!(4), r#"4 is not one of [1,2,3]"#)]
@ -157,8 +157,8 @@ mod tests {
#[test_case(&json!({"minProperties": 1}), &json!({}), r#"{} has less than 1 property"#)]
#[test_case(&json!({"multipleOf": 2}), &json!(7), r#"7 is not a multiple of 2"#)]
#[test_case(&json!({"not": {"type": "integer"}}), &json!(1), r#"{"type":"integer"} is not allowed for 1"#)]
#[test_case(&json!({"oneOf": [{"type": "integer"}, {"minimum": 2}]}), &json!(1.1), r#"1.1 is not valid under any of the given schemas"#)]
#[test_case(&json!({"oneOf": [{"type": "integer"}, {"minimum": 2}]}), &json!(3), r#"3 is valid under more than one of the given schemas"#)]
#[test_case(&json!({"oneOf": [{"type": "integer"}, {"minimum": 2}]}), &json!(1.1), r#"1.1 is not valid under any of the schemas listed in the 'oneOf' keyword"#)]
#[test_case(&json!({"oneOf": [{"type": "integer"}, {"minimum": 2}]}), &json!(3), r#"3 is valid under more than one of the schemas listed in the 'oneOf' keyword"#)]
#[test_case(&json!({"pattern": "^a*$"}), &json!("abc"), r#""abc" does not match "^a*$""#)]
#[test_case(&json!({"properties": {"foo": {}, "bar": {}}, "required": ["foo"]}), &json!({"bar": 1}), r#""foo" is a required property"#)]
#[test_case(&json!({"type": "integer"}), &json!(1.1), r#"1.1 is not of type "integer""#)]