2020import celpy
2121from celpy import celtypes
2222
23+ from protovalidate .config import Config
2324from protovalidate .internal import string_format
24- from protovalidate .internal .matches import cel_matches
25+ from protovalidate .internal .matches import matches as protovalidate_matches
2526from protovalidate .internal .rules import MessageType , field_to_cel
2627
2728# See https://html.spec.whatwg.org/multipage/input.html#valid-e-mail-address
@@ -1554,14 +1555,31 @@ def __peek(self, char: str) -> bool:
15541555 return self ._index < len (self ._string ) and self ._string [self ._index ] == char
15551556
15561557
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
1558+ def get_matches_func (matcher : typing .Optional [typing .Callable [[str , str ], bool ]]):
1559+ if matcher is None :
1560+ matcher = protovalidate_matches
1561+
1562+ def cel_matches (text : celtypes .Value , pattern : celtypes .Value ) -> celpy .Result :
1563+ if not isinstance (text , celtypes .StringType ):
1564+ msg = "invalid argument for text, expected string"
1565+ raise celpy .CELEvalError (msg )
1566+ if not isinstance (pattern , celtypes .StringType ):
1567+ msg = "invalid argument for pattern, expected string"
1568+ raise celpy .CELEvalError (msg )
1569+
1570+ b = matcher (text , pattern )
1571+ return celtypes .BoolType (b )
1572+
1573+ return cel_matches
1574+
1575+
1576+ def make_extra_funcs (config : Config ) -> dict [str , celpy .CELFunction ]:
1577+ string_fmt = string_format .StringFormat ()
15601578 return {
15611579 # Missing standard functions
15621580 "format" : string_fmt .format ,
15631581 # Overridden standard functions
1564- "matches" : cel_matches ,
1582+ "matches" : get_matches_func ( config . regex_matches_func ) ,
15651583 # protovalidate specific functions
15661584 "getField" : cel_get_field ,
15671585 "isNan" : cel_is_nan ,
@@ -1575,6 +1593,3 @@ def make_extra_funcs(locale: str) -> dict[str, celpy.CELFunction]:
15751593 "isHostAndPort" : cel_is_host_and_port ,
15761594 "unique" : cel_unique ,
15771595 }
1578-
1579-
1580- EXTRA_FUNCS = make_extra_funcs ("en_US" )
0 commit comments