Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose failing JToken in ValidationError #1692

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/NJsonSchema.Tests/Validation/ObjectValidationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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]
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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]
Expand Down
4 changes: 4 additions & 0 deletions src/NJsonSchema/Validation/ValidationError.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -63,6 +64,9 @@ public ValidationError(ValidationErrorKind errorKind, string? propertyName, stri
/// <summary>Gets the schema element that contains the validation rule. </summary>
public JsonSchema Schema { get; private set; }

/// <summary>Gets the JToken of the element failed the validation. </summary>
public JToken? Token { get; private set; }

/// <summary>Returns a string that represents the current object.</summary>
/// <returns>A string that represents the current object.</returns>
/// <filterpriority>2</filterpriority>
Expand Down