You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Michele <mi...@finconsgroup.com> on 2016/03/15 20:21:53 UTC

How to validate Body Content in Map Object

Hi everyone,

a common requirement in my business is to provide a validation to data read
by File.

Is there a way to apply a validation on Map java object?

My example route is

from("file:uri.inbound").unmarshal(customCSVReader).split(body()).to("validate????")

Thanks in advance

Best Regards

Michele



--
View this message in context: http://camel.465427.n5.nabble.com/How-to-validate-Body-Content-in-Map-Object-tp5779118.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: How to validate Body Content in Map Object

Posted by Brad Johnson <br...@mediadriver.com>.
You may want to look at Camel Beanio and the Beanio integration.  Much of
what you seem to want to do can be done as part of the process of reading
the file in.  You can specify which data types to convert the various
fields into and which to ignore and so on.

http://www.beanio.org/2.0/docs/reference/index.html#CSVStreamFormat

On Wed, Mar 16, 2016 at 11:15 AM, Michele <michele.mazzilli@finconsgroup.com
> wrote:

> Hi,
>
> i will manage different file (txt/csv/fixed-length) where each of them has
> a
> different structure and criterions of validation (ex. type of data (string,
> date, numeric), max length, is valid date by pattern...).
>
> Both solutions are good, XML version of some validation using the simple
> language or CustomValidator.
> Probably i will use a CustomValidator driven a custom xml configuration.
>
> I have seen beanio and the question is: Is this a valid xml configuration
> file?
> <stream name="employeeFile" format="csv">
> <record name="employee" class="java.util.LinkedHashMap">
>
> thanks a lot for your suggestions
>
> Greetings
>
> Michele
>
>
>
>
>
>
>
>
> --
> View this message in context:
> http://camel.465427.n5.nabble.com/How-to-validate-Body-Content-in-Map-Object-tp5779118p5779167.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>

Re: How to validate Body Content in Map Object

Posted by Michele <mi...@finconsgroup.com>.
Hi, 

i will manage different file (txt/csv/fixed-length) where each of them has a
different structure and criterions of validation (ex. type of data (string,
date, numeric), max length, is valid date by pattern...). 

Both solutions are good, XML version of some validation using the simple
language or CustomValidator.
Probably i will use a CustomValidator driven a custom xml configuration.

I have seen beanio and the question is: Is this a valid xml configuration
file? 
<stream name="employeeFile" format="csv">
<record name="employee" class="java.util.LinkedHashMap">

thanks a lot for your suggestions

Greetings

Michele








--
View this message in context: http://camel.465427.n5.nabble.com/How-to-validate-Body-Content-in-Map-Object-tp5779118p5779167.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: How to validate Body Content in Map Object

Posted by Brad Johnson <br...@mediadriver.com>.
Michelle,

You are using a custom CSV reader it would appear rather than one of the
off the shelf readers like beanio or bindy or xxx that come with Camel.
Obviously you've left that a very black box so I don't really know what you
are doing it in but if that is truly custom you may want to validate the
CSV contents as you read it in.

What sort of validation are you trying to do.  If you could be a bit more
specific about it that would help suggest some possible solutions.  Claus
shows the XML version of some validation using the simple language and
that's probably what you'll have to go with if you want such flexibility.
Simple also works in the Java DSL if that's your preference.

How many different record types are you dealing with?

If you create custom validators that doesn't mean you are writing a POJO
per map.  For example, if my validator class is:

public class MyValidator {

     public Boolean validateAllNotNull(Map map)
{
     //iterate through the entry set and verify that there aren't any null
entities in the values.
}

}

...that could be reused anywhere you want to verify that things were
unmarshaled correctly.

More importantly, how are you going to determine which validator mechanisms
to call after you unmarshal your CSVs into maps.  If you say there are too
many to create POJOs for, which may well be true, you'd still have to
create many different routes to send the maps to the right set of
validators.

So if you can give us a more concrete example of what you're trying to do
we can probably help with some suggestions.  Definitely give this a look
again too:

http://camel.apache.org/validate.html

On Wed, Mar 16, 2016 at 3:23 AM, Claus Ibsen <cl...@gmail.com> wrote:

> Ah quick thought could be to add a component that validates using a
> Camel language, and uses simple by default, then you can do
>
> body[foo] == 'Hello'
> body[bar] > 100
>
> eg in a plain text file where each line is a predicate.
>
> <to uri="camel-validator:myFile.txt/">
>
> It could also be some kind of xml file where you can setup multiple
> groups in the same file, and then refer to which one
>
> <to uri="camel-validator:myFile.xml?rule=foo"/>
>
> <validate-rules>
> <validate id="foo">
>   <simple>body[foo] == 'Hello'</simple>
>   <simple>body[bar] &gt; 100</simple>
> </validate>
> <validate id="dude">
>   <simple>body[yay] == 'Hello'</simple>
>   <simple>body[something] in ['a', 'b']</simple>
> </validate>
> </validate-rules>
>
>
>
> On Wed, Mar 16, 2016 at 9:19 AM, Claus Ibsen <cl...@gmail.com>
> wrote:
> > No there is no such component.
> >
> > Creating a pojo vs a xml both requires work. The former is just java,
> > the latter introduces a new limited language.
> >
> > How much validation do you need, eg what are your criteria's?
> >
> > On Wed, Mar 16, 2016 at 9:08 AM, Michele
> > <mi...@finconsgroup.com> wrote:
> >> Hi Ranx,
> >>
> >> thanks so much.
> >>
> >> Exactly, key/values validation... I have different file with different
> >> criterions of validations and to create different Pojo is much
> expensive.
> >>
> >> Is there a compenent in camel with xml configuration to validate a Map
> >> Object?
> >>
> >> Thaks in advance
> >>
> >> Best Regards
> >>
> >> Michele
> >>
> >>
> >>
> >> --
> >> View this message in context:
> http://camel.465427.n5.nabble.com/How-to-validate-Body-Content-in-Map-Object-tp5779118p5779142.html
> >> Sent from the Camel - Users mailing list archive at Nabble.com.
> >
> >
> >
> > --
> > Claus Ibsen
> > -----------------
> > http://davsclaus.com @davsclaus
> > Camel in Action 2: https://www.manning.com/ibsen2
>
>
>
> --
> Claus Ibsen
> -----------------
> http://davsclaus.com @davsclaus
> Camel in Action 2: https://www.manning.com/ibsen2
>

