So I’m a little late in writing my blog and I apologize for my horrible time management.
I was working on the bug I planned on for release 0.2 in my open source class. Turns out, it was easier than I thought it would be.
The issue basically was to write tests that would make sure that the file that contains app templates as simple JSON data is in fact valid JSON and confines to a proper format. One of the contributors even suggested an excellent library, tv4 for getting this done. Luckily, my previous experience with JSON schemas proved to be very helpful. However, even though I had experience with schemas before, I often found myself referencing the JSON schema docs for refreshers.
I began by doing some prep work which included installing the library and going through their docs, writing the basic skeleton for my tests and determining what my tests would be. I ended up coming up with two tests in mind – one that makes sure that everything in that JSON file has the necessary properties that templates need along with some simple data type validation, format validation, etc.; as for my second test, it basically checked to make sure that every template name was localized, i.e. was present as a key in another JSON file.
I began writing the schema piece by piece. Building it based on a breadth first approach. This meant that I would first, exam the outermost object, create a ‘shallow’ schema for each property with simple checks. If a property was an object, I would basically assign a variable that would later be defined to hold the schema, to that property’s schema. In that way, I created the outermost objects schema. However, if I ran the test, it wouldn’t work as there were several references to variables that did not exist. These were the variables that I was going to create next that would hold the schemas for the properties that were objects. I kept using this approach until I was completely done writing the schema. After running the test a few times, couple of small bug fixes and voila…my first test was finished.
The second one needed some thinking. I had to do something tricky to only validate one property of each object, viz. the name. I had to create the schema skeleton for the JSON object and effectively provide ’empty’ validation for the properties that I did not care about. Then, I was able to provide a custom validation for the name by checking whether it was in the localization file (the tv4 docs allowed me to provide my own validation function…yay)!
I submitted my PR on Saturday very very early in the morning (like at 2am). Unfortunately, by the time I came to write this blog post, it was already merged into the main repo…whoops 😛