public void Validate() { var configure = new FluentConfiguration(); var validationDef = new ValidationDef <EntityWithDate>(); validationDef.Define(e => e.Value).Satisfy(v => v.Year == DateTime.Today.Year).WithMessage("In this year"); validationDef.Define(e => e.NullValue).Satisfy(v => v.HasValue).WithMessage("HasValue"); configure.Register(validationDef).SetDefaultValidatorMode(ValidatorMode.UseExternal); var ve = new ValidatorEngine(); ve.Configure(configure); Assert.That(ve.IsValid(new EntityWithDate { Value = DateTime.Today, NullValue = DateTime.Today })); Assert.That(!ve.IsValid(new EntityWithDate())); Assert.That(!ve.IsValid(new EntityWithDate { Value = DateTime.Today })); Assert.That(!ve.IsValid(new EntityWithDate { NullValue = DateTime.Today })); }
public void ValidateDecimal() { var configure = new FluentConfiguration(); var validationDef = new ValidationDef <EntityWithFloats <decimal> >(); validationDef.Define(e => e.Value).Satisfy(v => v > 0).WithMessage("Value > 0"); validationDef.Define(e => e.NullValue).Satisfy(v => v.HasValue).WithMessage("HasValue"); configure.Register(validationDef).SetDefaultValidatorMode(ValidatorMode.UseExternal); var ve = new ValidatorEngine(); ve.Configure(configure); Assert.That(ve.IsValid(new EntityWithFloats <decimal> { Value = 1, NullValue = 1 })); Assert.That(!ve.IsValid(new EntityWithFloats <decimal>())); Assert.That(!ve.IsValid(new EntityWithFloats <decimal> { Value = 1 })); Assert.That(!ve.IsValid(new EntityWithFloats <decimal> { NullValue = 1 })); }
public void ValidateChangedPropertyOfProxy() { var validatorConf = new FluentConfiguration(); validatorConf.SetDefaultValidatorMode(ValidatorMode.UseExternal); var vDefSimple = new ValidationDef <SimpleWithRelation>(); vDefSimple.Define(s => s.Name).MatchWith("OK"); vDefSimple.Define(s => s.Relation).IsValid(); validatorConf.Register(vDefSimple); var vDefRelation = new ValidationDef <Relation>(); vDefRelation.Define(s => s.Description).MatchWith("OK"); validatorConf.Register(vDefRelation); var engine = new ValidatorEngine(); engine.Configure(validatorConf); object savedIdRelation; // fill DB using (ISession s = OpenSession()) using (ITransaction tx = s.BeginTransaction()) { var relation = new Relation { Description = "OK" }; savedIdRelation = s.Save(relation); tx.Commit(); } using (ISession s = OpenSession()) { var proxy = s.Load <Relation>(savedIdRelation); proxy.Description = "no"; Assert.AreEqual(1, engine.ValidatePropertyValue(proxy, p => p.Description).Length); Assert.DoesNotThrow(() => engine.ValidatePropertyValue(proxy, p => p.Description)); Assert.AreEqual(1, engine.ValidatePropertyValue(proxy, "Description").Length); Assert.DoesNotThrow(() => engine.ValidatePropertyValue(proxy, "Description")); } CleanDb(); }
public void ValidateInitializedProxyAtDeepLevel() { var validatorConf = new FluentConfiguration(); validatorConf.SetDefaultValidatorMode(ValidatorMode.UseExternal); var vDefSimple = new ValidationDef <SimpleWithRelation>(); vDefSimple.Define(s => s.Name).MatchWith("OK"); vDefSimple.Define(s => s.Relation).IsValid(); validatorConf.Register(vDefSimple); var vDefRelation = new ValidationDef <Relation>(); vDefRelation.Define(s => s.Description).MatchWith("OK"); validatorConf.Register(vDefRelation); var engine = new ValidatorEngine(); engine.Configure(validatorConf); object savedIdRelation; // fill DB using (ISession s = OpenSession()) using (ITransaction tx = s.BeginTransaction()) { var relation = new Relation { Description = "OK" }; savedIdRelation = s.Save(relation); tx.Commit(); } using (ISession s = OpenSession()) { var proxy = s.Load <Relation>(savedIdRelation); NHibernateUtil.Initialize(proxy); Assert.That(engine.IsValid(new SimpleWithRelation { Name = "OK", Relation = proxy })); Assert.DoesNotThrow(() => engine.AssertValid(new SimpleWithRelation { Name = "OK", Relation = proxy })); } CleanDb(); }
public void ValidateWithMultipleConditions() { var configure = new FluentConfiguration(); var validationDef = new ValidationDef<EntityWithString>(); validationDef.Define(e => e.Name) .Satisfy(name => name != null && name.StartsWith("ab")).WithMessage("Name should start with 'ab'") .And .Satisfy(name => name != null && name.EndsWith("zz")).WithMessage("Name should end with 'zz'"); configure.Register(validationDef).SetDefaultValidatorMode(ValidatorMode.UseExternal); var ve = new ValidatorEngine(); ve.Configure(configure); Assert.That(ve.IsValid(new EntityWithString { Name = "abczz" })); Assert.That(!ve.IsValid(new EntityWithString { Name = "bc" })); var iv = ve.Validate(new EntityWithString {Name = "abc"}); Assert.That(iv.Length, Is.EqualTo(1)); Assert.That(iv.Select(i => i.Message).First(), Is.EqualTo("Name should end with 'zz'")); iv = ve.Validate(new EntityWithString { Name = "zz" }); Assert.That(iv.Length, Is.EqualTo(1)); Assert.That(iv.Select(i => i.Message).First(), Is.EqualTo("Name should start with 'ab'")); iv = ve.Validate(new EntityWithString { Name = "bc" }); Assert.That(iv.Length, Is.EqualTo(2)); var messages = iv.Select(i => i.Message); Assert.That(messages, Has.Member("Name should start with 'ab'") & Has.Member("Name should end with 'zz'")); }
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 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 Validate() { var configure = new FluentConfiguration(); var validationDef = new ValidationDef<EntityWithDate>(); validationDef.Define(e => e.Value).Satisfy(v => v.Year == DateTime.Today.Year).WithMessage("In this year"); validationDef.Define(e => e.NullValue).Satisfy(v => v.HasValue).WithMessage("HasValue"); configure.Register(validationDef).SetDefaultValidatorMode(ValidatorMode.UseExternal); var ve = new ValidatorEngine(); ve.Configure(configure); Assert.That(ve.IsValid(new EntityWithDate { Value = DateTime.Today, NullValue = DateTime.Today })); Assert.That(!ve.IsValid(new EntityWithDate())); Assert.That(!ve.IsValid(new EntityWithDate { Value = DateTime.Today })); Assert.That(!ve.IsValid(new EntityWithDate { NullValue = DateTime.Today })); }
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 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 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 ShouldAddValidationDefInstance() { var fc = new FluentConfiguration(); var vd = new ValidationDef <Address>(); vd.Define(x => x.Country).MaxLength(10); fc.Register(vd); var mp = (IMappingsProvider)fc; Assert.That(mp.GetMappings().Any(m => m.EntityType == typeof(Address))); }
public void ValidateChangedPropertyOfProxy() { var validatorConf = new FluentConfiguration(); validatorConf.SetDefaultValidatorMode(ValidatorMode.UseExternal); var vDefSimple = new ValidationDef<SimpleWithRelation>(); vDefSimple.Define(s => s.Name).MatchWith("OK"); vDefSimple.Define(s => s.Relation).IsValid(); validatorConf.Register(vDefSimple); var vDefRelation = new ValidationDef<Relation>(); vDefRelation.Define(s => s.Description).MatchWith("OK"); validatorConf.Register(vDefRelation); var engine = new ValidatorEngine(); engine.Configure(validatorConf); object savedIdRelation; // fill DB using (ISession s = OpenSession()) using (ITransaction tx = s.BeginTransaction()) { var relation = new Relation { Description = "OK" }; savedIdRelation = s.Save(relation); tx.Commit(); } using (ISession s = OpenSession()) { var proxy = s.Load<Relation>(savedIdRelation); proxy.Description = "no"; Assert.AreEqual(1, engine.ValidatePropertyValue(proxy, p => p.Description).Length); Assert.DoesNotThrow(() => engine.ValidatePropertyValue(proxy, p => p.Description)); Assert.AreEqual(1, engine.ValidatePropertyValue(proxy, "Description").Length); Assert.DoesNotThrow(() => engine.ValidatePropertyValue(proxy, "Description")); } CleanDb(); }
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 ValidateHasValidElementsWithProxies() { var validatorConf = new FluentConfiguration(); validatorConf.SetDefaultValidatorMode(ValidatorMode.UseExternal); var vDefSimple = new ValidationDef <SimpleWithCollection>(); vDefSimple.Define(s => s.Relations).HasValidElements(); validatorConf.Register(vDefSimple); var vDefRelation = new ValidationDef <Relation>(); vDefRelation.Define(s => s.Description).MatchWith("OK"); validatorConf.Register(vDefRelation); var engine = new ValidatorEngine(); engine.Configure(validatorConf); object savedIdRelation; // fill DB using (ISession s = OpenSession()) using (ITransaction tx = s.BeginTransaction()) { var relation = new Relation { Description = "OK" }; savedIdRelation = s.Save(relation); tx.Commit(); } using (ISession s = OpenSession()) { var proxy = s.Load <Relation>(savedIdRelation); var simpleWithCol = new SimpleWithCollection(); simpleWithCol.Relations = new List <Relation> { proxy }; Assert.DoesNotThrow(() => engine.Validate(simpleWithCol)); proxy.Description = "No-o-k"; Assert.IsFalse(engine.IsValid(simpleWithCol)); } CleanDb(); }
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 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 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 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 can_validate_legs() { var validationDef = new ValidationDef<Cat>(); validationDef.Define(c => c.Legs).GreaterThanOrEqualTo(2); var vc = new FluentConfiguration(); vc.SetDefaultValidatorMode(ValidatorMode.UseExternal); vc.Register(validationDef); var ve = new ValidatorEngine(); ve.Configure(vc); ve.Validate(new Cat {Legs = 0}).Should().Have.Count.EqualTo(1); ve.Validate(new Cat {Legs = 3}).Should().Be.Empty(); }
public void Validate() { var configure = new FluentConfiguration(); var validationDef = new ValidationDef<EntityWithCollection>(); validationDef.Define(e => e.Value).Satisfy(v => v != null && v.Any(e => e == "something")).WithMessage("Should contain 'something'"); configure.Register(validationDef).SetDefaultValidatorMode(ValidatorMode.UseExternal); var ve = new ValidatorEngine(); ve.Configure(configure); Assert.That(ve.IsValid(new EntityWithCollection { Value = new[]{"b", "something"} })); Assert.That(!ve.IsValid(new EntityWithCollection())); Assert.That(!ve.IsValid(new EntityWithCollection { Value = new[] { "b" } })); }
public void ValidateWithSingleCondition() { var configure = new FluentConfiguration(); var validationDef = new ValidationDef<EntityWithString>(); validationDef.Define(e => e.Name).Satisfy(name => name != null && name.StartsWith("ab")).WithMessage( "Name should start with 'ab'"); configure.Register(validationDef).SetDefaultValidatorMode(ValidatorMode.UseExternal); var ve = new ValidatorEngine(); ve.Configure(configure); Assert.That(ve.IsValid(new EntityWithString {Name = "abc"})); Assert.That(!ve.IsValid(new EntityWithString { Name = "bc" })); }
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 ValidateWithMultipleConditions() { var configure = new FluentConfiguration(); var validationDef = new ValidationDef <EntityWithString>(); validationDef.Define(e => e.Name) .Satisfy(name => name != null && name.StartsWith("ab")).WithMessage("Name should start with 'ab'") .And .Satisfy(name => name != null && name.EndsWith("zz")).WithMessage("Name should end with 'zz'"); configure.Register(validationDef).SetDefaultValidatorMode(ValidatorMode.UseExternal); var ve = new ValidatorEngine(); ve.Configure(configure); Assert.That(ve.IsValid(new EntityWithString { Name = "abczz" })); Assert.That(!ve.IsValid(new EntityWithString { Name = "bc" })); var iv = ve.Validate(new EntityWithString { Name = "abc" }); Assert.That(iv.Length, Is.EqualTo(1)); Assert.That(iv.Select(i => i.Message).First(), Is.EqualTo("Name should end with 'zz'")); iv = ve.Validate(new EntityWithString { Name = "zz" }); Assert.That(iv.Length, Is.EqualTo(1)); Assert.That(iv.Select(i => i.Message).First(), Is.EqualTo("Name should start with 'ab'")); iv = ve.Validate(new EntityWithString { Name = "bc" }); Assert.That(iv.Length, Is.EqualTo(2)); var messages = iv.Select(i => i.Message); Assert.That(messages, Has.Member("Name should start with 'ab'") & Has.Member("Name should end with 'zz'")); }
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); }
public void ValidateHasValidElementsWithProxies() { var validatorConf = new FluentConfiguration(); validatorConf.SetDefaultValidatorMode(ValidatorMode.UseExternal); var vDefSimple = new ValidationDef<SimpleWithCollection>(); vDefSimple.Define(s => s.Relations).HasValidElements(); validatorConf.Register(vDefSimple); var vDefRelation = new ValidationDef<Relation>(); vDefRelation.Define(s => s.Description).MatchWith("OK"); validatorConf.Register(vDefRelation); var engine = new ValidatorEngine(); engine.Configure(validatorConf); object savedIdRelation; // fill DB using (ISession s = OpenSession()) using (ITransaction tx = s.BeginTransaction()) { var relation = new Relation { Description = "OK" }; savedIdRelation = s.Save(relation); tx.Commit(); } using (ISession s = OpenSession()) { var proxy = s.Load<Relation>(savedIdRelation); var simpleWithCol = new SimpleWithCollection(); simpleWithCol.Relations = new List<Relation> {proxy}; Assert.DoesNotThrow(() => engine.Validate(simpleWithCol)); proxy.Description = "No-o-k"; Assert.IsFalse(engine.IsValid(simpleWithCol)); } CleanDb(); }
public void ValidateWithSingleCondition() { var configure = new FluentConfiguration(); var validationDef = new ValidationDef <EntityWithString>(); validationDef.Define(e => e.Name).Satisfy(name => name != null && name.StartsWith("ab")).WithMessage( "Name should start with 'ab'"); configure.Register(validationDef).SetDefaultValidatorMode(ValidatorMode.UseExternal); var ve = new ValidatorEngine(); ve.Configure(configure); Assert.That(ve.IsValid(new EntityWithString { Name = "abc" })); Assert.That(!ve.IsValid(new EntityWithString { Name = "bc" })); }
public void Validate() { var configure = new FluentConfiguration(); var validationDef = new ValidationDef <EntityWithCollection>(); validationDef.Define(e => e.Value).Satisfy(v => v != null && v.Any(e => e == "something")).WithMessage("Should contain 'something'"); configure.Register(validationDef).SetDefaultValidatorMode(ValidatorMode.UseExternal); var ve = new ValidatorEngine(); ve.Configure(configure); Assert.That(ve.IsValid(new EntityWithCollection { Value = new[] { "b", "something" } })); Assert.That(!ve.IsValid(new EntityWithCollection())); Assert.That(!ve.IsValid(new EntityWithCollection { Value = new[] { "b" } })); }
public void can_validate_legs() { var validationDef = new ValidationDef <Cat>(); validationDef.Define(c => c.Legs).GreaterThanOrEqualTo(2); var vc = new FluentConfiguration(); vc.SetDefaultValidatorMode(ValidatorMode.UseExternal); vc.Register(validationDef); var ve = new ValidatorEngine(); ve.Configure(vc); ve.Validate(new Cat { Legs = 0 }).Should().Have.Count.EqualTo(1); ve.Validate(new Cat { Legs = 3 }).Should().Be.Empty(); }
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); }
public void ValidateNotInitializeProxyAtFirstLevel() { var validatorConf = new FluentConfiguration(); validatorConf.SetDefaultValidatorMode(ValidatorMode.UseExternal); var vDefSimple = new ValidationDef <SimpleWithRelation>(); vDefSimple.Define(s => s.Name).MatchWith("OK"); validatorConf.Register(vDefSimple); var engine = new ValidatorEngine(); engine.Configure(validatorConf); object savedId; // fill DB using (ISession s = OpenSession()) using (ITransaction tx = s.BeginTransaction()) { savedId = s.Save(new SimpleWithRelation { Name = "OK" }); tx.Commit(); } using (ISession s = OpenSession()) { var proxy = s.Load <SimpleWithRelation>(savedId); Assert.That(engine.IsValid(proxy)); Assert.DoesNotThrow(() => engine.AssertValid(proxy)); Assert.That(!NHibernateHelper.IsInitialized(proxy), "should not initialize the proxy"); } CleanDb(); }
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 ShouldAddValidationDefInstance() { var fc = new FluentConfiguration(); var vd = new ValidationDef<Address>(); vd.Define(x => x.Country).MaxLength(10); fc.Register(vd); var mp = (IMappingsProvider)fc; Assert.That(mp.GetMappings().Any(m => m.EntityType == typeof (Address))); }
public void ShouldWorkWithEnum() { var v = new ValidationDef <KnownRules>(); Assert.That(() => v.Define(x => x.Enum), Throws.Nothing); }
public void ShouldWorkWithEnum() { var v = new ValidationDef <KnownRules>(); ActionAssert.NotThrow(() => v.Define(x => x.Enum)); }
public void ShouldWorkWithEnum() { var v = new ValidationDef<KnownRules>(); ActionAssert.NotThrow(() => v.Define(x => x.Enum)); }
public void ValidateNotInitializeProxyAtFirstLevel() { var validatorConf = new FluentConfiguration(); validatorConf.SetDefaultValidatorMode(ValidatorMode.UseExternal); var vDefSimple = new ValidationDef<SimpleWithRelation>(); vDefSimple.Define(s => s.Name).MatchWith("OK"); validatorConf.Register(vDefSimple); var engine = new ValidatorEngine(); engine.Configure(validatorConf); object savedId; // fill DB using (ISession s = OpenSession()) using (ITransaction tx = s.BeginTransaction()) { savedId = s.Save(new SimpleWithRelation { Name = "OK" }); tx.Commit(); } using (ISession s = OpenSession()) { var proxy = s.Load<SimpleWithRelation>(savedId); Assert.That(engine.IsValid(proxy)); Assert.DoesNotThrow(() => engine.AssertValid(proxy)); Assert.That(!NHibernateUtil.IsInitialized(proxy), "should not initialize the proxy"); } CleanDb(); }
public void ValidateInitializedProxyAtDeepLevel() { var validatorConf = new FluentConfiguration(); validatorConf.SetDefaultValidatorMode(ValidatorMode.UseExternal); var vDefSimple = new ValidationDef<SimpleWithRelation>(); vDefSimple.Define(s => s.Name).MatchWith("OK"); vDefSimple.Define(s => s.Relation).IsValid(); validatorConf.Register(vDefSimple); var vDefRelation = new ValidationDef<Relation>(); vDefRelation.Define(s => s.Description).MatchWith("OK"); validatorConf.Register(vDefRelation); var engine = new ValidatorEngine(); engine.Configure(validatorConf); object savedIdRelation; // fill DB using (ISession s = OpenSession()) using (ITransaction tx = s.BeginTransaction()) { var relation = new Relation{ Description = "OK" }; savedIdRelation = s.Save(relation); tx.Commit(); } using (ISession s = OpenSession()) { var proxy = s.Load<Relation>(savedIdRelation); NHibernateUtil.Initialize(proxy); Assert.That(engine.IsValid(new SimpleWithRelation { Name = "OK", Relation = proxy })); Assert.DoesNotThrow(() => engine.AssertValid(new SimpleWithRelation { Name = "OK", Relation = proxy })); } CleanDb(); }