Skip to content

Commit a2fe978

Browse files
Pass --strict_message conformance (minus duration/timestamp)
We can't currently pass the conformance tests involving duration+timestamp types, as cel-python does not support nanoseconds in its duration implementation (linked issue). Otherwise, this gets us to a conformant spot. Related to #255.
1 parent 8786f15 commit a2fe978

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export PATH := $(BIN):$(PATH)
1111
export GOBIN := $(abspath $(BIN))
1212
# Set to use a different Python interpreter. For example, `PYTHON=python make test`.
1313
PYTHON ?= python3
14-
CONFORMANCE_ARGS ?= --strict --expected_failures=tests/conformance/nonconforming.yaml --timeout 10s
14+
CONFORMANCE_ARGS ?= --strict --strict_message --expected_failures=tests/conformance/nonconforming.yaml --timeout 10s
1515
ADD_LICENSE_HEADER := $(BIN)/license-header \
1616
--license-type apache \
1717
--copyright-holder "Buf Technologies, Inc." \

protovalidate/internal/string_format.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,16 +146,30 @@ def format_string(self, arg: celtypes.Value) -> celpy.Result:
146146
if isinstance(arg, celtypes.StringType):
147147
return arg
148148
if isinstance(arg, celtypes.BytesType):
149-
return celtypes.StringType(arg.hex())
149+
return celtypes.StringType(arg)
150150
if isinstance(arg, celtypes.ListType):
151151
return self.format_list(arg)
152+
if isinstance(arg, celtypes.BoolType):
153+
# True -> true
154+
return celtypes.StringType(str(arg).lower())
155+
if isinstance(arg, celtypes.DoubleType):
156+
return celtypes.StringType(f"{arg:.0f}")
157+
if isinstance(arg, celtypes.TimestampType):
158+
base = arg.isoformat()
159+
if arg.getMilliseconds() != 0:
160+
base = arg.isoformat(timespec="milliseconds")
161+
return celtypes.StringType(base.removesuffix("+00:00") + "Z")
152162
return celtypes.StringType(arg)
153163

154164
def format_value(self, arg: celtypes.Value) -> celpy.Result:
155165
if isinstance(arg, (celtypes.StringType, str)):
156166
return celtypes.StringType(quote(arg))
157167
if isinstance(arg, celtypes.UintType):
158168
return celtypes.StringType(arg)
169+
if isinstance(arg, celtypes.DurationType):
170+
return celtypes.StringType(f'duration("{arg.seconds + (arg.microseconds // 1e6):.0f}s")')
171+
if isinstance(arg, celtypes.DoubleType):
172+
return celtypes.StringType(f"{arg:f}")
159173
return self.format_string(arg)
160174

161175
def format_list(self, arg: celtypes.ListType) -> celpy.Result:
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
# celpy doesn't support nano seconds
2+
# ref: https://github.com/cloud-custodian/cel-python/issues/43
23
standard_constraints/well_known_types/duration:
34
- gte_lte/invalid/above
45
- lte/invalid
5-
- not in/valid
6+
- gte/invalid
7+
- gt/invalid
8+
- in/invalid
9+
- "not in/valid"
610
standard_constraints/well_known_types/timestamp:
711
- gte_lte/invalid/above
812
- lte/invalid
13+
standard_constraints/repeated:
14+
- duration/gte/invalid

0 commit comments

Comments
 (0)