ZsBT / mrz-java

Machine-Readable Zone parser for Java
70 stars 40 forks source link

A MrzParserException is thrown when the dates in the MRZ are invalid (e.g 1999 September 35) #13

Closed GoUpNorth closed 6 years ago

GoUpNorth commented 6 years ago

I noticed that when we try to parse an MRZ in which either the date of birth or the date of expiry is invalid, then a MrzParserException is thrown and all the other parsed data are lost.

The exception message looks like this: Failed to parse MRZ PASSPORT P<GBRUK<SPECIMEN<<ANGELA<ZOE<<<<<<<<<<<<<<<< 9250764733GBR8809417F2007162<<<<<<<<<<<<<<08 at 17-19,1: Failed to parse MRZ date: invalid day value 41: must be 1..31

It could be interesting to continue the parsing even if the dates are incorrect, and leave the integrator check if they are correct or not.

From what I saw in the code, the modifications would have to be made in the MrzParser and MrzDate classes.

Invalid MRZ examples: "P<GBRUK<SPECIMEN<<ANGELA<ZOE<<<<<<<<<<<<<<<<" "9250764733GBR8809417F2007162<<<<<<<<<<<<<<08" Invalid date of birth

"P<GBRUK<SPECIMEN<<ANGELA<ZOE<<<<<<<<<<<<<<<<" "9250764733GBR8809117F2007462<<<<<<<<<<<<<<08" Invalid date of expiry

Regards, P-A

ZsBT commented 6 years ago

Indeed invalid dates could be interesting, however this kind of error must be handled somehow. I can imagine a custom exception class that can be caught.

GoUpNorth commented 6 years ago

Well my idea was not to stop the parsing if the dates are invalid. I was more thinking about adding a boolean flag in the MrzDate class, that would indicate if the date is valid or not. A bit like what you did in the MrzRecord class with the booleans corresponding to the different checksums validity.

ZsBT commented 6 years ago

I see. As MrzRecord already contains validity booleans, I think the validDateOfBirth and validExpirationDate should be also false when its MrzDate validity boolean is false. Shall you have a working code, don't hesitate to send a pull request in this repo.

GoUpNorth commented 6 years ago

Ok I'll do that.

ZsBT commented 6 years ago

fixed with merged pull request #14 .