Skip to content

Commit

Permalink
Read validation JSON with DateParseHandling.None, closes #906
Browse files Browse the repository at this point in the history
  • Loading branch information
RicoSuter committed Feb 22, 2019
1 parent f7224a3 commit e11d404
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
32 changes: 32 additions & 0 deletions src/NJsonSchema.Tests/Validation/SchemaTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -379,5 +379,37 @@ public async Task When_multiple_types_fail_with_errors_take_the_best_group()
Assert.Equal(1, errors.Count);
Assert.Contains(errors, e => e.Kind == ValidationErrorKind.NoTypeValidates);
}

[Fact]
public async Task When_datetime_with_regex_validation_then_datetime_is_not_altered()
{
//// Arrange
var schemaJson = @"
{
""$schema"": ""http://json-schema.org/draft-07/schema#"",
""type"": ""object"",
""required"": [
""my_datetime""
],
""properties"": {
""my_datetime"": {
""type"": ""string"",
""pattern"": ""^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}\\.\\d{3}Z$""
}
}
}";

var json = @"
{
""my_datetime"": ""2018-12-19T16:58:07.270Z""
}";

//// Act
var schema = await JsonSchema4.FromJsonAsync(schemaJson);
var errors = schema.Validate(json);

//// Assert
Assert.Equal(0, errors.Count);
}
}
}
12 changes: 10 additions & 2 deletions src/NJsonSchema/Validation/JsonSchemaValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using Newtonsoft.Json;
Expand Down Expand Up @@ -52,8 +53,15 @@ public JsonSchemaValidator()
/// <returns>The list of validation errors.</returns>
public ICollection<ValidationError> Validate(string jsonData, JsonSchema4 schema)
{
var jsonObject = JToken.Parse(jsonData);
return Validate(jsonObject, schema);
using (var reader = new StringReader(jsonData))
using (var jsonReader = new JsonTextReader(reader)
{
DateParseHandling = DateParseHandling.None
})
{
var jsonObject = JToken.ReadFrom(jsonReader);
return Validate(jsonObject, schema);
}
}

/// <summary>Validates the given JSON token.</summary>
Expand Down

0 comments on commit e11d404

Please sign in to comment.