Skip to content

Commit

Permalink
Merge pull request #443 from RSuter/master
Browse files Browse the repository at this point in the history
Release v9.4.4
  • Loading branch information
RicoSuter authored Aug 4, 2017
2 parents b9e6151 + 6953ef9 commit 3d3d55b
Show file tree
Hide file tree
Showing 11 changed files with 119 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFrameworks>netstandard1.3;net451</TargetFrameworks>
<Description>JSON Schema draft v4 reader, generator and validator for .NET</Description>
<Version>9.4.3</Version>
<Version>9.4.4</Version>
<PackageTags>json schema validation generator .net</PackageTags>
<Copyright>Copyright © Rico Suter, 2017</Copyright>
<PackageLicenseUrl>https://github.com/rsuter/NJsonSchema/blob/master/LICENSE.md</PackageLicenseUrl>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFrameworks>netstandard1.3;net451</TargetFrameworks>
<Description>JSON Schema draft v4 reader, generator and validator for .NET</Description>
<Version>9.4.3</Version>
<Version>9.4.4</Version>
<PackageTags>json schema validation generator .net</PackageTags>
<Copyright>Copyright © Rico Suter, 2017</Copyright>
<PackageLicenseUrl>https://github.com/rsuter/NJsonSchema/blob/master/LICENSE.md</PackageLicenseUrl>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFrameworks>netstandard1.3;net451</TargetFrameworks>
<Description>JSON Schema draft v4 reader, generator and validator for .NET</Description>
<Version>9.4.3</Version>
<Version>9.4.4</Version>
<PackageTags>json schema validation generator .net</PackageTags>
<Copyright>Copyright © Rico Suter, 2017</Copyright>
<PackageLicenseUrl>https://github.com/rsuter/NJsonSchema/blob/master/LICENSE.md</PackageLicenseUrl>
Expand Down
18 changes: 18 additions & 0 deletions src/NJsonSchema.Tests/Conversion/TypeToSchemaTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,24 @@ public async Task When_converting_regex_property_then_it_should_be_set_as_patter
Assert.AreEqual("regex", schema.Properties["RegexString"].Pattern);
}

public class ClassWithRegexDictionaryProperty
{
[RegularExpression("^\\d+\\.\\d+\\.\\d+\\.\\d+$")]
public Dictionary<string, string> Versions { get; set; }
}

[TestMethod]
public async Task When_dictionary_property_has_regex_attribute_then_regex_is_added_to_additionalProperties()
{
//// Act
var schema = await JsonSchema4.FromTypeAsync<ClassWithRegexDictionaryProperty>();
var json = schema.ToJson();

//// Assert
Assert.IsNull(schema.Properties["Versions"].Pattern);
Assert.IsNotNull(schema.Properties["Versions"].AdditionalPropertiesSchema.ActualSchema.Pattern);
}

[TestMethod]
public async Task When_converting_range_property_then_it_should_be_set_as_min_max()
{
Expand Down
1 change: 1 addition & 0 deletions src/NJsonSchema.Tests/NJsonSchema.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
<Compile Include="Generation\XmlObjectTests.cs" />
<Compile Include="Serialization\ExtensionDataTests.cs" />
<Compile Include="Validation\EnumValidationTests.cs" />
<Compile Include="Validation\FormatTimeSpanTests.cs" />
<Compile Include="Validation\FormatTimeTests.cs" />
<Compile Include="Validation\FormatDateTests.cs" />
<Compile Include="Validation\LineInformationTest.cs" />
Expand Down
35 changes: 34 additions & 1 deletion src/NJsonSchema.Tests/Validation/EnumValidationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class EnumValidationTests
public async Task When_enum_is_defined_without_type_then_validation_succeeds_for_correct_value()
{
//// Arrange
var json =
var json =
@"{
""enum"": [
""commercial"",
Expand Down Expand Up @@ -45,5 +45,38 @@ public async Task When_enum_is_defined_without_type_then_validation_fails_for_wr
//// Assert
Assert.AreEqual(1, errors.Count);
}

[TestMethod]
public async Task When_enumeration_has_null_then_validation_works()
{
//// Arrange
var json = @"
{
""properties"": {
""SalutationType"": {
""type"": [
""string"",
""null""
],
""enum"": [
""Mr"",
""Mrs"",
""Dr"",
""Ms"",
null
]
}
}
}";

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

//// Act
var errors = schema.Validate(@"{ ""SalutationType"": ""Prof"" }");

//// Assert
Assert.AreEqual(1, errors.Count);
}
}
}
45 changes: 45 additions & 0 deletions src/NJsonSchema.Tests/Validation/FormatTimeSpanTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json.Linq;
using NJsonSchema.Validation;

