Skip to content

Commit

Permalink
Fix validation of null values. (#1394)
Browse files Browse the repository at this point in the history
* Fix validation of null values.

* Change default for schema type.
  • Loading branch information
SebastianStehle committed Aug 5, 2021
1 parent cb4a712 commit 7d4b4a5
Show file tree
Hide file tree
Showing 4 changed files with 192 additions and 44 deletions.
8 changes: 3 additions & 5 deletions src/NJsonSchema.Tests/Validation/EnumValidationTests.cs
Expand Up @@ -87,8 +87,7 @@ public async Task When_enumeration_has_null_and_value_is_null_then_no_validation
""properties"": {
""SalutationType"": {
""type"": [
""string"",
""null""
""string""
],
""enum"": [
""Mr"",
Expand Down Expand Up @@ -120,8 +119,7 @@ public async Task When_enumeration_doesnt_have_null_and_value_is_null_then_valid
""properties"": {
""SalutationType"": {
""type"": [
""string"",
""null""
""string""
],
""enum"": [
""Mr"",
Expand All @@ -140,7 +138,7 @@ public async Task When_enumeration_doesnt_have_null_and_value_is_null_then_valid
var errors = schema.Validate(@"{ ""SalutationType"": null }");

//// Assert
Assert.Equal(1, errors.Count);
Assert.Equal(2, errors.Count);
}
}
}
123 changes: 115 additions & 8 deletions src/NJsonSchema.Tests/Validation/NullPropertyTests.cs
@@ -1,3 +1,5 @@
using NJsonSchema.Generation;
using System;
using System.Threading.Tasks;
using Xunit;

Expand Down Expand Up @@ -27,26 +29,131 @@ public void When_property_can_be_null_then_null_is_allowed()
Assert.Equal(0, errors.Count);
}

public class NullablePropertyClass
public class NullablePropertyClass<T>
{
public QueryRule ReportRules { get; set; }
public T Value { get; set; }
}

public class QueryRule
{
public string Name { get; set; }
}

[Fact]
public async Task When_property_can_be_null_then_null_is_allowed2()
[Theory]
[InlineData(SchemaType.JsonSchema)]
[InlineData(SchemaType.OpenApi3)]
[InlineData(SchemaType.Swagger2)]
public async Task When_object_property_can_be_null_then_null_is_allowed(SchemaType schemaType)
{
//// Arrange
var schema = JsonSchema.FromType<NullablePropertyClass>();
var schemaData = schema.ToJson();
var schema = JsonSchema.FromType<NullablePropertyClass<QueryRule>>(new JsonSchemaGeneratorSettings
{
SchemaType = schemaType
});

//// Act
var data = "{ 'ReportRules': null }";
var errors = schema.Validate(data);
var data = "{ 'Value': null }";
var errors = schema.Validate(data, schemaType);

//// Assert
Assert.Equal(0, errors.Count);
}

[Theory]
[InlineData(SchemaType.JsonSchema)]
[InlineData(SchemaType.OpenApi3)]
[InlineData(SchemaType.Swagger2)]
public async Task When_number_property_can_be_null_then_null_is_allowed(SchemaType schemaType)
{
//// Arrange
var schema = JsonSchema.FromType<NullablePropertyClass<int?>>(new JsonSchemaGeneratorSettings
{
SchemaType = schemaType
});

//// Act
var data = "{ 'Value': null }";
var errors = schema.Validate(data, schemaType);

//// Assert
Assert.Equal(0, errors.Count);
}

[Theory]
[InlineData(SchemaType.JsonSchema)]
[InlineData(SchemaType.OpenApi3)]
[InlineData(SchemaType.Swagger2)]
public async Task When_string_property_can_be_null_then_null_is_allowed(SchemaType schemaType)
{
//// Arrange
var schema = JsonSchema.FromType<NullablePropertyClass<string>>(new JsonSchemaGeneratorSettings
{
SchemaType = schemaType
});

//// Act
var data = "{ 'Value': null }";
var errors = schema.Validate(data, schemaType);

//// Assert
Assert.Equal(0, errors.Count);
}

[Theory]
[InlineData(SchemaType.JsonSchema)]
[InlineData(SchemaType.OpenApi3)]
[InlineData(SchemaType.Swagger2)]
public async Task When_boolean_property_can_be_null_then_null_is_allowed(SchemaType schemaType)
{
//// Arrange
var schema = JsonSchema.FromType<NullablePropertyClass<bool?>>(new JsonSchemaGeneratorSettings
{
SchemaType = schemaType
});

//// Act
var data = "{ 'Value': null }";
var errors = schema.Validate(data, schemaType);

//// Assert
Assert.Equal(0, errors.Count);
}

[Theory]
[InlineData(SchemaType.JsonSchema)]
[InlineData(SchemaType.OpenApi3)]
[InlineData(SchemaType.Swagger2)]
public async Task When_array_property_can_be_null_then_null_is_allowed(SchemaType schemaType)
{
//// Arrange
var schema = JsonSchema.FromType<NullablePropertyClass<int[]>>(new JsonSchemaGeneratorSettings
{
SchemaType = schemaType
});

//// Act
var data = "{ 'Value': null }";
var errors = schema.Validate(data, schemaType);

//// Assert
Assert.Equal(0, errors.Count);
}

[Theory]
[InlineData(SchemaType.JsonSchema)]
[InlineData(SchemaType.OpenApi3)]
[InlineData(SchemaType.Swagger2)]
public async Task When_enum_property_can_be_null_then_null_is_allowed(SchemaType schemaType)
{
//// Arrange
var schema = JsonSchema.FromType<NullablePropertyClass<AttributeTargets?>>(new JsonSchemaGeneratorSettings
{
SchemaType = schemaType
});

//// Act
var data = "{ 'Value': null }";
var errors = schema.Validate(data, schemaType);

//// Assert
Assert.Equal(0, errors.Count);
Expand Down
23 changes: 23 additions & 0 deletions src/NJsonSchema/JsonSchema.cs
Expand Up @@ -888,6 +888,29 @@ public ICollection<ValidationError> Validate(JToken token, params IFormatValidat
return validator.Validate(token, ActualSchema);
}

/// <summary>Validates the given JSON data against this schema.</summary>
/// <param name="jsonData">The JSON data to validate. </param>
/// <param name="schemaType">The type of the schema.</param>
/// <param name="customValidators">Custom validators to validate the JSON.</param>
/// <exception cref="JsonReaderException">Could not deserialize the JSON data.</exception>
/// <returns>The collection of validation errors. </returns>
public ICollection<ValidationError> Validate(string jsonData, SchemaType schemaType, params IFormatValidator[] customValidators)
{
var validator = new JsonSchemaValidator(customValidators);
return validator.Validate(jsonData, ActualSchema, schemaType);
}

/// <summary>Validates the given JSON token against this schema.</summary>
/// <param name="token">The token to validate. </param>
/// <param name="schemaType">The type of the schema.</param>
/// <param name="customValidators">Custom validators to validate the token.</param>
/// <returns>The collection of validation errors. </returns>
public ICollection<ValidationError> Validate(JToken token, SchemaType schemaType, params IFormatValidator[] customValidators)
{
var validator = new JsonSchemaValidator(customValidators);
return validator.Validate(token, ActualSchema, schemaType);
}

private static JsonObjectType ConvertStringToJsonObjectType(string value)
{
// Section 3.5:
Expand Down

0 comments on commit 7d4b4a5

Please sign in to comment.