Skip to content

Commit

Permalink
Merge pull request #250 from Jumoo/dev/validation
Browse files Browse the repository at this point in the history
Validation of XML, and Datatypes for v8
  • Loading branch information
KevinJump authored Nov 23, 2023
2 parents 96b3210 + 540554b commit df715f0
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,14 @@
}

function validate(status) {
vm.validating = true;
uSyncMigrationService.validate(status)
.then(function (result) {
vm.validating = false;
vm.validation = result.data;
vm.sourceValid = vm.validation.success;
}, function (error) {
vm.validating = false;
vm.error = error.data.ExceptionMessage;
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,14 @@ <h2><localize key="usyncmigrate_convertTitle"></localize></h2>
<div class="flex justify-between">
<div class="umb-textstring umb-property-editor">{{vm.migrationStatus.source}}</div>
<div>
<umb-button action="vm.showValidation = !vm.showValidation"
label-key="usyncmigrate_showValidation"
button-style="link"></umb-button>

<div ng-if="vm.validating == true">
validating ...
</div>
<div ng-if="vm.validating == false">
<umb-button action="vm.showValidation = !vm.showValidation"
label-key="usyncmigrate_showValidation"
button-style="link"></umb-button>
</div>
</div>
</div>
</umb-control-group>
Expand Down Expand Up @@ -136,10 +140,23 @@ <h2><localize key="usyncmigrate_convertTitle"></localize></h2>


<div ng-if="!vm.migrationStatus.migrated || vm.showValidation">
<usync-migration-results results="vm.validation"
action="Validation"
is-valid="vm.sourceValid">
</usync-migration-results>

<div ng-if="vm.validating">
<umb-box>
<umb-box-content>
<div class="text-center">
Validation in progress....<br/>
(For big sites this can take a while)
</div>
</umb-box-content>
</umb-box>
</div>
<div ng-if="vm.validating == false">
<usync-migration-results results="vm.validation"
action="Validation"
is-valid="vm.sourceValid">
</usync-migration-results>
</div>
</div>
</div>

Expand Down
61 changes: 58 additions & 3 deletions uSync.Migrations.Core/Handlers/Eight/DataTypeMigrationHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,44 @@

using Umbraco.Cms.Core.Events;
using Umbraco.Cms.Core.Services;
using Umbraco.Extensions;

using uSync.Core;
using uSync.Migrations.Core.Composing;
using uSync.Migrations.Core.Context;
using uSync.Migrations.Core.Handlers.Shared;
using uSync.Migrations.Core.Migrators;
using uSync.Migrations.Core.Migrators.Models;
using uSync.Migrations.Core.Models;
using uSync.Migrations.Core.Services;
using uSync.Migrations.Core.Validation;

namespace uSync.Migrations.Core.Handlers.Eight;
[SyncMigrationHandler(BackOfficeConstants.Groups.Settings, uSyncMigrations.Priorities.DataTypes,
SourceVersion = 8,
SourceFolderName = "DataTypes",
TargetFolderName = "DataTypes")]
internal class DataTypeMigrationHandler : SharedDataTypeHandler, ISyncMigrationHandler
internal class DataTypeMigrationHandler : SharedDataTypeHandler, ISyncMigrationHandler, ISyncMigrationValidator
{
private readonly SyncPropertyMigratorCollection _migrators;

public DataTypeMigrationHandler(
IEventAggregator eventAggregator,
ISyncMigrationFileService migrationFileService,
IDataTypeService dataTypeService,
ILogger<DataTypeMigrationHandler> logger)
ILogger<DataTypeMigrationHandler> logger,
SyncPropertyMigratorCollection migrators)
: base(eventAggregator, migrationFileService, dataTypeService, logger)
{ }
{
_migrators = migrators;
}

protected override string GetEditorAlias(XElement source)
=> source.Element("Info")?.Element("EditorAlias").ValueOrDefault(string.Empty) ?? string.Empty;

string GetName(XElement source)
=> source.Element("Info")?.Element("Name").ValueOrDefault(string.Empty) ?? string.Empty;

protected override string GetDatabaseType(XElement source)
=> source.Element("Info")?.Element("DatabaseType").ValueOrDefault(string.Empty) ?? string.Empty;

Expand Down Expand Up @@ -79,4 +91,47 @@ protected override string GetNewDatabaseType(ISyncPropertyMigrator? migrator, Sy
protected override object? GetDataTypeConfig(ISyncPropertyMigrator? migrator, SyncMigrationDataTypeProperty dataTypeProperty, SyncMigrationContext context)
=> migrator?.GetConfigValues(dataTypeProperty, context) ?? MakeEmptyLabelConfig(dataTypeProperty);

public IEnumerable<MigrationMessage> Validate(SyncValidationContext validationContext)
{
var messages = new List<MigrationMessage>();

var dataTypes = Path.Combine(validationContext.Options.Source, "DataTypes");
var migrators = _migrators.GetPreferredMigratorList(validationContext.Options.PreferredMigrators);

foreach (var file in Directory.GetFiles(dataTypes, "*.config", SearchOption.AllDirectories))
{
try
{
var source = XElement.Load(file);
var alias = source.GetAlias();
var key = source.GetKey();
var editorAlias = GetEditorAlias(source);

var name = GetName(source);
var databaseType = GetDatabaseType(source);

if (key == Guid.Empty) throw new Exception("Missing Key value");
if (string.IsNullOrEmpty(editorAlias)) throw new Exception("Id (EditorAlias) value");
if (string.IsNullOrEmpty(name)) throw new Exception("Missing Name value");
if (string.IsNullOrEmpty(databaseType)) throw new Exception("Missing database type");

if (!migrators.Any(x => x.EditorAlias.InvariantEquals(editorAlias)))
{
messages.Add(new MigrationMessage(ItemType, name, MigrationMessageType.Warning)
{
Message = $"there is no migrator for {editorAlias} value will be untouched but might not import correctly"
});
}
}
catch (Exception ex)
{
messages.Add(new MigrationMessage(ItemType, Path.GetFileName(file), MigrationMessageType.Error)
{
Message = $"The Datatype file seems to be corrupt or missing something {ex.Message}"
});
}
}

return messages;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ public IEnumerable<MigrationMessage> Validate(SyncValidationContext validationCo
if (results.Count == 0)
{
return new MigrationMessage("Migrators", "Migrators OK", MigrationMessageType.Success)
{
Message = "there are non duplicate property migrators"
}
.AsEnumerableOfOne();
}
else
Expand Down
56 changes: 56 additions & 0 deletions uSync.Migrations.Core/Validation/XMLValidator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

using Umbraco.Extensions;

using uSync.BackOffice.Services;
using uSync.Migrations.Core.Context;
using uSync.Migrations.Core.Models;
using uSync.Migrations.Core.Services;

namespace uSync.Migrations.Core.Validation;
internal class XMLValidator : ISyncMigrationValidator
{
private readonly SyncFileService _fileService;

public XMLValidator(SyncFileService fileService)
{
_fileService = fileService;
}

public IEnumerable<MigrationMessage> Validate(SyncValidationContext validationContext)
{
var folder = _fileService.GetAbsPath(validationContext.Metadata.SourceFolder);
var files = Directory.GetFiles(folder, "*.config", SearchOption.AllDirectories);

var errors = new List<MigrationMessage>();

foreach (var file in files)
{
try
{
var node = XElement.Load(file);
}
catch
{
var message = new MigrationMessage("XML", "XML Validation", MigrationMessageType.Error);
var relativeFileName = file.Substring(validationContext.Metadata.SourceFolder.Length);
message.Message += $"Failed: {relativeFileName} is not valid xml";
errors.Add(message);
}
}

if (errors.Count > 0)
return errors;

return new MigrationMessage("XML", "XML Validation", MigrationMessageType.Success)
{
Message = "All uSync files in the source folder appear to be valid xml"
}.AsEnumerableOfOne();

}
}

0 comments on commit df715f0

Please sign in to comment.