Headless CMS and Content Managment Hub
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.
 
 
 
 
 

47 lines
1.7 KiB

// ==========================================================================
// Squidex Headless CMS
// ==========================================================================
// Copyright (c) Squidex UG (haftungsbeschraenkt)
// All rights reserved. Licensed under the MIT license.
// ==========================================================================
using Squidex.Domain.Apps.Core.ValidateContent;
using Squidex.Domain.Apps.Entities.Contents.Repositories;
namespace Squidex.Extensions.Validation
{
public sealed class CompositeUniqueValidatorFactory : IValidatorsFactory
{
private const string Prefix = "unique:";
private readonly IContentRepository contentRepository;
public CompositeUniqueValidatorFactory(IContentRepository contentRepository)
{
this.contentRepository = contentRepository;
}
public IEnumerable<IValidator> CreateContentValidators(ValidationContext context, ValidatorFactory createFieldValidator)
{
foreach (var validatorTag in ValidatorTags(context.Root.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;
}
}
}
}
}