namespace NJsonSchema.Tests.Validation
{
[TestClass]
public class FormatTimeSpanTests
{
[TestMethod]
public void When_format_time_span_incorrect_then_validation_fails()
{
//// Arrange
var schema = new JsonSchema4();
schema.Type = JsonObjectType.String;
schema.Format = JsonFormatStrings.TimeSpan;

var token = new JValue("test");

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

//// Assert
Assert.AreEqual(ValidationErrorKind.TimeSpanExpected, errors.First().Kind);
}

[TestMethod]
public void When_format_time_span_correct_then_validation_succeeds()
{
//// Arrange
var schema = new JsonSchema4();
schema.Type = JsonObjectType.String;
schema.Format = JsonFormatStrings.TimeSpan;

var token = new JValue("1:30:45");

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

//// Assert
Assert.AreEqual(0, errors.Count());
}
}
}
7 changes: 6 additions & 1 deletion src/NJsonSchema/Generation/JsonSchemaGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,12 @@ public void ApplyPropertyAnnotations(JsonSchema4 jsonProperty, Newtonsoft.Json.S

dynamic regexAttribute = attributes.TryGetIfAssignableTo("System.ComponentModel.DataAnnotations.RegularExpressionAttribute");
if (regexAttribute != null)
jsonProperty.Pattern = regexAttribute.Pattern;
{
if (propertyTypeDescription.IsDictionary)
jsonProperty.AdditionalPropertiesSchema.Pattern = regexAttribute.Pattern;
else
jsonProperty.Pattern = regexAttribute.Pattern;
}

if (propertyTypeDescription.Type == JsonObjectType.Number ||
propertyTypeDescription.Type == JsonObjectType.Integer)
Expand Down
2 changes: 1 addition & 1 deletion src/NJsonSchema/NJsonSchema.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFrameworks>netstandard1.0;net40;net45</TargetFrameworks>
<Description>JSON Schema draft v4 reader, generator and validator for .NET</Description>
<Version>9.4.3</Version>
<Version>9.4.4</Version>
<PackageTags>json schema validation generator .net</PackageTags>
<Copyright>Copyright © Rico Suter, 2017</Copyright>
<PackageLicenseUrl>https://github.com/rsuter/NJsonSchema/blob/master/LICENSE.md</PackageLicenseUrl>
Expand Down
9 changes: 8 additions & 1 deletion src/NJsonSchema/Validation/JsonSchemaValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ private void ValidateNull(JToken token, JsonSchema4 schema, JsonObjectType type,

private void ValidateEnum(JToken token, JsonSchema4 schema, string propertyName, string propertyPath, List<ValidationError> errors)
{
if (schema.Enumeration.Count > 0 && schema.Enumeration.All(v => v.ToString() != token.ToString()))
if (schema.Enumeration.Count > 0 && schema.Enumeration.All(v => v?.ToString() != token?.ToString()))
errors.Add(new ValidationError(ValidationErrorKind.NotInEnumeration, propertyName, propertyPath, token, schema));
}

Expand Down Expand Up @@ -200,6 +200,13 @@ private void ValidateString(JToken token, JsonSchema4 schema, JsonObjectType typ
errors.Add(new ValidationError(ValidationErrorKind.TimeExpected, propertyName, propertyPath, token, schema));
}

if (schema.Format == JsonFormatStrings.TimeSpan)
{
TimeSpan timeSpanResult;
if (token.Type != JTokenType.TimeSpan && TimeSpan.TryParse(value, out timeSpanResult) == false)
errors.Add(new ValidationError(ValidationErrorKind.TimeSpanExpected, propertyName, propertyPath, token, schema));
}

if (schema.Format == JsonFormatStrings.Uri)
{
Uri uriResult;
Expand Down
3 changes: 3 additions & 0 deletions src/NJsonSchema/Validation/ValidationErrorKind.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ public enum ValidationErrorKind
/// <summary>A time is expected. </summary>
TimeExpected,

/// <summary>A time-span is expected. </summary>
TimeSpanExpected,

/// <summary>An URI is expected. </summary>
UriExpected,

Expand Down

0 comments on commit 3d3d55b

Please sign in to comment.