Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/NHibernate.Validator.Tests/Base/Address.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ public string Zip
set { zip = value; }
}

[Length(Max = 1000, Tags = "T1")]
[Length(Max = 2000, Tags = "T2")]
public string Line2
{
get { return line2; }
Expand Down Expand Up @@ -130,4 +132,4 @@ public AddressDef()
Define(x => x.AddressFlags).Enum();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,19 @@ public void ApplyOnEnumColumn()
Assert.That(serialColumn.CheckConstraint, Is.Null.Or.Empty, "Validator annotation should not generate check for [Flag]ed Enums");
}

[Test]
public void SameAttributeWithDifferentTagsOnPropertyDoNotModifyNhibMappings()
{
PersistentClass classMapping = cfg.GetClassMapping(typeof(Address));
IEnumerator ie = classMapping.GetProperty("Line2").ColumnIterator.GetEnumerator();
ie.MoveNext();
Column serialColumn = (Column) ie.Current;


Assert.AreEqual(serialColumn.Length, 255, "Same attribute with different tags must not change Nhib mappings");
}


/// <summary>
/// Test pre-update/save events and custom interpolator
/// </summary>
Expand Down
9 changes: 8 additions & 1 deletion src/NHibernate.Validator.Tests/Mappings/MixAddress.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,12 @@ public bool InternalValid
get { return internalValid; }
set { internalValid = value; }
}


[Min(1, Tags = "T1")] //This attribute will be redefined in xml mapping
[Min(1000, Tags = "T2")]
[Min(333, Tags = "T3")] //And this also will be redefined by xml
public int Num { get; set; }

}
}
}
6 changes: 5 additions & 1 deletion src/NHibernate.Validator.Tests/Mappings/MixAddress.nhv.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,9 @@
<property name="InternalValid">
<asserttrue />
</property>

<property name="Num">
<min value="100" tags="T1 T3" />
</property>
</class>
</nhv-mapping>
</nhv-mapping>
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
using System.Reflection;
using NHibernate.Validator.Cfg;
using NHibernate.Validator.Cfg.MappingSchema;
using NHibernate.Validator.Constraints;
using NHibernate.Validator.Mappings;
using NUnit.Framework;
using SharpTestsEx;
using RangeAttribute=NHibernate.Validator.Constraints.RangeAttribute;

namespace NHibernate.Validator.Tests.Mappings
Expand Down Expand Up @@ -95,18 +97,30 @@ public void MemberAttributes()
Assert.AreEqual(9999, ra.Max);
}
}

mi = typeof(MixAddress).GetProperty("Num");
mas = new List<Attribute>(rm.GetMemberAttributes(mi));
Assert.AreEqual(2, mas.Count);
foreach (var ma in mas)
{
if (ma is MinAttribute mia)
{
Assert.Contains(mia.Value, new[] { 100, 1000 });
}
}

}

[Test]
public void Members()
{
IClassMapping rm = new AttributeOverXmlClassMapping(GetXmlClassMapping(typeof (MixAddress)));
var mi = new List<MemberInfo>(rm.GetMembers());
Assert.AreEqual(16, mi.Count); // the members of the class by reflection
Assert.AreEqual(18, mi.Count); // the members of the class by reflection

rm = new XmlOverAttributeClassMapping(GetXmlClassMapping(typeof (MixAddress)));
mi = new List<MemberInfo>(rm.GetMembers());
Assert.AreEqual(16, mi.Count);
Assert.AreEqual(18, mi.Count);
}
}
}
}
105 changes: 105 additions & 0 deletions src/NHibernate.Validator.Tests/Specifics/NHV119/Fixture.cs
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();
}
}

}
11 changes: 11 additions & 0 deletions src/NHibernate.Validator.Tests/Specifics/NHV119/Mappings.nhv.xml
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>
18 changes: 18 additions & 0 deletions src/NHibernate.Validator.Tests/Specifics/NHV119/Model1.cs
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
}
}
}
4 changes: 2 additions & 2 deletions src/NHibernate.Validator.Tests/Utils/AttributeUtilsFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ public class AttributeUtilsFixture
[Test]
public void AttributeCanBeMultiplied()
{
PatternAttribute patternAttribute = new PatternAttribute();
var patternAttribute = new PatternAttribute();
Assert.AreEqual(true, (AttributeUtils.AttributeAllowsMultiple(patternAttribute)));
}

