-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
220 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
src/main/java/build/buf/protovalidate/internal/evaluator/TimestampEvaluator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
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; | ||
this.nanosDescriptor = nanosDescriptor; | ||
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"; | ||
} | ||
|
||
if (errorMessage.length() != 0) { | ||
Violation violation = | ||
Violation.newBuilder() | ||
.setConstraintId("timestamp.valid") | ||
.setMessage(errorMessage) | ||
.build(); | ||
violationList.add(violation); | ||
if (failFast) { | ||
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.
Oops, something went wrong.
10 changes: 10 additions & 0 deletions
10
src/main/java/build/buf/validate/TimestampRulesOrBuilder.java
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters