mirror of https://github.com/Squidex/squidex.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
52 lines
1.8 KiB
52 lines
1.8 KiB
// ==========================================================================
|
|
// Squidex Headless CMS
|
|
// ==========================================================================
|
|
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|
// All rights reserved. Licensed under the MIT license.
|
|
// ==========================================================================
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using Squidex.Domain.Apps.Core.ValidateContent;
|
|
using Squidex.Domain.Apps.Entities.Contents.Repositories;
|
|
using Squidex.Infrastructure;
|
|
|
|
namespace Squidex.Extensions.Validation
|
|
{
|
|
public sealed class CompositeUniqueValidatorFactory : IValidatorsFactory
|
|
{
|
|
private const string Prefix = "unique:";
|
|
private readonly IContentRepository contentRepository;
|
|
|
|
public CompositeUniqueValidatorFactory(IContentRepository contentRepository)
|
|
{
|
|
Guard.NotNull(contentRepository, nameof(contentRepository));
|
|
|
|
this.contentRepository = contentRepository;
|
|
}
|
|
|
|
public IEnumerable<IValidator> CreateContentValidators(ValidationContext context, FieldValidatorFactory createFieldValidator)
|
|
{
|
|
foreach (var validatorTag in ValidatorTags(context.Schema.Properties.Tags))
|
|
{
|
|
yield return new CompositeUniqueValidator(validatorTag, contentRepository);
|
|
}
|
|
}
|
|
|
|
private static IEnumerable<string> ValidatorTags(IEnumerable<string> tags)
|
|
{
|
|
if (tags == null)
|
|
{
|
|
yield break;
|
|
}
|
|
|
|
foreach (var tag in tags)
|
|
{
|
|
if (tag.StartsWith(Prefix, StringComparison.OrdinalIgnoreCase) && tag.Length > Prefix.Length)
|
|
{
|
|
yield return tag;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|