From 913d29274edec445b2b7373cbbc913fc8ebccb9f Mon Sep 17 00:00:00 2001 From: "grzegorz.russek" Date: Thu, 20 Sep 2018 05:35:36 +0000 Subject: [PATCH] --- AmalgamationTool/DynamORM.Amalgamation.cs | 90 +++++++- DynamORM.Tests/DynamORM.Tests.csproj | 24 ++- DynamORM.Tests/Helpers/AttachToDebugger.cs | 17 +- .../Helpers/Dynamic/DynamicParserTests.cs | 32 +-- DynamORM.Tests/Helpers/PoolingTests.cs | 14 +- .../Validation/ObjectValidationTest.cs | 80 +++++++ .../Modify/DynamicModificationTests.cs | 48 ++--- .../DynamicNoSchemaModificationTests.cs | 8 +- .../DynamicTypeSchemaModificationTests.cs | 6 +- DynamORM.Tests/Modify/ParserTests.cs | 24 +-- .../Properties/Resources.Designer.cs | 4 +- DynamORM.Tests/Select/DynamicAccessTests.cs | 168 +++++++-------- .../Select/DynamicNoSchemaAccessTests.cs | 6 +- .../Select/DynamicTypeSchemaAccessTests.cs | 4 +- DynamORM.Tests/Select/LegacyParserTests.cs | 44 ++-- DynamORM.Tests/Select/ParserTests.cs | 128 +++++------ DynamORM.Tests/Select/TypedAccessTests.cs | 203 +++++++++--------- DynamORM/Mapper/DynamicTypeMap.cs | 41 +++- DynamORM/Validation/RequiredAttribute.cs | 51 ++++- 19 files changed, 607 insertions(+), 385 deletions(-) create mode 100644 DynamORM.Tests/Helpers/Validation/ObjectValidationTest.cs diff --git a/AmalgamationTool/DynamORM.Amalgamation.cs b/AmalgamationTool/DynamORM.Amalgamation.cs index 37db94a..dbf2055 100644 --- a/AmalgamationTool/DynamORM.Amalgamation.cs +++ b/AmalgamationTool/DynamORM.Amalgamation.cs @@ -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) - result.AddRange(map.ValidateObject(item)); + + var list = v as IEnumerable; + + if (list == null) + { + var enumerable = v as IEnumerable; + if (enumerable != null) + list = enumerable.Cast(); + } + + 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 { /// Required attribute can be used to validate fields in objects using mapper class. - [AttributeUsage(AttributeTargets.Property)] + [AttributeUsage(AttributeTargets.Property, AllowMultiple = true)] public class RequiredAttribute : Attribute { /// Gets or sets minimum value or length of field. @@ -13559,6 +13593,9 @@ namespace DynamORM /// Gets or sets a value indicating whether property value is required or not. public bool Required { get; set; } + /// Gets or sets a value indicating whether this is an element requirement. + public bool ElementRequirement { get; set; } + /// Initializes a new instance of the class. /// This field will be required. 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).Count(); + int? cnt = null; + + var list = (val as IEnumerable); + if (list != null) + cnt = list.Count(); + else + { + var enumerable = (val as IEnumerable); + if (enumerable != null) + cnt = enumerable.Cast().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; } diff --git a/DynamORM.Tests/DynamORM.Tests.csproj b/DynamORM.Tests/DynamORM.Tests.csproj index 7ebb425..6ee31c5 100644 --- a/DynamORM.Tests/DynamORM.Tests.csproj +++ b/DynamORM.Tests/DynamORM.Tests.csproj @@ -1,5 +1,5 @@  - + Debug AnyCPU @@ -10,7 +10,7 @@ Properties DynamORM.Tests DynamORM.Tests - v4.0 + v4.7.1 512 False @@ -20,6 +20,8 @@ False False OnBuildSuccess + + Full @@ -63,14 +65,8 @@ - - C:\Program Files %28x86%29\NUnit 2.6.3\bin\framework\nunit.framework.dll - - - ..\..\..\Libs\SQLite\Any\System.Data.SQLite.dll - @@ -79,6 +75,7 @@ + @@ -113,6 +110,17 @@ DynamORM + + + 1.2.1 + + + 1.2.1 + + + 1.0.109.1 + +