diff --git a/src/NJsonSchema.Tests/Validation/ObjectValidationTests.cs b/src/NJsonSchema.Tests/Validation/ObjectValidationTests.cs index b74268380..1cae72fc5 100644 --- a/src/NJsonSchema.Tests/Validation/ObjectValidationTests.cs +++ b/src/NJsonSchema.Tests/Validation/ObjectValidationTests.cs @@ -24,6 +24,7 @@ public void When_token_is_not_object_then_validation_should_fail() //// Assert Assert.Equal(ValidationErrorKind.ObjectExpected, errors.First().Kind); + Assert.Equal("10", errors.First().Token?.ToString()); } [Fact] @@ -47,6 +48,7 @@ public void When_required_property_is_missing_then_it_should_be_in_error_list() Assert.Equal("Foo", errors.First().Property); Assert.Equal("#/Foo", errors.First().Path); Assert.Equal(ValidationErrorKind.PropertyRequired, errors.First().Kind); + Assert.Equal("{}", errors.First().Token?.ToString()); } [Fact] @@ -134,6 +136,7 @@ public void When_string_property_required_but_integer_provided_then_it_should_fa Assert.Equal(ValidationErrorKind.StringExpected, errors.First().Kind); Assert.Equal("Foo", errors.First().Property); Assert.Equal("#/Foo", errors.First().Path); + Assert.Equal("10", errors.First().Token?.ToString()); } [Fact] @@ -180,6 +183,7 @@ public void When_case_sensitive_and_property_has_different_casing_then_it_should //// Assert Assert.Equal(ValidationErrorKind.NoAdditionalPropertiesAllowed, errors.First().Kind); + Assert.Equal("\"foo\": 5", errors.First().Token?.ToString()); } [Fact] diff --git a/src/NJsonSchema/Validation/ValidationError.cs b/src/NJsonSchema/Validation/ValidationError.cs index 1ba7886d5..a8561ff5a 100644 --- a/src/NJsonSchema/Validation/ValidationError.cs +++ b/src/NJsonSchema/Validation/ValidationError.cs @@ -25,6 +25,7 @@ public ValidationError(ValidationErrorKind errorKind, string? propertyName, stri Kind = errorKind; Property = propertyName; Path = propertyPath != null ? "#/" + propertyPath : "#"; + Token = token; var lineInfo = token as IJsonLineInfo; HasLineInfo = lineInfo != null && lineInfo.HasLineInfo(); @@ -63,6 +64,9 @@ public ValidationError(ValidationErrorKind errorKind, string? propertyName, stri /// Gets the schema element that contains the validation rule. public JsonSchema Schema { get; private set; } + /// Gets the JToken of the element failed the validation. + public JToken? Token { get; private set; } + /// Returns a string that represents the current object. /// A string that represents the current object. /// 2