Browse Source

Fixed usage of data type attribute.

pull/361/head
Sebastian Stehle 7 years ago
parent
commit
b427f03047
  1. 4
      extensions/Squidex.Extensions/Actions/Discourse/DiscourseAction.cs
  2. 4
      extensions/Squidex.Extensions/Actions/Email/EmailAction.cs
  3. 2
      extensions/Squidex.Extensions/Actions/Medium/MediumAction.cs
  4. 13
      src/Squidex.Domain.Apps.Core.Operations/HandleRules/RuleRegistry.cs
  5. 26
      tests/Squidex.Domain.Apps.Core.Tests/Operations/HandleRules/RuleElementRegistryTests.cs

4
extensions/Squidex.Extensions/Actions/Discourse/DiscourseAction.cs

@ -49,11 +49,11 @@ namespace Squidex.Extensions.Actions.Discourse
public string Title { get; set; }
[Display(Name = "Topic", Description = "The optional topic id.")]
[DataType(DataType.Custom)]
[DataType(DataType.Text)]
public int? Topic { get; set; }
[Display(Name = "Category", Description = "The optional category id.")]
[DataType(DataType.Custom)]
[DataType(DataType.Text)]
public int? Category { get; set; }
}
}

4
extensions/Squidex.Extensions/Actions/Email/EmailAction.cs

@ -26,12 +26,12 @@ namespace Squidex.Extensions.Actions.Email
[Required]
[Display(Name = "Server Port", Description = "The port to the SMTP server.")]
[DataType(DataType.Custom)]
[DataType(DataType.Text)]
public int ServerPort { get; set; }
[Required]
[Display(Name = "Use SSL", Description = "Specify whether the SMPT client uses Secure Sockets Layer (SSL) to encrypt the connection.")]
[DataType(DataType.Custom)]
[DataType(DataType.Text)]
public bool ServerUseSsl { get; set; }
[Required]

2
extensions/Squidex.Extensions/Actions/Medium/MediumAction.cs

@ -51,7 +51,7 @@ namespace Squidex.Extensions.Actions.Medium
public string PublicationId { get; set; }
[Display(Name = "Is Html", Description = "Indicates whether the content is markdown or html.")]
[DataType(DataType.Custom)]
[DataType(DataType.Text)]
public bool IsHtml { get; set; }
}
}

13
src/Squidex.Domain.Apps.Core.Operations/HandleRules/RuleRegistry.cs

@ -87,7 +87,7 @@ namespace Squidex.Domain.Apps.Core.HandleRules
var type = property.PropertyType;
if ((property.GetCustomAttribute<RequiredAttribute>() != null || (type.IsValueType && !IsNullable(type))) && type != typeof(bool) && type != typeof(bool?))
if ((GetDataAttribute<RequiredAttribute>(property) != null || (type.IsValueType && !IsNullable(type))) && type != typeof(bool) && type != typeof(bool?))
{
actionProperty.IsRequired = true;
}
@ -97,7 +97,7 @@ namespace Squidex.Domain.Apps.Core.HandleRules
actionProperty.IsFormattable = true;
}
var dataType = property.GetCustomAttribute<DataTypeAttribute>()?.DataType;
var dataType = GetDataAttribute<DataTypeAttribute>(property)?.DataType;
if (type == typeof(bool) || type == typeof(bool?))
{
@ -135,6 +135,15 @@ namespace Squidex.Domain.Apps.Core.HandleRules
actionTypes[name] = definition;
}
private static T GetDataAttribute<T>(PropertyInfo property) where T : ValidationAttribute
{
var result = property.GetCustomAttribute<T>();
result?.IsValid(null);
return result;
}
private static bool IsNullable(Type type)
{
return type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>);

26
tests/Squidex.Domain.Apps.Core.Tests/Operations/HandleRules/RuleElementRegistryTests.cs

@ -27,6 +27,18 @@ namespace Squidex.Domain.Apps.Core.Operations.HandleRules
}
}
[RuleAction(
IconImage = "<svg></svg>",
IconColor = "#1e5470",
Display = "Action display",
Description = "Action description.",
ReadMore = "https://www.readmore.com/")]
public sealed class MyInvalidRuleAction : RuleAction
{
[DataType(DataType.Custom)]
public string Custom { get; set; }
}
[RuleAction(
IconImage = "<svg></svg>",
IconColor = "#1e5470",
@ -53,16 +65,16 @@ namespace Squidex.Domain.Apps.Core.Operations.HandleRules
[DataType(DataType.Password)]
public string Password { get; set; }
[DataType(DataType.Custom)]
[DataType(DataType.Text)]
public bool Boolean { get; set; }
[DataType(DataType.Custom)]
[DataType(DataType.Text)]
public bool? BooleanOptional { get; set; }
[DataType(DataType.Custom)]
[DataType(DataType.Text)]
public int Number { get; set; }
[DataType(DataType.Custom)]
[DataType(DataType.Text)]
public int? NumberOptional { get; set; }
}
@ -167,5 +179,11 @@ namespace Squidex.Domain.Apps.Core.Operations.HandleRules
currentDefinition.Should().BeEquivalentTo(expected);
}
[Fact]
public void Should_throw_exception_if_validation_attribute_used_incorrectly()
{
Assert.Throws<InvalidOperationException>(() => sut.Add<MyInvalidRuleAction>());
}
}
}

Loading…
Cancel
Save