예제 #1
0
    def test_compile_full(self):
        regex = re_compile_full('.?foo(d|bar)')

        # These should match
        self.assertIsNotNone(regex.match('food'))
        self.assertIsNotNone(regex.match('foobar'))
        self.assertIsNotNone(regex.match('0food'))
        self.assertIsNotNone(regex.match('!foobar'))

        # These shouldn't
        self.assertIsNone(regex.match('foodbar'))
        self.assertIsNone(regex.match('foobar0'))
        self.assertIsNone(regex.match('..foobar'))
        self.assertIsNone(regex.match('..foobarz'))
예제 #2
0
    def from_json_dict(
            cls,
            json_dict  # type: Dict[str, Any]
    ):
        # type: (...) -> StringSpec
        """ Make a StringSpec object from a dictionary containing its
            properties.

            :param dict json_dict: This dictionary must contain an
                `'encoding'` key associated with a Python-conformant
                encoding. It must also contain a `'hashing'` key, whose
                contents are passed to :class:`FieldHashingProperties`.
                Permitted keys also include `'pattern'`, `'case'`,
                `'minLength'`, and `'maxLength'`.
            :raises InvalidSchemaError: When a regular expression is
                provided but is not a valid pattern.
        """
        # noinspection PyCompatibility
        result = cast(
            StringSpec,  # Go away, Mypy.
            super().from_json_dict(json_dict))

        format_ = json_dict['format']
        if 'encoding' in format_ and result.hashing_properties:
            result.hashing_properties.encoding = format_['encoding']

        if 'pattern' in format_:
            pattern = format_['pattern']
            try:
                result.regex = re_compile_full(pattern)
            except (SyntaxError, re.error) as e:
                msg = "Invalid regular expression '{}.'".format(pattern)
                e_new = InvalidSchemaError(msg)
                e_new.json_field_spec = json_dict
                raise_from(e_new, e)
            result.regex_based = True

        else:
            result.case = format_.get('case', StringSpec._DEFAULT_CASE)
            result.min_length = format_.get('minLength')
            result.max_length = format_.get('maxLength')
            result.regex_based = False

        return result
예제 #3
0
            raise ValueError(msg.format(case))

        if min_length < 0:
            msg = 'min_length must be non-negative, but is {}'
            raise ValueError(msg.format(min_length))

        # type checker thinks max_length is of type None
        # noinspection PyTypeChecker
        if max_length is not None and max_length <= 0:
            msg = 'max_length must be positive, but is {}'
            raise ValueError(msg.format(max_length))

        if regex_based:
            regex_str = cast(str, regex)
            try:
                compiled_regex = re_compile_full(regex_str)
                self.regex = compiled_regex
            except (SyntaxError, re.error) as e:
                msg = "invalid regular expression '{}.'".format(regex_str)
                e_new = InvalidEntryError(msg)
                e_new.field_spec = self
                raise_from(e_new, e)
        else:
            self.case = case
            self.min_length = min_length
            self.max_length = max_length

        self.regex_based = regex_based

    @classmethod
    def from_json_dict(