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.
 
 
 
 
 

84 lines
2.4 KiB

// ==========================================================================
// Squidex Headless CMS
// ==========================================================================
// Copyright (c) Squidex UG (haftungsbeschraenkt)
// All rights reserved. Licensed under the MIT license.
// ==========================================================================
using System.Collections.Immutable;
namespace Squidex.Domain.Apps.Core.ValidateContent
{
public sealed record ValidationContext
{
public ImmutableQueue<string> Path { get; private set; } = ImmutableQueue<string>.Empty;
public bool IsOptional { get; init; }
public RootContext Root { get; }
public ValidationMode Mode { get; init; }
public ValidationAction Action { get; init; }
public ValidationContext(RootContext rootContext)
{
Root = rootContext;
}
public void AddError(IEnumerable<string> path, string message)
{
Root.AddError(path, message);
}
public ValidationContext Optimized(bool optimized = true)
{
return WithMode(optimized ? ValidationMode.Optimized : ValidationMode.Default);
}
public ValidationContext AsPublishing(bool publish = true)
{
return WithAction(publish ? ValidationAction.Publish : ValidationAction.Upsert);
}
public ValidationContext Optional(bool isOptional = true)
{
if (IsOptional == isOptional)
{
return this;
}
return this with { IsOptional = isOptional };
}
public ValidationContext WithAction(ValidationAction action)
{
if (Action == action)
{
return this;
}
return this with { Action = action };
}
public ValidationContext WithMode(ValidationMode mode)
{
if (Mode == mode)
{
return this;
}
return this with { Mode = mode };
}
public ValidationContext Nested(string property)
{
return this with { Path = Path.Enqueue(property) };
}
public ValidationContext Nested(string property, bool isOptional)
{
return this with { Path = Path.Enqueue(property), IsOptional = isOptional };
}
}
}