-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add timestamp.valid evaluator #53
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,88 @@ | ||||||
// Copyright 2023 Buf Technologies, Inc. | ||||||
// | ||||||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||||||
// you may not use this file except in compliance with the License. | ||||||
// You may obtain a copy of the License at | ||||||
// | ||||||
// http://www.apache.org/licenses/LICENSE-2.0 | ||||||
// | ||||||
// Unless required by applicable law or agreed to in writing, software | ||||||
// distributed under the License is distributed on an "AS IS" BASIS, | ||||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||
// See the License for the specific language governing permissions and | ||||||
// limitations under the License. | ||||||
|
||||||
package build.buf.protovalidate.internal.evaluator; | ||||||
|
||||||
import build.buf.protovalidate.ValidationResult; | ||||||
import build.buf.protovalidate.exceptions.ExecutionException; | ||||||
import build.buf.validate.Violation; | ||||||
import com.google.protobuf.Descriptors; | ||||||
import com.google.protobuf.Message; | ||||||
import java.util.ArrayList; | ||||||
import java.util.List; | ||||||
|
||||||
/** | ||||||
* A specialized evaluator for applying some {@link build.buf.validate.TimestampRules} (only the | ||||||
* `valid` rule currently) to an {@link com.google.protobuf.Timestamp} message. This is handled | ||||||
* outside CEL which handles {@link com.google.protobuf.Timestamp} as an abstract type, thus not | ||||||
* allowing access to the message fields. | ||||||
*/ | ||||||
class TimestampEvaluator implements Evaluator { | ||||||
private final long maxTimestamp = +253402300799L; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
We should make these static constants but I also think it improves readability if we use the timestamp strings instead of the epoch second here. |
||||||
private final long minTimestamp = -62135596800L; | ||||||
|
||||||
private final Descriptors.FieldDescriptor secondsDescriptor; | ||||||
private final Descriptors.FieldDescriptor nanosDescriptor; | ||||||
private final boolean valid; | ||||||
|
||||||
/** Constructs a new evaluator for {@link build.buf.validate.TimestampRules} messages. */ | ||||||
TimestampEvaluator( | ||||||
Descriptors.FieldDescriptor secondsDescriptor, | ||||||
Descriptors.FieldDescriptor nanosDescriptor, | ||||||
boolean valid) { | ||||||
this.secondsDescriptor = secondsDescriptor; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
this.nanosDescriptor = nanosDescriptor; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
this.valid = valid; | ||||||
} | ||||||
|
||||||
@Override | ||||||
public ValidationResult evaluate(Value val, boolean failFast) throws ExecutionException { | ||||||
Message timestampValue = val.messageValue(); | ||||||
if (timestampValue == null) { | ||||||
return ValidationResult.EMPTY; | ||||||
} | ||||||
List<Violation> violationList = new ArrayList<>(); | ||||||
if (valid) { | ||||||
long seconds = (long) timestampValue.getField(secondsDescriptor); | ||||||
int nanos = (int) timestampValue.getField(nanosDescriptor); | ||||||
|
||||||
String errorMessage = ""; | ||||||
if (seconds < minTimestamp) { | ||||||
errorMessage = "timestamp before 0001-01-01"; | ||||||
} else if (seconds > maxTimestamp) { | ||||||
errorMessage = "timestamp after 9999-12-31"; | ||||||
} else if (nanos < 0 || nanos >= 1e9) { | ||||||
errorMessage = "timestamp has out-of-range nanos"; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it would be useful if the error message included details of all parts of the timestamp which are invalid. So if both the seconds and nanos values are invalid, we should return that in the error message. Do we also want to include in the error message the expected range of the timestamp nanos? |
||||||
} | ||||||
|
||||||
if (errorMessage.length() != 0) { | ||||||
Violation violation = | ||||||
Violation.newBuilder() | ||||||
.setConstraintId("timestamp.valid") | ||||||
.setMessage(errorMessage) | ||||||
.build(); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should set the field path on the violation as well. |
||||||
violationList.add(violation); | ||||||
if (failFast) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This isn't looping over anything here, so I think we can just remove this if statement. |
||||||
return new ValidationResult(violationList); | ||||||
} | ||||||
} | ||||||
} | ||||||
return new ValidationResult(violationList); | ||||||
} | ||||||
|
||||||
@Override | ||||||
public boolean tautology() { | ||||||
return !valid; | ||||||
} | ||||||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think about returning early if
!fieldConstraints.getTimestamp.getValid()
? I don't see any sense in creating the evaluator in that case since there is only one rule and otherwise it is a no-op.