public NHibernateValidatorClientModelValidator(ModelMetadata metadata, ControllerContext controllerContext, IClassValidator validator, ValidatorEngine engine) : base(metadata, controllerContext) { _engine = engine; _validator = validator; this.PropertyName = metadata.PropertyName; }
public void Collection() { Tv tv = new Tv(); tv.name = "France 2"; Presenter presNok = new Presenter(); presNok.name = null; Presenter presOk = new Presenter(); presOk.name = "Thierry Ardisson"; tv.presenters.Add(presOk); tv.presenters.Add(presNok); IClassValidator validator = GetClassValidator(typeof(Tv)); var values = validator.GetInvalidValues(tv).ToArray(); values.Should().Not.Be.Empty(); values.Single().PropertyPath.Should().Be.EqualTo("presenters[1].name"); tv.presenters.Clear(); tv.dontNeedDeepValidation = new List <string>(); tv.dontNeedDeepValidation.Add("something"); values = validator.GetInvalidValues(tv).ToArray(); values.Should().Be.Empty(); tv.dontNeedDeepValidation.Add("something else"); values = validator.GetInvalidValues(tv).ToArray(); values.Should().Not.Be.Empty(); values.Single().PropertyPath.Should().Be.EqualTo("dontNeedDeepValidation"); }
public void Dictionary() { IClassValidator validator = GetClassValidator(typeof(Tv)); Tv tv = new Tv(); tv.name = "France 2"; Show showOk = new Show(); showOk.name = "Tout le monde en parle"; Show showNok = new Show(); showNok.name = null; tv.shows.Add("Midnight", showOk); tv.shows.Add("Primetime", showNok); tv.shows.Add("Nothing", null); var values = validator.GetInvalidValues(tv).ToArray(); values.Should().Not.Be.Empty(); values.Single().PropertyPath.Should().Be.EqualTo("shows[Primetime].name"); //Inverted dictionary tv = new Tv(); tv.name = "France 2"; tv.validatableInKey = new Dictionary <Simple, string>(); tv.validatableInKey.Add(new Simple("Exalibur"), "Coll1"); tv.validatableInKey.Add(new Simple(), "Coll2"); values = validator.GetInvalidValues(tv).ToArray(); values.Should().Not.Be.Empty(); values.Single().PropertyPath.Should().Be.EqualTo("validatableInKey[null].name"); }
public void OneToOneValid() { IClassValidator vtor = GetClassValidator(typeof(Blog)); Blog b = new Blog(); b.Author = new Author(); vtor.GetInvalidValues(b).Should().Have.Count.EqualTo(2); }
public void GetInvalidValuesOfEntity() { IClassValidator cv = GetClassValidator(typeof(Address)); cv.GetInvalidValues(null).Should().Be.Empty(); ActionAssert.Throws <ArgumentException>(() => cv.GetInvalidValues(new Suricato())); }
/// <summary> /// Create a new instance of <see cref="ValidatableElement"/> for a root element. /// </summary> /// <param name="entityType">The type of the entity.</param> /// <param name="validator">The validator.</param> /// <remarks> /// The root element is the entity it self. /// </remarks> public ValidatableElement(System.Type entityType, IClassValidator validator) { if (entityType == null) throw new ArgumentNullException("entityType"); if (validator == null) throw new ArgumentNullException("validator"); this.entityType = entityType; this.validator = validator; }
[Test] //NHV-104 public void DefaultMessageErrorShouldExists() { var computer = new Computer { IpAddress = "aaa.bbb.ccc" }; IClassValidator classValidator = GetClassValidator(typeof(Computer)); var invalidValue = classValidator.GetInvalidValues(computer).First(); invalidValue.Message.Should().Not.Be.EqualTo("{validator.ipaddress}"); }
private static void ApplyValidatorToDDL(PersistentClass persistentClass, ValidatorEngine ve) { try { IClassValidator classValidator = ve.GetClassValidator(persistentClass.MappedClass); classValidator.Apply(persistentClass); } catch (Exception ex) { log.Warn( string.Format("Unable to apply constraints on DDL for [MappedClass={0}]", persistentClass.MappedClass.FullName), ex); } }
public void ImageValidationTest() { WebImage webImage = new WebImage(); webImage.position = 1; webImage.filename = "testunknown"; IClassValidator validator = GetClassValidator(typeof(WebImage)); validator.GetInvalidValues(webImage).Should().Not.Be.Empty(); webImage.filename = Path.GetTempFileName(); validator.GetInvalidValues(webImage).Should().Be.Empty(); }
public void ValidatorAttribute() { IClassValidator userValidator = GetClassValidator(typeof(Usuario)); var u = new Usuario(); userValidator.GetInvalidValues(u).Should().Be.Empty(); u.Cep = "40280902x"; var iv = userValidator.GetInvalidValues(u); iv.Should().Not.Be.Empty(); iv.Single().Message.Should().Be.EqualTo("Número de CEP inválido."); u.Cep = "40280902"; userValidator.GetInvalidValues(u).Should().Be.Empty(); }
public void TestInvalidIPAddresses() { var computer = new Computer(); IClassValidator classValidator = GetClassValidator(typeof(Computer)); computer.IpAddress = "aaa.bbb.ccc"; classValidator.GetInvalidValues(computer).Should("aaa.bbb.ccc is not a valid IP").Not.Be.Empty(); computer.IpAddress = "260.255.255.255"; classValidator.GetInvalidValues(computer).Should("260.255.255.255 is not a valid IP").Not.Be.Empty(); computer.IpAddress = "192.999.0.0"; classValidator.GetInvalidValues(computer).Should("192.999.0.0 is not a valid IP").Not.Be.Empty(); }
public void ValidatorAttribute() { IClassValidator userValidator = GetClassValidator(typeof(Cliente)); Cliente c = new Cliente(); userValidator.GetInvalidValues(c).Should().Be.Empty(); c.Piva = "00007016322"; var iv = userValidator.GetInvalidValues(c); iv.Should().Not.Be.Empty(); iv.Single().Message.Should().Be.EqualTo("Partita IVA incorretta."); c.Piva = "00523580520"; userValidator.GetInvalidValues(c).Should().Be.Empty(); }
public void SizeArrayWithValid() { HasArrayWithValid hsc = new HasArrayWithValid(); IClassValidator vtor = GetClassValidator(typeof(HasArrayWithValid)); hsc.Shows = new Show[] { new Show("s1"), new Show("s2") }; vtor.GetInvalidValues(hsc).Should().Be.Empty(); hsc.Shows = new Show[] { new Show("s1"), new Show(null) }; vtor.GetInvalidValues(hsc).Should().Not.Be.Empty(); hsc.Shows = new Show[] { new Show("s1"), new Show("s2"), new Show("s3"), new Show("s4"), new Show("s5") }; vtor.GetInvalidValues(hsc).Should().Have.Count.EqualTo(1); }
public void testEmail() { userValidator = GetClassValidator(typeof(User)); isRightEmail("*****@*****.**"); isRightEmail(""); isRightEmail(null); isWrongEmail("emmanuel.hibernate.org"); isRightEmail("emmanuel@hibernate"); isRightEmail("emma-n_uel@hibernate"); isWrongEmail("emma [email protected]"); isWrongEmail("emma([email protected]"); isWrongEmail("emmanuel@"); isRightEmail("*****@*****.**"); isRightEmail("[email protected]"); isWrongEmail("emma\[email protected]"); isWrongEmail("emma@[email protected]"); isWrongEmail("emmanuel@@hibernate.org"); isWrongEmail("emmanuel @ hibernate.org"); isRightEmail("emmanuel@[123.12.2.11]"); isWrongEmail(".emma@[email protected]"); }
private static void RunSerializationTest(IClassValidator cv) { Address a = new Address(); Address.blacklistedZipCode = null; a.Country = "Australia"; a.Zip = "1221341234123"; a.State = "Vic"; a.Line1 = "Karbarook Ave"; a.Id = 3; var validationMessages = cv.GetInvalidValues(a); // All work before serialization validationMessages.Should().Have.Count.EqualTo(2); //static field is tested also validationMessages.Select(iv => iv.Message).Satisfy(vm => vm.All(m => m.StartsWith("prefix_"))); // Serialize and deserialize ClassValidator cvAfter = (ClassValidator)SerializationHelper.Deserialize(SerializationHelper.Serialize(cv)); validationMessages = cvAfter.GetInvalidValues(a); // Now test after validationMessages.Should().Have.Count.EqualTo(2); //static field is tested also validationMessages.Select(iv => iv.Message).Satisfy(vm => vm.All(m => m.StartsWith("prefix_"))); }
private static void RunSerializationTest(IClassValidator cv) { Address a = new Address(); Address.blacklistedZipCode = null; a.Country = "Australia"; a.Zip = "1221341234123"; a.State = "Vic"; a.Line1 = "Karbarook Ave"; a.Id = 3; InvalidValue[] validationMessages = cv.GetInvalidValues(a); // All work before serialization Assert.AreEqual(2, validationMessages.Length); //static field is tested also Assert.IsTrue(validationMessages[0].Message.StartsWith("prefix_")); Assert.IsTrue(validationMessages[1].Message.StartsWith("prefix_")); // Serialize and deserialize ClassValidator cvAfter = (ClassValidator)SerializationHelper.Deserialize(SerializationHelper.Serialize(cv)); validationMessages = cvAfter.GetInvalidValues(a); // Now test after Assert.AreEqual(2, validationMessages.Length); //static field is tested also Assert.IsTrue(validationMessages[0].Message.StartsWith("prefix_")); Assert.IsTrue(validationMessages[1].Message.StartsWith("prefix_")); }
public NHibernateValidatorModelValidator(ModelMetadata metadata, ControllerContext controllerContext, IClassValidator validator) : base(metadata, controllerContext) { _validator = validator; }
/// <summary> /// Create a new instance of <see cref="ValidatableElement"/> for a composite element. /// </summary> /// <param name="entityType">The type of the composite element.</param> /// <param name="validator">The validator of the composite element.</param> /// <param name="getter">The getter of the composite element inside the root entity.</param> public ValidatableElement(System.Type entityType, IClassValidator validator, IGetter getter) : this(entityType, validator) { this.getter = getter; }