import functools


def nullable(method):
    """Helper decorator to make validators, encoders and decoders
    work for non required values.

    If the given `value` is not :keyword:`None` then the decorated
    method will be called as usually. If :keyword:`None` is passed
    instead nothing will be called.

    The :class:`validators.String` validator is a good example::

        class String(object):
            def validate(self, value):
                if value is not None:
                    pass # Do the validation here ...

    Now the same but using the `@nullable` decorator::

        @nullable
        def validate(self, value):
            pass # Do the validation here ...

    """

    @functools.wraps(method)
    def wrapper(self, value):
        if value is not None:
            return method(self, value)

    return wrapper