-
Notifications
You must be signed in to change notification settings - Fork 12
NHV-119 - Handle multiple attributes #17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| using System.Reflection; | ||
| using NHibernate.Validator.Cfg; | ||
| using NHibernate.Validator.Cfg.Loquacious; | ||
| using NHibernate.Validator.Engine; | ||
| using NUnit.Framework; | ||
| using SharpTestsEx; | ||
|
|
||
| namespace NHibernate.Validator.Tests.Specifics.NHV119 | ||
| { | ||
|
|
||
| [TestFixture] | ||
| public class Fixture : BaseValidatorFixture | ||
| { | ||
| [Test] | ||
| public void TestSameAttributeByDifferentTagsAttributeConfig() | ||
| { | ||
| //var vl = GetClassValidator(typeof(Model1)); | ||
|
|
||
| var vtor = new ValidatorEngine(); | ||
|
|
||
| var m = new Model1(); | ||
|
|
||
| m.Qnt = 1000; | ||
| vtor.Validate(m).Should().Be.Empty(); | ||
|
|
||
| m.Qnt = 50; | ||
| vtor.Validate(m).Should().Not.Be.Empty(); | ||
|
|
||
| m.Qnt = 100; | ||
| vtor.Validate(m, "T2").Should().Be.Empty(); | ||
|
|
||
| vtor.Validate(m, "T2", null).Should().Not.Be.Empty(); | ||
|
|
||
| m.Qnt = 10; | ||
| vtor.Validate(m, "T1").Should().Be.Empty(); | ||
| } | ||
|
|
||
| [Test] | ||
| public void TestSameAttributeByDifferentTagsXmlOverAttributeConfig() | ||
| { | ||
| //var vl = GetClassValidator(typeof(Model1)); | ||
|
|
||
| var vtor = new ValidatorEngine(); | ||
| var cfg = new XmlConfiguration(); | ||
| cfg.Properties[Environment.ValidatorMode] = "OverrideAttributeWithExternal"; | ||
| string an = Assembly.GetExecutingAssembly().FullName; | ||
| cfg.Mappings.Add(new MappingConfiguration(an, "NHibernate.Validator.Tests.Specifics.NHV119.Mappings.nhv.xml")); | ||
| vtor.Configure(cfg); | ||
|
|
||
| var m = new Model1(); | ||
|
|
||
| m.Qnt = 1000; | ||
| vtor.Validate(m).Should().Be.Empty(); | ||
|
|
||
| m.Qnt = 100; | ||
| vtor.Validate(m).Should().Not.Be.Empty(); | ||
|
|
||
| m.Qnt = 20; | ||
| vtor.Validate(m, "T1").Should().Be.Empty(); | ||
|
|
||
| m.Qnt = 19; | ||
| vtor.Validate(m, "T1").Should().Not.Be.Empty(); | ||
|
|
||
|
|
||
| m.Qnt = 199; | ||
| vtor.Validate(m, "T2").Should().Not.Be.Empty(); | ||
|
|
||
| m.Qnt = 200; | ||
| vtor.Validate(m, "T2").Should().Be.Empty(); | ||
| } | ||
|
|
||
| [Test] | ||
| public void TestSameAttributeByDifferentTagsFluentConfig() | ||
| { | ||
| //var vl = GetClassValidator(typeof(Model1)); | ||
|
|
||
|
|
||
| var configure = new FluentConfiguration(); | ||
| configure.Register(new[] { typeof(Model1Validation) }) | ||
| .SetDefaultValidatorMode(ValidatorMode.UseExternal); | ||
|
|
||
| var vtor = new ValidatorEngine(); | ||
| vtor.Configure(configure); | ||
|
|
||
| var m = new Model1(); | ||
|
|
||
| m.Qnt = 3000; | ||
| vtor.Validate(m).Should().Be.Empty(); | ||
|
|
||
| m.Qnt = 300; | ||
| vtor.Validate(m).Should().Not.Be.Empty(); | ||
|
|
||
| m.Qnt = 30; | ||
| vtor.Validate(m, "T1").Should().Be.Empty(); | ||
|
|
||
|
|
||
| m.Qnt = 299; | ||
| vtor.Validate(m, "T2").Should().Not.Be.Empty(); | ||
|
|
||
| m.Qnt = 3001; | ||
| vtor.Validate(m, "T2").Should().Be.Empty(); | ||
| } | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| <?xml version="1.0" encoding="utf-8" ?> | ||
| <nhv-mapping xmlns="urn:nhibernate-validator-1.0" | ||
| namespace="NHibernate.Validator.Tests.Specifics.NHV119" | ||
| assembly="NHibernate.Validator.Tests"> | ||
| <class name="Model1"> | ||
| <property name="Qnt"> | ||
| <min value="20" tags="T1"/> | ||
| <min value="200" tags="T2"/> | ||
| </property> | ||
| </class> | ||
| </nhv-mapping> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Text; | ||
| using System.Threading.Tasks; | ||
| using NHibernate.Validator.Constraints; | ||
|
|
||
| namespace NHibernate.Validator.Tests.Specifics.NHV119 | ||
| { | ||
| public class Model1 | ||
| { | ||
| [Min(10, Tags = "T1")] | ||
| [Min(777, Tags = "T2")] //This attribute will be ignored (redefined), by next one | ||
| [Min(100, Tags = "T2")] //And this will be redefined by xml config in TestSameAttributeByDifferentTagsXmlOverAttributeConfig test | ||
| [Min(1000)] | ||
| public int Qnt { get; set; } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Text; | ||
| using System.Threading.Tasks; | ||
| using NHibernate.Validator.Cfg.Loquacious; | ||
|
|
||
| namespace NHibernate.Validator.Tests.Specifics.NHV119 | ||
| { | ||
| public class Model1Validation : ValidationDef<Model1> | ||
| { | ||
| public Model1Validation() | ||
| { | ||
| Define(x => x.Qnt).GreaterThanOrEqualTo(30).WithTags("T1"); | ||
| Define(x => x.Qnt).GreaterThanOrEqualTo(300).WithTags("T2"); | ||
| Define(x => x.Qnt).GreaterThanOrEqualTo(3000).WithTags("T2"); //This definition must override previous one | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,14 +3,15 @@ | |
| using System.Globalization; | ||
| using NHibernate.Mapping; | ||
| using NHibernate.Validator.Engine; | ||
| using NHibernate.Validator.Util; | ||
|
|
||
| namespace NHibernate.Validator.Constraints | ||
| { | ||
| /// <summary> | ||
| /// Max restriction on a numeric annotated element | ||
| /// </summary> | ||
| [Serializable] | ||
| [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)] | ||
| [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = true)] | ||
| public class DecimalMaxAttribute : EmbeddedRuleArgsAttribute, IRuleArgs, IValidator, IPropertyConstraint | ||
| { | ||
| private string message = "{validator.max}"; | ||
|
|
@@ -98,6 +99,9 @@ public bool IsValid(object value, IConstraintValidatorContext validatorContext) | |
|
|
||
| public void Apply(Property property) | ||
| { | ||
| if (AttributeUtils.AttributeUsedMultipleTimesOnProperty(property, GetType())) | ||
| return; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems to be done for all |
||
|
|
||
| IEnumerator ie = property.ColumnIterator.GetEnumerator(); | ||
| ie.MoveNext(); | ||
| var col = (Column)ie.Current; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The variable name should be changed too.