Browse Source

Extracted schema validation.

pull/141/head
Sebastian Stehle 8 years ago
parent
commit
eba11c469d
  1. 9
      src/Squidex.Domain.Apps.Core/Schemas/AssetsFieldProperties.cs
  2. 9
      src/Squidex.Domain.Apps.Core/Schemas/BooleanFieldProperties.cs
  3. 1
      src/Squidex.Domain.Apps.Core/Schemas/DateTimeField.cs
  4. 37
      src/Squidex.Domain.Apps.Core/Schemas/DateTimeFieldProperties.cs
  5. 27
      src/Squidex.Domain.Apps.Core/Schemas/Field.cs
  6. 14
      src/Squidex.Domain.Apps.Core/Schemas/FieldProperties.cs
  7. 2
      src/Squidex.Domain.Apps.Core/Schemas/Field{T}.cs
  8. 9
      src/Squidex.Domain.Apps.Core/Schemas/GeolocationFieldProperties.cs
  9. 6
      src/Squidex.Domain.Apps.Core/Schemas/JsonFieldProperties.cs
  10. 37
      src/Squidex.Domain.Apps.Core/Schemas/NumberFieldProperties.cs
  11. 9
      src/Squidex.Domain.Apps.Core/Schemas/ReferencesFieldProperties.cs
  12. 36
      src/Squidex.Domain.Apps.Core/Schemas/Schema.cs
  13. 32
      src/Squidex.Domain.Apps.Core/Schemas/StringFieldProperties.cs

9
src/Squidex.Domain.Apps.Core/Schemas/AssetsFieldProperties.cs

@ -6,7 +6,6 @@
// All rights reserved.
// ==========================================================================
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
using Squidex.Infrastructure;
@ -50,13 +49,5 @@ namespace Squidex.Domain.Apps.Core.Schemas
{
return new JArray();
}
protected override IEnumerable<ValidationError> ValidateCore()
{
if (MaxItems.HasValue && MinItems.HasValue && MinItems.Value >= MaxItems.Value)
{
yield return new ValidationError("Max items must be greater than min items", nameof(MinItems), nameof(MaxItems));
}
}
}
}

9
src/Squidex.Domain.Apps.Core/Schemas/BooleanFieldProperties.cs

@ -6,7 +6,6 @@
// All rights reserved.
// ==========================================================================
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
using Squidex.Infrastructure;
@ -50,13 +49,5 @@ namespace Squidex.Domain.Apps.Core.Schemas
{
return DefaultValue;
}
protected override IEnumerable<ValidationError> ValidateCore()
{
if (!Editor.IsEnumValue())
{
yield return new ValidationError("Editor is not a valid value", nameof(Editor));
}
}
}
}

1
src/Squidex.Domain.Apps.Core/Schemas/DateTimeField.cs

@ -14,7 +14,6 @@ using NJsonSchema;
using NodaTime;
using NodaTime.Text;
using Squidex.Domain.Apps.Core.Schemas.Validators;
using Squidex.Infrastructure;
namespace Squidex.Domain.Apps.Core.Schemas
{

37
src/Squidex.Domain.Apps.Core/Schemas/DateTimeFieldProperties.cs

@ -7,7 +7,6 @@
// ==========================================================================
using System;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
using NodaTime;
using Squidex.Infrastructure;
@ -108,41 +107,5 @@ namespace Squidex.Domain.Apps.Core.Schemas
return DefaultValue?.ToString();
}
}
protected override IEnumerable<ValidationError> ValidateCore()
{
if (!Editor.IsEnumValue())
{
yield return new ValidationError("Editor is not a valid value", nameof(Editor));
}
if (MaxValue.HasValue && MinValue.HasValue && MinValue.Value >= MaxValue.Value)
{
yield return new ValidationError("Max value must be greater than min value", nameof(MinValue), nameof(MaxValue));
}
if (DefaultValue.HasValue && MinValue.HasValue && DefaultValue.Value < MinValue.Value)
{
yield return new ValidationError("Default value must be greater than min value", nameof(DefaultValue));
}
if (DefaultValue.HasValue && MaxValue.HasValue && DefaultValue.Value > MaxValue.Value)
{
yield return new ValidationError("Default value must be less than max value", nameof(DefaultValue));
}
if (CalculatedDefaultValue.HasValue)
{
if (!CalculatedDefaultValue.Value.IsEnumValue())
{
yield return new ValidationError("Calculated default value is not valid", nameof(CalculatedDefaultValue));
}
if (DefaultValue.HasValue)
{
yield return new ValidationError("Calculated default value and default value cannot be used together", nameof(CalculatedDefaultValue), nameof(DefaultValue));
}
}
}
}
}

27
src/Squidex.Domain.Apps.Core/Schemas/Field.cs

