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

Fixed validation of JTokens with type Uri #1403

Open
wants to merge 2 commits 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
18 changes: 18 additions & 0 deletions src/NJsonSchema.Tests/Validation/FormatUriTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Linq;
using Newtonsoft.Json.Linq;
using NJsonSchema.Validation;
Expand Down Expand Up @@ -40,5 +41,22 @@ public void When_format_uri_correct_then_validation_succeeds()
//// Assert
Assert.Empty(errors);
}

[Fact]
public void When_uri_token_is_of_type_uri_then_validation_succeeds()
{
//// Arrange
var schema = new JsonSchema();
schema.Type = JsonObjectType.String;
schema.Format = JsonFormatStrings.Uri;

var token = new JValue(new Uri("http://rsuter.com"));

//// Act
var errors = schema.Validate(token);

//// Assert
Assert.Empty(errors);
}
}
}
18 changes: 17 additions & 1 deletion src/NJsonSchema/Validation/JsonSchemaValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,23 @@ private void ValidateString(JToken token, JsonSchema schema, JsonObjectType type

if (isString)
{
var value = token.Type == JTokenType.Date ? (token as JValue).ToString("yyyy-MM-ddTHH:mm:ssK") : token.Value<string>();
string value;

switch (token.Type)
{
case JTokenType.Date:
value = (token as JValue)?.ToString("yyyy-MM-ddTHH:mm:ssK");
break;

case JTokenType.Uri:
value = (token as JValue)?.ToString();
break;

default:
value = token.Value<string>();
break;
}

if (value != null)
{
if (!string.IsNullOrEmpty(schema.Pattern))
Expand Down