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
2.2 KiB
52 lines
2.2 KiB
// ==========================================================================
|
|
// ValidationTestExtensions.cs
|
|
// Squidex Headless CMS
|
|
// ==========================================================================
|
|
// Copyright (c) Squidex Group
|
|
// All rights reserved.
|
|
// ==========================================================================
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Threading.Tasks;
|
|
using Newtonsoft.Json.Linq;
|
|
using Squidex.Domain.Apps.Core.Schemas;
|
|
using Squidex.Domain.Apps.Core.ValidateContent;
|
|
using Squidex.Domain.Apps.Core.ValidateContent.Validators;
|
|
|
|
namespace Squidex.Domain.Apps.Core.Operations.ValidateContent
|
|
{
|
|
public static class ValidationTestExtensions
|
|
{
|
|
private static readonly Task<IReadOnlyList<Guid>> ValidIds = Task.FromResult<IReadOnlyList<Guid>>(new List<Guid>());
|
|
|
|
public static readonly ValidationContext ValidContext = new ValidationContext((x, y) => ValidIds, x => ValidIds);
|
|
|
|
public static Task ValidateAsync(this IValidator validator, object value, IList<string> errors, ValidationContext context = null)
|
|
{
|
|
return validator.ValidateAsync(value, context ?? ValidContext, errors.Add);
|
|
}
|
|
|
|
public static Task ValidateOptionalAsync(this IValidator validator, object value, IList<string> errors, ValidationContext context = null)
|
|
{
|
|
return validator.ValidateAsync(value, (context ?? ValidContext).Optional(true), errors.Add);
|
|
}
|
|
|
|
public static Task ValidateAsync(this Field field, JToken value, IList<string> errors, ValidationContext context = null)
|
|
{
|
|
return field.ValidateAsync(value, context ?? ValidContext, errors.Add);
|
|
}
|
|
|
|
public static Task ValidateOptionalAsync(this Field field, JToken value, IList<string> errors, ValidationContext context = null)
|
|
{
|
|
return field.ValidateAsync(value, (context ?? ValidContext).Optional(true), errors.Add);
|
|
}
|
|
|
|
public static ValidationContext InvalidContext(Guid assetId)
|
|
{
|
|
var invalidIds = Task.FromResult<IReadOnlyList<Guid>>(new List<Guid> { assetId });
|
|
|
|
return new ValidationContext((x, y) => invalidIds, x => invalidIds);
|
|
}
|
|
}
|
|
}
|
|
|