|
|
|
@ -6,6 +6,7 @@ |
|
|
|
// All rights reserved.
|
|
|
|
// ==========================================================================
|
|
|
|
|
|
|
|
using System; |
|
|
|
using System.Collections.Generic; |
|
|
|
using Squidex.Domain.Apps.Core.Schemas; |
|
|
|
using Squidex.Infrastructure; |
|
|
|
@ -14,11 +15,11 @@ namespace Squidex.Domain.Apps.Write.Schemas.Guards |
|
|
|
{ |
|
|
|
public static class SchemaFieldGuard |
|
|
|
{ |
|
|
|
public static void GuardValidSchemaFieldName(string name) |
|
|
|
public static void GuardCanAdd(Schema schema, string name) |
|
|
|
{ |
|
|
|
if (!name.IsSlug()) |
|
|
|
{ |
|
|
|
var error = new ValidationError("Name must be a valid slug", "Name"); |
|
|
|
var error = new ValidationError("Name must be a valid slug.", "Name"); |
|
|
|
|
|
|
|
throw new ValidationException("Cannot add a new field.", error); |
|
|
|
} |
|
|
|
@ -102,7 +103,7 @@ namespace Squidex.Domain.Apps.Write.Schemas.Guards |
|
|
|
{ |
|
|
|
if (a.MaxItems.HasValue && a.MinItems.HasValue && a.MinItems.Value >= a.MaxItems.Value) |
|
|
|
{ |
|
|
|
yield return new ValidationError("Max items must be greater than min items", |
|
|
|
yield return new ValidationError("Max items must be greater than min items.", |
|
|
|
nameof(a.MinItems), |
|
|
|
nameof(a.MaxItems)); |
|
|
|
} |
|
|
|
@ -114,7 +115,7 @@ namespace Squidex.Domain.Apps.Write.Schemas.Guards |
|
|
|
{ |
|
|
|
if (!b.Editor.IsEnumValue()) |
|
|
|
{ |
|
|
|
yield return new ValidationError("Editor is not a valid value", |
|
|
|
yield return new ValidationError("Editor is not a valid value.", |
|
|
|
nameof(b.Editor)); |
|
|
|
} |
|
|
|
|
|
|
|
@ -125,7 +126,7 @@ namespace Squidex.Domain.Apps.Write.Schemas.Guards |
|
|
|
{ |
|
|
|
if (!g.Editor.IsEnumValue()) |
|
|
|
{ |
|
|
|
yield return new ValidationError("Editor is not a valid value", |
|
|
|
yield return new ValidationError("Editor is not a valid value.", |
|
|
|
nameof(g.Editor)); |
|
|
|
} |
|
|
|
|
|
|
|
@ -136,7 +137,7 @@ namespace Squidex.Domain.Apps.Write.Schemas.Guards |
|
|
|
{ |
|
|
|
if (r.MaxItems.HasValue && r.MinItems.HasValue && r.MinItems.Value >= r.MaxItems.Value) |
|
|
|
{ |
|
|
|
yield return new ValidationError("Max items must be greater than min items", |
|
|
|
yield return new ValidationError("Max items must be greater than min items.", |
|
|
|
nameof(r.MinItems), |
|
|
|
nameof(r.MaxItems)); |
|
|
|
} |
|
|
|
@ -148,25 +149,25 @@ namespace Squidex.Domain.Apps.Write.Schemas.Guards |
|
|
|
{ |
|
|
|
if (!d.Editor.IsEnumValue()) |
|
|
|
{ |
|
|
|
yield return new ValidationError("Editor is not a valid value", |
|
|
|
yield return new ValidationError("Editor is not a valid value.", |
|
|
|
nameof(d.Editor)); |
|
|
|
} |
|
|
|
|
|
|
|
if (d.DefaultValue.HasValue && d.MinValue.HasValue && d.DefaultValue.Value < d.MinValue.Value) |
|
|
|
{ |
|
|
|
yield return new ValidationError("Default value must be greater than min value", |
|
|
|
yield return new ValidationError("Default value must be greater than min value.", |
|
|
|
nameof(d.DefaultValue)); |
|
|
|
} |
|
|
|
|
|
|
|
if (d.DefaultValue.HasValue && d.MaxValue.HasValue && d.DefaultValue.Value > d.MaxValue.Value) |
|
|
|
{ |
|
|
|
yield return new ValidationError("Default value must be less than max value", |
|
|
|
yield return new ValidationError("Default value must be less than max value.", |
|
|
|
nameof(d.DefaultValue)); |
|
|
|
} |
|
|
|
|
|
|
|
if (d.MaxValue.HasValue && d.MinValue.HasValue && d.MinValue.Value >= d.MaxValue.Value) |
|
|
|
{ |
|
|
|
yield return new ValidationError("Max value must be greater than min value", |
|
|
|
yield return new ValidationError("Max value must be greater than min value.", |
|
|
|
nameof(d.MinValue), |
|
|
|
nameof(d.MaxValue)); |
|
|
|
} |
|
|
|
@ -175,13 +176,13 @@ namespace Squidex.Domain.Apps.Write.Schemas.Guards |
|
|
|
{ |
|
|
|
if (!d.CalculatedDefaultValue.Value.IsEnumValue()) |
|
|
|
{ |
|
|
|
yield return new ValidationError("Calculated default value is not valid", |
|
|
|
yield return new ValidationError("Calculated default value is not valid.", |
|
|
|
nameof(d.CalculatedDefaultValue)); |
|
|
|
} |
|
|
|
|
|
|
|
if (d.DefaultValue.HasValue) |
|
|
|
{ |
|
|
|
yield return new ValidationError("Calculated default value and default value cannot be used together", |
|
|
|
yield return new ValidationError("Calculated default value and default value cannot be used together.", |
|
|
|
nameof(d.CalculatedDefaultValue), |
|
|
|
nameof(d.DefaultValue)); |
|
|
|
} |
|
|
|
@ -194,38 +195,38 @@ namespace Squidex.Domain.Apps.Write.Schemas.Guards |
|
|
|
{ |
|
|
|
if (!n.Editor.IsEnumValue()) |
|
|
|
{ |
|
|
|
yield return new ValidationError("Editor is not a valid value", |
|
|
|
yield return new ValidationError("Editor is not a valid value.", |
|
|
|
nameof(n.Editor)); |
|
|
|
} |
|
|
|
|
|
|
|
if ((n.Editor == NumberFieldEditor.Radio || n.Editor == NumberFieldEditor.Dropdown) && (n.AllowedValues == null || n.AllowedValues.Count == 0)) |
|
|
|
{ |
|
|
|
yield return new ValidationError("Radio buttons or dropdown list need allowed values", |
|
|
|
yield return new ValidationError("Radio buttons or dropdown list need allowed values.", |
|
|
|
nameof(n.AllowedValues)); |
|
|
|
} |
|
|
|
|
|
|
|
if (n.DefaultValue.HasValue && n.MinValue.HasValue && n.DefaultValue.Value < n.MinValue.Value) |
|
|
|
{ |
|
|
|
yield return new ValidationError("Default value must be greater than min value", |
|
|
|
yield return new ValidationError("Default value must be greater than min value.", |
|
|
|
nameof(n.DefaultValue)); |
|
|
|
} |
|
|
|
|
|
|
|
if (n.DefaultValue.HasValue && n.MaxValue.HasValue && n.DefaultValue.Value > n.MaxValue.Value) |
|
|
|
{ |
|
|
|
yield return new ValidationError("Default value must be less than max value", |
|
|
|
yield return new ValidationError("Default value must be less than max value.", |
|
|
|
nameof(n.DefaultValue)); |
|
|
|
} |
|
|
|
|
|
|
|
if (n.MaxValue.HasValue && n.MinValue.HasValue && n.MinValue.Value >= n.MaxValue.Value) |
|
|
|
{ |
|
|
|
yield return new ValidationError("Max value must be greater than min value", |
|
|
|
yield return new ValidationError("Max value must be greater than min value.", |
|
|
|
nameof(n.MinValue), |
|
|
|
nameof(n.MaxValue)); |
|
|
|
} |
|
|
|
|
|
|
|
if (n.AllowedValues != null && n.AllowedValues.Count > 0 && (n.MinValue.HasValue || n.MaxValue.HasValue)) |
|
|
|
{ |
|
|
|
yield return new ValidationError("Either allowed values or min and max value can be defined", |
|
|
|
yield return new ValidationError("Either allowed values or min and max value can be defined.", |
|
|
|
nameof(n.AllowedValues), |
|
|
|
nameof(n.MinValue), |
|
|
|
nameof(n.MaxValue)); |
|
|
|
@ -238,32 +239,32 @@ namespace Squidex.Domain.Apps.Write.Schemas.Guards |
|
|
|
{ |
|
|
|
if (!s.Editor.IsEnumValue()) |
|
|
|
{ |
|
|
|
yield return new ValidationError("Editor is not a valid value", |
|
|
|
yield return new ValidationError("Editor is not a valid value.", |
|
|
|
nameof(s.Editor)); |
|
|
|
} |
|
|
|
|
|
|
|
if ((s.Editor == StringFieldEditor.Radio || s.Editor == StringFieldEditor.Dropdown) && (s.AllowedValues == null || s.AllowedValues.Count == 0)) |
|
|
|
{ |
|
|
|
yield return new ValidationError("Radio buttons or dropdown list need allowed values", |
|
|
|
yield return new ValidationError("Radio buttons or dropdown list need allowed values.", |
|
|
|
nameof(s.AllowedValues)); |
|
|
|
} |
|
|
|
|
|
|
|
if (s.Pattern != null && !s.Pattern.IsValidRegex()) |
|
|
|
{ |
|
|
|
yield return new ValidationError("Pattern is not a valid expression", |
|
|
|
yield return new ValidationError("Pattern is not a valid expression.", |
|
|
|
nameof(s.Pattern)); |
|
|
|
} |
|
|
|
|
|
|
|
if (s.MaxLength.HasValue && s.MinLength.HasValue && s.MinLength.Value >= s.MaxLength.Value) |
|
|
|
{ |
|
|
|
yield return new ValidationError("Max length must be greater than min length", |
|
|
|
yield return new ValidationError("Max length must be greater than min length.", |
|
|
|
nameof(s.MinLength), |
|
|
|
nameof(s.MaxLength)); |
|
|
|
} |
|
|
|
|
|
|
|
if (s.AllowedValues != null && s.AllowedValues.Count > 0 && (s.MinLength.HasValue || s.MaxLength.HasValue)) |
|
|
|
{ |
|
|
|
yield return new ValidationError("Either allowed values or min and max length can be defined", |
|
|
|
yield return new ValidationError("Either allowed values or min and max length can be defined.", |
|
|
|
nameof(s.AllowedValues), |
|
|
|
nameof(s.MinLength), |
|
|
|
nameof(s.MaxLength)); |
|
|
|
|