public void ShouldWorkWithStringConstraint() { PropertyInfo lpi = typeof(KnownRules).GetProperty("StrProp", membersBindingFlags); var v = new ValidationDef <KnownRules>(); var expectedMessage = "The StrProp is too long {Max}"; v.Define(x => x.StrProp).MaxLength(10).WithMessage(expectedMessage).And.NotNullable().And.NotEmpty(); IClassMapping cm = ((IMappingSource)v).GetMapping(); var mAttrs = cm.GetMemberAttributes(lpi); Assert.That(mAttrs.Count(), Is.EqualTo(3)); var lengthAttribute = mAttrs.OfType <LengthAttribute>().FirstOrDefault(); Assert.That(lengthAttribute, Is.Not.Null); Assert.That(lengthAttribute.Max, Is.EqualTo(10)); Assert.That(lengthAttribute.Message, Is.EqualTo(expectedMessage)); Assert.That(mAttrs.OfType <NotNullAttribute>().FirstOrDefault(), Is.Not.Null); Assert.That(mAttrs.OfType <NotEmptyAttribute>().FirstOrDefault(), Is.Not.Null); v = new ValidationDef <KnownRules>(); v.Define(x => x.StrProp).NotNullable().And.IsEmail(); cm = ((IMappingSource)v).GetMapping(); mAttrs = cm.GetMemberAttributes(lpi); Assert.That(mAttrs.OfType <EmailAttribute>().FirstOrDefault(), Is.Not.Null); Assert.That(mAttrs.OfType <NotNullAttribute>().FirstOrDefault(), Is.Not.Null); }
public void Loquacious() { var v = new ValidationDef <KnownRules>(); v.Define(x => x.StrProp).IsNumeric(); IClassMapping cm = ((IMappingSource)v).GetMapping(); PropertyInfo lpi = typeof(KnownRules).GetProperty("StrProp"); Assert.That(cm.GetMemberAttributes(lpi).Count(), Is.EqualTo(1)); Assert.That(cm.GetMemberAttributes(lpi).First(), Is.InstanceOf <IsNumericAttribute>()); }
public void ShouldWorkCodiceFiscaleExtensions() { var v = new ValidationDef <Cliente>(); const string expectedMessage = "Codice fiscale non valido"; v.Define(x => x.CodiceFiscale).IsCodiceFiscale().WithMessage(expectedMessage); IClassMapping cm = ((IMappingSource)v).GetMapping(); PropertyInfo pi = typeof(Cliente).GetProperty("CodiceFiscale", membersBindingFlags); Assert.That(cm.GetMemberAttributes(pi).Count(), Is.EqualTo(1)); CodiceFiscaleAttribute first = cm.GetMemberAttributes(pi).OfType <CodiceFiscaleAttribute>().FirstOrDefault(); Assert.That(first, Is.Not.Null); Assert.That(first.Message, Is.EqualTo(expectedMessage)); }
public void ShouldWorkWithCollectionConstraints() { PropertyInfo lpi = typeof(KnownRules).GetProperty("ArrProp", membersBindingFlags); var v = new ValidationDef <KnownRules>(); v.Define(x => x.ArrProp).SizeBetween(1, 9); IClassMapping cm = ((IMappingSource)v).GetMapping(); var mAttrs = cm.GetMemberAttributes(lpi); Assert.That(mAttrs.Count(), Is.EqualTo(1)); v = new ValidationDef <KnownRules>(); v.Define(x => x.ArrProp).NotNullable().And.SizeBetween(1, 9); cm = ((IMappingSource)v).GetMapping(); mAttrs = cm.GetMemberAttributes(lpi); Assert.That(mAttrs.Count(), Is.EqualTo(2)); }
public void ShouldAssignRuleArgsOptions() { PropertyInfo lpi = typeof(KnownRules).GetProperty("DtProp", membersBindingFlags); var v = new ValidationDef <KnownRules>(); string expected = "{validator.past}"; v.Define(x => x.DtProp).IsInThePast(); IClassMapping cm = ((IMappingSource)v).GetMapping(); Assert.That(cm.GetMemberAttributes(lpi).OfType <PastAttribute>().First().Message, Is.EqualTo(expected)); v = new ValidationDef <KnownRules>(); expected = "The date is in the past."; v.Define(x => x.DtProp).IsInThePast().WithMessage(expected); cm = ((IMappingSource)v).GetMapping(); Assert.That(cm.GetMemberAttributes(lpi).OfType <PastAttribute>().First().Message, Is.EqualTo(expected)); }
public void ShouldAddPropertiesValidators() { var v = new ValidationDef <KnownRules>(); v.Define(x => x.DtProp).IsInThePast(); IClassMapping cm = ((IMappingSource)v).GetMapping(); PropertyInfo lpi = typeof(KnownRules).GetProperty("DtProp", membersBindingFlags); Assert.That(cm.GetMemberAttributes(lpi).Count(), Is.EqualTo(1)); Assert.That(cm.GetMemberAttributes(lpi).First(), Is.InstanceOf <PastAttribute>()); var kv = new KnownRulesSimpleValidationDef(); cm = ((IMappingSource)kv).GetMapping(); Assert.That(cm.GetMemberAttributes(lpi).Count(), Is.EqualTo(1)); Assert.That(cm.GetMemberAttributes(lpi).First(), Is.InstanceOf <PastAttribute>()); }
public void ShouldWorkWithGuid() { PropertyInfo lpi = typeof(KnownRules).GetProperty("GuidProp", membersBindingFlags); var v = new ValidationDef <KnownRules>(); v.Define(x => x.GuidProp).NotEmpty(); IClassMapping cm = ((IMappingSource)v).GetMapping(); cm.GetMemberAttributes(lpi).Select(x => x.GetType()).Single().Should().Be.EqualTo <NotNullNotEmptyAttribute>(); }
public void ShouldWorkPartitaIvaExtensions() { const string expectedMessage = "Partita IVA non valida"; var v = new ValidationDef <Cliente>(); v.Define(x => x.Piva).NotNullable().And.IsPartitaIva().WithMessage(expectedMessage); PropertyInfo pi = typeof(Cliente).GetProperty("Piva", membersBindingFlags); IClassMapping cm = ((IMappingSource)v).GetMapping(); Assert.That(cm.GetMemberAttributes(pi).Count(), Is.EqualTo(2)); PartitaIvaAttribute pa = cm.GetMemberAttributes(pi).OfType <PartitaIvaAttribute>().FirstOrDefault(); Assert.That(pa, Is.Not.Null); Assert.That(pa.Message, Is.EqualTo(expectedMessage)); pi = typeof(Cliente).GetProperty("NumPiva", membersBindingFlags); v = new ValidationDef <Cliente>(); v.Define(x => x.NumPiva).IsPartitaIva().WithMessage(expectedMessage); cm = ((IMappingSource)v).GetMapping(); Assert.That(cm.GetMemberAttributes(pi).Count(), Is.EqualTo(1)); pa = cm.GetMemberAttributes(pi).OfType <PartitaIvaAttribute>().FirstOrDefault(); Assert.That(pa, Is.Not.Null); }
protected void MixMembersWith(HashSet<MemberInfo> lmembers, IClassMapping mapping) { foreach (MemberInfo info in mapping.GetMembers()) { lmembers.Add(info); IEnumerable<Attribute> mas = mapping.GetMemberAttributes(info); if (mas != null) { List<Attribute> attrs; if (!membersAttributesDictionary.TryGetValue(info, out attrs)) { membersAttributesDictionary[info] = new List<Attribute>(mas); } else { CombineAttribute(mas, attrs); membersAttributesDictionary[info] = attrs; } } } }
protected void MixMembersWith(HashSet <MemberInfo> lmembers, IClassMapping mapping) { foreach (MemberInfo info in mapping.GetMembers()) { lmembers.Add(info); IEnumerable <Attribute> mas = mapping.GetMemberAttributes(info); if (mas != null) { List <Attribute> attrs; if (!membersAttributesDictionary.TryGetValue(info, out attrs)) { membersAttributesDictionary[info] = new List <Attribute>(mas); } else { CombineAttribute(mas, attrs); membersAttributesDictionary[info] = attrs; } } } }