Skip to content

Commit

Permalink
Improved content validation
Browse files Browse the repository at this point in the history
  • Loading branch information
usercode committed Mar 26, 2024
1 parent 76d45f5 commit 15df966
Show file tree
Hide file tree
Showing 42 changed files with 207 additions and 158 deletions.
3 changes: 1 addition & 2 deletions src/DragonFly.API.Core/ContentItems/RestContentItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using DragonFly.Validations;

namespace DragonFly.API;

Expand All @@ -29,7 +28,7 @@ public class RestContentItem : RestContentBase
/// ValidationContext
/// </summary>
[JsonPropertyOrder(4)]
public ValidationContext? ValidationContext { get; set; }
public ValidationState? ValidationContext { get; set; }

/// <summary>
/// Fields
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static ContentItem ToModel(this RestContentItem restContentItem)
contentItem.PublishedAt = restContentItem.PublishedAt;
contentItem.Version = restContentItem.Version;
contentItem.SchemaVersion = restContentItem.SchemaVersion;
contentItem.ValidationContext = restContentItem.ValidationContext;
contentItem.ValidationState = restContentItem.ValidationContext;

foreach (var restField in restContentItem.Fields)
{
Expand Down Expand Up @@ -97,7 +97,7 @@ public static RestContentItem ToRest(this ContentItem contentItem, bool includeS
restContentItem.PublishedAt = contentItem.PublishedAt;
restContentItem.Version = contentItem.Version;
restContentItem.SchemaVersion = contentItem.SchemaVersion;
restContentItem.ValidationContext = contentItem.ValidationContext;
restContentItem.ValidationContext = contentItem.ValidationState;

foreach (var field in contentItem.Fields)
{
Expand Down
13 changes: 5 additions & 8 deletions src/DragonFly.App.Server/Models/Product2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public partial class Product2
[StringField(Required = true, Index = true, MaxLength = 1024, ReferenceField = true, ListField = true, QueryField = true)]
private string? _title;

[SlugField(Index = true, QueryField = true)]
[SlugField(Index = true, QueryField = true, TargetField = nameof(Title))]
private SlugField _slug;

[BoolField(Index = true, QueryField = true)]
Expand All @@ -23,22 +23,19 @@ public partial class Product2
[IntegerField(Index = true, QueryField = true)]
private long? _quantity;

[FloatField(Index = true, QueryField = true)]
[FloatField]
private double? _quantity2;

[HtmlField]
private BlockField _htmlContent;

[BlockField]
private BlockField _mainContent;

[AssetField(ShowPreview = true, Index = true, QueryField = true)]
private AssetField _image;

[AssetField(ShowPreview = true, Index = true, QueryField = true)]
[AssetField(ShowPreview = true)]
private Asset _image2;

[ReferenceField(Index = true, QueryField = true)]
[ReferenceField]
private ReferenceField _customerA;

[ReferenceField]
Expand All @@ -47,7 +44,7 @@ public partial class Product2
[ReferenceField]
private Customer? _customerC;

[GeolocationField(Index = true, QueryField = true)]
[GeolocationField]
private GeolocationField _location;

[TextField]
Expand Down
2 changes: 1 addition & 1 deletion src/DragonFly.Client/Modules/ContentInitializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public Task ExecuteAsync(IDragonFlyApi api)
api.Field().Add<StringField>().WithFieldView<StringFieldView>().WithOptionView<StringFieldOptionsView>().WithQueryView<StringFieldQueryView>();
api.Field().Add<FloatField>().WithFieldView<FloatFieldView>().WithOptionView<FloatFieldOptionsView>().WithQueryView<FloatFieldQueryView>();
api.Field().Add<IntegerField>().WithFieldView<IntegerFieldView>().WithOptionView<IntegerFieldOptionsView>().WithQueryView<IntegerFieldQueryView>();
api.Field().Add<SlugField>().WithFieldView<SlugFieldView>();
api.Field().Add<SlugField>().WithFieldView<SlugFieldView>().WithOptionView<SlugFieldOptionsView>();
api.Field().Add<TextField>().WithFieldView<TextFieldView>();
api.Field().Add<HtmlField>().WithFieldView<HtmlFieldView>();
api.Field().Add<XmlField>().WithFieldView<XmlFieldView>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
}
</div>
<div class="col-lg-1">
@if (ShowValidationErrors && ContentItem.ValidationContext.State == ValidationState.Invalid)
@if (ShowValidationErrors && ContentItem.ValidationState.Result == ValidationResult.Invalid)
{
<i class="fa-solid fa-triangle-exclamation" title="@ContentItem.ValidationContext.GetMessage()"></i>
<i class="fa-solid fa-triangle-exclamation" title="@ContentItem.ValidationState.Message"></i>
}
</div>
@* no list fields available? Show id field only *@
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

using BlazorStrap;
using DragonFly.Client.Base;
using DragonFly.Validations;
using DragonFly.Razor.Base;
using Microsoft.AspNetCore.Components;
using System;
Expand Down Expand Up @@ -52,7 +51,7 @@ public ContentItemDetailBase()

public bool IsFieldValid(string field)
{
return Entity.ValidationContext.Errors.All(x => x.Field != field);
return Entity.ValidationState.Errors.All(x => x.Field != field);
}

protected BSColor GetFieldColor(string field)
Expand Down Expand Up @@ -159,7 +158,7 @@ protected override void OnGuiStateChanged()
}
}

