[bv-dev] Configure validation constraints at runtime

Marcos Antonio marcos_antonio_ps at hotmail.com
Mon Feb 24 13:37:31 EST 2014


Sorry. A minor correction. In my last reply I meant @Size, not @Length.

Marcos

----------------------------------------
> From: marcos_antonio_ps at hotmail.com
> To: beanvalidation-dev at lists.jboss.org
> Date: Mon, 24 Feb 2014 21:29:16 +0300
> Subject: Re: [bv-dev] Configure validation constraints at runtime
>
> Hello, Emmanuel.
>
> I'm glad that you liked the suggestion.
>
> My use case is this:
>
> I'm working in my own framework to create applications. In this framework the majority of things is configurable in the client. So if a client want to change something in the application to fit his needs, I can configure this for him there without the need to change and compile the application (Internally these configurations are stored in a relational database).
>
> Amont the things that are configurable on the client are formats. So in my framework I have, for example, a @Mask annotation that I defined that I can use like this in a bean.
>
> @Mask("(##) ####-####")
> public String getPhoneNumber()
> {
>     return _phoneNumber;
> }
>
> As you may have guessed, everything in the application that displays this property (including editable fields) will be formated with the mask (format) above. But if some client want to change the format for this particular phone number (or even lots of phone numbers that share the same format) I can configure this only for him, without changing the default format applied on the bean.
>
> I also have defined other custom annotations for formats like @Minimo and @Maximo (for @Min and @Max - sorry they are in Portuguese, otherwise they would conflict with the standard ones) and @TamanhoMaximo (for @Length). So I noticed that there are some similarities between my formats and the ones provided by the bean validation specification. Think of this example:
>
> @TamanhoMaximo(50)
> @Length(50)
> public String getName()
> {
>     return _name;
> }
>
> There are some kind of redundancy here. Why not just have the @Length annotation and have my fremework 'derive' my similar custom annotation from the @Length annotation. Yes I could do that, but the problem is that I can no longer configure the length of the 'name' property on the client, because there's no way I can make the bean validation to see the new value. So I have to stick to this:
>
> @TamanhoMaximo(50)
> public String getName()
> {
>     return _name;
> }
>
> and let my bean unprotected when updates are applied to it without a GUI. If I could configure the bean validation constraints at runtime I could have this:
>
> @Length(50)
> public String getName()
> {
>     return _name;
> }
>
> and I would have to best of both worlds.
>
> Hope I expressed myself clearly.
>
> Marcos 		 	   		  


More information about the beanvalidation-dev mailing list