Skip to content

Commit 750f9a9

Browse files
committed
Making the Binary Filter Left and Right optional and validations
1 parent 784d094 commit 750f9a9

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

src/Deveel.Filter.Model.Web/Deveel.Filter.Model.Web.xml

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Deveel.Filter.Model.Web/Filters/BinaryFilterModel.cs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,22 @@ namespace Deveel.Filters {
66
/// <summary>
77
/// A model that represents a binary filter
88
/// </summary>
9-
public sealed class BinaryFilterModel {
9+
public sealed class BinaryFilterModel : IValidatableObject {
1010
/// <summary>
1111
/// The left side of the binary filter
1212
/// </summary>
13-
[JsonPropertyName("left"), Required]
13+
[JsonPropertyName("left")]
1414
public FilterModel? Left { get; set; }
1515

1616
/// <summary>
1717
/// The right side of the binary filter
1818
/// </summary>
19-
[JsonPropertyName("right"), Required]
19+
[JsonPropertyName("right")]
2020
public FilterModel? Right { get; set; }
2121

22+
/// <summary>
23+
/// Extension data for the dynamic properties of the filter.
24+
/// </summary>
2225
[JsonExtensionData, SimpleValue]
2326
public IDictionary<string, JsonElement>? BinaryData { get; set; }
2427

@@ -34,5 +37,22 @@ internal BinaryFilter BuildFilter(FilterType filterType) {
3437

3538
return Filter.Binary(left, right, filterType);
3639
}
40+
41+
IEnumerable<ValidationResult> IValidatableObject.Validate(ValidationContext validationContext) {
42+
if (BinaryData == null && Left == null && Right == null)
43+
yield return new ValidationResult("The left and right side of the binary filter must be specified", new[] {nameof(Left), nameof(Right)});
44+
45+
if (BinaryData != null && (Left != null || Right != null))
46+
yield return new ValidationResult("The left and right side of the binary filter must not be present if the data are set", new[] {nameof(Left), nameof(Right)});
47+
48+
if (BinaryData != null && BinaryData.Count == 0)
49+
yield return new ValidationResult("The binary data must not be empty", new[] {nameof(BinaryData)});
50+
51+
if (Left != null && Right != null && BinaryData != null)
52+
yield return new ValidationResult("The left and right side of the binary filter must not be present if the data are set", new[] {nameof(Left), nameof(Right)});
53+
54+
if (BinaryData == null && (Left == null || Right == null))
55+
yield return new ValidationResult("The left and right side of the binary filter must be specified", new[] {nameof(Left), nameof(Right)});
56+
}
3757
}
3858
}

test/Deveel.Filter.Model.Web.XUnit/Filters/ModelValidationTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ public static void ValidateInvalidBinaryModel() {
3838
Assert.False(Validator.TryValidateObject(model, context, resuls, true));
3939

4040
Assert.Single(resuls);
41-
Assert.Equal("Right", resuls[0].MemberNames.First());
41+
Assert.Equal("Left", resuls[0].MemberNames.ElementAt(0));
42+
Assert.Equal("Right", resuls[0].MemberNames.ElementAt(1));
4243
}
4344

4445
[Fact]

0 commit comments

Comments
 (0)