@ -107,36 +107,13 @@ namespace Squidex.Domain.Apps.Core.Schemas
}
public Field Update(FieldProperties newProperties)
{
ThrowIfLocked();
return UpdateInternal(newProperties);
}
public Field Rename(string newName)
{
ThrowIfLocked();
ThrowIfSameName(newName);
return Clone<Field>(clone => clone.fieldName = newName);
}
private void ThrowIfLocked()
{
if (isLocked)
{
throw new DomainException($"Field {fieldId} is locked.");
throw new InvalidOperationException($"Field {fieldId} is locked.");
}
}
private void ThrowIfSameName(string newName)
{
if (!newName.IsSlug())
{
var error = new ValidationError("Name must be a valid slug", "Name");
throw new ValidationException($"Cannot rename the field '{fieldName}' ({fieldId})", error);
}
return UpdateInternal(newProperties);
}
public void AddToEdmType(EdmStructuredType edmType, PartitionResolver partitionResolver, string schemaName, Func<EdmComplexType, EdmComplexType> typeResolver)

14
src/Squidex.Domain.Apps.Core/Schemas/FieldProperties.cs

@ -6,14 +6,12 @@
// All rights reserved.
// ==========================================================================
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
using Squidex.Infrastructure;
using Squidex.Infrastructure.Json;
namespace Squidex.Domain.Apps.Core.Schemas
{
public abstract class FieldProperties : NamedElementPropertiesBase, IValidatable
public abstract class FieldProperties : NamedElementPropertiesBase
{
private bool isRequired;
private bool isListField;
@ -67,15 +65,5 @@ namespace Squidex.Domain.Apps.Core.Schemas
{
return value.IsNull();
}
public void Validate(IList<ValidationError> errors)
{
foreach (var error in ValidateCore())
{
errors.Add(error);
}
}
protected abstract IEnumerable<ValidationError> ValidateCore();
}
}

2
src/Squidex.Domain.Apps.Core/Schemas/Field{T}.cs

@ -50,8 +50,6 @@ namespace Squidex.Domain.Apps.Core.Schemas
throw new ArgumentException($"Properties must be of type '{typeof(T)}", nameof(newProperties));
}
newProperties.Validate(() => $"Cannot update field with id '{Id}', because the settings are invalid.");
return typedProperties;
}
}

9
src/Squidex.Domain.Apps.Core/Schemas/GeolocationFieldProperties.cs

@ -6,7 +6,6 @@
// All rights reserved.
// ==========================================================================
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
using Squidex.Infrastructure;
@ -35,13 +34,5 @@ namespace Squidex.Domain.Apps.Core.Schemas
{
return null;
}
protected override IEnumerable<ValidationError> ValidateCore()
{
if (!Editor.IsEnumValue())
{
yield return new ValidationError("Editor is not a valid value", nameof(Editor));
}
}
}
}

6
src/Squidex.Domain.Apps.Core/Schemas/JsonFieldProperties.cs

@ -6,7 +6,6 @@
// All rights reserved.
// ==========================================================================
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
using Squidex.Infrastructure;
@ -19,10 +18,5 @@ namespace Squidex.Domain.Apps.Core.Schemas
{
return JValue.CreateNull();
}
protected override IEnumerable<ValidationError> ValidateCore()
{
yield break;
}
}
}

37
src/Squidex.Domain.Apps.Core/Schemas/NumberFieldProperties.cs

@ -6,7 +6,6 @@
// All rights reserved.
// ==========================================================================
using System.Collections.Generic;
using System.Collections.Immutable;
using Newtonsoft.Json.Linq;
using Squidex.Infrastructure;
@ -96,41 +95,5 @@ namespace Squidex.Domain.Apps.Core.Schemas
{
return DefaultValue;
}
protected override IEnumerable<ValidationError> ValidateCore()
{
if (!Editor.IsEnumValue())
{
yield return new ValidationError("Editor is not a valid value", nameof(Editor));
}
if ((Editor == NumberFieldEditor.Radio || Editor == NumberFieldEditor.Dropdown) && (AllowedValues == null || AllowedValues.Count == 0))
{
yield return new ValidationError("Radio buttons or dropdown list need allowed values", nameof(AllowedValues));
}
if (MaxValue.HasValue && MinValue.HasValue && MinValue.Value >= MaxValue.Value)
{
yield return new ValidationError("Max value must be greater than min value", nameof(MinValue), nameof(MaxValue));
}
if (DefaultValue.HasValue && MinValue.HasValue && DefaultValue.Value < MinValue.Value)
{
yield return new ValidationError("Default value must be greater than min value", nameof(DefaultValue));
}
if (DefaultValue.HasValue && MaxValue.HasValue && DefaultValue.Value > MaxValue.Value)
{
yield return new ValidationError("Default value must be less than max value", nameof(DefaultValue));
}
if (AllowedValues != null && AllowedValues.Count > 0 && (MinValue.HasValue || MaxValue.HasValue))
{
yield return new ValidationError("Either allowed values or min and max value can be defined",
nameof(AllowedValues),
nameof(MinValue),
nameof(MaxValue));
}
}
}
}

