Source code for py_abac.policy.conditions.string.regex_match

"""
    String regex match conditions
"""

import re

from marshmallow import Schema, fields, post_load, ValidationError

from .base import StringCondition


[docs]class RegexMatch(StringCondition): """ Condition for string `what` matches regex `value` """ def _is_satisfied(self, what) -> bool: return re.search(self.value, what) is not None
[docs]def validate_regex(value): """ Validate given regex. Throws ValidationError exception for invalid regex expressions. """ # noinspection PyBroadException try: re.compile(value) except Exception: raise ValidationError("Invalid regex expression '{}'.".format(value))
[docs]class RegexMatchSchema(Schema): """ JSON schema for regex match string condition """ value = fields.String(required=True, allow_none=False, validate=validate_regex)
[docs] @post_load def post_load(self, data, **_): # pylint: disable=missing-docstring,no-self-use return RegexMatch(**data)