public static void CanAdd(Schema schema, AddField command) { Guard.NotNull(command, nameof(command)); Validate.It(() => "Cannot add a new field.", e => { if (!command.Name.IsPropertyName()) { e("Name must be a valid javascript property name.", nameof(command.Name)); } if (command.Properties == null) { e(Not.Defined("Properties"), nameof(command.Properties)); } else { var errors = FieldPropertiesValidator.Validate(command.Properties); errors.Foreach(x => x.WithPrefix(nameof(command.Properties)).AddTo(e)); } if (command.ParentFieldId.HasValue) { var arrayField = GuardHelper.GetArrayFieldOrThrow(schema, command.ParentFieldId.Value, false); if (arrayField.FieldsByName.ContainsKey(command.Name)) { e("A field with the same name already exists."); } } else { if (command.ParentFieldId == null && !command.Partitioning.IsValidPartitioning()) { e(Not.Valid("Partitioning"), nameof(command.Partitioning)); } if (schema.FieldsByName.ContainsKey(command.Name)) { e("A field with the same name already exists."); } } }); }
public static void CanAdd(AddField command, Schema schema) { Guard.NotNull(command, nameof(command)); Validate.It(e => { if (!command.Name.IsPropertyName()) { e(Not.ValidJavascriptName(nameof(command.Name)), nameof(command.Name)); } if (command.Properties == null) { e(Not.Defined(nameof(command.Properties)), nameof(command.Properties)); } else { var errors = FieldPropertiesValidator.Validate(command.Properties); errors.Foreach((x, _) => x.WithPrefix(nameof(command.Properties)).AddTo(e)); } if (command.ParentFieldId.HasValue) { var arrayField = GuardHelper.GetArrayFieldOrThrow(schema, command.ParentFieldId.Value, false); if (arrayField.FieldsByName.ContainsKey(command.Name)) { e(T.Get("schemas.fieldNameAlreadyExists")); } } else { if (command.ParentFieldId == null && !command.Partitioning.IsValidPartitioning()) { e(Not.Valid("Partitioning"), nameof(command.Partitioning)); } if (schema.FieldsByName.ContainsKey(command.Name)) { e(T.Get("schemas.fieldNameAlreadyExists")); } } }); }
private static void ValidateField(UpsertSchemaFieldBase field, string prefix, AddValidation e) { if (!field.Name.IsPropertyName()) { e("Field name must be a valid javascript property name.", $"{prefix}.{nameof(field.Name)}"); } if (field.Properties == null) { e(Not.Defined("Field properties"), $"{prefix}.{nameof(field.Properties)}"); } else { var errors = FieldPropertiesValidator.Validate(field.Properties); errors.Foreach(x => x.WithPrefix($"{prefix}.{nameof(field.Properties)}").AddTo(e)); } }
public static void CanUpdate(Schema schema, UpdateField command) { Guard.NotNull(command, nameof(command)); var field = GuardHelper.GetFieldOrThrow(schema, command.FieldId, command.ParentFieldId, false); Validate.It(() => "Cannot update field.", e => { if (command.Properties == null) { e(Not.Defined("Properties"), nameof(command.Properties)); } else { var errors = FieldPropertiesValidator.Validate(command.Properties); errors.Foreach(x => x.WithPrefix(nameof(command.Properties)).AddTo(e)); } }); }
public static Task CanCreate(CreateSchema command, IAppProvider appProvider) { Guard.NotNull(command, nameof(command)); return(Validate.It(() => "Cannot create schema.", async error => { if (!command.Name.IsSlug()) { error(new ValidationError("Name must be a valid slug.", nameof(command.Name))); } if (await appProvider.GetSchemaAsync(command.AppId.Id, command.Name) != null) { error(new ValidationError($"A schema with name '{command.Name}' already exists", nameof(command.Name))); } if (command.Fields?.Count > 0) { var index = 0; foreach (var field in command.Fields) { index++; var prefix = $"Fields.{index}"; if (!field.Partitioning.IsValidPartitioning()) { error(new ValidationError("Partitioning is not valid.", $"{prefix}.{nameof(field.Partitioning)}")); } if (!field.Name.IsPropertyName()) { error(new ValidationError("Name must be a valid property name.", $"{prefix}.{nameof(field.Name)}")); } if (field.Properties == null) { error(new ValidationError("Properties is required.", $"{prefix}.{nameof(field.Properties)}")); } else { var errors = FieldPropertiesValidator.Validate(field.Properties); foreach (var e in errors) { error(e.WithPrefix(prefix)); } } if (field.Nested?.Count > 0) { if (!(field.Properties is ArrayFieldProperties)) { error(new ValidationError("Only array fields can have nested fields.", $"{prefix}.{nameof(field.Partitioning)}")); } else { var nestedIndex = 0; foreach (var nestedField in field.Nested) { nestedIndex++; var nestedPrefix = $"Fields.{index}.Nested.{nestedIndex}"; if (!nestedField.Name.IsPropertyName()) { error(new ValidationError("Name must be a valid property name.", $"{prefix}.{nameof(nestedField.Name)}")); } if (nestedField.Properties == null) { error(new ValidationError("Properties is required.", $"{prefix}.{nameof(nestedField.Properties)}")); } else { var errors = FieldPropertiesValidator.Validate(nestedField.Properties); foreach (var e in errors) { error(e.WithPrefix(nestedPrefix)); } } } } if (field.Nested.Select(x => x.Name).Distinct().Count() != field.Nested.Count) { error(new ValidationError("Fields cannot have duplicate names.", $"{prefix}.Nested")); } } } if (command.Fields.Select(x => x.Name).Distinct().Count() != command.Fields.Count) { error(new ValidationError("Fields cannot have duplicate names.", nameof(command.Fields))); } } })); }