Skip to content
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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ private void buildValue(
processEnumConstraints(fieldDescriptor, fieldConstraints, valueEvaluator);
processMapConstraints(fieldDescriptor, fieldConstraints, valueEvaluator);
processRepeatedConstraints(fieldDescriptor, fieldConstraints, forItems, valueEvaluator);
processTimestampConstraints(fieldDescriptor, fieldConstraints, valueEvaluator);
}

private void processFieldExpressions(
Expand Down Expand Up @@ -360,6 +361,21 @@ private void processRepeatedConstraints(
valueEvaluatorEval.append(listEval);
}

private void processTimestampConstraints(
FieldDescriptor fieldDescriptor,
FieldConstraints fieldConstraints,
ValueEvaluator valueEvaluatorEval) {
if (fieldDescriptor.getType() != FieldDescriptor.Type.MESSAGE
|| !fieldDescriptor.getMessageType().getFullName().equals("google.protobuf.Timestamp")) {
return;
Copy link
Member

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.

}
FieldDescriptor secondsDesc = fieldDescriptor.getMessageType().findFieldByName("seconds");
FieldDescriptor nanosDesc = fieldDescriptor.getMessageType().findFieldByName("nanos");
TimestampEvaluator timestampEvaluatorEval =
new TimestampEvaluator(secondsDesc, nanosDesc, fieldConstraints.getTimestamp().getValid());
valueEvaluatorEval.append(timestampEvaluatorEval);
}

private static List<CompiledProgram> compileConstraints(List<Constraint> constraints, Env env)
throws CompilationException {
List<Expression> expressions = Expression.fromConstraints(constraints);
Expand Down
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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private final long maxTimestamp = +253402300799L;
private static final long MAX_TIMESTAMP = ZonedDateTime.parse("9999-12-31T23:59:59Z").toEpochSecond();

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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
this.secondsDescriptor = secondsDescriptor;
this.secondsDescriptor = Objects.requireNonNull(secondsDescriptor, "secondsDescriptor");

this.nanosDescriptor = nanosDescriptor;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
this.nanosDescriptor = nanosDescriptor;
this.nanosDescriptor = Objects.requireNonNull(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";
Copy link
Member

Choose a reason for hiding this comment

The 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();
Copy link
Member

Choose a reason for hiding this comment

The 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) {
Copy link
Member

Choose a reason for hiding this comment

The 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;
}
}
83 changes: 83 additions & 0 deletions src/main/java/build/buf/validate/TimestampRules.java

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions 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.

35 changes: 18 additions & 17 deletions src/main/java/build/buf/validate/ValidateProto.java

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/main/resources/buf/validate/validate.proto
Original file line number Diff line number Diff line change
Expand Up @@ -3697,4 +3697,9 @@ message TimestampRules {
id: "timestamp.within",
expression: "this < now-rules.within || this > now+rules.within ? 'value must be within %s of now'.format([rules.within]) : ''"
}];

// `valid` specifies that this field, of the `google.protobuf.Timestamp` type, must adhere to the documented specification:
// * the `seconds` field must be from 0001-01-01T00:00:00Z to 9999-12-31T23:59:59Z inclusive.
// * the `nanos` field must be from 0 to 999,999,999 inclusive.
bool valid = 10;
}
Loading