Example #1
0
 private static void ValidateData(ContentDataCommand command, AddValidation e)
 {
     if (command.Data == null)
     {
         e(Not.Defined("Data"), nameof(command.Data));
     }
 }
Example #2
0
 private static void ValidateData(ContentDataCommand command, AddValidation e)
 {
     if (command.Data == null)
     {
         e("Data is required.", nameof(command.Data));
     }
 }
Example #3
0
        private async Task ValidateAsync(ContentDataCommand command, Func <string> message, bool enrich = false)
        {
            Guard.Valid(command, nameof(command), message);

            var taskForApp    = appProvider.FindAppByIdAsync(command.AppId.Id);
            var taskForSchema = schemas.FindSchemaByIdAsync(command.SchemaId.Id);

            await Task.WhenAll(taskForApp, taskForSchema);

            var languages = new HashSet <Language>(taskForApp.Result.Languages);

            var schemaObject = taskForSchema.Result.Schema;
            var schemaErrors = new List <ValidationError>();

            await command.Data.ValidateAsync(schemaObject, languages, schemaErrors);

            if (schemaErrors.Count > 0)
            {
                throw new ValidationException(message(), schemaErrors);
            }

            if (enrich)
            {
                command.Data.Enrich(schemaObject, languages);
            }
        }
Example #4
0
        private async Task <object> UpdateAsync(ContentDataCommand c, Func <NamedContentData, NamedContentData> newDataFunc, bool partial)
        {
            var isProposal = c.AsDraft && Snapshot.Status == Status.Published;

            var currentData =
                isProposal ?
                Snapshot.DataDraft :
                Snapshot.Data;

            var newData = newDataFunc(currentData);

            if (!currentData.Equals(newData))
            {
                var ctx = await CreateContext(Snapshot.AppId.Id, Snapshot.SchemaId.Id, Snapshot.Id, () => "Failed to update content.");

                if (partial)
                {
                    await ctx.ValidatePartialAsync(c.Data);
                }
                else
                {
                    await ctx.ValidateAsync(c.Data);
                }

                newData = await ctx.ExecuteScriptAndTransformAsync(s => s.Update, "Update", c, newData, Snapshot.Data);

                if (isProposal)
                {
                    ProposeUpdate(c, newData);
                }
                else
                {
                    Update(c, newData);
                }
            }

            return(Snapshot);
        }
        private async Task ValidateAsync(ContentDataCommand command, Func <string> message, bool enrich = false)
        {
            Guard.Valid(command, nameof(command), message);

            var taskForApp    = appProvider.FindAppByIdAsync(command.AppId.Id);
            var taskForSchema = schemas.FindSchemaByIdAsync(command.SchemaId.Id);

            await Task.WhenAll(taskForApp, taskForSchema);

            var schemaObject = taskForSchema.Result.Schema;
            var schemaErrors = new List <ValidationError>();

            var appId = command.AppId.Id;

            var validationContext =
                new ValidationContext(
                    (contentIds, schemaId) =>
            {
                return(contentRepository.QueryNotFoundAsync(appId, schemaId, contentIds.ToList()));
            },
                    assetIds =>
            {
                return(assetRepository.QueryNotFoundAsync(appId, assetIds.ToList()));
            });

            await command.Data.ValidateAsync(validationContext, schemaObject, taskForApp.Result.PartitionResolver, schemaErrors);

            if (schemaErrors.Count > 0)
            {
                throw new ValidationException(message(), schemaErrors);
            }

            if (enrich)
            {
                command.Data.Enrich(schemaObject, taskForApp.Result.PartitionResolver);
            }
        }