-
Notifications
You must be signed in to change notification settings - Fork 54
Description
Feature description:
The well-known google.protobuf.Timestamp specification imposes some constraints on the fields (see source):
message Timestamp {
// Represents seconds of UTC time since Unix epoch
// 1970-01-01T00:00:00Z. Must be from 0001-01-01T00:00:00Z to
// 9999-12-31T23:59:59Z inclusive.
int64 seconds = 1;
// Non-negative fractions of a second at nanosecond resolution. Negative
// second values with fractions must still have non-negative nanos values
// that count forward in time. Must be from 0 to 999,999,999
// inclusive.
int32 nanos = 2;
}
This feature request suggests to enhance protovalidate to support this validation out-of-the-box.
Problem it solves or use case:
With the current implementation, developers would have to program this validation outside of protovalidate (or maybe use some complicated CEL expression). In Go, the specification validation is typically done with CheckValid. However, this has some issues:
- Overhead for the developer.
- Inconsistent validation end user experience.
Proposed implementation or solution:
I propose to add an extra validation rule for timestamp. For the implementation I am not sure since I am not experienced with CEL. From what I can see, the general idea is either:
- Have an implementation in CEL using existing expression logic. I tried implementing the constraints as a CEL expression. The validation on seconds seems easy enough, but the nanoseconds seem unavailable in a CEL expression (There might still be a way.)
- Adding a new CEL function and implementing it for each language (like isNaN).
Contribution:
I would be willing to implement this feature; as long as there is consensus on how it should be implemented.
Examples or references:
- protoc-gen-validate-go did seem to produce code which checked the specification validity using CheckValid. See here.
Additional context:
- While this proposal proposes an explicit validation rule, this might actually be something which should be done implicitly. This because the validation 1) is on the specification of the message and 2) is not context-aware.