1515import math
1616import re
1717import typing
18+ from collections .abc import Callable
1819from urllib import parse as urlparse
1920
2021import celpy
2122from celpy import celtypes
2223
24+ from protovalidate .config import Config
2325from protovalidate .internal import string_format
24- from protovalidate .internal .matches import cel_matches
26+ from protovalidate .internal .matches import matches as protovalidate_matches
2527from protovalidate .internal .rules import MessageType , field_to_cel
2628
2729# See https://html.spec.whatwg.org/multipage/input.html#valid-e-mail-address
@@ -1554,14 +1556,31 @@ def __peek(self, char: str) -> bool:
15541556 return self ._index < len (self ._string ) and self ._string [self ._index ] == char
15551557
15561558
1557- def make_extra_funcs (locale : str ) -> dict [str , celpy .CELFunction ]:
1558- # For now, ignoring the type.
1559- string_fmt = string_format .StringFormat (locale ) # type: ignore
1559+ def get_matches_func (matcher : typing .Optional [Callable [[str , str ], bool ]]):
1560+ if matcher is None :
1561+ matcher = protovalidate_matches
1562+
1563+ def cel_matches (text : celtypes .Value , pattern : celtypes .Value ) -> celpy .Result :
1564+ if not isinstance (text , celtypes .StringType ):
1565+ msg = "invalid argument for text, expected string"
1566+ raise celpy .CELEvalError (msg )
1567+ if not isinstance (pattern , celtypes .StringType ):
1568+ msg = "invalid argument for pattern, expected string"
1569+ raise celpy .CELEvalError (msg )
1570+
1571+ b = matcher (text , pattern )
1572+ return celtypes .BoolType (b )
1573+
1574+ return cel_matches
1575+
1576+
1577+ def make_extra_funcs (config : Config ) -> dict [str , celpy .CELFunction ]:
1578+ string_fmt = string_format .StringFormat ()
15601579 return {
15611580 # Missing standard functions
15621581 "format" : string_fmt .format ,
15631582 # Overridden standard functions
1564- "matches" : cel_matches ,
1583+ "matches" : get_matches_func ( config . regex_matches_func ) ,
15651584 # protovalidate specific functions
15661585 "getField" : cel_get_field ,
15671586 "isNan" : cel_is_nan ,
@@ -1575,6 +1594,3 @@ def make_extra_funcs(locale: str) -> dict[str, celpy.CELFunction]:
15751594 "isHostAndPort" : cel_is_host_and_port ,
15761595 "unique" : cel_unique ,
15771596 }
1578-
1579-
1580- EXTRA_FUNCS = make_extra_funcs ("en_US" )
0 commit comments