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

@@ -27,6 +27,7 @@
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
@@ -156,7 +157,7 @@ namespace DynamORM.Mapper
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);
@@ -165,8 +166,42 @@ namespace DynamORM.Mapper
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;

View File

@@ -1,4 +1,5 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
@@ -7,7 +8,7 @@ using DynamORM.Mapper;
namespace DynamORM.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>
@@ -22,6 +23,9 @@ namespace DynamORM.Validation
/// <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)
@@ -68,10 +72,20 @@ namespace DynamORM.Validation
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?)
@@ -102,9 +116,19 @@ namespace DynamORM.Validation
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;
@@ -114,6 +138,21 @@ namespace DynamORM.Validation
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;
}