[Test]
public void AttributeCannotBeMultiplied()
{
LengthAttribute lenghtAttribute = new LengthAttribute();
var lenghtAttribute = new IBANAttribute();
Copy link
Member

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.

Assert.AreEqual(false, (AttributeUtils.AttributeAllowsMultiple(lenghtAttribute)));
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/NHibernate.Validator/Constraints/DecimalMaxAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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}";
Expand Down Expand Up @@ -98,6 +99,9 @@ public bool IsValid(object value, IConstraintValidatorContext validatorContext)

public void Apply(Property property)
{
if (AttributeUtils.AttributeUsedMultipleTimesOnProperty(property, GetType()))
return;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to be done for all IPropertyConstraint with AllowMultiple. Then better move that check into ClassValidator, for not calling this method at all when constraints should not be generated (when the attribute allows multiple and is defined multiple times). By the way, this should allow to fix the component case, since ClassValidator has access to more data about the property.


IEnumerator ie = property.ColumnIterator.GetEnumerator();
ie.MoveNext();
var col = (Column)ie.Current;
Expand Down
6 changes: 5 additions & 1 deletion src/NHibernate.Validator/Constraints/DecimalMinAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
using System.Globalization;
using NHibernate.Mapping;
using NHibernate.Validator.Engine;
using NHibernate.Validator.Util;

namespace NHibernate.Validator.Constraints
{
/// <summary>
/// Min restriction on a numeric annotated element (or the string representation of a numeric)
/// </summary>
[Serializable]
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = true)]
public class DecimalMinAttribute : EmbeddedRuleArgsAttribute, IRuleArgs, IValidator, IPropertyConstraint
{
private string message = "{validator.min}";
Expand Down Expand Up @@ -95,6 +96,9 @@ public bool IsValid(object value, IConstraintValidatorContext validatorContext)

public void Apply(Property property)
{
if (AttributeUtils.AttributeUsedMultipleTimesOnProperty(property, GetType()))
return;

IEnumerator ie = property.ColumnIterator.GetEnumerator();
ie.MoveNext();
var col = (Column)ie.Current;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using System;
using NHibernate.Validator.Engine;
using NHibernate.Validator.Util;

namespace NHibernate.Validator.Constraints
{
[Serializable]
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = true)]
[AllowsMultipleWithIntersectingTags]
[CLSCompliant(false)]
public class DelegatedValidatorAttribute : EmbeddedRuleArgsAttribute, IValidatorInstanceProvider, IRuleArgs
{
Expand All @@ -23,4 +25,4 @@ public IValidator Validator
get { return validatorInstance; }
}
}
}
}
9 changes: 7 additions & 2 deletions src/NHibernate.Validator/Constraints/LengthAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
using System;
using System.Collections;
using System.Reflection;
using NHibernate.Mapping;
using NHibernate.Validator.Engine;
using NHibernate.Validator.Util;

namespace NHibernate.Validator.Constraints
{
/// <summary>
/// Apply some length restrictions to the annotated element. It has to be a string
/// </summary>
[Serializable]
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = true)]
public class LengthAttribute : EmbeddedRuleArgsAttribute, IRuleArgs, IValidator, IPropertyConstraint
{
private string message = "{validator.length}";
Expand Down Expand Up @@ -67,6 +69,9 @@ public bool IsValid(object value, IConstraintValidatorContext validatorContext)

public void Apply(Property property)
{
if (AttributeUtils.AttributeUsedMultipleTimesOnProperty(property, GetType()))
return;

IEnumerator ie = property.ColumnIterator.GetEnumerator();
ie.MoveNext();
var col = (Column)ie.Current;
Expand All @@ -79,4 +84,4 @@ public void Apply(Property property)

#endregion
}
}
}
Loading