WCF & Validation Block - Not validating

Topics: Validation Application Block
Jul 24, 2010 at 5:13 PM
Edited Jul 24, 2010 at 5:17 PM
Hi. I'm testing the validation block with my datacontracts/datamembers and I've a problem that the validation doesn't throws any exception with objects with wrongs attribute's values. I've created one class named CLIENTE and other named ENDERECO (cliente's attribute):


[DataContract] public class Cliente {
public long idCliente { get; set; }

/// <summary>
/// Obrigatorio - Razão social da empresa ou nome da pessoa
/// </summary>
[DataMember]
[NotNullValidator]
[StringLengthValidator(300)]
public string RazaoSocial { get; set; }


/// <summary>
/// Obrigatorio - CPF ou CNPJ do Cliente.
/// Utilizado pelo sistema terceiro para identificar o cliente
/// </summary>
[DataMember]
[NotNullValidator]
[StringLengthValidator(11, RangeBoundaryType.Inclusive, 14, RangeBoundaryType.Inclusive)]
[RegexValidator("[0-9]")]
public string CpfCnpj { get; set; }

/// <summary>
/// Obrigatorio - Dados de endereço
/// </summary>
[DataMember]
[NotNullValidator]
public Endereco Endereco { get; set; }
}



In the class Endereco, I've write:

[DataContract]
public class Endereco
{
/// <summary>
/// Obrigatorio - Estado (UF)
/// </summary>
[DataMember]
[NotNullValidator]
public Estado UF { get; set; }

/// <summary>
/// Obrigatorio - Logradouro (Rua Alvarenga Peixoto / Av. Alvares Cabral...)
/// </summary>
[DataMember]
[NotNullValidator]
[StringLengthValidator(60)]
public string Logradouro { get; set; }
}

My interface is declared like that:

[ValidationBehavior]
[ServiceContract]
public interface MyInterface
{
[OperationContract]
[FaultContract(typeof(ValidationFault))]
RetornoInserirCliente InserirCliente([NotNullValidator] Cliente cliente, bool atualizar);
}


In client-side, when I call the method InserirCliente passing Cliente's instance with no value in RazaoSocial or CpfCnpj or Endereco=null it throw correct FaultException<ValidationFault> but if I fill RazaoSocial and CpfCnpj (with wrong value, like 'abc324de455' it doesnt throw any validation exception, the RegexValidator() dont seems to work, only NotNull & StringLengthValidator seems to work, or I'm doing something wrong with RegexValidator?) and just instanciate Endereco (Cliente.Endeco = new Endereco(), but without putting any value in its attributes that have validators too) no exceptions is thrown.

Someone have an idea please?

Any help is very appreciated.
WCF / C# 3.0 / .NET 3.5 / VS 2008 / Enterprise Library 5

Thank you,
Christophe T. Chavey.
Jul 26, 2010 at 7:32 AM

The Notnull and StringLength works as expected so I'm guessing it's the pattern you specified in the RegExValidator.  I'm not an expert in regular expressions but it seems that the value 'abc324de455' will match since it contains numbers.  If you only supplied letters, I think there would be a validation error.  I suggest you google for the correct regex pattern.

 

Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
entlib.support@Avanade.com