Example #1
0
def xor_key(first, second, trafaret):
    """
    xor_key - takes `first` and `second` key names and `trafaret`.

    Checks if we have only `first` or only `second` in data, not both,
    and at least one.

    Then checks key value against trafaret.
    """
    trafaret = t.ensure_trafaret(trafaret)

    def check_(value):
        if (first in value) ^ (second in value):
            key = first if first in value else second
            yield first, t.catch_error(trafaret, value[key]), (key, )
        elif first in value and second in value:
            yield (
                first,
                t.DataError(
                    error='correct only if {} is not defined'.format(second),
                    code=codes.ONLY_ONE_MUST_BE_DEFINED), (first, ))
            yield (second,
                   t.DataError(
                       error='correct only if {} is not defined'.format(first),
                       code=codes.ONLY_ONE_MUST_BE_DEFINED), (second, ))
        else:
            yield (first,
                   t.DataError(
                       error='is required if {} is not defined'.format(second),
                       code=codes.ONE_IS_REQUIRED), (first, ))
            yield (second,
                   t.DataError(
                       error='is required if {} is not defined'.format(first),
                       code=codes.ONE_IS_REQUIRED), (second, ))

    return check_
Example #2
0
 def test_ensure(self):
     with pytest.raises(RuntimeError):
         t.ensure_trafaret(123)
Example #3
0
 def __init__(self, trafarets):
     self.trafarets = [
         t.ensure_trafaret(trafaret) for trafaret in trafarets
     ]