mirror of https://github.com/Squidex/squidex.git
5 changed files with 99 additions and 4 deletions
@ -0,0 +1,32 @@ |
|||||
|
// ==========================================================================
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
||||
|
// All rights reserved. Licensed under the MIT license.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using System.Collections.Generic; |
||||
|
using System.Linq; |
||||
|
using System.Threading.Tasks; |
||||
|
using Squidex.Infrastructure.Tasks; |
||||
|
|
||||
|
namespace Squidex.Domain.Apps.Core.ValidateContent.Validators |
||||
|
{ |
||||
|
public sealed class UniqueValuesValidator<T> : IValidator |
||||
|
{ |
||||
|
public Task ValidateAsync(object value, ValidationContext context, AddError addError) |
||||
|
{ |
||||
|
if (value is IEnumerable<T> items && items.Any()) |
||||
|
{ |
||||
|
var itemsArray = items.ToArray(); |
||||
|
|
||||
|
if (itemsArray.Length != itemsArray.Distinct().Count()) |
||||
|
{ |
||||
|
addError(context.Path, "Must not contain duplicate values."); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
return TaskHelper.Done; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,61 @@ |
|||||
|
// ==========================================================================
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
||||
|
// All rights reserved. Licensed under the MIT license.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using System.Collections.Generic; |
||||
|
using System.Threading.Tasks; |
||||
|
using FluentAssertions; |
||||
|
using Squidex.Domain.Apps.Core.ValidateContent.Validators; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Squidex.Domain.Apps.Core.Operations.ValidateContent.Validators |
||||
|
{ |
||||
|
public class UniqueValuesValidatorTests |
||||
|
{ |
||||
|
private readonly List<string> errors = new List<string>(); |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task Should_not_add_error_if_value_is_null() |
||||
|
{ |
||||
|
var sut = new UniqueValuesValidator<int>(); |
||||
|
|
||||
|
await sut.ValidateAsync(null, errors); |
||||
|
|
||||
|
Assert.Empty(errors); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task Should_not_add_error_if_value_is_not_collection() |
||||
|
{ |
||||
|
var sut = new UniqueValuesValidator<int>(); |
||||
|
|
||||
|
await sut.ValidateAsync("value", errors); |
||||
|
|
||||
|
Assert.Empty(errors); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task Should_not_add_error_if_array_contains_no_duplicates() |
||||
|
{ |
||||
|
var sut = new UniqueValuesValidator<int>(); |
||||
|
|
||||
|
await sut.ValidateAsync(new[] { 1, 2, 3 }, errors); |
||||
|
|
||||
|
Assert.Empty(errors); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task Should_add_error_if_array_contains_duplicates() |
||||
|
{ |
||||
|
var sut = new UniqueValuesValidator<int>(); |
||||
|
|
||||
|
await sut.ValidateAsync(new[] { 1, 2, 2, 3 }, errors); |
||||
|
|
||||
|
errors.Should().BeEquivalentTo( |
||||
|
new[] { "Must not contain duplicate values." }); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue