Add tests and fix bugs in validation logic.

This fixes the following:

1. Earlier if there was a undefined datum, it always looked for field in it leading to an exception. 
2. It adds the support for the optional field to have a default value. 

It also adds test cases to test the validation logic.