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

Fixing issue 105 date-time format validation problem #106

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions Src/Newtonsoft.Json.Schema.Tests/Issues/Issue0105Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#region License
// Copyright (c) Newtonsoft. All Rights Reserved.
// License: https://raw.github.com/JamesNK/Newtonsoft.Json.Schema/master/LICENSE.md
#endregion

using Newtonsoft.Json.Linq;
#if DNXCORE50
using Xunit;
using Test = Xunit.FactAttribute;
using Assert = Newtonsoft.Json.Schema.Tests.XUnitAssert;
#else
using NUnit.Framework;
#endif

namespace Newtonsoft.Json.Schema.Tests.Issues
{
[TestFixture]
public class Issue0105Test : TestFixtureBase
{
[TestCase("1977-02-01T00:00:00Z")]
[TestCase("1977-02-01T00:00:00.0Z")]
[TestCase("1977-02-01T00:00:00.00Z")]
[TestCase("1977-02-01T00:00:00.000Z")]
[TestCase("1977-02-01T00:00:00.0000Z")]
[TestCase("1977-02-01T00:00:00.00000Z")]
[TestCase("1977-02-01T00:00:00+01:00")]
[TestCase("1977-02-01T00:00:00.0+01:00")]
[TestCase("1977-02-01T00:00:00.00+01:00")]
[TestCase("1977-02-01T00:00:00.000+01:00")]
[TestCase("1977-02-01T00:00:00.0000+01:00")]
[TestCase("1977-02-01T00:00:00.00000+01:00")]
public void DateTimeValidPatterns(string validDateTimePattern)
{
JSchema schema = JSchema.Parse(SchemaJson);

var o = new JObject
{
["someDate"] = validDateTimePattern
};

Assert.That(o.IsValid(schema), Is.True, "Should allow valid date-time pattern");
}

[TestCase("1977-02-01T00:00:00")]
[TestCase("1977-02-01T00:00:00.000")]
[TestCase("1977-02-01T00:00:00+01")]
[TestCase("1977-02-01T00:00:00.000+01")]
[TestCase("1977-02-01T00:00:00A")]
public void DateTimeInvalidPatterns(string invalidDateTimePattern)
{
JSchema schema = JSchema.Parse(SchemaJson);

var o = new JObject
{
["someDate"] = invalidDateTimePattern
};

Assert.That(o.IsValid(schema), Is.False, "Should not allow invalid date-time patterns");
}

private const string SchemaJson = @"{
""$schema"": ""http://json-schema.org/draft-04/schema#"",
""type"": ""object"",
""additionalProperties"": false,
""properties"": {
""someDate"": {
""type"": ""string"",
""description"": ""Some date"",
""format"": ""date-time""
}
}
}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,8 @@ private static bool ValidateFormat(string format, string value)
}
case Constants.Formats.DateTime:
{
return DateTime.TryParseExact(value, @"yyyy-MM-dd\THH:mm:ss.FFFFFFFK", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime temp);
return DateTime.TryParseExact(value, @"yyyy-MM-dd\THH:mm:ss.FFFFFFFZ", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime temp1) ||
DateTime.TryParseExact(value, @"yyyy-MM-dd\THH:mm:ss.FFFFFFFzzz", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime temp2);
}
case Constants.Formats.UtcMilliseconds:
{
Expand Down