9
src/Squidex.Domain.Apps.Core/Schemas/ReferencesFieldProperties.cs

@ -7,7 +7,6 @@
// ==========================================================================
using System;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
using Squidex.Infrastructure;
@ -66,13 +65,5 @@ namespace Squidex.Domain.Apps.Core.Schemas
{
return new JArray();
}
protected override IEnumerable<ValidationError> ValidateCore()
{
if (MaxItems.HasValue && MinItems.HasValue && MinItems.Value >= MaxItems.Value)
{
yield return new ValidationError("Max items must be greater than min items", nameof(MinItems), nameof(MaxItems));
}
}
}
}

36
src/Squidex.Domain.Apps.Core/Schemas/Schema.cs

@ -59,7 +59,7 @@ namespace Squidex.Domain.Apps.Core.Schemas
{
Guard.NotNull(fields, nameof(fields));
Guard.NotNull(properties, nameof(properties));
Guard.ValidSlug(name, nameof(name));
Guard.NotNullOrEmpty(name, nameof(name));
fieldsById = fields.ToImmutableDictionary(x => x.Id);
fieldsByName = fields.ToImmutableDictionary(x => x.Name, StringComparer.OrdinalIgnoreCase);
@ -76,13 +76,6 @@ namespace Squidex.Domain.Apps.Core.Schemas
public static Schema Create(string name, SchemaProperties newProperties)
{
if (!name.IsSlug())
{
var error = new ValidationError("Name must be a valid slug", "Name");
throw new ValidationException("Cannot create a new schema", error);
}
return new Schema(name, false, newProperties, ImmutableList<Field>.Empty);
}
@ -128,36 +121,21 @@ namespace Squidex.Domain.Apps.Core.Schemas
return UpdateField(fieldId, field => field.Rename(newName));
}
public Schema DeleteField(long fieldId)
{
if (fieldsById.TryGetValue(fieldId, out var field) && field.IsLocked)
{
throw new DomainException($"Field {fieldId} is locked.");
}
return new Schema(name, isPublished, properties, fields.Where(x => x.Id != fieldId).ToImmutableList());
}
public Schema Publish()
{
if (isPublished)
{
throw new DomainException("Schema is already published");
}
return new Schema(name, true, properties, fields);
}
public Schema Unpublish()
{
if (!isPublished)
{
throw new DomainException("Schema is not published");
}
return new Schema(name, false, properties, fields);
}
public Schema DeleteField(long fieldId)
{
return new Schema(name, isPublished, properties, fields.Where(x => x.Id != fieldId).ToImmutableList());
}
public Schema ReorderFields(List<long> ids)
{
Guard.NotNull(ids, nameof(ids));
@ -192,7 +170,7 @@ namespace Squidex.Domain.Apps.Core.Schemas
if (fieldsById.Values.Any(f => f.Name == field.Name && f.Id != field.Id))
{
throw new ValidationException($"A field with name '{field.Name}' already exists.");
throw new ArgumentException($"A field with name '{field.Name}' already exists.", nameof(field));
}
ImmutableList<Field> newFields;

32
src/Squidex.Domain.Apps.Core/Schemas/StringFieldProperties.cs

@ -6,7 +6,6 @@
// All rights reserved.
// ==========================================================================
using System.Collections.Generic;
using System.Collections.Immutable;
using Newtonsoft.Json.Linq;
using Squidex.Infrastructure;
@ -132,36 +131,5 @@ namespace Squidex.Domain.Apps.Core.Schemas
{
return value.IsNull() || (value is JValue jValue && Equals(jValue.Value, string.Empty));
}
protected override IEnumerable<ValidationError> ValidateCore()
{
if (!Editor.IsEnumValue())
{
yield return new ValidationError("Editor is not a valid value", nameof(Editor));
}
if ((Editor == StringFieldEditor.Radio || Editor == StringFieldEditor.Dropdown) && (AllowedValues == null || AllowedValues.Count == 0))
{
yield return new ValidationError("Radio buttons or dropdown list need allowed values", nameof(AllowedValues));
}
if (MaxLength.HasValue && MinLength.HasValue && MinLength.Value >= MaxLength.Value)
{
yield return new ValidationError("Max length must be greater than min length", nameof(MinLength), nameof(MaxLength));
}
if (Pattern != null && !Pattern.IsValidRegex())
{
yield return new ValidationError("Pattern is not a valid expression", nameof(Pattern));
}
if (AllowedValues != null && AllowedValues.Count > 0 && (MinLength.HasValue || MaxLength.HasValue))
{
yield return new ValidationError("Either allowed values or min and max length can be defined",
nameof(AllowedValues),
nameof(MinLength),
nameof(MaxLength));
}
}
}
}

Loading…
Cancel
Save