jbarberan / beanio

Automatically exported from code.google.com/p/beanio
0 stars 0 forks source link

maxLength ignored during delimited bean writing #96

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
I am using BeanIO to convert a bean (with a number of sub-records and so on) 
into a pipe-separated value string. However, the inputs to this bean aren't 
guaranteed to line up with the maximum lengths allowed at the other end, so 
some validation needs to occur.

I have set the max length property in the @Field annotation, but it's being 
completely ignored during the writing of the bean - if I set a value of "ABC" 
on a field with a maxLength of 1, I get output like "...|ABC|...", which isn't 
what I want. I would prefer that it is either trimmed, or, preferably, rejected 
with an exception. If needed, I can attach some sample code to demonstrate the 
issue.

Checking the code, Field simply doesn't perform any of this checking when 
marshalling. I had a look at perhaps doing it in the same way as the 
unmarshaller does, but I'm not sure if that's a good idea (would also require a 
bunch of copy-paste coding).

Incidentally, I would also prefer it if during any bean writing, it threw an 
exception if the value was longer than the length or maxLength of the field - 
silently trimming it for Fixed-length records and ignoring it for delimited 
records is (A) inconsistent, and (B) dangerous, at least in my mind. I can 
raise a new issue about that, if you prefer.

Original issue reported on code.google.com by nzipsi@gmail.com on 16 Dec 2013 at 4:47

GoogleCodeExporter commented 8 years ago
Validation rules only apply during unmarshalling.  There are plenty of other 
bean validation frameworks for validating objects.  So the described logic is 
by design, and delivers the best possible performance.

That said, I will investigate creating a system/beanio property setting that 
would enable validation on marshalling.

Thanks,
Kevin

Original comment by kevin.s...@gmail.com on 17 Dec 2013 at 3:18

GoogleCodeExporter commented 8 years ago
I'm also interested and applying validation rules during marshalling.  Since my 
validation rules (field maxlength, required, etc) are already defined in my 
mapping file, it doesn't make much sense to duplicate these rules in another 
bean validation framework.

Original comment by patrick....@gmail.com on 27 Aug 2014 at 2:58

GoogleCodeExporter commented 8 years ago
Snapshot 3.0.0 JAR attached.  Note that 3.x requires JDK 1.7 or higher.

This feature is disabled by default, but can be enabled using the following in 
beanio.properties:

org.beanio.validateOnMarshal=true

This will cause all marshalling to throw an InvalidBeanException if a 
marshalled field does not meet the following configured validation rules:
- required / nillable
- minimum field length
- maximum field length
- regex

Original comment by kevin.s...@gmail.com on 8 Sep 2014 at 3:13

Attachments:

GoogleCodeExporter commented 8 years ago
Issue 116 has been merged into this issue.

Original comment by kevin.s...@gmail.com on 8 Sep 2014 at 3:21