Friday, January 24, 2014

TastyPie's Validation.is_valid: a case of careless naming

TastyPie is a utility to add RESTful APIs to your Django app. TastyPie abstracts some of the more mechanical aspects of REST API implementation and provides high-level interfaces that make it easy to get up and running.

One of the hooks provided by TastyPie is validation. It provides its own Validation class for you to subclass if you need to execute some custom validation logic. All your logic goes in an is_valid() method. This method returns a dictionary of error messages. If there are no validation errors, an empty dictionary gets returned, signalling that all is well.

...wait, what? So this is how it works, ideally:

>>> my_validator.is_valid('good data')
{}
>>> bool(my_validator.is_valid('good_data'))
False
>>> my_validator.is_valid('bad data')
{'__all__': 'Bad input'}
>>> bool(my_validator.is_valid('bad data'))
True

Does this prevent TastyPie from achieving its goal? No. But was this name poorly chosen? I think so. Small mental mixups like this add up fast. And especially in a language like Python, where the emphasis is on concise, easy-to-read code, it seems like quite a blunder. Frameworks should be used when they simplify and enhance development, so the names should be well-chosen and meaningful.

No comments:

Post a Comment