This commit is contained in:
grzegorz.russek
2018-09-20 05:35:36 +00:00
parent fe36953bd5
commit 913d29274e
19 changed files with 607 additions and 385 deletions

View File

@@ -13432,7 +13432,7 @@ namespace DynamORM
var v = prop.Get(val);
foreach (var r in prop.Requirements)
foreach (var r in prop.Requirements.Where(x => !x.ElementRequirement))
{
var valid = r.ValidateSimpleValue(prop, v);
@@ -13441,8 +13441,42 @@ namespace DynamORM
if (prop.Type.IsArray || prop.IsGnericEnumerable)
{
var map = DynamicMapperCache.GetMapper(prop.ArrayType);
foreach (var item in val as IEnumerable<object>)
result.AddRange(map.ValidateObject(item));
var list = v as IEnumerable<object>;
if (list == null)
{
var enumerable = v as IEnumerable;
if (enumerable != null)
list = enumerable.Cast<object>();
}
if (list != null)
foreach (var item in list)
{
if (prop.Requirements.Any(x => x.ElementRequirement))
{
foreach (var re in prop.Requirements.Where(x => x.ElementRequirement))
{
var validelem = re.ValidateSimpleValue(prop.ArrayType, prop.ArrayType.IsGenericEnumerable(), item);
if (validelem == ValidateResult.NotSupported)
{
result.AddRange(map.ValidateObject(item));
break;
}
else if (validelem != ValidateResult.Valid)
result.Add(new ValidationResult()
{
Property = prop,
Requirement = r,
Value = item,
});
}
}
else
result.AddRange(map.ValidateObject(item));
}
}
continue;
@@ -13544,7 +13578,7 @@ namespace DynamORM
namespace Validation
{
/// <summary>Required attribute can be used to validate fields in objects using mapper class.</summary>
[AttributeUsage(AttributeTargets.Property)]
[AttributeUsage(AttributeTargets.Property, AllowMultiple = true)]
public class RequiredAttribute : Attribute
{
/// <summary>Gets or sets minimum value or length of field.</summary>
@@ -13559,6 +13593,9 @@ namespace DynamORM
/// <summary>Gets or sets a value indicating whether property value is required or not.</summary>
public bool Required { get; set; }
/// <summary>Gets or sets a value indicating whether this is an element requirement.</summary>
public bool ElementRequirement { get; set; }
/// <summary>Initializes a new instance of the <see cref="RequiredAttribute" /> class.</summary>
/// <param name="required">This field will be required.</param>
public RequiredAttribute(bool required = true)
@@ -13605,10 +13642,20 @@ namespace DynamORM
internal ValidateResult ValidateSimpleValue(DynamicPropertyInvoker dpi, object val)
{
if (val == null && Required)
return ValidateResult.ValueIsMissing;
return ValidateSimpleValue(dpi.Type, dpi.IsGnericEnumerable, val);
}
if (dpi.Type.IsValueType)
internal ValidateResult ValidateSimpleValue(Type type, bool isGnericEnumerable, object val)
{
if (val == null)
{
if (Required)
return ValidateResult.ValueIsMissing;
else
return ValidateResult.Valid;
}
if (type.IsValueType)
{
if (val is decimal || val is long || val is int || val is float || val is double || val is short || val is byte ||
val is decimal? || val is long? || val is int? || val is float? || val is double? || val is short? || val is byte?)
@@ -13639,9 +13686,19 @@ namespace DynamORM
return ValidateResult.Valid;
}
}
else if (dpi.Type.IsArray || dpi.IsGnericEnumerable)
else if (type.IsArray || isGnericEnumerable)
{
var cnt = (val as IEnumerable<object>).Count();
int? cnt = null;
var list = (val as IEnumerable<object>);
if (list != null)
cnt = list.Count();
else
{
var enumerable = (val as IEnumerable);
if (enumerable != null)
cnt = enumerable.Cast<object>().Count();
}
if (Min.HasValue && Min.Value > cnt)
return ValidateResult.TooFewElementsInCollection;
@@ -13651,6 +13708,21 @@ namespace DynamORM
return ValidateResult.Valid;
}
else if (type == typeof(string))
{
var str = (string)val;
if (Min.HasValue && Min.Value > str.Length)
return ValidateResult.ValueTooShort;
if (Max.HasValue && Max.Value < str.Length)
return ValidateResult.ValueTooLong;
if (Pattern != null && !Pattern.IsMatch(str))
return ValidateResult.ValueDontMatchPattern;
return ValidateResult.Valid;
}
return ValidateResult.NotSupported;
}