Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,6 @@ format: install $(BIN)/buf $(BIN)/license-header ## Format code
.PHONY: test
test: generate install gettestdata ## Run unit tests
uv run -- python -m unittest
$(MAKE) testextra

.PHONY: testextra
testextra:
uv sync --extra re2
uv run -- python -m unittest

.PHONY: conformance
conformance: $(BIN)/protovalidate-conformance generate install ## Run conformance tests
Expand Down
6 changes: 0 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,6 @@ To install the package, use `pip`:
pip install protovalidate
```

Protovalidate also has an optional dependency on [google-re2](https://pypi.org/project/google-re2/). If you require re2 syntax for your regular expressions, install the extra dep as follows:

```shell
pip install protovalidate[re2]
```

## Documentation

Comprehensive documentation for Protovalidate is available at [protovalidate.com][protovalidate].
Expand Down
21 changes: 2 additions & 19 deletions protovalidate/internal/extra_func.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,12 @@
from urllib import parse as urlparse

import celpy
import re2
from celpy import celtypes

from protovalidate.internal import string_format
from protovalidate.internal.rules import MessageType, field_to_cel

_USE_RE2 = True
try:
import re2
except ImportError:
_USE_RE2 = False

# See https://html.spec.whatwg.org/multipage/input.html#valid-e-mail-address
_email_regex = re.compile(
r"^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$"
Expand Down Expand Up @@ -1559,16 +1554,7 @@ def __peek(self, char: str) -> bool:
return self._index < len(self._string) and self._string[self._index] == char


def cel_matches_re(text: str, pattern: str) -> celpy.Result:
try:
m = re.search(pattern, text)
except re.error as ex:
return celpy.CELEvalError("match error", ex.__class__, ex.args)

return celtypes.BoolType(m is not None)


def cel_matches_re2(text: str, pattern: str) -> celpy.Result:
def cel_matches(text: str, pattern: str) -> celpy.Result:
try:
m = re2.search(pattern, text)
except re2.error as ex:
Expand All @@ -1577,9 +1563,6 @@ def cel_matches_re2(text: str, pattern: str) -> celpy.Result:
return celtypes.BoolType(m is not None)


cel_matches = cel_matches_re2 if _USE_RE2 else cel_matches_re


def make_extra_funcs() -> dict[str, celpy.CELFunction]:
string_fmt = string_format.StringFormat()
return {
Expand Down
3 changes: 0 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ dynamic = ["version"]
dependencies = [
"protobuf>=5",
"cel-python==0.2.*",
]
[project.optional-dependencies]
re2 = [
# We need at least this version, which started publishing wheels for Python 3.13.
# Ref: https://github.com/google/re2/issues/516
"google-re2>=1.1.20250722; python_version == '3.13'",
Expand Down
21 changes: 3 additions & 18 deletions test/test_matches.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,18 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import importlib.util
import unittest

import celpy
from celpy import celtypes

from protovalidate.internal.extra_func import cel_matches_re, cel_matches_re2

_USE_RE2 = True
spec = importlib.util.find_spec("re2")
if spec is None:
_USE_RE2 = False
from protovalidate.internal.extra_func import cel_matches


class TestCollectViolations(unittest.TestCase):
@unittest.skipUnless(_USE_RE2, "Requires 're2'")
def test_function_matches_re2(self):
empty_string = celtypes.StringType("")
# \z is valid re2 syntax for end of text
self.assertTrue(cel_matches_re2(empty_string, "^\\z"))
self.assertTrue(cel_matches(empty_string, "^\\z"))
# \Z is invalid re2 syntax
self.assertIsInstance(cel_matches_re2(empty_string, "^\\Z"), celpy.CELEvalError)

@unittest.skipUnless(_USE_RE2 is False, "Requires 're'")
def test_function_matches_re(self):
empty_string = celtypes.StringType("")
# \z is invalid re syntax
self.assertIsInstance(cel_matches_re(empty_string, "^\\z"), celpy.CELEvalError)
# \Z is valid re syntax for end of text
self.assertTrue(cel_matches_re(empty_string, "^\\Z"))
self.assertIsInstance(cel_matches(empty_string, "^\\Z"), celpy.CELEvalError)
13 changes: 4 additions & 9 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.