Re: How to validate Body Content in Map Object

Posted by Claus Ibsen <cl...@gmail.com>.
Ah quick thought could be to add a component that validates using a
Camel language, and uses simple by default, then you can do

body[foo] == 'Hello'
body[bar] > 100

eg in a plain text file where each line is a predicate.

<to uri="camel-validator:myFile.txt/">

It could also be some kind of xml file where you can setup multiple
groups in the same file, and then refer to which one

<to uri="camel-validator:myFile.xml?rule=foo"/>

<validate-rules>
<validate id="foo">
  <simple>body[foo] == 'Hello'</simple>
  <simple>body[bar] &gt; 100</simple>
</validate>
<validate id="dude">
  <simple>body[yay] == 'Hello'</simple>
  <simple>body[something] in ['a', 'b']</simple>
</validate>
</validate-rules>



On Wed, Mar 16, 2016 at 9:19 AM, Claus Ibsen <cl...@gmail.com> wrote:
> No there is no such component.
>
> Creating a pojo vs a xml both requires work. The former is just java,
> the latter introduces a new limited language.
>
> How much validation do you need, eg what are your criteria's?
>
> On Wed, Mar 16, 2016 at 9:08 AM, Michele
> <mi...@finconsgroup.com> wrote:
>> Hi Ranx,
>>
>> thanks so much.
>>
>> Exactly, key/values validation... I have different file with different
>> criterions of validations and to create different Pojo is much expensive.
>>
>> Is there a compenent in camel with xml configuration to validate a Map
>> Object?
>>
>> Thaks in advance
>>
>> Best Regards
>>
>> Michele
>>
>>
>>
>> --
>> View this message in context: http://camel.465427.n5.nabble.com/How-to-validate-Body-Content-in-Map-Object-tp5779118p5779142.html
>> Sent from the Camel - Users mailing list archive at Nabble.com.
>
>
>
> --
> Claus Ibsen
> -----------------
> http://davsclaus.com @davsclaus
> Camel in Action 2: https://www.manning.com/ibsen2



-- 
Claus Ibsen
-----------------
http://davsclaus.com @davsclaus
Camel in Action 2: https://www.manning.com/ibsen2

Re: How to validate Body Content in Map Object

Posted by Claus Ibsen <cl...@gmail.com>.
No there is no such component.

Creating a pojo vs a xml both requires work. The former is just java,
the latter introduces a new limited language.

How much validation do you need, eg what are your criteria's?

On Wed, Mar 16, 2016 at 9:08 AM, Michele
<mi...@finconsgroup.com> wrote:
> Hi Ranx,
>
> thanks so much.
>
> Exactly, key/values validation... I have different file with different
> criterions of validations and to create different Pojo is much expensive.
>
> Is there a compenent in camel with xml configuration to validate a Map
> Object?
>
> Thaks in advance
>
> Best Regards
>
> Michele
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/How-to-validate-Body-Content-in-Map-Object-tp5779118p5779142.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



-- 
Claus Ibsen
-----------------
http://davsclaus.com @davsclaus
Camel in Action 2: https://www.manning.com/ibsen2

Re: How to validate Body Content in Map Object

Posted by Michele <mi...@finconsgroup.com>.
Hi Ranx,

thanks so much.

Exactly, key/values validation... I have different file with different
criterions of validations and to create different Pojo is much expensive.

Is there a compenent in camel with xml configuration to validate a Map
Object?

Thaks in advance

Best Regards

Michele



--
View this message in context: http://camel.465427.n5.nabble.com/How-to-validate-Body-Content-in-Map-Object-tp5779118p5779142.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: How to validate Body Content in Map Object

Posted by Ranx <br...@mediadriver.com>.
How do you want to validate that map's contents?  Do you want to validate
specific fields or key/values?  There are a number of options.  You could
invoke a Java bean or even the Java class you are in if you are using the
Java DSL (which it looks like you are.)

If you use a POJO you might do

.bean(MyValidator.class, "doValidate(${body})") and check or rectify content
in it or possibly throw an exception.  Or use .bean(this,
doValidate(${body})") to validate using the same object.  I don't recall
that exact syntax as I rarely use Java DSL and almost always go from XML to
Java invocations and then when I need to go from Java back to Camel I invoke
routes via endpoints I inject into wrapper classes.  Everything in Java
stays pure Java and everything in Camel stays pure Camel with non mixing. 
so be sure to check the syntax on those if you use them.


There's also the standard Camel validation mechanics.
http://camel.apache.org/validate.html



--
View this message in context: http://camel.465427.n5.nabble.com/How-to-validate-Body-Content-in-Map-Object-tp5779118p5779129.html
Sent from the Camel - Users mailing list archive at Nabble.com.