foreach (ValidationError error in Entity.ValidationContext.Errors)
foreach (ValidationError error in Entity.ValidationState.Errors)
{
Notifications.Add(new NotificationItem(NotificationType.Error, error.Message));
}
Expand Down Expand Up @@ -201,7 +200,7 @@ protected virtual async Task PreviewAsync()

protected override void OnSaving(SavingEventArgs args)
{
if (Entity.Validate() == ValidationState.Invalid)
if (Entity.Validate() == ValidationResult.Invalid)
{
OnGuiStateChanged();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@
@* fields *@
@foreach (var part in items)
{
<div class="field" style="grid-column: @(Column+1); grid-row: @(part.FieldIndex + 3)">
<BSAlert Color="GetFieldColor(part.FieldName)">
<div class="field" style="grid-column: @(Column+1); grid-row: @(part.FieldIndex + 3)">
@ComponentManager.CreateComponent(IsReadOnly, part.FieldName, part.ContentField, part.SchemaField.Options)
</BSAlert>
</div>
}

Expand All @@ -47,7 +45,7 @@
{
string fieldPath = field;

if (Content.ValidationContext.Errors.Any(x => x.Field == field))
if (Content.ValidationState.Errors.Any(x => x.Field == field))
{
return BSColor.Danger;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
<i class="fa-solid fa-check" title="Published at @contentItem.PublishedAt" style="@(contentItem.PublishedAt == null ? "visibility: hidden" : "")"></i>
</div>
<div class="col-lg-auto">
<i class="fa-solid fa-circle-exclamation text-danger" title="@contentItem.ValidationContext.GetMessage()" style="@(contentItem.ValidationContext.State == ValidationState.Valid ? "visibility: hidden" : "")"></i>
<i class="fa-solid fa-circle-exclamation text-danger" title="@contentItem.ValidationState.Message" style="@(contentItem.ValidationState.Result == ValidationResult.Valid ? "visibility: hidden" : "")"></i>
</div>
@* no list fields available? Show id field only *@
@if (contentItem.Schema.ListFields.Any() == false)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
@inherits FieldOptionsComponent<SlugFieldOptions>

<BSCard>
<BSCard CardType="CardType.Body">
<BSCard CardType="CardType.Title">

</BSCard>
<BSRow class="form-group">
<label class="col-sm-2 col-form-label">Required</label>
<div class="col-sm-8">
<CheckBoxView @bind-Value="@Options.IsRequired" />
</div>
</BSRow>
<BSRow class="form-group">
<label class="col-sm-2 col-form-label">Searchable</label>
<div class="col-sm-8">
<CheckBoxView @bind-Value="@Options.IsSearchable" />
</div>
</BSRow>
<BSRow class="form-group">
<label class="col-sm-2 col-form-label">Target field</label>
<div class="col-sm-8">
<input type="text" class="form-control" @bind-value="@Options.TargetField" />
</div>
</BSRow>
</BSCard>
</BSCard>
1 change: 0 additions & 1 deletion src/DragonFly.Client/_Imports.razor
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
@using DragonFly.Client.Pages.ContentItems.Query
@using DragonFly.Client.Pages.ContentSchemas
@using DragonFly.Client.Helpers
@using DragonFly.Validations
@using BlazorStrap
@using BlazorStrap.V5
@attribute [Authorize]
Expand Down
10 changes: 7 additions & 3 deletions src/DragonFly.Core/Api/DragonFlyApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@
// https://github.com/usercode/DragonFly
// MIT License

namespace DragonFly.Init;
namespace DragonFly;

/// <summary>
/// DragonFlyApi
/// </summary>
public class DragonFlyApi : IDragonFlyApi
public sealed class DragonFlyApi : IDragonFlyApi
{
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
public static IDragonFlyApi Default { get; internal set; }
#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.

public DragonFlyApi(IServiceProvider serviceProvider)
{
ServiceProvider = serviceProvider;
Expand All @@ -17,5 +21,5 @@ public DragonFlyApi(IServiceProvider serviceProvider)
/// <summary>
/// ServiceProvider
/// </summary>
public IServiceProvider ServiceProvider { get; }
public IServiceProvider ServiceProvider { get; }
}
4 changes: 3 additions & 1 deletion src/DragonFly.Core/Api/Init/DragonFlyApiExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public static class DragonFlyApiExtensions
{
public static async Task InitAsync(this IDragonFlyApi api)
{
DragonFlyApi.Default = api;

ILogger<DragonFlyApi> logger = api.ServiceProvider.GetRequiredService<ILogger<DragonFlyApi>>();

using IServiceScope scope = api.ServiceProvider.CreateScope();
Expand All @@ -34,7 +36,7 @@ public static async Task InitAsync(this IDragonFlyApi api)
logger.LogInformation("Initializing {type}", GetName(item));

await item.ExecuteAsync(api);
}
}
}

private static string? GetName(object initializer)
Expand Down
32 changes: 7 additions & 25 deletions src/DragonFly.Core/Modules/ContentItems/Models/ContentItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// https://github.com/usercode/DragonFly
// MIT License

using DragonFly.Validations;

namespace DragonFly;

/// <summary>
Expand Down Expand Up @@ -36,26 +34,22 @@ public ContentItem(ContentSchema schema, Guid id)
/// </summary>
public virtual ContentSchema Schema { get => _schema; set => _schema = value; }

private ContentFields _fields = new ContentFields();

/// <summary>
/// Fields
/// </summary>
public virtual ContentFields Fields { get => _fields; set => _fields = value; }

private ValidationContext _validationContext = new ValidationContext();
public virtual ContentFields Fields { get; set; } = new ContentFields();

/// <summary>
/// ValidationContext
/// </summary>
public virtual ValidationContext ValidationContext { get => _validationContext; set => _validationContext = value; }
public virtual ValidationState ValidationState { get; set; } = new ValidationState();

/// <summary>
/// Validates the content item.
/// </summary>
public virtual ValidationState Validate()
public virtual ValidationResult Validate()
{
ValidationContext validationContext = new ValidationContext(ValidationState.Valid);
ValidationContext validationContext = new ValidationContext(this);

foreach (var field in Fields)
{
Expand All @@ -67,29 +61,17 @@ public virtual ValidationState Validate()
}
}

ValidationContext = validationContext;
ValidationState = validationContext.Execute();

return validationContext.State;
return ValidationState.Result;
}

public override int GetHashCode()
{
return HashCode.Combine(Schema.Name, Id);
}

public override bool Equals(object? obj)
{
if (obj is ContentItem other)
{
return Equals(other);
}
else
{
return false;
}
}

public bool Equals(ContentItem? other)
public override bool Equals(ContentItem? other)
{
if (other is null)
{
Expand Down
16 changes: 13 additions & 3 deletions src/DragonFly.Core/Modules/ContentItems/Models/Entity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace DragonFly;

public abstract class Entity<T> : IEntity
public abstract class Entity<T> : IEntity, IEquatable<T>
where T : IEntity
{
protected Guid _id;
Expand All @@ -20,10 +20,20 @@ public override bool Equals(object? obj)
{
if (obj is T other)
{
return Id == other.Id;
return Equals(other);
}

return base.Equals(obj);
return false;
}

public virtual bool Equals(T? other)
{
if (other is null)
{
return false;
}

return _id == other.Id;
}

public override int GetHashCode()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// https://github.com/usercode/DragonFly
// MIT License

using DragonFly.Validations;

namespace DragonFly;

/// <summary>
Expand All @@ -15,13 +13,12 @@ public partial class ArrayField
{
public ArrayField()
{
Items = new List<ArrayFieldItem>();
}

/// <summary>
/// Items
/// </summary>
public IList<ArrayFieldItem> Items { get; set; }
public IList<ArrayFieldItem> Items { get; set; }= new List<ArrayFieldItem>();

public override void Clear()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// https://github.com/usercode/DragonFly
// MIT License

using DragonFly.Validations;

namespace DragonFly;

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// MIT License

using System.Text.RegularExpressions;
using DragonFly.Validations;

namespace DragonFly;

Expand Down
Loading

0 comments on commit 15df966

Please sign in to comment.