Validate fields in Python dictionary using dictionary of lambdas I would love some feedback on this.  I use it to validate web-form objects before persisting them in the dangerously-lazy MongoDb.  I have a class and many validators for each of my collections.  Is there a better way to write or go about this?

class ValidationError(Exception):
    pass    

class User(object):

    @staticmethod
    def validate(data):

        def _id(val):
            if not isinstance(val, ObjectId):
                raise ValidationError('not ObjectId')
            else:
                return val

        map_keys = dict(
            _id=lambda x: _id(x)
        )

        def count(val):
            try:
                return int(val)
            except TypeError:
                raise ValidationError('%s not integer' % val)

        new_data = {}

        for key in data:
            try:
                new_data[key] = map_keys[key](data[key])
            except KeyError:
                raise ValidationError('Key not in User: "%s"' % key)
            except ValidationError, error:
                print key, error
                raise
        return new_data
lambda is redundant here as you can use _id directly as well:

    map_keys = dict(
        _id=_id
    )


Other than that, your approach seems ok. You are reinventing the wheel though and it might be a good idea to research existing libraries.