mirror of https://github.com/Squidex/squidex.git
39 changed files with 305 additions and 275 deletions
@ -1,57 +0,0 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschränkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using System; |
|||
using System.Collections.Concurrent; |
|||
using System.Threading.Tasks; |
|||
using Newtonsoft.Json.Linq; |
|||
using Squidex.Domain.Apps.Core.Schemas; |
|||
using Squidex.Infrastructure; |
|||
using Squidex.Infrastructure.Json; |
|||
|
|||
namespace Squidex.Domain.Apps.Core.ValidateContent |
|||
{ |
|||
public static class FieldExtensions |
|||
{ |
|||
public static void AddError(this ConcurrentBag<ValidationError> errors, string message, IField field, IFieldPartitionItem partitionItem = null) |
|||
{ |
|||
AddError(errors, message, !string.IsNullOrWhiteSpace(field.RawProperties.Label) ? field.RawProperties.Label : field.Name, field.Name, partitionItem); |
|||
} |
|||
|
|||
public static void AddError(this ConcurrentBag<ValidationError> errors, string message, string fieldName, IFieldPartitionItem partitionItem = null) |
|||
{ |
|||
AddError(errors, message, fieldName, fieldName, partitionItem); |
|||
} |
|||
|
|||
public static void AddError(this ConcurrentBag<ValidationError> errors, string message, string displayName, string fieldName, IFieldPartitionItem partitionItem = null) |
|||
{ |
|||
if (partitionItem != null && partitionItem != InvariantPartitioning.Instance.Master) |
|||
{ |
|||
displayName += $" ({partitionItem.Key})"; |
|||
} |
|||
|
|||
errors.Add(new ValidationError(message.Replace("<FIELD>", displayName), fieldName)); |
|||
} |
|||
|
|||
public static async Task ValidateAsync(this IField field, JToken value, ValidationContext context, Action<string> addError) |
|||
{ |
|||
try |
|||
{ |
|||
var typedValue = value.IsNull() ? null : JsonValueConverter.ConvertValue(field, value); |
|||
|
|||
foreach (var validator in ValidatorsFactory.CreateValidators(field)) |
|||
{ |
|||
await validator.ValidateAsync(typedValue, context, addError); |
|||
} |
|||
} |
|||
catch |
|||
{ |
|||
addError("<FIELD> is not a valid value."); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,27 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
namespace Squidex.Domain.Apps.Core.ValidateContent.Validators |
|||
{ |
|||
public static class Formatter |
|||
{ |
|||
public static ErrorFormatter Combine(string field, ErrorFormatter formatter) |
|||
{ |
|||
return (innerField, message) => |
|||
{ |
|||
if (!string.IsNullOrWhiteSpace(innerField)) |
|||
{ |
|||
formatter($"{field}.{innerField}", message); |
|||
} |
|||
else |
|||
{ |
|||
formatter(field, message); |
|||
} |
|||
}; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,70 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using System.Collections.Generic; |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace Squidex.Domain.Apps.Core.ValidateContent.Validators |
|||
{ |
|||
public sealed class ObjectValidator<TValue> : IValidator |
|||
{ |
|||
private readonly IDictionary<string, (bool IsOptional, IValidator Validator)> schema; |
|||
private readonly bool isPartial; |
|||
private readonly string fieldType; |
|||
private readonly TValue fieldDefault; |
|||
|
|||
public ObjectValidator(IDictionary<string, (bool IsOptional, IValidator Validator)> schema, bool isPartial, string fieldType, TValue fieldDefault) |
|||
{ |
|||
this.schema = schema; |
|||
this.fieldDefault = fieldDefault; |
|||
this.fieldType = fieldType; |
|||
this.isPartial = isPartial; |
|||
} |
|||
|
|||
public async Task ValidateAsync(object value, ValidationContext context, ErrorFormatter addError) |
|||
{ |
|||
if (value is IReadOnlyDictionary<string, TValue> values) |
|||
{ |
|||
foreach (var fieldData in values) |
|||
{ |
|||
var name = fieldData.Key; |
|||
|
|||
if (!schema.ContainsKey(name)) |
|||
{ |
|||
Formatter.Combine(name, addError)(null, $"Not a known {fieldType}."); |
|||
} |
|||
} |
|||
|
|||
var tasks = new List<Task>(); |
|||
|
|||
foreach (var field in schema) |
|||
{ |
|||
var name = field.Key; |
|||
|
|||
if (!values.TryGetValue(name, out var fieldValue)) |
|||
{ |
|||
if (isPartial) |
|||
{ |
|||
continue; |
|||
} |
|||
|
|||
fieldValue = fieldDefault; |
|||
} |
|||
|
|||
var (isOptional, validator) = field.Value; |
|||
|
|||
var fieldContext = context.Optional(isOptional); |
|||
var fieldFormatter = Formatter.Combine(name, addError); |
|||
|
|||
tasks.Add(validator.ValidateAsync(fieldValue, fieldContext, fieldFormatter)); |
|||
} |
|||
|
|||
await Task.WhenAll(tasks); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue