Skip to content

Commit

Permalink
Adding ability to control auto-validation (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
vudodov committed Sep 8, 2019
1 parent 3296693 commit 2e081f5
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 7 deletions.
25 changes: 25 additions & 0 deletions V.Udodov.Json.Tests/EntityTests.cs
Expand Up @@ -203,5 +203,30 @@ public void WhenTryingToGetFlexibleDataEntityItemItShouldThrow()

result.Should().Be(12);
}

[Fact]
public void WhenSettingEntityFlexibleDataWithSchemaAndAutoValidationIsEnabledAndDataIsInvalidItShouldThrow()
{
const string schema = @"{
'type': 'object',
'properties': {
'shoe_size': { 'type': 'number', 'minimum': 5, 'maximum': 12, 'multipleOf': 1.0 }
}
}";

var entityMock = new EntityMock
{
AutoValidate = false,
ExtensionDataJsonSchema = schema
};
Action action = () => entityMock["shoe_size"] = 15;
Action validation = () => entityMock.Validate();

action.Should().NotThrow<JsonEntityValidationException>();
validation
.Should().Throw<JsonEntityValidationException>()
.And.Errors
.Should().HaveCount(1);
}
}
}
43 changes: 36 additions & 7 deletions V.Udodov.Json/Entity.cs
Expand Up @@ -13,6 +13,16 @@ public class Entity
[JsonIgnore] private JSchema _extensionDataJsonSchema;
[JsonExtensionData] private readonly IDictionary<string, JToken> _data = new Dictionary<string, JToken>();

/// <summary>
/// Enables auto-validation of object against JsonSchema on add attempt.
/// </summary>
[JsonIgnore] public bool AutoValidate { get; set; }

public Entity()
{
AutoValidate = true;
}

/// <summary>
/// Get Full JSON Schema including class properties and configured flexible data schema.
/// </summary>
Expand Down Expand Up @@ -93,15 +103,18 @@ private void Set(string key, object value)
{
var token = JToken.FromObject(value);

if (_extensionDataJsonSchema != null)
if (AutoValidate)
{
var obj = JObject.FromObject(_data);
obj[key] = token;
if (_extensionDataJsonSchema != null)
{
var obj = JObject.FromObject(_data);
obj[key] = token;

if (!obj.IsValid(_extensionDataJsonSchema, out IList<ValidationError> errors))
throw new JsonEntityValidationException(
$"Validation for value {token} failed against JSON schema {_extensionDataJsonSchema}.",
errors);
if (!obj.IsValid(_extensionDataJsonSchema, out IList<ValidationError> errors))
throw new JsonEntityValidationException(
$"Validation for value {token} failed against JSON schema {_extensionDataJsonSchema}.",
errors);
}
}

_data[key] = token;
Expand Down Expand Up @@ -132,5 +145,21 @@ object JTokenToObject(JToken source)
value = null;
return false;
}

/// <summary>
/// Validates if current entity state is valid against JsonSchema.
/// </summary>
/// <exception cref="JsonEntityValidationException"></exception>
public void Validate()
{
if (_extensionDataJsonSchema == null) return;

var obj = JObject.FromObject(_data);

if (!obj.IsValid(_extensionDataJsonSchema, out IList<ValidationError> errors))
throw new JsonEntityValidationException(
$"Validation failed against JSON schema {_extensionDataJsonSchema}.",
errors);
}
}
}

0 comments on commit 2e081f5

Please sign in to comment.