Skip to content

Commit 084944d

Browse files
authored
Add support for isNan and isInf (#64)
And update to protovaldiate v0.4.0
1 parent dda35af commit 084944d

5 files changed

Lines changed: 79 additions & 56 deletions

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ LICENSE_HEADER := $(BIN)/license-header \
1919
--license-type apache \
2020
--copyright-holder "Buf Technologies, Inc." \
2121
--year-range "$(COPYRIGHT_YEARS)"
22-
PROTOVALIDATE_VERSION ?= v0.3.1
22+
PROTOVALIDATE_VERSION ?= v0.4.0
2323

2424
.PHONY: help
2525
help: ## Describe useful make targets

gen/buf/validate/validate_pb2.py

Lines changed: 46 additions & 46 deletions
Large diffs are not rendered by default.

protovalidate/internal/extra_func.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import math
1516
from ipaddress import IPv4Address, IPv6Address, ip_address
1617
from urllib import parse as urlparse
1718

@@ -105,6 +106,31 @@ def is_hostname(string: celtypes.Value) -> celpy.Result:
105106
return celtypes.BoolType(_validate_hostname(string))
106107

107108

109+
def is_nan(val: celtypes.Value) -> celpy.Result:
110+
if not isinstance(val, celtypes.DoubleType):
111+
msg = "invalid argument, expected double"
112+
raise celpy.EvalError(msg)
113+
return celtypes.BoolType(math.isnan(val))
114+
115+
116+
def is_inf(val: celtypes.Value, sign: None | celtypes.Value = None) -> celpy.Result:
117+
if not isinstance(val, celtypes.DoubleType):
118+
msg = "invalid argument, expected double"
119+
raise celpy.EvalError(msg)
120+
if sign is None:
121+
return celtypes.BoolType(math.isinf(val))
122+
123+
if not isinstance(sign, celtypes.IntType):
124+
msg = "invalid argument, expected int"
125+
raise celpy.EvalError(msg)
126+
if sign > 0:
127+
return celtypes.BoolType(math.isinf(val) and val > 0)
128+
elif sign < 0:
129+
return celtypes.BoolType(math.isinf(val) and val < 0)
130+
else:
131+
return celtypes.BoolType(math.isinf(val))
132+
133+
108134
def unique(val: celtypes.Value) -> celpy.Result:
109135
if not isinstance(val, celtypes.ListType):
110136
msg = "invalid argument, expected list"
@@ -115,7 +141,11 @@ def unique(val: celtypes.Value) -> celpy.Result:
115141
def make_extra_funcs(locale: str) -> dict[str, celpy.CELFunction]:
116142
string_fmt = string_format.StringFormat(locale)
117143
return {
144+
# Missing standard functions
118145
"format": string_fmt.format,
146+
# protovalidate specific functions
147+
"isNan": is_nan,
148+
"isInf": is_inf,
119149
"isIp": is_ip,
120150
"isEmail": is_email,
121151
"isUri": is_uri,

tests/conformance/nonconforming.yaml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,3 @@ standard_constraints/well_known_types/duration:
66
standard_constraints/well_known_types/timestamp:
77
- gte_lte/invalid/above
88
- lte/invalid
9-
# celpy cannot divide by zero
10-
standard_constraints/float:
11-
- finite/neginf
12-
standard_constraints/double:
13-
- finite/neginf

tests/validate_test.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,8 @@ def test_ninf(self):
2323
msg = numbers_pb2.DoubleFinite()
2424
msg.val = float("-inf")
2525
violations = protovalidate.collect_violations(msg)
26-
# TODO: update celpy to support divide by zero (which returns inf or -inf)
27-
self.assertEqual(len(violations.violations), 0)
28-
# self.assertEqual(len(violations.violations), 1)
29-
# self.assertEqual(violations.violations[0].field_path, "val")
26+
self.assertEqual(len(violations.violations), 1)
27+
self.assertEqual(violations.violations[0].constraint_id, "double.finite")
3028

3129
def test_map_key(self):
3230
msg = maps_pb2.MapKeys()

0 commit comments

Comments
 (0)